Skip to content

Commit

Permalink
migrate noVarRequiresRule to 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya committed Jan 22, 2015
1 parent eebd9b2 commit 9a641ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ module.exports = function(grunt) {
'src/rules/memberOrderingRule.ts',
'src/rules/quoteMarkRule.ts',
'src/rules/radixRule.ts',
'src/rules/semicolonRule.ts'
'src/rules/semicolonRule.ts',
'src/rules/noVarRequiresRule.ts'
],
outDir: 'build/rules/'
},
Expand Down Expand Up @@ -119,6 +120,7 @@ module.exports = function(grunt) {
'test/rules/labelUndefinedRuleTests.ts',
'test/rules/maxLineLengthRuleTests.ts',
'test/rules/memberOrderingRuleTests.ts',
'test/rules/noVarRequiresRuleTests.ts',
'test/rules/quoteMarkRuleTests.ts',
'test/rules/radixRuleTests.ts',
'test/rules/semicolonRuleTests.ts',
Expand Down
28 changes: 15 additions & 13 deletions src/rules/noVarRequiresRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,33 @@
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = "require statement not part of an import statment";

public apply(syntaxTree: TypeScript.SyntaxTree): Lint.RuleFailure[] {
return this.applyWithWalker(new RequiresWalker(syntaxTree, this.getOptions()));
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
var requiresWalker = new RequiresWalker(sourceFile, this.getOptions());
return this.applyWithWalker(requiresWalker);
}
}

class RequiresWalker extends Lint.ScopeAwareRuleWalker<{}> {

constructor(syntaxTree: TypeScript.SyntaxTree, options: Lint.IOptions) {
super(syntaxTree, options);
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
}

public createScope(): {} {
return {};
}

public visitInvocationExpression(node: TypeScript.InvocationExpressionSyntax) {
if (this.getCurrentDepth() <= 1 && TypeScript.isToken(node.expression)) {
var expressionText = (<TypeScript.ISyntaxToken> node.expression).text();
if (expressionText === "require") {
// if we're using require as invocation then it's not part of an import statement
var position = this.getPosition() + TypeScript.leadingTriviaWidth(node);
this.addFailure(this.createFailure(position, TypeScript.width(node), Rule.FAILURE_STRING));
}
public visitCallExpression(node: ts.CallExpression) {
var expression = node.expression;

if (this.getCurrentDepth() <= 1 &&
expression.kind === ts.SyntaxKind.Identifier &&
node.getFirstToken().getText() === "require") {
// if we're calling (invoking) require, then it's not part of an import statement
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
super.visitInvocationExpression(node);

super.visitCallExpression(node);
}

}

0 comments on commit 9a641ef

Please sign in to comment.