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 887
Rule to prevent certain keywords being used as variable names #735
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,15 +32,16 @@ | |
"./rules/noConditionalAssignmentRule.ts", | ||
"./rules/noConsecutiveBlankLinesRule.ts", | ||
"./rules/noConsoleRule.ts", | ||
"./rules/noConstructRule.ts", | ||
"./rules/noConstructorVarsRule.ts", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a negative diff, simply correcting ordering |
||
"./rules/noConstructRule.ts", | ||
"./rules/noDebuggerRule.ts", | ||
"./rules/noDuplicateKeyRule.ts", | ||
"./rules/noDuplicateVariableRule.ts", | ||
"./rules/noEmptyRule.ts", | ||
"./rules/noEvalRule.ts", | ||
"./rules/noInferrableTypesRule.ts", | ||
"./rules/noInternalModuleRule.ts", | ||
"./rules/noKeywordNamedVariablesRule", | ||
"./rules/noRequireImportsRule.ts", | ||
"./rules/noShadowedVariableRule.ts", | ||
"./rules/noStringLiteralRule.ts", | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a comprehensive list of keywords/types that tsc will let you redefine? It looks good to me, just want to make sure there isn't anything missing or extraneous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkillian started with this to kick off discussion.
Arguably this list could include any ambient variable from
lib.d.ts
, likeNumber
,String
andBoolean
The idea behind this rule is to increase the likelihood of writing code that is unambiguously understood by as many as possible. You could easily chuck
RegExp
,Array
and other core interfaces that have real class equivalents in there too...But then you could probably well argue it goes too far to include
event
for example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotchya, I'm fine with it like this for this PR. Changing the list is easy and essentially backwards-compatible, so that can be discussed down the road.