This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #735 from myitcv/no_keyword_variable_name
Rule to prevent certain keywords being used as variable names
- Loading branch information
Showing
11 changed files
with
320 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2014 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as Lint from "../lint"; | ||
import * as ts from "typescript"; | ||
|
||
const BAD_NAMES = [ "any", "Number", "number", "String", "string", "Boolean", "boolean", "undefined" ]; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static FAILURE_STRING = "variable name clashes with keyword/type: "; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new NoKeywordNamedVariablesWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class NoKeywordNamedVariablesWalker extends Lint.RuleWalker { | ||
public visitBindingElement(node: ts.BindingElement) { | ||
if (node.name.kind === ts.SyntaxKind.Identifier) { | ||
this.handleVariableName(<ts.Identifier> node.name); | ||
} | ||
super.visitBindingElement(node); | ||
} | ||
|
||
public visitParameterDeclaration(node: ts.ParameterDeclaration) { | ||
if (node.name.kind === ts.SyntaxKind.Identifier) { | ||
this.handleVariableName(<ts.Identifier> node.name); | ||
} | ||
super.visitParameterDeclaration(node); | ||
} | ||
|
||
public visitVariableDeclaration(node: ts.VariableDeclaration) { | ||
if (node.name.kind === ts.SyntaxKind.Identifier) { | ||
this.handleVariableName(<ts.Identifier> node.name); | ||
} | ||
super.visitVariableDeclaration(node); | ||
} | ||
|
||
private handleVariableName(name: ts.Identifier) { | ||
const variableName = name.text; | ||
|
||
if(BAD_NAMES.indexOf(variableName) !== -1) { | ||
this.addFailure(this.createFailure(name.getStart(), name.getWidth(), Rule.FAILURE_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
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,23 @@ | ||
let any = 1; | ||
let Number = 2; | ||
let number = 3; | ||
let String = 4; | ||
let string = 5; | ||
let Boolean = 6; | ||
let boolean = 7; | ||
let undefined = 8; | ||
|
||
let Any = 1; | ||
let anyName = 2; | ||
let NumbeR = 3 | ||
let NumberEg = 4; | ||
let numbeR = 5; | ||
let numberEg = 6; | ||
let StrinG = 7; | ||
let StringEg = 8; | ||
let BooleaN = 9; | ||
let BooleanEg = 10; | ||
let booleaN = 11; | ||
let booleanEg = 12; | ||
let undefineD = 13; | ||
let undefinedEg = 14; |
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,8 @@ | ||
let any: any; | ||
let Number: Number; | ||
let number: number; | ||
let String: String; | ||
let string: string; | ||
let Boolean: Boolean; | ||
let boolean: boolean; | ||
let undefined: any; |
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,17 @@ | ||
function F1(any) {} | ||
function F2(Number) {} | ||
function F3(number) {} | ||
function F4(String) {} | ||
function F5(string) {} | ||
function F6(Boolean) {} | ||
function F7(boolean) {} | ||
function F8(undefined) {} | ||
|
||
function G1(any: any) {} | ||
function G2(Number: Number) {} | ||
function G3(number: number) {} | ||
function G4(String: String) {} | ||
function G5(string: string) {} | ||
function G6(Boolean: Boolean) {} | ||
function G7(boolean: boolean) {} | ||
function G8(undefined: any) {} |
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,8 @@ | ||
let [ any ] = [ 1 ]; | ||
let [ Number ] = [ 2 ]; | ||
let [ number ] = [ 3 ]; | ||
let [ String ] = [ 4 ]; | ||
let [ string ] = [ 5 ]; | ||
let [ Boolean ] = [ 6 ]; | ||
let [ boolean ] = [ 7 ]; | ||
let [ undefined ] = [ 8 ]; |
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,8 @@ | ||
let { any } = { any: 1 }; | ||
let { Number } = { Number: 1 }; | ||
let { number } = { number: 1 }; | ||
let { String } = { String: 1 }; | ||
let { string } = { string: 1 }; | ||
let { Boolean } = { Boolean: 1 }; | ||
let { boolean } = { boolean: 1 }; | ||
let { undefined } = { undefined: 1 }; |
Oops, something went wrong.