From e8be03c73a54b356d5adebaa5785776889b84258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Mon, 17 Mar 2025 22:53:43 +0900 Subject: [PATCH 1/3] fix: remove optional chaining --- .../src/rules/no-array-constructor.ts | 10 +++--- .../tests/rules/no-array-constructor.test.ts | 32 +++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-array-constructor.ts b/packages/eslint-plugin/src/rules/no-array-constructor.ts index a315789334e4..02b571ce4827 100644 --- a/packages/eslint-plugin/src/rules/no-array-constructor.ts +++ b/packages/eslint-plugin/src/rules/no-array-constructor.ts @@ -2,7 +2,7 @@ import type { TSESTree } from '@typescript-eslint/utils'; import { AST_NODE_TYPES } from '@typescript-eslint/utils'; -import { createRule, isOptionalCallExpression } from '../util'; +import { createRule } from '../util'; export default createRule({ name: 'no-array-constructor', @@ -32,8 +32,7 @@ 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, @@ -43,7 +42,10 @@ export default createRule({ return fixer.replaceText(node, '[]'); } const fullText = context.sourceCode.getText(node); - const preambleLength = node.callee.range[1] - node.range[0]; + const preambleLength = + node.parent.type === AST_NODE_TYPES.ChainExpression + ? node.callee.range[1] + 2 - node.range[0] + : node.callee.range[1] - node.range[0]; return fixer.replaceText( node, diff --git a/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts b/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts index 4ce184ce984b..54d23365c16e 100644 --- a/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts +++ b/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts @@ -33,8 +33,6 @@ ruleTester.run('no-array-constructor', rule, { 'Array.foo?.();', 'Array?.(1, 2, 3);', 'Array?.();', - 'Array?.(0, 1, 2);', - 'Array?.(x, y);', ], invalid: [ @@ -58,6 +56,16 @@ ruleTester.run('no-array-constructor', rule, { ], output: '[];', }, + { + code: 'Array?.();', + errors: [ + { + messageId, + type: AST_NODE_TYPES.CallExpression, + }, + ], + output: '[];', + }, { code: 'new Array(x, y);', errors: [ @@ -78,6 +86,16 @@ ruleTester.run('no-array-constructor', rule, { ], output: '[x, y];', }, + { + code: 'Array?.(x, y);', + errors: [ + { + messageId, + type: AST_NODE_TYPES.CallExpression, + }, + ], + output: '[x, y];', + }, { code: 'new Array(0, 1, 2);', errors: [ @@ -98,6 +116,16 @@ 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: ` new Array(0, 1, 2); From 63ffec8422695e326821a245cbcf31d4bd8de314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Wed, 26 Mar 2025 02:35:19 +0900 Subject: [PATCH 2/3] fix: add space, coment add case --- .../src/rules/no-array-constructor.ts | 49 ++++++++++++++----- .../tests/rules/no-array-constructor.test.ts | 42 ++++++++++++++++ 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-array-constructor.ts b/packages/eslint-plugin/src/rules/no-array-constructor.ts index 02b571ce4827..12a8a70082a0 100644 --- a/packages/eslint-plugin/src/rules/no-array-constructor.ts +++ b/packages/eslint-plugin/src/rules/no-array-constructor.ts @@ -1,6 +1,10 @@ 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 } from '../util'; @@ -21,6 +25,37 @@ 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 = sourceCode.getTokenAfter(node.callee); + + // if(firstToken == null){ + // return "" + // } + + do { + if (firstToken == null) { + return ''; + } + const fisrtTokenCandidate = sourceCode.getTokenAfter(firstToken); + + if (fisrtTokenCandidate == null || fisrtTokenCandidate === lastToken) { + return ''; + } + firstToken = fisrtTokenCandidate; + } 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 @@ -38,19 +73,9 @@ export default createRule({ node, messageId: 'useLiteral', fix(fixer) { - if (node.arguments.length === 0) { - return fixer.replaceText(node, '[]'); - } - const fullText = context.sourceCode.getText(node); - const preambleLength = - node.parent.type === AST_NODE_TYPES.ChainExpression - ? node.callee.range[1] + 2 - node.range[0] - : node.callee.range[1] - node.range[0]; + const argsText = getArgumentsText(node); - return fixer.replaceText( - node, - `[${fullText.slice(preambleLength + 1, -1)}]`, - ); + return fixer.replaceText(node, `[${argsText}]`); }, }); } diff --git a/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts b/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts index 54d23365c16e..bbba24e7b75c 100644 --- a/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts +++ b/packages/eslint-plugin/tests/rules/no-array-constructor.test.ts @@ -66,6 +66,16 @@ ruleTester.run('no-array-constructor', rule, { ], 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: [ @@ -96,6 +106,16 @@ ruleTester.run('no-array-constructor', rule, { ], 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: [ @@ -128,6 +148,28 @@ ruleTester.run('no-array-constructor', rule, { }, { 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); `, errors: [ From de18cb9e7d776a6436de35a48d5bfc842425200e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=80=E1=85=B5=E1=86=B7=E1=84=89=E1=85=A1=E1=86=BC?= =?UTF-8?q?=E1=84=83=E1=85=AE?= Date: Thu, 27 Mar 2025 23:24:49 +0900 Subject: [PATCH 3/3] fix: testcase error --- .../src/rules/no-array-constructor.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-array-constructor.ts b/packages/eslint-plugin/src/rules/no-array-constructor.ts index 12a8a70082a0..93b4256066a4 100644 --- a/packages/eslint-plugin/src/rules/no-array-constructor.ts +++ b/packages/eslint-plugin/src/rules/no-array-constructor.ts @@ -36,26 +36,18 @@ export default createRule({ return ''; } - let firstToken = sourceCode.getTokenAfter(node.callee); - - // if(firstToken == null){ - // return "" - // } + let firstToken: TSESTree.Expression | TSESTree.Token | null = node.callee; do { - if (firstToken == null) { + firstToken = sourceCode.getTokenAfter(firstToken); + if (!firstToken || firstToken === lastToken) { return ''; } - const fisrtTokenCandidate = sourceCode.getTokenAfter(firstToken); - - if (fisrtTokenCandidate == null || fisrtTokenCandidate === lastToken) { - return ''; - } - firstToken = fisrtTokenCandidate; } 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