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

fix: fix illegal decorator check #6723

Merged
merged 20 commits into from Jun 17, 2023

Conversation

fisker
Copy link
Contributor

@fisker fisker commented Mar 21, 2023

PR Checklist

Overview

Copied from prettier https://github.com/prettier/prettier/blob/ddf3b43c33e2e98f6413b5232ad623876d96738e/src/language-js/parse/postprocess/typescript.js#L46

@typescript-eslint
Copy link
Contributor

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.

@netlify
Copy link

netlify bot commented Mar 21, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit e7743d4
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/648d0d26b3171f00080ba75d
😎 Deploy Preview https://deploy-preview-6723--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@fisker fisker changed the title Fix illegal decorator check fix: fix illegal decorator check Mar 21, 2023
@nx-cloud
Copy link

nx-cloud bot commented Mar 21, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit e7743d4. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 31 targets

Sent with 💌 from NxCloud.

@codecov
Copy link

codecov bot commented Mar 21, 2023

Codecov Report

Merging #6723 (1bbf432) into v6 (89b14f2) will decrease coverage by 0.48%.
The diff coverage is 17.94%.

❗ Current head 1bbf432 differs from pull request most recent head e7743d4. Consider uploading reports for the commit e7743d4 to get more accurate results

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     
Flag Coverage Δ
unittest 87.28% <17.94%> (-0.48%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
packages/typescript-estree/src/node-utils.ts 54.77% <0.00%> (-6.92%) ⬇️
packages/typescript-estree/src/convert.ts 31.11% <16.66%> (-0.21%) ⬇️
packages/typescript-estree/src/getModifiers.ts 69.23% <100.00%> (+1.04%) ⬆️

... and 35 files with indirect coverage changes

@fisker fisker marked this pull request as ready for review March 21, 2023 10:21
@bradzacher bradzacher added this to the 6.0.0 milestone Mar 21, 2023
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;
Copy link
Member

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?

Copy link
Contributor Author

@fisker fisker Mar 21, 2023

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.Decorators, unless we call getDecorators again.

Copy link
Member

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?

Copy link
Contributor Author

@fisker fisker Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know.

unless we can call getDecorators again.

But I checked both modifiers and decorators here, and getModifiers() + getDecorators() is node.modifiers

Copy link
Member

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
}

Copy link
Contributor Author

@fisker fisker Apr 19, 2023

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

  1. Remove those check
  2. Add option to skip check

?

Copy link
Member

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?.(
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Member

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

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!

Copy link
Contributor Author

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

Copy link
Member

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).

@JoshuaKGoldberg
Copy link
Member

@fisker just checking in - are you waiting for us on anything for this one? I'm not sure from reading through the comments, sorry.

@fisker
Copy link
Contributor Author

fisker commented Jun 16, 2023

I don't know how ts.hasAmbientModifier works, and can't access ts.hasAmbientModifier directly, so I suggest use () => false for now. #6723 (comment)

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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!

@JoshuaKGoldberg JoshuaKGoldberg merged commit c456f8c into typescript-eslint:v6 Jun 17, 2023
42 of 44 checks passed
@fisker fisker deleted the decorator-check branch June 17, 2023 01:51
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: typescript-estree Issues related to @typescript-eslint/typescript-estree
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants