-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Incorrectly triggers no-undef and no-redeclare in some cases #2477
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
Comments
#2462 covers the |
Should 4.x be finding types from third-party namespaces? Things like import * as React from 'react';
function Hello(): JSX.Element { // 'JSX' is not defined. eslint(no-undef)
return <h1>Hello</h1>;
}
export default Hello; ↓ ↓ ↓
I read the release notes, and it mentions |
The recommended set turns of Some people do prefer to have the rule on so they can get the errors via a lint run. Each to their own.
You can define any additional globals you need for your project: https://eslint.org/docs/user-guide/configuring#specifying-globals |
Right, understood. What do we need to do in order to get
Sure, but does that apply to React/JSX? We didn't have to do that in v3 - is that because v3 was "broken" and v4 is fixed, as is somewhat mentioned in the release notes? |
Pre v4 the scope analyser (this piece of infra which backs rules like The implementation was a hastily patched version of the core eslint scope analyser in an attempt to teach it about some common use cases for types, but it was a bad approach to solve the problem because typescript's types scope is super complex. There were many bugs, and it sucked. For v4 I forked the core eslint scope analyser and more or less rewrote it from the ground up so that it can analyse and understand the dual scope (values and types) nature of TS. So now you need to define globals for global types (as you would for global variables) so that rules can correctly understand your project. |
Thanks for the clarification and all your work on this. |
What about the no-redeclare case?
this triggers |
Btw, after fixing a lot of instances of the no-redeclare issues in our large codebase it seems that the current behavior is beneficial. Some 80%-90% of the situations where type is named the same as function/variable are not correct and requires rename of the type. Only the remaining cases are ones where it is actually intended to simulate class/enum-like variable/type combo. |
I think most codebases will not want to allow type/variable redeclaration like that. For those cases just using a disable comment is probably better to indicate that it's entirely intention they're named the same. But I think adding an option to allow it (defaulting to false) is probably a good idea for codebases that use the pattern a lot. TS will catch any actual issues that might arise so no point in completely blocking it. |
Yes I admit that disallow type/variable redeclaration is reasonable, we can note it in rule's document maybe |
Fixes #2455 And part of #2477 JSX is a first-class citizen of TS, so we should really support it as well. I was going to just rely upon `eslint-plugin-react`'s patch lint rules (`react/jsx-uses-react` and `react/jsx-uses-vars`), but that leaves gaps in our tooling. For example #2455, `consistent-type-imports` makes assumptions and can create invalid fixes for react without this change. We could add options to that lint rule for the factory, but that is kind-of a sub-par experience and future rule authors will likely run into similar problems. - Adds full scope analysis support for JSX. - Adds two new `parserOption`: - `jsxPragma` - the name to use for constructing JSX elements. Defaults to `"React"`. Will be auto detected from the tsconfig. - `jsxFragmentName` - the name that unnamed JSX fragments use. Defaults to `null` (i.e. assumes `React.Fragment`). Will be auto detected from the tsconfig.
Fixes #2455 And part of #2477 JSX is a first-class citizen of TS, so we should really support it as well. I was going to just rely upon `eslint-plugin-react`'s patch lint rules (`react/jsx-uses-react` and `react/jsx-uses-vars`), but that leaves gaps in our tooling. For example #2455, `consistent-type-imports` makes assumptions and can create invalid fixes for react without this change. We could add options to that lint rule for the factory, but that is kind-of a sub-par experience and future rule authors will likely run into similar problems. - Adds full scope analysis support for JSX. - Adds two new `parserOption`: - `jsxPragma` - the name to use for constructing JSX elements. Defaults to `"React"`. Will be auto detected from the tsconfig. - `jsxFragmentName` - the name that unnamed JSX fragments use. Defaults to `null` (i.e. assumes `React.Fragment`). Will be auto detected from the tsconfig.
|
@bradzacher regarding I have a class that represents a react component but in order to be used as one it has to be wrapped in a /**
* react component to implement Popout_Cls hook class.
* @see Popout_Cls for full details
*/
export const Popout = Popout_Cls.createComponent();
/** type is an alias to Popout_Cls so that importing react component also imports ref type */
export type Popout = Popout_Cls;
export default Popout; This way the behaviour of importing a So at least for me an option to allow type and const overlap, but only when they are declared directly sequentially would be a nice option, but don't worry about implementing it just for me. 😛 |
Using the latest release (eslint, typescript-eslint), I still get "no-redeclare" errors for all instances of function (header) overloading. |
I also get |
@bradzacher The Using
|
Use @typescript-eslint versions of the no-shadow and no-redeclare rules. Remove the no-undef rule since that is covered by typescript and not in the recommended set of rules. See also <typescript-eslint/typescript-eslint#2477 (comment)>.
Use @typescript-eslint versions of the no-shadow and no-redeclare rules. Remove the no-undef rule since that is covered by typescript and not in the recommended set of rules. See also <typescript-eslint/typescript-eslint#2477 (comment)>.
Use @typescript-eslint versions of the no-shadow and no-redeclare rules. Remove the no-undef rule since that is covered by typescript and not in the recommended set of rules. See also <typescript-eslint/typescript-eslint#2477 (comment)>.
Repro
https://github.com/otakustay/typescript-eslint-issue
Expected Result
No errors
Actual Result
Additional Info
Versions
@typescript-eslint/parser
4.0.1
TypeScript
4.0.2
ESLint
7.8.1
node
14.7.0
The text was updated successfully, but these errors were encountered: