Skip to content

Commit b79ade6

Browse files
feat: support TypeScript syntax in no-array-constructor (#19493)
* feat: support TypeScript syntax in no-array-constructor * add typescript support info in meta * apply suggestions * add optional chaining tests * fix conflict * add typescript examples in docs * update * add incorrect examples in docs
1 parent 0c65c62 commit b79ade6

3 files changed

Lines changed: 194 additions & 0 deletions

File tree

docs/src/rules/no-array-constructor.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,46 @@ const createArray = Array => new Array();
5353

5454
:::
5555

56+
This rule additionally supports TypeScript type syntax.
57+
58+
Examples of **correct** code for this rule:
59+
60+
:::correct
61+
62+
```ts
63+
/*eslint no-array-constructor: "error"*/
64+
65+
new Array<number>(1, 2, 3);
66+
67+
new Array<Foo>();
68+
69+
Array<number>(1, 2, 3);
70+
71+
Array<Foo>();
72+
73+
Array?.foo();
74+
```
75+
76+
:::
77+
78+
Examples of **incorrect** code for this rule:
79+
80+
:::incorrect
81+
82+
```ts
83+
/*eslint no-array-constructor: "error"*/
84+
85+
new Array();
86+
87+
new Array(0, 1, 2);
88+
89+
Array?.(x, y);
90+
91+
Array?.(0, 1, 2);
92+
```
93+
94+
:::
95+
5696
## When Not To Use It
5797

5898
This rule enforces a nearly universal stylistic concern. That being said, this

lib/rules/no-array-constructor.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const {
2424
/** @type {import('../shared/types').Rule} */
2525
module.exports = {
2626
meta: {
27+
dialects: ["javascript", "typescript"],
28+
language: "javascript",
2729
type: "suggestion",
2830

2931
docs: {
@@ -84,6 +86,7 @@ module.exports = {
8486
if (
8587
node.callee.type !== "Identifier" ||
8688
node.callee.name !== "Array" ||
89+
node.typeArguments ||
8790
(node.arguments.length === 1 &&
8891
node.arguments[0].type !== "SpreadElement")
8992
) {

tests/lib/rules/no-array-constructor.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,154 @@ ruleTester.run("no-array-constructor", rule, {
486486
})),
487487
],
488488
});
489+
490+
const ruleTesterTypeScript = new RuleTester({
491+
languageOptions: {
492+
parser: require("@typescript-eslint/parser"),
493+
},
494+
});
495+
496+
ruleTesterTypeScript.run("no-array-constructor", rule, {
497+
valid: [
498+
"new Array(x);",
499+
"Array(x);",
500+
"new Array(9);",
501+
"Array(9);",
502+
"new foo.Array();",
503+
"foo.Array();",
504+
"new Array.foo();",
505+
"Array.foo();",
506+
507+
// TypeScript
508+
"new Array<Foo>(1, 2, 3);",
509+
"new Array<Foo>();",
510+
"Array<Foo>(1, 2, 3);",
511+
"Array<Foo>();",
512+
"Array<Foo>(3);",
513+
514+
//optional chain
515+
"Array?.(x);",
516+
"Array?.(9);",
517+
"foo?.Array();",
518+
"Array?.foo();",
519+
"foo.Array?.();",
520+
"Array.foo?.();",
521+
"Array?.<Foo>(1, 2, 3);",
522+
"Array?.<Foo>();",
523+
],
524+
525+
invalid: [
526+
{
527+
code: "new Array();",
528+
errors: [
529+
{
530+
messageId: "preferLiteral",
531+
suggestions: [
532+
{
533+
messageId: "useLiteral",
534+
output: "[];",
535+
},
536+
],
537+
},
538+
],
539+
},
540+
{
541+
code: "Array();",
542+
errors: [
543+
{
544+
messageId: "preferLiteral",
545+
suggestions: [
546+
{
547+
messageId: "useLiteral",
548+
output: "[];",
549+
},
550+
],
551+
},
552+
],
553+
},
554+
{
555+
code: "new Array(x, y);",
556+
errors: [
557+
{
558+
messageId: "preferLiteral",
559+
suggestions: [
560+
{
561+
messageId: "useLiteral",
562+
output: "[x, y];",
563+
},
564+
],
565+
},
566+
],
567+
},
568+
{
569+
code: "Array(x, y);",
570+
errors: [
571+
{
572+
messageId: "preferLiteral",
573+
suggestions: [
574+
{
575+
messageId: "useLiteral",
576+
output: "[x, y];",
577+
},
578+
],
579+
},
580+
],
581+
},
582+
{
583+
code: "new Array(0, 1, 2);",
584+
errors: [
585+
{
586+
messageId: "preferLiteral",
587+
suggestions: [
588+
{
589+
messageId: "useLiteral",
590+
output: "[0, 1, 2];",
591+
},
592+
],
593+
},
594+
],
595+
},
596+
{
597+
code: "Array(0, 1, 2);",
598+
errors: [
599+
{
600+
messageId: "preferLiteral",
601+
suggestions: [
602+
{
603+
messageId: "useLiteral",
604+
output: "[0, 1, 2];",
605+
},
606+
],
607+
},
608+
],
609+
},
610+
{
611+
code: "Array?.(0, 1, 2);",
612+
errors: [
613+
{
614+
messageId: "preferLiteral",
615+
suggestions: [
616+
{
617+
messageId: "useLiteral",
618+
output: "[0, 1, 2];",
619+
},
620+
],
621+
},
622+
],
623+
},
624+
{
625+
code: "Array?.(x, y);",
626+
errors: [
627+
{
628+
messageId: "preferLiteral",
629+
suggestions: [
630+
{
631+
messageId: "useLiteral",
632+
output: "[x, y];",
633+
},
634+
],
635+
},
636+
],
637+
},
638+
],
639+
});

0 commit comments

Comments
 (0)