-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
* 'Array?.(x);', | ||
* 'Array?.(9);', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks!
There was a problem hiding this 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!
* 'Array?.(x);', | ||
* 'Array?.(9);', |
There was a problem hiding this comment.
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
lib/rules/no-array-constructor.js
Outdated
@@ -81,6 +84,7 @@ module.exports = { | |||
if ( | |||
node.callee.type !== "Identifier" || | |||
node.callee.name !== "Array" || | |||
sourceCode.getTokenAfter(node.callee)?.value === "<" || |
There was a problem hiding this comment.
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:
sourceCode.getTokenAfter(node.callee)?.value === "<" || | |
node.typeArguments || |
There was a problem hiding this comment.
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.
@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. |
👋 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! |
We're clear! To get rid of these merge conflicts, you can merge from git merge -X ours main |
@JoshuaKGoldberg please check the latest changes to see if your feedback has been addressed. |
There was a problem hiding this 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🌟 lovely, thanks!
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 coderight now espree parses these codes and thus rule reports them and gives suggestions which are syntax errors like
Is there anything you'd like reviewers to focus on?
Refs #19173