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
fix(eslint-plugin): [prefer-optional-chain] fix ThisExpression
and PrivateIdentifier
errors
#6028
Changes from 8 commits
71cb502
5acc1af
a3b6eda
a68b13a
7e2185e
1a30e94
21cfbd2
0ae6c45
ecfacff
3ce9b0a
c3c458e
9c15f28
ab70949
e58dab9
12effef
0afc516
8efa51d
57b86c1
99528f0
cbb5168
b6d265c
54d4664
ac8b6a2
a2a6314
37716b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -194,6 +194,18 @@ ruleTester.run('prefer-optional-chain', rule, { | |||||||||
'match && match$1 !== undefined;', | ||||||||||
'foo !== null && foo !== undefined;', | ||||||||||
"x['y'] !== undefined && x['y'] !== null;", | ||||||||||
// private properties | ||||||||||
'this.#a && this.#b;', | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should still report a complaint: class Violation {
#property = 0;
test() {
const self = Math.random() ? this : undefined;
self && self#.property;
}
} I suspect the issue is returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bump on this 😇 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The highlighted line has two different properties ( typescript-eslint/packages/eslint-plugin/tests/rules/prefer-optional-chain.test.ts Lines 141 to 144 in 0afc516
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah sorry you're right, I missed that they're the same property. But this actually brings up that there's a bug in this PR for private identifiers: microsoft/TypeScript#42734 blocks them from being used. class Violation {
#property = 0;
test() {
const self = Math.random() ? this : undefined;
self && self.#property;
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So do I revert it for now? What do you suggest? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it shouldn't throw an error - that's definitely a bug. But it also shouldn't complain. The rule should effectively ignore this case for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kept the private properties in computed nodes |
||||||||||
'!this.#a || !this.#b;', | ||||||||||
'a.#foo && a.#foo.bar;', | ||||||||||
'!a.#foo || !a.#foo.bar;', | ||||||||||
'a.#foo?.bar;', | ||||||||||
'!a.#foo?.bar;', | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More test cases to consider: foo().#a
a.b.#a
new A().#b
(await a).#b I have no idea why this rule is so strict about what expressions it can lint, but you can in fact use private properties on all expressions, just that some may have nonsensical behaviors (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I fix the rule to not intentionally throw? this seems irrational to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would love to change this rule to not intentionally throw, or at least catch the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah as Brad said in #6028 (comment), I think the rule should no longer throw. Throwing is useful for us finding missing combinations but is inconvenient for users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, so I misunderstood his comment |
||||||||||
'!foo().#a || a;', | ||||||||||
'!a.b.#a || a;', | ||||||||||
'!new A().#b || a;', | ||||||||||
'!(await a).#b || a;', | ||||||||||
"!(foo as any).bar || 'anything';", | ||||||||||
// currently do not handle complex computed properties | ||||||||||
'foo && foo[bar as string] && foo[bar as string].baz;', | ||||||||||
'foo && foo[1 + 2] && foo[1 + 2].baz;', | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit like a hack since typescript doesn't seem to think they are assignable to
MemberExpression
if (node.object.type === AST_NODE_TYPES.TSAsExpression) {}
shows a typescript error, but with Array#includes it does notThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit of a hack 😄 but the root cause is a bug in our typings, I think. A node's member can be an
await
ornew
. [typescript-eslint playground]For now, you can use a
// @ts-expect-error
and link to#6239#6192