[go: up one dir, main page]

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(typescript-estree): disallow using as the variable keyword for for..in loops #7649

Merged
merged 7 commits into from
Jan 12, 2024

Conversation

Solo-steven
Copy link
Contributor
@Solo-steven Solo-steven commented Sep 15, 2023

PR Checklist

Overview

fix issue #7555 by check TypeScript nodeflag, one problem is I not sure I add test case in correct file, if there are some wrong, please tell me, thanks ~

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @Solo-steven!

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 Sep 15, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 908aea3
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65a05b55499c920008cd9650
😎 Deploy Preview https://deploy-preview-7649--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: 98 (🟢 up 2 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (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.

@Solo-steven
Copy link
Contributor Author
Solo-steven commented Sep 15, 2023

The reason that the CI job failed seems because OOM ( out of memory ), local linter result is pass, and since I only changed the typescript-estree package, and the CI log shows that this package is passing linter, I belive re-run the CI pipeline should be ok ~ 😄


Screenshot 2023-09-15 at 5 33 26 PM

Copy link
Member
@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

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

The only thing forbidden is for (using x in y), because in this case x is statically known to be non-disposable (it can only be string/symbol), so this is likely a mistake. For all other constructs, x can actually be a disposable, and the grammar allows using.

expect(() => instance.convertProgram()).toThrow();
});
it('using should be forbidden in for of statement ', () => {
const ast = convertCode('for(using foo of {});');
Copy link
Member

Choose a reason for hiding this comment

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

This is valid code.

expect(() => instance.convertProgram()).toThrow();
});
it('using should be forbidden in for statement ', () => {
const ast = convertCode('for(using i; i <= 0 ; ++i);');
Copy link
Member

Choose a reason for hiding this comment

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

So is this.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Oct 10, 2023
@JoshuaKGoldberg
Copy link
Member

👋 Hey @Solo-steven! Just checking in, is this still something you have time for? No worries if not - I just don't want to leave it hanging. Thanks!

@JoshuaKGoldberg JoshuaKGoldberg added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Oct 17, 2023
@Solo-steven
Copy link
Contributor Author

👋 Hey @Solo-steven! Just checking in, is this still something you have time for? No worries if not - I just don't want to leave it hanging. Thanks!

Hi @JoshuaKGoldberg , sorry for the delay, I am missing this issue from my schedule, I change the test case and only disallow using when using is in for-of statement, and thanks @Josh-Cena 's check ~

@JoshuaKGoldberg
Copy link
Member

Okie dokie, when you're ready for it to be re-reviewed you can re-request a review.

@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Oct 21, 2023
@bradzacher bradzacher added bug Something isn't working and removed stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period labels Nov 10, 2023
@bradzacher bradzacher changed the title feat(typescript-estree): check for init not use using keyword fix(typescript-estree): disallow using as the variable keyword for for..in loops Nov 10, 2023
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.

thanks for your work on this! sorry for taking so long to get back to this.

Comment on lines 3464 to 3470
if (
!(
initializer.flags & ts.NodeFlags.Const ||
initializer.flags & ts.NodeFlags.Let ||
initializer.flags === ts.NodeFlags.None
)
) {
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 checking for it "not being one of the valid states", instead can we just check for the one state we want to error on - (initializer.flags & ts.NodeFlags.Using) !== 0

) {
this.#throwError(
initializer,
" The left-hand side of a 'for...in' statement cannot be a 'using' declaration.",
Copy link
Member

Choose a reason for hiding this comment

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

nit remove the space

Suggested change
" The left-hand side of a 'for...in' statement cannot be a 'using' declaration.",
"The left-hand side of a 'for...in' statement cannot be a 'using' declaration.",

Comment on lines 364 to 372
describe('using should be forbidden in for-related initializer ', () => {
it('using should be forbidden in for in statement ', () => {
const ast = convertCode('for(using foo in {});');

const instance = new Converter(ast);

expect(() => instance.convertProgram()).toThrow();
});
});
Copy link
Member

Choose a reason for hiding this comment

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

We don't put AST tests in this location - this is more for testing our infra.
Instead we place our AST tests in the ast-spec package alongside the AST declarations.

We haven't written proper docs for this yet (sorry!) but the steps for doing this is as follows:

  1. within this folder create a folder called fixtures.
  2. within fixtures create a _error_ folder
  3. within _error_ create a folder with a name for your case - for example maybe using-initializer?
  4. within that folder, create a file called fixture.ts
  5. within that file add your test code (eg for(using foo in {});)
  6. cd packages/ast-spec && yarn test to run the fixture tests and generate the necessary snapshots.

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label Nov 10, 2023
@Josh-Cena
Copy link
Member

Hi @Solo-steven, do you have time to take a look at the review?

@Solo-steven
Copy link
Contributor Author

Hi @Solo-steven, do you have time to take a look at the review?

Hi @Josh-Cena Sorry about the late response, I am kind of busy this month, but I will fix this PR this weekend! thanks for your reminder ~

@JoshuaKGoldberg
Copy link
Member

👋 checking in again @Solo-steven - by "this month" do you mean January?

No worries if you're busy, just checking in!

@JoshuaKGoldberg JoshuaKGoldberg added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Jan 8, 2024
…e test case to ast-spec

* shorten syntax check for using keyword in for-in statement.
* move test file to ast-spec.
@Solo-steven
Copy link
Contributor Author

I @JoshuaKGoldberg sorry about being so late, i pushed a new commit to fix the above problem, thanks for your patience and comment on this PR ~

@JoshuaKGoldberg
Copy link
Member

Ok great, thanks!

Tip: if you ever want us to take another look, re-requesting review through the GitHub UI will auto-remove the awaiting response label. Nice little automation we set up 😄

@JoshuaKGoldberg JoshuaKGoldberg removed awaiting response Issues waiting for a reply from the OP or another party stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period labels Jan 11, 2024
@bradzacher bradzacher changed the title fix(typescript-estree): disallow using as the variable keyword for for..in loops fix(typescript-estree): disallow using as the variable keyword for for..in loops Jan 11, 2024
bradzacher
bradzacher previously approved these changes Jan 11, 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.

this LGTM - thanks for adding this in!

@bradzacher bradzacher merged commit 7e75e84 into typescript-eslint:main Jan 12, 2024
57 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: for (using foo in {}); should be invalid
4 participants