Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use-flow-type marks a reference as used with the wrong reference when names are the same #340

Open
willmruzek opened this issue Jun 1, 2018 · 8 comments
Labels

Comments

@willmruzek
Copy link

Code

import {
	contextType,
} from 'someModule';

export function getLabelSets(
	contextType: $Keys<typeof contextType>,
) { ... }

Versions

"babel-eslint": "^8.2.3",
"eslint": "^4.19.1",
"eslint-plugin-flowtype": "^2.48.0",

Expected

No errors.

Actual

error  'contextType' is defined but never used  no-unused-vars
@gajus gajus added the bug label Jun 1, 2018
@gajus
Copy link
Owner

gajus commented Jun 1, 2018

@mruzekw Can you give a try fixing this?

@willmruzek
Copy link
Author

willmruzek commented Jun 1, 2018

Sure. I don't know what the full scope of the bug is. Is it all typeof clauses in type annotations? Just in generic types? So I'll have to do some digging.

@gajus
Copy link
Owner

gajus commented Jun 1, 2018

Couldn't tell without digging into it myself.

@willmruzek
Copy link
Author

willmruzek commented Jun 6, 2018

So it looks like this only happens when a module def is the same name as a function param.

Here are a couple of examples where this takes affect:

// @flow

import { contextType } from './someModule';

// Should error but doesn't
export function getLabelSets(
	contextType: $Keys<typeof contextType>,
) {
  return; // Notice how contextType function param is not used
}

// Doesn't count the module level definition because the identifier was found in the function scope
export function getLabelSets(
	contextType: $Keys<typeof contextType>,
) {
  return contextType;
}

@willmruzek willmruzek changed the title typeof in $Keys<...> type doesn't count towards no-unused-vars use-flow-type marks a reference as used with the wrong reference when names are the same Jun 6, 2018
@willmruzek
Copy link
Author

Alright, in an attempt to fix this I've found that context.markVariableAsUsed(name) does not provide a way to to set a starting scope. The scope returned for a GenericTypeAnnotation for a function parameter is the function scope. Since the param name is the same as the module-level variable, it still counts it. What we need though is a way to say "skip the function scope because the type reference will be in a parent scope".

Not sure how to proceed here other than raise an issue in the ESLint repo.

@pnevyk
Copy link
Contributor

pnevyk commented Jun 7, 2018

I may be missing something, but would it help if this scope lookup started one scope up?

@willmruzek
Copy link
Author

@pnevyk That's how I attempted to fix it. But notice the markVariableAsUsed implementation here:

https://github.com/eslint/eslint/blob/master/lib/linter.js#L660-L678

@pnevyk
Copy link
Contributor

pnevyk commented Jun 7, 2018

@mruzekw Thanks for clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants