How to use the @fimbul/ymir.predicate function in @fimbul/ymir

To help you get started, we’ve selected a few @fimbul/ymir examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fimbullinter / wotan / packages / mimir / src / rules / type-assertion.ts View on Github external
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)) {