Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { ConfigurableRule, typescriptOnly, excludeDeclarationFiles, RuleContext, Replacement, predicate } from '@fimbul/ymir';
import * as ts from 'typescript';
import { WrappedAst, getWrappedNodeAtPosition, isAsExpression, isTypeAssertion, isBinaryExpression } from 'tsutils';
import { expressionNeedsParensWhenReplacingNode } from '../utils';
export interface Options {
style: 'classic' | 'as';
}
@typescriptOnly
@excludeDeclarationFiles
@predicate((sourceFile) => sourceFile.languageVariant === ts.LanguageVariant.Standard || 'excludes JSX files')
export class Rule extends ConfigurableRule {
public parseOptions(options: Partial | null | undefined): Options {
return {
style: options && options.style === 'classic' ? 'classic' : 'as',
};
}
public apply() {
return this.options.style === 'classic' ? enforceClassicTypeAssertion(this.context) : enforceAsTypeAssertion(this.context);
}
}
function enforceClassicTypeAssertion(context: RuleContext) {
const re = /\bas\b/g;
let wrappedAst: WrappedAst | undefined;
for (let match = re.exec(context.sourceFile.text); match !== null; match = re.exec(context.sourceFile.text)) {