-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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: fix illegal decorator check #6723
fix: fix illegal decorator check #6723
Conversation
Thanks for the PR, @fisker! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## v6 #6723 +/- ##
==========================================
- Coverage 87.75% 87.28% -0.48%
==========================================
Files 372 376 +4
Lines 12814 12974 +160
Branches 3800 3833 +33
==========================================
+ Hits 11245 11324 +79
- Misses 1193 1262 +69
- Partials 376 388 +12
Flags with carried forward coverage won't be shown. Click here to find out more.
|
if (nodeHasIllegalDecorators(node)) { | ||
this.#throwError( | ||
node.illegalDecorators[0], | ||
'Decorators are not valid here.', | ||
); | ||
} | ||
|
||
for (const modifier of getModifiers(node) ?? []) { | ||
// @ts-expect-error -- this is safe as it's guarded | ||
const modifiers: ts.ModifierLike[] = node.modifiers; |
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.
instead of using .modifiers
can we use getModifiers
here?
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.
getModifiers
only returns ts.Modifier
, but we need ts.Decorator
s, unless we call getDecorators
again.
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.
In that same file there is the getDecorators
util which should do what you want I think?
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.
I know.
unless we
cancall getDecorators again.
But I checked both modifiers and decorators here, and getModifiers() + getDecorators()
is node.modifiers
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.
I'm not quite sure I understand - your code below does "for each modifier, only action the decorators"
So couldn't the code be
for (const decorator of getDecorators(node)) {
// your code
}
for (const modifier of getModifiers(node)) {
// old code
}
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.
Ideally, it should work, but I still can't get why you want 4 loops when you can do it in 1 loop.
Anyway, I tried, but turns out getModifiers() + getDecorators()
not equals to node.modifiers
.
ts.canHaveModifiers()
and ts.canHaveDecorators()
are stopping me to access modifiers on some node. You can try it to see the failed tests.
Should we
- Remove those check
- Add option to skip check
?
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.
The problem here is that they purposely removed node.modifiers
from the types in TSv5 - so we definitely don't want to be accessing it directly if we can avoid it as it's clear that their intention is for it to be a truly private property.
I guess we could add an option to skip the checks?
useLegacyDecorators => | ||
// @ts-expect-error -- internal api | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
ts.nodeCanBeDecorated?.( |
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.
we can't use this function, or at least not without version checks.
in versions before 5.0 this function did not take boolean
as the first argument - so by calling it like this we will cause a crash.
Given it's an internal API - I think we might want to consider rewriting it for ourselves? The logic will be pretty static given the spec doesn't change much if at all.
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.
in versions before 5.0 this function did not take boolean as the first argument
Didn't know that.
I think we might want to consider rewriting it for ourselves?
I can try.
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.
Added acaee6b, but I don't know the equivalent of ts.hasAmbientModifier
, can you help?
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.
hasAmbientModifier
was only added in TS 4.3, and currently on v6 we are supporting 4.2
However based on the 2y lifecycle of versions that DefinitelyTyped supports (which we want to roughly align with) we will actually now be able to bump the minimum version to 4.3.
We can queue a separate PR for that then update this PR to use ts.hasAmbientModifier
(#6923)
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.
ts.hasAmbientModifier
seems not available https://github.com/typescript-eslint/typescript-eslint/actions/runs/4739172024/jobs/8413765640?pr=6723#step:4:98
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.
it's still a private, internal API!
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.
Let's use a fake one for now? a84fa78
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.
If we're going with a fake () => false
one then I just removed it altogether. Seems like everything is working well without it? I posted a link in microsoft/TypeScript#52727 (comment).
@fisker just checking in - are you waiting for us on anything for this one? I'm not sure from reading through the comments, sorry. |
I don't know how |
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.
Swell, thanks @fisker! I'll merge this now. 🚀 Thanks for the informative discussions!
PR Checklist
Overview
Copied from prettier https://github.com/prettier/prettier/blob/ddf3b43c33e2e98f6413b5232ad623876d96738e/src/language-js/parse/postprocess/typescript.js#L46