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

"no-shadowed-variable": vars shouldn't be shadowed by later declared vars #3078

Closed
benny-medflyt opened this issue Jul 26, 2017 · 4 comments · Fixed by #3389
Closed

"no-shadowed-variable": vars shouldn't be shadowed by later declared vars #3078

benny-medflyt opened this issue Jul 26, 2017 · 4 comments · Fixed by #3389

Comments

@benny-medflyt
Copy link

Bug Report

  • TSLint version: 5.5.0
  • TypeScript version: 2.4.2
  • Running TSLint via: CLI

TypeScript code being linted

export function foo(wam: boolean) {
    if (wam) {
        const now = new Date();
        return now.getTime();
    }

    const now = new Date();
    return now.getTime() + 1000;
}

with tslint.json configuration:

{
    "no-shadowed-variable": true
}

Actual behavior

ERROR: 3:15     no-shadowed-variable  Shadowed name: 'now'

Expected behavior

Even though the inner now variable is inside an inner scope where a variable of the same name resides, I don't believe that there is actually any shadowing.

Indeed the inner block cannot access the outer now variable. If we try (by commenting out line 3) then we get this tsc compile error:

error TS2448: Block-scoped variable 'now' used before its declaration

Code in the style above is pretty common IMHO and is readable, and should not trigger "no-shadowed-variable" (as I have shown there is technically no shadowing)

@ajafff
Copy link
Contributor

ajafff commented Jul 27, 2017

You're right that it doesn't make much sense in your example.

As soon as you have a closure in your block, that a different story:

export function foo(wam: boolean) {
    if (wam) {
        const now = new Date(); // if you remove this line, the next line will use `now` from the outer scope
        setTimeout(() => console.log(now));
    }

    const now = new Date();
    setTimeout(() => console.log(now));
}

If there is enough demand from the community, we could add an option to ignore variables that are shadowed in their temporal dead zone, i.e. shadowed before the declaration.

@benny-medflyt
Copy link
Author

If you remove that line, then the code won't pass tslint due to "no-use-before-declare"

@shelacek
Copy link

shelacek commented Aug 1, 2017

I think, this is valid concern. Please add option that take temporal dead zone into account. We have such great keywords like let and const, but they are devalued by this rule.

@adidahiya
Copy link
Contributor

If you remove that line, then the code won't pass tslint due to "no-use-before-declare"

Note that we don't really take this rule into account when making TSLint design decisions; it's not very useful with let / const syntax, it's slow to compute, and it's not enabled in the built-in configuration presets.

@ajafff ajafff self-assigned this Oct 23, 2017
@adidahiya adidahiya added this to the TSLint v5.9 milestone Nov 28, 2017
nathanleiby added a commit to Clever/dev-handbook that referenced this issue Aug 4, 2018
comment was
```
// To enable this, we need whitelist support and for this bug to be fixed: palantir/tslint#3078
```

remove it in order to have a valid JSON
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants