|
| 1 | + |
| 2 | +import AstUtils = require('./AstUtils'); |
| 3 | + |
| 4 | + |
| 5 | +export class Rule extends Lint.Rules.AbstractRule { |
| 6 | + public static FAILURE_STRING = "forbidden: Function constructor with string arguments "; |
| 7 | + |
| 8 | + public apply(sourceFile : ts.SourceFile): Lint.RuleFailure[] { |
| 9 | + var documentRegistry = ts.createDocumentRegistry(); |
| 10 | + var languageServiceHost = Lint.createLanguageServiceHost("file.ts", sourceFile.getFullText()); |
| 11 | + var languageService = ts.createLanguageService(languageServiceHost, documentRegistry); |
| 12 | + return this.applyWithWalker(new NoFunctionConstructorWithStringArgsWalker(sourceFile, this.getOptions(), languageService)); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +class NoFunctionConstructorWithStringArgsWalker extends Lint.RuleWalker { |
| 17 | + |
| 18 | + private languageService: ts.LanguageService; |
| 19 | + private typeChecker : ts.TypeChecker; |
| 20 | + |
| 21 | + public constructor(sourceFile : ts.SourceFile, options : Lint.IOptions, languageServices : ts.LanguageService) { |
| 22 | + super(sourceFile, options); |
| 23 | + this.languageService = languageServices; |
| 24 | + this.typeChecker = this.languageService.getProgram().getTypeChecker(); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + protected visitNewExpression(node: ts.NewExpression): void { |
| 29 | + var functionName = AstUtils.getFunctionName(node); |
| 30 | + if (functionName === "Function") { |
| 31 | + if(node.arguments.length > 0){ |
| 32 | + this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING)); |
| 33 | + } |
| 34 | + } |
| 35 | + super.visitNewExpression(node); |
| 36 | + } |
| 37 | +} |
0 commit comments