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 885
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no-duplicate-variable: handle parameters (#2597)
[new-rule-option] `no-duplicate-variable` adds `check-parameters` to check if variable has the same name as a parameter
- Loading branch information
Showing
5 changed files
with
249 additions
and
41 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
5 changes: 5 additions & 0 deletions
5
test/rules/no-duplicate-variable/check-parameters/tslint.json
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,5 @@ | ||
{ | ||
"rules": { | ||
"no-duplicate-variable": [true, "check-parameters"] | ||
} | ||
} |
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,177 @@ | ||
var duplicated = 1; | ||
|
||
function f(x) { | ||
var x; | ||
} | ||
|
||
class Test { | ||
private myFunc() { | ||
var notDuplicated = 123, | ||
duplicated = 234, | ||
someFunc = () => { | ||
var notDuplicated = 345; | ||
}; | ||
|
||
var duplicated = null; | ||
~~~~~~~~~~ [err % ('duplicated')] | ||
} | ||
} | ||
|
||
function test() { | ||
var notDuplicated = 123, | ||
duplicated = 234, | ||
someFunc = () => { | ||
var notDuplicated = 345; | ||
}; | ||
|
||
var duplicated = null; | ||
~~~~~~~~~~ [err % ('duplicated')] | ||
} | ||
|
||
duplicated = 2; | ||
var duplicated = 3; | ||
~~~~~~~~~~ [err % ('duplicated')] | ||
|
||
// valid code | ||
module tmp { | ||
var path = require("path"); | ||
export class MyType { | ||
path: string; | ||
} | ||
} | ||
|
||
module MyModule { | ||
export class ClassA { | ||
id: string; | ||
} | ||
|
||
export class ClassB { | ||
id: string; | ||
} | ||
} | ||
|
||
var a = { | ||
foo(): void { | ||
var bar = 1; | ||
}, | ||
baz(): void { | ||
var bar = 1; | ||
} | ||
}; | ||
|
||
class AccessorTest { | ||
get accesor1(): number { | ||
var x = 0; | ||
return x; | ||
} | ||
|
||
get accesor2(): number { | ||
var x = 0; | ||
return x; | ||
} | ||
|
||
} | ||
|
||
class NoDupConstructor { | ||
private test: string; | ||
constructor() { | ||
var test = "test"; | ||
this.test = test; | ||
} | ||
} | ||
|
||
// valid/invalid code | ||
function letTesting() { | ||
var a = 1; | ||
let b = 1; | ||
let d = 1; | ||
if (true) { | ||
let a = 2; | ||
let b = 2; | ||
let c = 2; | ||
var d = 2; | ||
var e = 2; | ||
} | ||
else { | ||
let b = 3; | ||
let c = 3; | ||
let e = 3; | ||
let f = 3; | ||
} | ||
var f = 4; | ||
} | ||
|
||
// failure: two arguments have the same name. | ||
function testArguments1(arg: number, arg: number): void { | ||
} | ||
|
||
// local var/let declarations shadow arguments. Use options "check-parameters" to catch this | ||
function testArguments2(x: number, y: number): void { | ||
var x = 1; | ||
let y = 2; | ||
} | ||
|
||
var references: {[vertex: string]: any}; | ||
var dependents: {[vertex: string]: any}; | ||
|
||
function blah(arg1: {[key: string]: any}, arg2: {[key:string]: any}) { | ||
} | ||
|
||
interface IClipboard { | ||
copy(key: string, state: any): void; | ||
paste(key: string): any; | ||
findMaxOrMin(values: any[], defaultValue: number, operation: (...values: any[]) => number); | ||
functionA: (value: string) => void; | ||
functionB: (value: string) => void; | ||
} | ||
|
||
try { | ||
// | ||
} catch (e) { | ||
e.blah(); | ||
// | ||
} | ||
|
||
try { | ||
// | ||
} catch (e) { | ||
e.blah(); | ||
// | ||
} | ||
|
||
function testDestructuring() { | ||
function myFunc() { | ||
return [1, 2]; | ||
} | ||
|
||
var [x, y] = myFunc(); | ||
var [z, z] = myFunc(); // failure | ||
~ [err % ('z')] | ||
|
||
let [x1, y1] = myFunc(); | ||
let [z1, z1] = myFunc(); // tsc error | ||
|
||
const [x2, y2] = myFunc(); | ||
const [z2, z2] = myFunc(); // tsc error | ||
|
||
let [a1, [b1, c1]] = [1, [2, 3]]; | ||
let [{a1, d1}] = [{a1: 1, d1: 4}]; // tsc error | ||
|
||
var [a2, [b2, c2]] = [1, [2, 3]]; | ||
var [{a2, d2}] = [{a2: 1, d2: 4}]; // failure | ||
~~ [err % ('a2')] | ||
|
||
function myFunc2([a, b]) { | ||
var a; | ||
return b; | ||
} | ||
|
||
var [x, y3] = myFunc(); // failure | ||
~ [err % ('x')] | ||
var [x3, ...y] = [1, 2, 3, 4]; // failure | ||
~ [err % ('y')] | ||
} | ||
|
||
function compileErrors(foo, {bar}, bar, foo, {baz}, [baz]) {} | ||
|
||
[err]: Duplicate variable: '%s' |
File renamed without changes.