Skip to content

fix(eslint-plugin): [no-array-constructor] remove optional chaining exemption #10963

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions packages/eslint-plugin/src/rules/no-array-constructor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { TSESTree } from '@typescript-eslint/utils';

import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import {
isOpeningParenToken,
isClosingParenToken,
} from '@typescript-eslint/utils/ast-utils';

import { createRule, isOptionalCallExpression } from '../util';
import { createRule } from '../util';

export default createRule({
name: 'no-array-constructor',
Expand All @@ -21,6 +25,29 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const sourceCode = context.sourceCode;

function getArgumentsText(
node: TSESTree.CallExpression | TSESTree.NewExpression,
) {
const lastToken = sourceCode.getLastToken(node);

if (lastToken == null || !isClosingParenToken(lastToken)) {
return '';
}

let firstToken: TSESTree.Expression | TSESTree.Token | null = node.callee;

do {
firstToken = sourceCode.getTokenAfter(firstToken);
if (!firstToken || firstToken === lastToken) {
return '';
}
} while (!isOpeningParenToken(firstToken));

return sourceCode.text.slice(firstToken.range[1], lastToken.range[0]);
}

/**
* Disallow construction of dense arrays using the Array constructor
* @param node node to evaluate
Expand All @@ -32,23 +59,15 @@ export default createRule({
node.arguments.length !== 1 &&
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'Array' &&
!node.typeArguments &&
!isOptionalCallExpression(node)
!node.typeArguments
) {
context.report({
node,
messageId: 'useLiteral',
fix(fixer) {
if (node.arguments.length === 0) {
return fixer.replaceText(node, '[]');
}
const fullText = context.sourceCode.getText(node);
const preambleLength = node.callee.range[1] - node.range[0];

return fixer.replaceText(
node,
`[${fullText.slice(preambleLength + 1, -1)}]`,
);
const argsText = getArgumentsText(node);

return fixer.replaceText(node, `[${argsText}]`);
},
});
}
Expand Down
74 changes: 72 additions & 2 deletions packages/eslint-plugin/tests/rules/no-array-constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ ruleTester.run('no-array-constructor', rule, {
'Array.foo?.();',
'Array?.<Foo>(1, 2, 3);',
'Array?.<Foo>();',
'Array?.(0, 1, 2);',
'Array?.(x, y);',
],

invalid: [
Expand All @@ -58,6 +56,26 @@ ruleTester.run('no-array-constructor', rule, {
],
output: '[];',
},
{
code: 'Array?.();',
errors: [
{
messageId,
type: AST_NODE_TYPES.CallExpression,
},
],
output: '[];',
},
{
code: '/* a */ /* b */ Array /* c */ /* d */ /* e */ /* f */?.(); /* g */ /* h */',
errors: [
{
messageId,
type: AST_NODE_TYPES.CallExpression,
},
],
output: '/* a */ /* b */ []; /* g */ /* h */',
},
{
code: 'new Array(x, y);',
errors: [
Expand All @@ -78,6 +96,26 @@ ruleTester.run('no-array-constructor', rule, {
],
output: '[x, y];',
},
{
code: 'Array?.(x, y);',
errors: [
{
messageId,
type: AST_NODE_TYPES.CallExpression,
},
],
output: '[x, y];',
},
{
code: '/* a */ /* b */ Array /* c */ /* d */ /* e */ /* f */?.(x, y); /* g */ /* h */',
errors: [
{
messageId,
type: AST_NODE_TYPES.CallExpression,
},
],
output: '/* a */ /* b */ [x, y]; /* g */ /* h */',
},
{
code: 'new Array(0, 1, 2);',
errors: [
Expand All @@ -98,6 +136,38 @@ ruleTester.run('no-array-constructor', rule, {
],
output: '[0, 1, 2];',
},
{
code: 'Array?.(0, 1, 2);',
errors: [
{
messageId,
type: AST_NODE_TYPES.CallExpression,
},
],
output: '[0, 1, 2];',
},
{
code: `
/* a */ /* b */ Array /* c */ /* d */ /* e */ /* f */?.(
0,
1,
2,
); /* g */ /* h */
`,
errors: [
{
messageId,
type: AST_NODE_TYPES.CallExpression,
},
],
output: `
/* a */ /* b */ [
0,
1,
2,
]; /* g */ /* h */
`,
},
{
code: `
new Array(0, 1, 2);
Expand Down
Loading