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
parser: v4.1+ breaks an eslint-plugin-react no-unused-vars
test that passes on v4.0
#3788
Comments
Pinned to v4.0 pending typescript-eslint/typescript-eslint#3788
Pinned to v4.0 pending typescript-eslint/typescript-eslint#3788
v4.0 introduced our new scope analyser, and v4.1 added support for handling the JSX pragma reference (pr: #2498). The reason your test is failing is because it's expecting there to be a No users should complain unless for some weird reason they want to see unused variable errors against There are config options you can provide to control this behaviour: |
Hmm, so you're saying that the TS parser as of v4.1 automatically adds a React import even if it's not present in the code?? |
To clarify: As of 4.1 our scope analyser just understands that var React;
<></> transpiles to var React;
React.jsx(React.Fragment,{}); So the scope analyser creates a reference at the JSX location to the top-level Example: This code fragment: typescript-eslint/packages/scope-manager/tests/fixtures/jsx/factory/default-jsxPragma-fragment.tsx Lines 3 to 5 in 043462e
Creates a scope tree with the Lines 7 to 27 in 043462e
Put another way - if they are using at least v4.1 of our parser - users do not need the |
ah, gotcha, thanks - that clarifies it. That's a bit unfortunate in one way - I'm encouraging people to extend |
No reason why you couldn't add |
hmm, that doesn't seem to be making the test pass - is there a way i can modify the test case so the TS eslint parser doesn't mark |
Experiencing the same issue. Getting false negatives from no-unused-vars rule. I'm not using react's automatic runtime, but typescript-eslint reports React as unused variable. Related #3303 |
@bradzacher ping on the above question :-) |
It should be typescript-eslint/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts Lines 1580 to 1605 in 02c6ff3
@smashercosmo - please don't piggy back on issues - file a new issue and follow the issue template to get support. |
@bradzacher i'm still not seeing this work. For example, in our /*eslint no-undef:1*/
var React;
class Hello extends React.Component {
render() {
return <this.props.tag />
}
} In v4 latest of the TS parser, we get this error output:
However, |
@ljharb - that issue is unrelated to this one. That issue is because our scope analyser does not understand that I didn't actually know that was even valid JSX (do people actually write that!?!) which is why it's not supported. File #4055 to track that bug. Funnily whilst playing around - TS type-checks this correctly; however flow does not. |
Thanks; I'm also seeing a few other examples (this has 2 but should have 1, the extra is below): /*eslint no-undef:1*/ var React; React.render(<appp.foo.Bar />);
which seems like a similar issue around namespaced jsx? However, with jsxPragma set to null, i'd expect the same behavior in v4.0.0 of the parser - ie, using the old jsx transform. |
(I might be misunderstanding here) |
hmm, you're right - in that case i'm confused why with v4.0 or below i don't get that error and why v4.1+ i do. The whole point of the |
Put another way, someone using typescript and jsx could be using any kind of jsx transpilation they like, via babel - so how can this parser make any assumptions about what the jsx transpiles to? All that said, |
Before 4.0 we essentially relied on the In 4.0 I forked and rebuilt the scope analyser completely so that it properly understands TypeScript types. Because of this you will find that some of
It's worth noting that it's not just JSX that we do this extended analysis for - we also do it for decorators!TS has support for an old version of the decorator spec. With the class Foo {}
@decorator
class Bar {
constructor(arg: Foo){ }
} In this code looks like // .......
Bar = __decorate([
decorator,
__metadata("design:paramtypes", [Foo])
], Bar); Why is this important? Because if - for example - that isn't a class declaration, but is instead an import - then it's really important what type of reference it is. Rules like our This extended analysis has a huge benefit in that it means that users no longer have to turn on 2 additional rules to make |
Doesn't that presume that TS users are using Is there a way I can disable these three behaviors via an option? If not, then I probably need to just disable all the "new TS parser" tests entirely for these three rules. |
Yes, but TS's type checking assumes this as well - it assumes certain semantics about how the transform will work - it has to or else it could not typecheck JSX at all. Given how huge react is - they simply assume a "react-like" transform which will do something akin to a JSX expression being transformed to this: I.e. it assumes that:
Which are all generally applicable assumptions to make for JSX implementations I believe.
No - by default TS will assume react because it's the largest - but it's user-configurable via the TS also supports
No, sorry. They're all baked into the scope analyser's logic with no gating.
I probably would do that yeah, unless you can annotate different expected errors depending on the version in case you wanted to make sure the rules "play nice" with the scope analyser. Considering how "simple" your rules are (they just mark variables as used) there's no reason they should ever clash. |
re Thanks, I'll go ahead and close this if in fact, these three rules are the only breakage on v4.1+. |
Confirmed; those three rules were the only failures on v4.1+. I have almost 200 failures on v5, but that's probably something different :-) |
Specifically, https://github.com/ljharb/eslint-plugin-react/runs/3415051454 shows the failures, but when I pin it to v4.0, they pass locally and in CI: https://github.com/ljharb/eslint-plugin-react/actions/runs/1164023886
This seems like a breaking change, but if there's a simple config change I can do to make the tests pass (and to tell users if they complain) that would likely suffice.
The text was updated successfully, but these errors were encountered: