Skip to content

fix(eslint-plugin): [no-unnecessary-type-assertion] conflict with TS for variables used before assignment #9209

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

Conversation

liuxingbaoyu
Copy link
Contributor

@liuxingbaoyu liuxingbaoyu commented Jun 2, 2024

PR Checklist

Overview

This PR makes the rule ignore var declared in child scopes.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @liuxingbaoyu!

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.

Copy link

netlify bot commented Jun 2, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 66ab8e1
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66be68ccc1d2400008d76eed
😎 Deploy Preview https://deploy-preview-9209--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🔴 down 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

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

@liuxingbaoyu liuxingbaoyu changed the title fix(eslint-plugin): no-unnecessary-type-assertion conflict with TS for variables used before assignment fix(eslint-plugin): rule no-unnecessary-type-assertion conflict with TS for variables used before assignment Jun 2, 2024
@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jun 3, 2024

This is a good example of the risks around sending a PR before the intended issue goes through triage. There might be a bigger issue that the intended issue is just one case of! 🙂

Edited to target #4513 instead.

Edit: d'oh, no, I'm wrong - this PR is ready for review as-is after all!

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.

Thanks for starting on this! I'm marking this as a draft because the root accepted issue is #4513, which adds on the idea of lets. Feel free to un-draft and re-request review once it's ready to address those too. Cheers! ✨

(I haven't looked at the source yet - in case it gets refactored to handle lets)

Edit: sorry, I was confused for a moment - ignore the drafting.

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as draft June 3, 2024 01:57
@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jun 3, 2024
@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as ready for review June 3, 2024 02:11
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.

Sorry for the noise - still requesting changes on a bit more testing. Still holding off on reviewing the source code a bit - will do soon!

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.

This looks great to me, thanks! 🧹 ✨

@JoshuaKGoldberg JoshuaKGoldberg added 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge and removed awaiting response Issues waiting for a reply from the OP or another party labels Jun 29, 2024
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

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

Just some very minor nitpicks, but this looks great to me! 🙂

@kirkwaiblinger kirkwaiblinger added the awaiting response Issues waiting for a reply from the OP or another party label Jul 7, 2024
@JoshuaKGoldberg JoshuaKGoldberg removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jul 8, 2024
@JoshuaKGoldberg JoshuaKGoldberg dismissed their stale review July 8, 2024 12:33

Deferring to Kirk's review

@kirkwaiblinger
Copy link
Member

👋 Hey @liuxingbaoyu ! Just checking in, is this still something you have time for? No worries if not - you got us 98% of the way there, so we can take care of the last few chores and merge it in for you if you like. But if you have a few minutes to take care of the last few chores.... then it saves us a few minutes ❤️

@kirkwaiblinger kirkwaiblinger added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Aug 1, 2024
@liuxingbaoyu
Copy link
Contributor Author

@kirkwaiblinger Sorry for forgetting about it. Thanks! I will finish it soon!

@liuxingbaoyu liuxingbaoyu force-pushed the fix-no-unnecessary-type-assertion branch from ea318d9 to 7891100 Compare August 2, 2024 09:44
Comment on lines +101 to +102
const declaratorNode: TSESTree.VariableDeclaration =
services.tsNodeToESTreeNodeMap.get(declaration);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed it when rebasing, I'm not sure if this will be affected.🤔
#9660

Copy link
Member

Choose a reason for hiding this comment

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

@auvred Could you double check this?

Copy link
Member

Choose a reason for hiding this comment

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

@liuxingbaoyu Thanks for calling this out. Good attention to detail!

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for such a long reply! I missed the notification :(


I think we can't just rely on services.tsNodeToESTreeNodeMap.get when dealing with declarations, as these declarations may be contained in other files that tsNodeToESTreeNodeMap doesn't know about.

Imagine the following:

// var-declaration.ts
var varDeclarationFromFixture = 1;

// file.ts
varDeclarationFromFixture!;

The rule throws runtime error on this code.

FAIL tests/rules/no-unnecessary-type-assertion.test.ts
  ● Test suite failed to run

    TypeError: Converting circular structure to JSON
        --> starting at object with constructor 'Object'
        |     property 'expression' -> object with constructor 'Object'
        --- property 'parent' closes the circle
        at stringify (<anonymous>)

      at messageParent (../../node_modules/jest-worker/build/workers/messageParent.js:29:19)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.074 s
Ran all test suites matching /no-unnecessary-type-assertion/i.

Since it requires more than one file to reproduce, I can't reproduce it in online playground, so here's the patch:

diff --git a/packages/eslint-plugin/tests/fixtures/tsconfig.json b/packages/eslint-plugin/tests/fixtures/tsconfig.json
index 6ae5e6473..c16815aaf 100644
--- a/packages/eslint-plugin/tests/fixtures/tsconfig.json
+++ b/packages/eslint-plugin/tests/fixtures/tsconfig.json
@@ -12,6 +12,7 @@
     "file.ts",
     "consistent-type-exports.ts",
     "mixed-enums-decl.ts",
-    "react.tsx"
+    "react.tsx",
+    "var-declaration.ts"
   ]
 }
diff --git a/packages/eslint-plugin/tests/fixtures/var-declaration.ts b/packages/eslint-plugin/tests/fixtures/var-declaration.ts
new file mode 100644
index 000000000..5253648a8
--- /dev/null
+++ b/packages/eslint-plugin/tests/fixtures/var-declaration.ts
@@ -0,0 +1 @@
+var varDeclarationFromFixture = 1;
diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
index afc0b3c3b..b636eec84 100644
--- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
+++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
@@ -1088,6 +1088,20 @@ const bar = foo.a;
     },
     {
       code: `
+varDeclarationFromFixture!;
+      `,
+      output: `
+varDeclarationFromFixture;
+      `,
+      errors: [
+        {
+          messageId: 'unnecessaryAssertion',
+          line: 3,
+        },
+      ],
+    },
+    {
+      code: `
 var x = 1;
 x!;
       `,

You can run git apply to apply it locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the test! It seems we can skip it in this case, since the declaration in the different file never appears used before assignment.

Copy link

codecov bot commented Aug 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.94%. Comparing base (2761ca8) to head (66ab8e1).
Report is 54 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9209      +/-   ##
==========================================
- Coverage   88.07%   87.94%   -0.14%     
==========================================
  Files         402      403       +1     
  Lines       13691    13806     +115     
  Branches     3982     4027      +45     
==========================================
+ Hits        12058    12141      +83     
- Misses       1324     1358      +34     
+ Partials      309      307       -2     
Flag Coverage Δ
unittest 87.94% <100.00%> (-0.14%) ⬇️

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

Files Coverage Δ
...-plugin/src/rules/no-unnecessary-type-assertion.ts 97.93% <100.00%> (+0.26%) ⬆️

... and 29 files with indirect coverage changes

kirkwaiblinger
kirkwaiblinger previously approved these changes Aug 2, 2024
Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

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

Approving contingent on #9209 (comment) being resolved.

Great work!

cats celebrating

@kirkwaiblinger kirkwaiblinger added 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge and removed awaiting response Issues waiting for a reply from the OP or another party labels Aug 2, 2024
@JoshuaKGoldberg JoshuaKGoldberg removed the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Aug 12, 2024
@JoshuaKGoldberg JoshuaKGoldberg requested a review from auvred August 12, 2024 16:23
Copy link
Member

@auvred auvred left a comment

Choose a reason for hiding this comment

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

Requesting changes on #9209 (comment). All these declarations are very tricky 🙂

@auvred auvred added the awaiting response Issues waiting for a reply from the OP or another party label Aug 13, 2024
@JoshuaKGoldberg JoshuaKGoldberg removed the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Aug 14, 2024
@kirkwaiblinger kirkwaiblinger requested a review from auvred August 15, 2024 22:34
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Aug 15, 2024
@kirkwaiblinger kirkwaiblinger added the bug Something isn't working label Aug 15, 2024
Copy link
Member

@auvred auvred left a comment

Choose a reason for hiding this comment

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

LGTM! 💯

@auvred auvred changed the title fix(eslint-plugin): rule no-unnecessary-type-assertion conflict with TS for variables used before assignment fix(eslint-plugin): [no-unnecessary-type-assertion] conflict with TS for variables used before assignment Aug 19, 2024
@auvred auvred added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Aug 19, 2024
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.

🙌

@JoshuaKGoldberg JoshuaKGoldberg merged commit 3ca8477 into typescript-eslint:main Aug 19, 2024
67 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-type-assertion] Conflict with TS for variables used before assignment
4 participants