From a4c7de7559335bea612aac32a6c4773a4bd65219 Mon Sep 17 00:00:00 2001 From: Holm Engelbrecht Date: Tue, 25 Aug 2015 16:01:13 +0200 Subject: [PATCH 1/7] Implemented optional trailing underscore for variableNameRule.ts --- src/rules/variableNameRule.ts | 13 +++++++++---- test/files/rules/varname.test.ts | 2 ++ test/rules/variableNameRuleTests.ts | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/rules/variableNameRule.ts b/src/rules/variableNameRule.ts index 71b8a8bea40..bf27cafd3e3 100644 --- a/src/rules/variableNameRule.ts +++ b/src/rules/variableNameRule.ts @@ -15,6 +15,7 @@ */ const OPTION_LEADING_UNDERSCORE = "allow-leading-underscore"; +const OPTION_TRAILING_UNDERSCORE = "allow-trailing-underscore"; export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING = "variable name must be in camelcase or uppercase"; @@ -71,15 +72,19 @@ class VariableNameWalker extends Lint.RuleWalker { private isCamelCase(name: string) { const firstCharacter = name.charAt(0); - const rest = name.substring(1); + const lastCharacter = name.charAt(name.length - 1); + const middle = name.substr(1, name.length - 2); if (name.length <= 0) { return true; - } else if (!this.hasOption(OPTION_LEADING_UNDERSCORE) && firstCharacter === "_") { + } + if (!this.hasOption(OPTION_LEADING_UNDERSCORE) && firstCharacter === "_") { return false; } - - return firstCharacter === firstCharacter.toLowerCase() && rest.indexOf("_") === -1; + if (!this.hasOption(OPTION_TRAILING_UNDERSCORE) && lastCharacter === "_") { + return false; + } + return firstCharacter === firstCharacter.toLowerCase() && middle.indexOf("_") === -1; } private isUpperCase(name: string) { diff --git a/test/files/rules/varname.test.ts b/test/files/rules/varname.test.ts index d68d9b2287e..9520fa094b6 100644 --- a/test/files/rules/varname.test.ts +++ b/test/files/rules/varname.test.ts @@ -30,3 +30,5 @@ export function anotherFunctionWithInvalidParamNames ([first_element, SecondElem export function functionWithInvalidSpread(invalid_arg: ...number) { // 1 failure // } + +let optionallyValid_ = "bar"; \ No newline at end of file diff --git a/test/rules/variableNameRuleTests.ts b/test/rules/variableNameRuleTests.ts index 920996e50b4..11301ecaab5 100644 --- a/test/rules/variableNameRuleTests.ts +++ b/test/rules/variableNameRuleTests.ts @@ -35,6 +35,7 @@ describe("", () => { createFailure([26, 56], [26, 69]), createFailure([26, 71], [26, 84]), createFailure([30, 43], [30, 54]), + createFailure([34, 5], [34, 21]) ]; const actualFailures = Lint.Test.applyRuleOnFile(fileName, VariableNameRule); @@ -56,4 +57,20 @@ describe("", () => { return optionallyValidFailures.some((f) => f.equals(failure)); })); }); + + it("ensures trailing underscores can optionally be legal", () => { + const options = [true, + "allow-trailing-underscore" + ]; + + const actualFailures = Lint.Test.applyRuleOnFile(fileName, VariableNameRule, options); + const optionallyValidFailures = [ + createFailure([34, 5], [34, 21]) + ]; + + // none of the optionally valid names should appear in the failures list + assert.isFalse(actualFailures.some((failure) => { + return optionallyValidFailures.some((f) => f.equals(failure)); + })); + }); }); From ea11076b99054674b514e7a03d3e31da51bbe9a2 Mon Sep 17 00:00:00 2001 From: Holm Engelbrecht Date: Wed, 26 Aug 2015 08:56:32 +0200 Subject: [PATCH 2/7] Added test for combination of leading and trailing underscores for variableNameRule.ts --- test/files/rules/varname.test.ts | 3 ++- test/rules/variableNameRuleTests.ts | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/test/files/rules/varname.test.ts b/test/files/rules/varname.test.ts index 9520fa094b6..a7dc617d0ed 100644 --- a/test/files/rules/varname.test.ts +++ b/test/files/rules/varname.test.ts @@ -31,4 +31,5 @@ export function functionWithInvalidSpread(invalid_arg: ...number) { // 1 failure // } -let optionallyValid_ = "bar"; \ No newline at end of file +let optionallyValid_ = "bar"; +let _$httpBackend_ = "leading and trailing"; \ No newline at end of file diff --git a/test/rules/variableNameRuleTests.ts b/test/rules/variableNameRuleTests.ts index 11301ecaab5..e36d1117b2f 100644 --- a/test/rules/variableNameRuleTests.ts +++ b/test/rules/variableNameRuleTests.ts @@ -35,7 +35,8 @@ describe("", () => { createFailure([26, 56], [26, 69]), createFailure([26, 71], [26, 84]), createFailure([30, 43], [30, 54]), - createFailure([34, 5], [34, 21]) + createFailure([34, 5], [34, 21]), + createFailure([35, 5], [35, 19]) ]; const actualFailures = Lint.Test.applyRuleOnFile(fileName, VariableNameRule); @@ -73,4 +74,21 @@ describe("", () => { return optionallyValidFailures.some((f) => f.equals(failure)); })); }); + + it("ensures leading & trailing underscores can optionally be legal", () => { + const options = [true, + "allow-leading-underscore", + "allow-trailing-underscore" + ]; + + const actualFailures = Lint.Test.applyRuleOnFile(fileName, VariableNameRule, options); + const optionallyValidFailures = [ + createFailure([35, 5], [35, 19]) + ]; + + // none of the optionally valid names should appear in the failures list + assert.isFalse(actualFailures.some((failure) => { + return optionallyValidFailures.some((f) => f.equals(failure)); + })); + }); }); From ba19fe5110b74b8acb44f8ade747d804279e1b50 Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Thu, 27 Aug 2015 11:57:34 -0400 Subject: [PATCH 3/7] Ignore names in function types when checking for shadowed variables --- lib/tslint.d.ts | 4 ++-- src/language/walker/syntaxWalker.ts | 8 ++++---- src/rules/noShadowedVariableRule.ts | 4 ++++ src/rules/noUnusedVariableRule.ts | 2 +- src/rules/tsconfig.json | 2 +- src/rules/whitespaceRule.ts | 4 ++-- test/files/rules/no-shadowed-variable.test.ts | 12 ++++++++++++ 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/tslint.d.ts b/lib/tslint.d.ts index 2ae69ec9680..537d20668f6 100644 --- a/lib/tslint.d.ts +++ b/lib/tslint.d.ts @@ -13,7 +13,7 @@ declare module Lint { protected visitCatchClause(node: ts.CatchClause): void; protected visitConditionalExpression(node: ts.ConditionalExpression): void; protected visitConstructorDeclaration(node: ts.ConstructorDeclaration): void; - protected visitConstructorType(node: ts.Node): void; + protected visitConstructorType(node: ts.FunctionOrConstructorTypeNode): void; protected visitContinueStatement(node: ts.BreakOrContinueStatement): void; protected visitDebuggerStatement(node: ts.Statement): void; protected visitDefaultClause(node: ts.DefaultClause): void; @@ -27,7 +27,7 @@ declare module Lint { protected visitForOfStatement(node: ts.ForOfStatement): void; protected visitFunctionDeclaration(node: ts.FunctionDeclaration): void; protected visitFunctionExpression(node: ts.FunctionExpression): void; - protected visitFunctionType(node: ts.Node): void; + protected visitFunctionType(node: ts.FunctionOrConstructorTypeNode): void; protected visitGetAccessor(node: ts.AccessorDeclaration): void; protected visitIdentifier(node: ts.Identifier): void; protected visitIfStatement(node: ts.IfStatement): void; diff --git a/src/language/walker/syntaxWalker.ts b/src/language/walker/syntaxWalker.ts index f4d4b033d02..13d1a678792 100644 --- a/src/language/walker/syntaxWalker.ts +++ b/src/language/walker/syntaxWalker.ts @@ -68,7 +68,7 @@ module Lint { this.walkChildren(node); } - protected visitConstructorType(node: ts.Node) { + protected visitConstructorType(node: ts.FunctionOrConstructorTypeNode) { this.walkChildren(node); } @@ -124,7 +124,7 @@ module Lint { this.walkChildren(node); } - protected visitFunctionType(node: ts.Node) { + protected visitFunctionType(node: ts.FunctionOrConstructorTypeNode) { this.walkChildren(node); } @@ -323,7 +323,7 @@ module Lint { break; case ts.SyntaxKind.ConstructorType: - this.visitConstructorType(node); + this.visitConstructorType( node); break; case ts.SyntaxKind.ContinueStatement: @@ -379,7 +379,7 @@ module Lint { break; case ts.SyntaxKind.FunctionType: - this.visitFunctionType(node); + this.visitFunctionType( node); break; case ts.SyntaxKind.GetAccessor: diff --git a/src/rules/noShadowedVariableRule.ts b/src/rules/noShadowedVariableRule.ts index a2adc7c9128..8fe02139a36 100644 --- a/src/rules/noShadowedVariableRule.ts +++ b/src/rules/noShadowedVariableRule.ts @@ -48,6 +48,10 @@ class NoShadowedVariableWalker extends Lint.BlockScopeAwareRuleWalker void) { + let okay1 = 1; +} + +function falsePositive2(okay2: any, done: (okay2: any) => void) { } + +interface FalsePositive3 { + diffuse: (pos: number) => number; + specular: (pos: number) => number; + pos: number; +} From 7fcf99afc16727a3b885eaf92fbb81b3e096d1d0 Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Fri, 28 Aug 2015 14:16:40 -0400 Subject: [PATCH 4/7] Prepare release v2.4.5 --- CHANGELOG.md | 5 +++++ README.md | 1 + package.json | 2 +- src/tslint.ts | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d276cfa49f..05be76aef53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Change Log === +v2.4.5 +--- +* [bugfix] #500 +* [enhancement] add `allow-trailing-underscore` option to `variable-name` rule + v2.4.4 --- * [bugfix] remove "typescript" block from package.json (#606) diff --git a/README.md b/README.md index e57403382f0..ec56ea63e30 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,7 @@ A sample configuration file with all options is available [here](https://github. * `check-function` checks that all top-level functions are using strict mode * `variable-name` allows only camelCased or UPPER_CASED variable names. Rule options: * `"allow-leading-underscore"` allows underscores at the beginnning. + * `"allow-trailing-underscore"` allows underscores at the end. * `whitespace` enforces spacing whitespace. Rule options: * `"check-branch"` checks branching statements (`if`/`else`/`for`/`while`) are followed by whitespace * `"check-decl"`checks that variable declarations have whitespace around the equals token diff --git a/package.json b/package.json index ea29a5cd475..fbdc0f58cd8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tslint", - "version": "2.4.4", + "version": "2.4.5", "description": "a static analysis linter for TypeScript", "bin": { "tslint": "./bin/tslint" diff --git a/src/tslint.ts b/src/tslint.ts index 5a0fd7a928c..ab8f61e7ebf 100644 --- a/src/tslint.ts +++ b/src/tslint.ts @@ -33,7 +33,7 @@ module Lint { } export class Linter { - public static VERSION = "2.4.4"; + public static VERSION = "2.4.5"; private fileName: string; private source: string; From 56efbddc7aaa68596fce202f4742b9b0691cf168 Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Fri, 28 Aug 2015 14:20:49 -0400 Subject: [PATCH 5/7] Tweak changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05be76aef53..3311ed39a9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Change Log v2.4.5 --- -* [bugfix] #500 +* [bugfix] fix false positives on `no-shadowed-variable` rule (#500) * [enhancement] add `allow-trailing-underscore` option to `variable-name` rule v2.4.4 From aed0660ac2ddf08681f989ea533d2f2ce4f8b4dd Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Fri, 28 Aug 2015 14:55:41 -0400 Subject: [PATCH 6/7] Copy typings over --- typings/typescriptServices.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/typings/typescriptServices.d.ts b/typings/typescriptServices.d.ts index 7ea3324afe0..3d4418a9750 100644 --- a/typings/typescriptServices.d.ts +++ b/typings/typescriptServices.d.ts @@ -1304,6 +1304,7 @@ declare namespace ts { diagnostics?: boolean; emitBOM?: boolean; help?: boolean; + init?: boolean; inlineSourceMap?: boolean; inlineSources?: boolean; jsx?: JsxEmit; @@ -1328,6 +1329,7 @@ declare namespace ts { rootDir?: string; sourceMap?: boolean; sourceRoot?: string; + suppressExcessPropertyErrors?: boolean; suppressImplicitAnyIndexErrors?: boolean; target?: ScriptTarget; version?: boolean; @@ -1523,7 +1525,7 @@ declare namespace ts { function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program; } declare namespace ts { - function parseCommandLine(commandLine: string[]): ParsedCommandLine; + function parseCommandLine(commandLine: string[], readFile?: (path: string) => string): ParsedCommandLine; /** * Read tsconfig.json file * @param fileName The path to the config file From 14374da77abe80063be8b872eff4b12dcff8430c Mon Sep 17 00:00:00 2001 From: Jason Killian Date: Fri, 28 Aug 2015 15:00:12 -0400 Subject: [PATCH 7/7] Prepare release v2.5.0-dev.5 --- CHANGELOG.md | 5 +++++ package.json | 2 +- src/tslint.ts | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8a573909b7..3ae218b2eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Change Log === +v2.5.0-dev.5 +--- +* Upgrade TypeScript compiler to v1.7.0-dev.20150828 +* [bugfix] Handle .tsx files appropriately (#597, #558) + v2.5.0-dev.4 --- * Upgrade TypeScript compiler to v1.6.0-dev.20150825 diff --git a/package.json b/package.json index 894a9cc6911..61f7c11f7bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tslint", - "version": "2.5.0-dev.4", + "version": "2.5.0-dev.5", "description": "a static analysis linter for TypeScript", "bin": { "tslint": "./bin/tslint" diff --git a/src/tslint.ts b/src/tslint.ts index c054644f83a..53f8d5e1433 100644 --- a/src/tslint.ts +++ b/src/tslint.ts @@ -33,7 +33,7 @@ module Lint { } export class Linter { - public static VERSION = "2.5.0-dev.4"; + public static VERSION = "2.5.0-dev.5"; private fileName: string; private source: string;