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

no-shadowed-variable: please add scope options #3000

Closed
rokoroku opened this issue Jul 6, 2017 · 6 comments · Fixed by #3030
Closed

no-shadowed-variable: please add scope options #3000

rokoroku opened this issue Jul 6, 2017 · 6 comments · Fixed by #3030

Comments

@rokoroku
Copy link

rokoroku commented Jul 6, 2017

  • TSLint version: v5.5.0
  • TypeScript version: 2.4.1
  • Running TSLint via: VSCode / gulp-tslint

Description

There is an update for no-shadowed-variable today #2598 .

Enhancement is really good, however, It breaks lots of our code.

It should be options for check interfaces, classes, type parameters, imports, etc so that we can granularly adapt to the rule with specific scopes.

@ajafff
Copy link
Contributor

ajafff commented Jul 6, 2017

It should be options for check interfaces, classes, type parameters, imports, etc so that we can granularly adapt to the rule with specific scopes.

Makes sense. I'd rather ignore specific declarations instead of enabling the desired checks.

My idea is to ignore names of ignored declarations completely.
Let's assume you have variables enabled and classes disabled:

  • variable shadowed by another variable -> error for the second variable
  • variable shadowed by class -> no error
  • class shadowed by variable -> no error
  • variable shadowed by class shadowed by variable -> error for the second variable
  • class shadowed by variable shadowed by class -> no error

@ajafff
Copy link
Contributor

ajafff commented Jul 6, 2017

Currently the following declarations are checked:

  • variables
  • parameters
  • all kinds of imports
  • functions
  • namespaces
  • enums
  • classes
  • interfaces
  • type parameters
  • type aliases

For which of them do we need an option to ignore it? Do we want to add an option for each of them (except variables and parameters) or an option to ignore all types (enum through type alias)?

@rokoroku
Copy link
Author

rokoroku commented Jul 7, 2017

My main breakages are

  1. same function name and callback argument
db.transaction((transaction) => { // Shadowed name: 'transaction'
  return model.doSomething({ transaction }); 
});
  1. parameters in a class decorator
class SomeClass {
  @memoize({ normalizer: ([arg1, arg2]) => `...` })
  someMethod(arg1, arg2) { ... } // Shadowed name: 'arg1', 'arg2'
}
  1. generics in class static methods
export class GenericClass<T = any> {
  data: T;
  static genericFunction<T>(args: T) { ... } // Shadowed name: 'T'
}

It seems like (1) can be achieved via ignoring function type, but (2) and (3) cannot be achieved in this way. (EDIT: (3) could be suppressed with ignoring type parameters, right? )

So I want to suggest specifying a scope to be checked.. e.g. block scope, function scope, class scope, namespace scope, and file scope.

@ajafff
Copy link
Contributor

ajafff commented Jul 10, 2017

(1) I cannot reproduce this with the code provided. There has to be a a function or variable named transaction in a parent scope for the error to occur. But I get your point.

The following would be expected:

transaction((transaction) => { // Shadowed name: 'transaction'
  // do stuff
});

(2) Cannot reproduce with the provided code snippet. This code should not result in an error.

(3) Yeah, that could be suppressed by ignoring type parameters.


Seems like it's better to add an option for each kind of declaration.

@rokoroku
Copy link
Author

(1) I found that I create a new variable named transaction below the function. It's my fault ;)

(2) Let me show you my screen:
2017-07-11 1 16 25

let memoize: any;
class TestClass {
    @memoize({ normalizer: (arg1) => true }, )
    getArg1(arg1: any, arg2: any) {
        return JSON.stringify(arguments);
    }

    @memoize({ normalizer: (arg2) => false }, )
    getArg2(arg1: any, arg2: any) {
        return JSON.stringify(arguments);
    }
}

I'll appreciate if you provide any options for this rule, regardless of my problem 👍

@ajafff
Copy link
Contributor

ajafff commented Jul 11, 2017

@rokoroku #3030 adds the ability to disable certain checks.
(2) is really a bug. My PR also fixes this.

adidahiya pushed a commit that referenced this issue Aug 2, 2017
[new-rule-option] `no-shadowed-variable` let's you optionally ignore certain kinds of declarations
[bugfix] `no-shadowed-variable` fixed false positive with parameter inside function decorator
Fixes: #3000
[bugfix] `no-shadowed-variable` don't warn for shadowed type parameter on static class members
Fixes: #3019
@adidahiya adidahiya modified the milestones: TSLint v5.6, TSLint 5.x Aug 2, 2017
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this issue Apr 9, 2018
[new-rule-option] `no-shadowed-variable` let's you optionally ignore certain kinds of declarations
[bugfix] `no-shadowed-variable` fixed false positive with parameter inside function decorator
Fixes: palantir#3000
[bugfix] `no-shadowed-variable` don't warn for shadowed type parameter on static class members
Fixes: palantir#3019
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.

3 participants