Repository: eslint/eslint
File: lib/rules/no-array-constructor.js (JavaScript)

Code before:
                return "";
            }

            let firstToken = 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 {ASTNode} node node to evaluate
         * @returns {void}
         * @private
         */
        function check(node) {
            if (
                node.callee.type !== "Identifier" ||
                node.callee.name !== "Array" ||
                sourceCode.getTokenAfter(node.callee)?.value === "<" ||
                node.arguments.length === 1 &&
                node.arguments[0].type !== "SpreadElement") {
                return;
            }

            const variable = getVariableByName(sourceCode.getScope(node), "Array");

            /*
             * Check if `Array` is a predefined global variable: predefined globals have no declarations,
             * meaning that the `identifiers` list of the variable object is empty.
             */
            if (variable && variable.identifiers.length === 0) {
                const argsText = getArgumentsText(node);
                let fixText;
                let messageId;

                /*
                 * Check if the suggested change should include a preceding semicolon or not.
                 * Due to JavaScript's ASI rules, a missing semicolon may be inserted automatically
                 * before an expression like `Array()` or `new Array()`, but not when the expression
                 * is changed into an array literal like `[]`.
                 */
                if (isStartOfExpressionStatement(node) && needsPrecedingSemicolon(sourceCode, node)) {
                    fixText = `;[${argsText}]`;
                    messageId = "useLiteralAfterSemicolon";

Review comment (suggestion):
Is there a reason to dip into tokens rather than using the AST directly? This is what the existing typescript-eslint rule does:

```suggestion
                node.typeArguments ||
```

Code after:
			}

			let firstToken = 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 {ASTNode} node node to evaluate
		 * @returns {void}
		 * @private
		 */
		function check(node) {
			if (
				node.callee.type !== "Identifier" ||
				node.callee.name !== "Array" ||
				node.typeArguments ||
				(node.arguments.length === 1 &&
					node.arguments[0].type !== "SpreadElement")
			) {
				return;
			}

			const variable = getVariableByName(
				sourceCode.getScope(node),
				"Array",
			);

			/*
			 * Check if `Array` is a predefined global variable: predefined globals have no declarations,
			 * meaning that the `identifiers` list of the variable object is empty.
			 */
			if (variable && variable.identifiers.length === 0) {
				const argsText = getArgumentsText(node);
				let fixText;
				let messageId;

				/*
				 * Check if the suggested change should include a preceding semicolon or not.
				 * Due to JavaScript's ASI rules, a missing semicolon may be inserted automatically