<commit_before>import * as ts from 'typescript';
import * as Lint from 'tslint';

import { ExtendedMetadata } from './utils/ExtendedMetadata';

import { AstUtils } from './utils/AstUtils';

export class Rule extends Lint.Rules.AbstractRule {
    public static metadata: ExtendedMetadata = {
        ruleName: 'no-exec-script',
        type: 'maintainability',
        description: 'Do not use the execScript functions',
        options: null, // tslint:disable-line:no-null-keyword
        optionsDescription: '',
        typescriptOnly: true,
        issueClass: 'SDL',
        issueType: 'Error',
        severity: 'Critical',
        level: 'Mandatory',
        group: 'Security',
        commonWeaknessEnumeration: '95, 676'
    };

    public static FAILURE_STRING: string = 'forbidden execScript: ';

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        return this.applyWithWalker(new NoEvalScriptWalker(sourceFile, this.getOptions()));
    }
}

class NoEvalScriptWalker extends Lint.RuleWalker {
    protected visitCallExpression(node: ts.CallExpression) {
        this.validateExpression(node);
        super.visitCallExpression(node);
    }

    private validateExpression(node: ts.CallExpression): void {
        const expression: ts.Expression = node.expression;
        const functionName: string = AstUtils.getFunctionName(node);
        if (functionName === 'execScript') {
            const msg: string = Rule.FAILURE_STRING + expression.getFullText().trim();
            this.addFailureAt(expression.getStart(), expression.getWidth(), msg);
        }
    }
}<commit_msg>convert no-exec-script rule to use walk function (#717)<commit_after>import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

import { ExtendedMetadata } from './utils/ExtendedMetadata';

import { AstUtils } from './utils/AstUtils';

export class Rule extends Lint.Rules.AbstractRule {
    public static metadata: ExtendedMetadata = {
        ruleName: 'no-exec-script',
        type: 'maintainability',
        description: 'Do not use the execScript functions',
        options: null, // tslint:disable-line:no-null-keyword
        optionsDescription: '',
        typescriptOnly: true,
        issueClass: 'SDL',
        issueType: 'Error',
        severity: 'Critical',
        level: 'Mandatory',
        group: 'Security',
        commonWeaknessEnumeration: '95, 676'
    };

    public static FAILURE_STRING: string = 'forbidden execScript: ';

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        return this.applyWithFunction(sourceFile, walk);
    }
}

function walk(ctx: Lint.WalkContext<void>) {
    function cb(node: ts.Node): void {
        if (tsutils.isCallExpression(node)) {
            const expression: ts.Expression = node.expression;
            const functionName: string = AstUtils.getFunctionName(node);
            if (functionName === 'execScript') {
                const msg: string = Rule.FAILURE_STRING + expression.getFullText().trim();
                ctx.addFailureAt(expression.getStart(), expression.getWidth(), msg);
            }
        }

        return ts.forEachChild(node, cb);
    }

    return ts.forEachChild(ctx.sourceFile, cb);
}
{'fraction_non_alphanumeric': 0.1159643186711781, 'fraction_numerical': 0.003998769609350969, 'mean_word_length': 2.995085995085995, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1324125', 'n_tokens_mistral': 923, 'n_tokens_neox': 882, 'n_words': 219}