This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] rebuilt AST walkers to improve performance https://palanti…
- Loading branch information
Showing
18 changed files
with
426 additions
and
326 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,60 @@ | ||
import * as Lint from 'tslint'; | ||
import * as ts from 'typescript'; | ||
import {StringLiteral, stringLiteralKinds} from '../node-kind'; | ||
import {stringLiteralKinds} from '../node-kind'; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new RuleWalker(sourceFile, this.getOptions())); | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
class RuleWalker extends Lint.RuleWalker { | ||
private names: string[] = []; | ||
|
||
visitCallExpression(node: ts.CallExpression) { | ||
const {expression} = node; | ||
const firstArgument: StringLiteral = node.arguments && (node.arguments[0] as StringLiteral); | ||
|
||
if ( | ||
firstArgument && | ||
expression && | ||
stringLiteralKinds.includes(firstArgument.kind) && | ||
firstArgument.text === 'child_process' && | ||
expression.getText() === 'require' | ||
) { | ||
const parent: ts.VariableDeclaration = node.parent as ts.VariableDeclaration; | ||
|
||
this.names.length = 0; | ||
|
||
if (parent && parent.kind === ts.SyntaxKind.VariableDeclaration) { | ||
this.names.push(parent.name.getText()); | ||
function walk(ctx: Lint.WalkContext<void>) { | ||
const names: string[] = []; | ||
|
||
function visitNode(node: ts.Node): void { | ||
switch (node.kind) { | ||
case ts.SyntaxKind.CallExpression: { | ||
const {expression, arguments: args} = node as ts.CallExpression; | ||
const firstArgument = args && args[0]; | ||
|
||
if ( | ||
firstArgument && | ||
expression && | ||
stringLiteralKinds.includes(firstArgument.kind) && | ||
(firstArgument as ts.StringLiteral).text === 'child_process' && | ||
(expression as ts.StringLiteral).text === 'require' | ||
) { | ||
const parent: ts.VariableDeclaration = node.parent as ts.VariableDeclaration; | ||
|
||
names.length = 0; | ||
|
||
if (parent && parent.kind === ts.SyntaxKind.VariableDeclaration) { | ||
names.push((parent.name as ts.Identifier).text); | ||
} | ||
|
||
ctx.addFailureAtNode(node, 'Found require("child_process")'); | ||
} | ||
break; | ||
} | ||
|
||
this.addFailureAtNode(node, 'Found require("child_process")'); | ||
case ts.SyntaxKind.PropertyAccessExpression: { | ||
const {name, expression} = node as ts.PropertyAccessExpression; | ||
|
||
if ( | ||
name && | ||
expression && | ||
name.text === 'exec' && | ||
names.indexOf((expression as ts.Identifier).text) >= 0 | ||
) { | ||
ctx.addFailureAtNode(node, 'Found child_process.exec() with non StringLiteral first argument'); | ||
} | ||
break; | ||
} | ||
default: | ||
// | ||
} | ||
|
||
super.visitCallExpression(node); | ||
return ts.forEachChild(node, visitNode); | ||
} | ||
|
||
visitPropertyAccessExpression(node: ts.PropertyAccessExpression) { | ||
const {name, expression} = node; | ||
|
||
if (name && expression && name.getText() === 'exec' && this.names.indexOf(expression.getText()) >= 0) { | ||
this.addFailureAtNode(node, 'Found child_process.exec() with non StringLiteral first argument'); | ||
} | ||
|
||
super.visitPropertyAccessExpression(node); | ||
} | ||
return ts.forEachChild(ctx.sourceFile, visitNode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.