Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #735 from myitcv/no_keyword_variable_name
Browse files Browse the repository at this point in the history
Rule to prevent certain keywords being used as variable names
  • Loading branch information
jkillian committed Oct 20, 2015
2 parents 4ef8e52 + 4f35de9 commit 135c6ac
Show file tree
Hide file tree
Showing 11 changed files with 320 additions and 47 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ A sample configuration file with all options is available [here](https://github.
* `no-eval` disallows `eval` function invocations.
* `no-inferrable-types` disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean
* `no-internal-module` disallows internal `module`, use `namespace` instead.
* `no-keyword-named-variables` disallows the use of certain TypeScript keywords (`any`, `Number`, `number`, `String`, `string`, `Boolean`, `boolean`, `undefined`) as variable or parameter names
* `no-require-imports` disallows require() style imports
* `no-string-literal` disallows object access via string literals.
* `no-switch-case-fall-through` disallows falling through case statements.
Expand Down
121 changes: 75 additions & 46 deletions docs/sample.tslint.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,64 @@
{
"rules": {
"align": [true,
"parameters",
"arguments",
"statements"],
"align": [
true,
"parameters",
"arguments",
"statements"
],
"ban": false,
"class-name": true,
"comment-format": [true,
"check-space",
"check-lowercase"
"comment-format": [
true,
"check-space",
"check-lowercase"
],
"curly": true,
"eofline": true,
"forin": true,
"indent": [true, "spaces"],
"indent": [
true,
"spaces"
],
"interface-name": true,
"jsdoc-format": true,
"label-position": true,
"label-undefined": true,
"max-line-length": [true, 140],
"max-line-length": [
true,
140
],
"member-access": true,
"member-ordering": [true,
"public-before-private",
"static-before-instance",
"variables-before-functions"
"member-ordering": [
true,
"public-before-private",
"static-before-instance",
"variables-before-functions"
],
"no-any": false,
"no-arg": true,
"no-bitwise": true,
"no-conditional-assignment": true,
"no-console": [true,
"debug",
"info",
"time",
"timeEnd",
"trace"
"no-console": [
true,
"debug",
"info",
"time",
"timeEnd",
"trace"
],
"no-construct": true,
"no-constructor-vars": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-shadowed-variable": true,
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-inferrable-types": false,
"no-internal-module": true,
"no-keyword-named-variables": true,
"no-require-imports": true,
"no-shadowed-variable": true,
"no-string-literal": true,
"no-switch-case-fall-through": true,
"no-trailing-whitespace": true,
Expand All @@ -57,46 +69,63 @@
"no-var-keyword": true,
"no-var-requires": true,
"object-literal-sort-keys": true,
"one-line": [true,
"check-open-brace",
"check-catch",
"check-else",
"check-whitespace"
"one-line": [
true,
"check-open-brace",
"check-catch",
"check-else",
"check-whitespace"
],
"quotemark": [
true,
"double",
"avoid-escape"
],
"quotemark": [true, "double", "avoid-escape"],
"radix": true,
"semicolon": true,
"switch-default": true,
"trailing-comma": [true, {
"trailing-comma": [
true,
{
"multiline": "always",
"singleline": "never"
}],
"triple-equals": [true, "allow-null-check"],
"typedef": [true,
"call-signature",
"parameter",
"property-declaration",
"variable-declaration",
"member-variable-declaration"
}
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [true, {
"typedef": [
true,
"call-signature",
"parameter",
"property-declaration",
"variable-declaration",
"member-variable-declaration"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}],
"use-strict": [true,
"check-module",
"check-function"
}
],
"use-strict": [
true,
"check-module",
"check-function"
],
"variable-name": false,
"whitespace": [true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}
58 changes: 58 additions & 0 deletions src/rules/noKeywordNamedVariablesRule.ts
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));
}
}
}
3 changes: 2 additions & 1 deletion src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@
"./rules/noConditionalAssignmentRule.ts",
"./rules/noConsecutiveBlankLinesRule.ts",
"./rules/noConsoleRule.ts",
"./rules/noConstructRule.ts",
"./rules/noConstructorVarsRule.ts",
"./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",
Expand Down
23 changes: 23 additions & 0 deletions test/files/rules/nokeywordnamedvariables_1.test.ts
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;
8 changes: 8 additions & 0 deletions test/files/rules/nokeywordnamedvariables_2.test.ts
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;
17 changes: 17 additions & 0 deletions test/files/rules/nokeywordnamedvariables_3.test.ts
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) {}
8 changes: 8 additions & 0 deletions test/files/rules/nokeywordnamedvariables_4.test.ts
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 ];
8 changes: 8 additions & 0 deletions test/files/rules/nokeywordnamedvariables_5.test.ts
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 };
Loading

0 comments on commit 135c6ac

Please sign in to comment.