forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d77b54e
commit 8463bdd
Showing
7 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/// <reference path='rule.ts'/> | ||
/// <reference path='abstractRule.ts'/> | ||
|
||
module Lint.Rules { | ||
|
||
export class VarNameUniquenessRule extends AbstractRule { | ||
public static FAILURE_STRING = "duplicate variable: '"; | ||
|
||
public apply(syntaxTree: TypeScript.SyntaxTree): RuleFailure[] { | ||
return this.applyWithWalker(new VarNameUniquenessWalker(syntaxTree)); | ||
} | ||
} | ||
|
||
class VarNameUniquenessWalker extends Lint.RuleWalker { | ||
private scopeStack: ScopeInfo[]; | ||
|
||
constructor(syntaxTree: TypeScript.SyntaxTree) { | ||
super(syntaxTree); | ||
|
||
// initialize stack with global scope | ||
this.scopeStack = [new ScopeInfo()]; | ||
} | ||
|
||
public visitNode(node: TypeScript.SyntaxNode): void { | ||
var isNewScope = this.isScopeBoundary(node); | ||
|
||
if (isNewScope) { | ||
this.scopeStack.push(new ScopeInfo()); | ||
} | ||
|
||
super.visitNode(node); | ||
|
||
if (isNewScope) { | ||
this.scopeStack.pop(); | ||
} | ||
} | ||
|
||
public visitVariableDeclarator(node: TypeScript.VariableDeclaratorSyntax): void { | ||
var identifier = node.identifier, | ||
variableName = identifier.text(), | ||
position = this.position() + identifier.leadingTriviaWidth(), | ||
currentScope = this.scopeStack[this.scopeStack.length - 1]; | ||
|
||
if (currentScope.variableNames.indexOf(variableName) >= 0) { | ||
var failureString = VarNameUniquenessRule.FAILURE_STRING + variableName + "'"; | ||
this.addFailure(this.createFailure(position, identifier.width(), failureString)); | ||
} else { | ||
currentScope.variableNames.push(variableName); | ||
} | ||
|
||
super.visitVariableDeclarator(node); | ||
} | ||
|
||
private isScopeBoundary(node: TypeScript.SyntaxNode): boolean { | ||
return node instanceof TypeScript.FunctionDeclarationSyntax | ||
|| node instanceof TypeScript.MemberFunctionDeclarationSyntax | ||
|| node instanceof TypeScript.SimpleArrowFunctionExpressionSyntax | ||
|| node instanceof TypeScript.ParenthesizedArrowFunctionExpressionSyntax; | ||
} | ||
} | ||
|
||
class ScopeInfo { | ||
public variableNames: string[] = []; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var duplicated = 1; | ||
|
||
class Test { | ||
private myFunc() { | ||
var notDuplicated = 123, | ||
duplicated = 234, | ||
someFunc = () => { | ||
var notDuplicated = 345; | ||
}; | ||
|
||
var duplicated = null; | ||
} | ||
} | ||
|
||
function test() { | ||
var notDuplicated = 123, | ||
duplicated = 234, | ||
someFunc = () => { | ||
var notDuplicated = 345; | ||
}; | ||
|
||
var duplicated = null; | ||
} | ||
|
||
duplicated = 2; | ||
var duplicated = 3; |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// <reference path='../references.ts' /> | ||
|
||
describe("<varnameuniqueness>", () => { | ||
it("ensures that variable declarations are unique within a scope", () => { | ||
var fileName = "rules/varnameuniqueness.test.ts"; | ||
var failureString = Lint.Rules.VarNameUniquenessRule.FAILURE_STRING + "duplicated'"; | ||
|
||
var createFailure = Lint.Test.createFailuresOnFile(fileName, failureString); | ||
var expectedFailures = [ | ||
createFailure([11, 13], [11, 23]), | ||
createFailure([22, 9], [22, 19]), | ||
createFailure([26, 5], [26, 15]) | ||
]; | ||
|
||
var actualFailures = Lint.Test.applyRuleOnFile(fileName, "varnameuniqueness"); | ||
Lint.Test.assertFailuresEqual(actualFailures, expectedFailures); | ||
}); | ||
}); |
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