forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no-shadowed-variable: add new option 'temporalDeadZone' (palantir#3389)
- Loading branch information
Showing
4 changed files
with
171 additions
and
18 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
89 changes: 89 additions & 0 deletions
89
test/rules/no-shadowed-variable/temporal-dead-zone/test.ts.lint
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,89 @@ | ||
['a', 'b', 'c'].map((path) => path.resolve(path)) | ||
~~~~ [err % ('path')] | ||
|
||
import * as path from 'path' | ||
|
||
export function foo(wam: boolean) { | ||
if (wam) { | ||
const now = new Date(); // no error because it's in the temporal dead zone of the outer `now` | ||
return now.getTime(); | ||
} | ||
|
||
const now = new Date(); | ||
return now.getTime() + 1000; | ||
} | ||
|
||
function foo(v) { | ||
if (v) { | ||
const Foo = null; // classes are block scoped, therefore no error in the temporal dead zone | ||
return Foo; | ||
} | ||
class Foo {} | ||
return new Foo(); | ||
} | ||
|
||
function foo(v) { | ||
if (v) { | ||
const Foo = null; // interfaces are block scoped, but hoisted -> error in temporal dead zone | ||
~~~ [err % ('Foo')] | ||
return Foo; | ||
} | ||
interface Foo {} | ||
class Foo {} | ||
return new Foo(); | ||
} | ||
|
||
function foo() { | ||
{ | ||
let tmp = 1; // error because `var` is hoisted | ||
~~~ [err % ('tmp')] | ||
return tmp; | ||
} | ||
var tmp = undefined; | ||
return tmp; | ||
} | ||
|
||
function foo(v) { | ||
let tmp = 1; | ||
if (v) { | ||
const tmp = v; | ||
~~~ [err % ('tmp')] | ||
return v; | ||
} else { | ||
const v = tmp; | ||
~ [err % ('v')] | ||
return v; | ||
} | ||
} | ||
|
||
// parameters also have a temporal dead zone | ||
function foo( | ||
a = function() {let b}, | ||
b, | ||
) {} | ||
|
||
{ | ||
{ | ||
class Foo {} // tdz | ||
} | ||
enum Foo {} // tdz | ||
{ | ||
class Foo {} // shadowing enum Foo | ||
~~~ [err % ('Foo')] | ||
} | ||
} | ||
class Foo {} | ||
{ | ||
{ | ||
class Foo {} | ||
~~~ [err % ('Foo')] | ||
} | ||
enum Foo {} | ||
~~~ [err % ('Foo')] | ||
{ | ||
class Foo {} | ||
~~~ [err % ('Foo')] | ||
} | ||
} | ||
|
||
[err]: Shadowed name: '%s' |
10 changes: 10 additions & 0 deletions
10
test/rules/no-shadowed-variable/temporal-dead-zone/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,10 @@ | ||
{ | ||
"rules": { | ||
"no-shadowed-variable": [ | ||
true, | ||
{ | ||
"temporalDeadZone": false | ||
} | ||
] | ||
} | ||
} |