Skip to content

feat(typescript-estree): restrict variable declarator definite/init combinations #9228

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

Merged
merged 11 commits into from
Aug 11, 2024

Conversation

Josh-Cena
Copy link
Member

PR Checklist

Overview

I added A LOT of fixtures (programmatically of course) and I noticed that Babel didn't give some errors while IMO they should.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @Josh-Cena!

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.

@Josh-Cena Josh-Cena requested a review from bradzacher June 3, 2024 14:32
Copy link

netlify bot commented Jun 3, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit be1b324
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66b8181951c1680008e2042f
😎 Deploy Preview https://deploy-preview-9228--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 (🟢 up 4 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.

Copy link

nx-cloud bot commented Jun 3, 2024

☁️ Nx Cloud Report

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

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@Josh-Cena Josh-Cena force-pushed the var-decl-ast-invariant branch from 16faace to cea3f68 Compare June 3, 2024 15:26
Copy link
Member Author

@Josh-Cena Josh-Cena Jun 3, 2024

Choose a reason for hiding this comment

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

Script for generating this pile of fixtures. Let me know if I should trim it down (especially the error combinations)

Copy link
Member

Choose a reason for hiding this comment

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

Nit: I wouldn't bother committing this - just leaving it as a note in the PR description should be enough for us to find it later if we want to reproduce this later.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok but I designed this script to allow us to:

  • Quickly identify error condition combinations
  • Quickly move a bunch of fixtures around valid/invalid cases
  • Quickly generate more combinations,

so I think overall it helps with maintenance

Comment on lines +20 to +27
"declaration/VariableDeclaration/fixtures/_error_/const-destructure-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/const-destructure-type-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/const-id-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/const-id-type-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/let-destructure-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/let-destructure-type-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/var-destructure-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/var-destructure-type-no-init/fixture.ts",
Copy link
Member Author

Choose a reason for hiding this comment

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

These are because we don't have the ability to detect whether a declaration is in an ambient context unless itself has declare

Comment on lines +59 to +75
"declaration/VariableDeclaration/fixtures/_error_/const-id-definite-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/const-id-definite-type-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-const-id-definite-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-const-id-definite-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-const-id-definite-type-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-let-id-definite-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-let-id-definite-type-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-var-id-definite-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/declare-var-id-definite-type-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/let-id-definite-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/let-id-definite-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/let-id-definite-type-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/using-id-definite-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/using-id-definite-type-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/var-id-definite-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/var-id-definite-no-init/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/var-id-definite-type-init/fixture.ts",
Copy link
Member Author

Choose a reason for hiding this comment

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

Babel doesn't detect whether a definite assignment is put on a non-declared let/var with type and no init. This is arguably a Babel bug

@bradzacher bradzacher added the AST PRs and Issues about the AST structure label Jun 4, 2024
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I wouldn't bother committing this - just leaving it as a note in the PR description should be enough for us to find it later if we want to reproduce this later.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Jun 17, 2024
@JoshuaKGoldberg
Copy link
Member

👋 @Josh-Cena did you mean to re-request review on this?

@Josh-Cena
Copy link
Member Author

I actually don't remember where the discussion left off. But yeah @bradzacher I think another look is worthwhile. I still want to keep the fixture generation script.

@Josh-Cena Josh-Cena requested a review from bradzacher July 12, 2024 00:35
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Jul 12, 2024
bradzacher
bradzacher previously approved these changes Jul 12, 2024
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

I can't see one - could you please add an error fixture for

for (using x! of []) {}

Other than that - looking fantastic!
We can land once you've got that!

@bradzacher bradzacher added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jul 12, 2024
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

Can we also add this

interface VariableDeclarator {
  parent: VariableDeclaration;
}

to https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/types/src/ts-estree.ts for #6225

@Josh-Cena
Copy link
Member Author

Will come back to it next week...!

@Josh-Cena
Copy link
Member Author

@bradzacher: unfortunately, VariableDeclarator is no longer an interface, but a union, so we can no longer merge it. If we really want to, we have to merge parent into individual constituents. Does that sound fine?

Copy link

codecov bot commented Jul 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.27%. Comparing base (07700dd) to head (d765c76).

Current head d765c76 differs from pull request most recent head be1b324

Please upload reports for the commit be1b324 to get more accurate results.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9228      +/-   ##
==========================================
+ Coverage   88.07%   88.27%   +0.20%     
==========================================
  Files         402      422      +20     
  Lines       13693    14679     +986     
  Branches     3982     4299     +317     
==========================================
+ Hits        12060    12958     +898     
- Misses       1324     1400      +76     
- Partials      309      321      +12     
Flag Coverage Δ
unittest 88.27% <ø> (+0.20%) ⬆️

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

see 115 files with indirect coverage changes

@bradzacher
Copy link
Member

Yeah that's perfectly fine - if you have a look at the file you'll see that's the common pattern.

Eventually we'll just want to add parent to the AST and merge the file into the main ast.

@bradzacher
Copy link
Member

Screenshot_20240720-110239.png github ffs

Me, trying to review this PR.
Ffs github

bradzacher
bradzacher previously approved these changes Jul 20, 2024
@bradzacher
Copy link
Member

Most of the other ast changes have gone into v8 - do we want to also merge this into v8 as well to avoid the inevitable merge conflicts?

@Josh-Cena
Copy link
Member Author

Mmmm, that's a good point, although other AST changes I sent have already gone into main.

@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 29, 2024
@Josh-Cena
Copy link
Member Author

Oof, I didn't realize this PR is still unmerged. I've resolved the conflicts.

@bradzacher bradzacher merged commit a4992ff into typescript-eslint:main Aug 11, 2024
56 of 57 checks passed
@Josh-Cena Josh-Cena deleted the var-decl-ast-invariant branch August 11, 2024 03:35
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
AST PRs and Issues about the AST structure
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AST tightening: around init/definite combinations on let/var/const declarators
3 participants