Skip to content

feat: support TypeScript syntax in no-array-constructor #19493

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 9 commits into from
Mar 24, 2025

Conversation

Tanujkanti4441
Copy link
Contributor

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Updated no-array-constructor rule to support TypeScript syntax now rule will allow follwing code

"new Array<Foo>(1, 2, 3);",
"new Array<Foo>();",
"Array<Foo>(1, 2, 3);",
"Array<Foo>();"

right now espree parses these codes and thus rule reports them and gives suggestions which are syntax errors like

new Array<Foo>(1, 2, 3); 
//fixes to 
[]<Foo>(1, 2, 3);

Is there anything you'd like reviewers to focus on?

Refs #19173

@Tanujkanti4441 Tanujkanti4441 requested a review from a team as a code owner March 10, 2025 08:44
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Mar 10, 2025
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Mar 10, 2025
@github-actions github-actions bot added the rule Relates to ESLint's core rules label Mar 10, 2025
Copy link

netlify bot commented Mar 10, 2025

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit 496654f
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/67de5dcf74cb5d0008f68a9a
😎 Deploy Preview https://deploy-preview-19493--docs-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 configuration.

Comment on lines 461 to 462
* 'Array?.(x);',
* 'Array?.(9);',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we allow code with optional chaining to support TypeScript syntax?

Copy link
Contributor

Choose a reason for hiding this comment

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

Optional chaining isn't a TypeScript syntax. Since Array?.() is already reported by the rule I think it should be here too, yeah. Filed an issue on the extension rule: typescript-eslint/typescript-eslint#10933

Copy link
Contributor Author

@Tanujkanti4441 Tanujkanti4441 Mar 11, 2025

Choose a reason for hiding this comment

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

But right now rule just report Array?.() not Array?.(1) should we keep this behavior or reporting all the code with optional chaining?

playground

Copy link
Member

Choose a reason for hiding this comment

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

Array?.(1) is not reported because it has exactly one argument. It creates an array of length 1, and not array that contains the number 1 as an element. So Array?.(1) cannot be replaced with [1].

For the same reason Array(foo) isn't reported, since it has one argument, but Array(foo, bar) is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, thanks!

Copy link
Contributor

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

Just a couple of suggestions, looks great to me!

Comment on lines 461 to 462
* 'Array?.(x);',
* 'Array?.(9);',
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional chaining isn't a TypeScript syntax. Since Array?.() is already reported by the rule I think it should be here too, yeah. Filed an issue on the extension rule: typescript-eslint/typescript-eslint#10933

@@ -81,6 +84,7 @@ module.exports = {
if (
node.callee.type !== "Identifier" ||
node.callee.name !== "Array" ||
sourceCode.getTokenAfter(node.callee)?.value === "<" ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to dip into tokens rather than using the AST directly? This is what the existing typescript-eslint rule does:

Suggested change
sourceCode.getTokenAfter(node.callee)?.value === "<" ||
node.typeArguments ||

Copy link
Contributor Author

Choose a reason for hiding this comment

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

was using @typescript-eslint/parser from astExplorer and it's nodes wasn't working so did this.

@nzakas
Copy link
Member

nzakas commented Mar 10, 2025

@JoshuaKGoldberg reminder: when you start triaging and issue or PR, please move it on the Triage board. In this case, it should move to "Implementing" because there is an open issue related to this PR.

@nzakas nzakas moved this from Needs Triage to Implementing in Triage Mar 10, 2025
@JoshuaKGoldberg
Copy link
Contributor

👋 Temporary notice, I'm about to merge #19355 chore: formatted files with Prettier via trunk fmt. We should hold off on merging other PRs till it's done. I'll post back soon to confirm we're clear. Cheers!
(context: #19355 (comment))

@JoshuaKGoldberg
Copy link
Contributor

We're clear! main is now formatted with Prettier via trunk fmt.

To get rid of these merge conflicts, you can merge from main and use branch's versions of all the conflicted files. Making a new commit should trigger auto-formatting.

git merge -X ours main

@nzakas
Copy link
Member

nzakas commented Mar 20, 2025

@JoshuaKGoldberg please check the latest changes to see if your feedback has been addressed.

Copy link
Contributor

@snitin315 snitin315 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

I'll leave it open for @JoshuaKGoldberg

@snitin315 snitin315 added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 22, 2025
Copy link
Contributor

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

🌟 lovely, thanks!

@JoshuaKGoldberg JoshuaKGoldberg moved this from Implementing to Merge Candidates in Triage Mar 24, 2025
@fasttime fasttime merged commit b79ade6 into eslint:main Mar 24, 2025
31 checks passed
@github-project-automation github-project-automation bot moved this from Merge Candidates to Complete in Triage Mar 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

6 participants