Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-array-constructor

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 3
πŸ“¦ Imports 5

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-array-constructor.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'no-array-constructor'
meta.type 'suggestion'
meta.docs.description 'Disallow generic Array constructors'
meta.docs.extendsBaseRule true
meta.docs.recommended 'recommended'
meta.fixable 'code'
meta.messages.useLiteral 'The array literal notation [] is preferable.'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

Name Source
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
isOpeningParenToken @typescript-eslint/utils/ast-utils
isClosingParenToken @typescript-eslint/utils/ast-utils
createRule ../util

Functions

create(context: any): { CallExpression: (node: TSESTree.CallExpression | TSESTree…

Parameters:

  • context any

Returns: { CallExpression: (node: TSESTree.CallExpression | TSESTree.NewExpression) => void; NewExpression: (node: TSESTree.CallExpression | TSESTree.NewExpression) => void; }

Calls:

  • sourceCode.getLastToken
  • isClosingParenToken (from @typescript-eslint/utils/ast-utils)
  • sourceCode.getTokenAfter
  • isOpeningParenToken (from @typescript-eslint/utils/ast-utils)
  • sourceCode.text.slice
  • context.report
  • getArgumentsText
  • fixer.replaceText

Internal Comments:

/**
     * Disallow construction of dense arrays using the Array constructor
     * @param node node to evaluate
     */

Code
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
     */
    function check(
      node: TSESTree.CallExpression | TSESTree.NewExpression,
    ): void {
      if (
        node.arguments.length !== 1 &&
        node.callee.type === AST_NODE_TYPES.Identifier &&
        node.callee.name === 'Array' &&
        !node.typeArguments
      ) {
        context.report({
          node,
          messageId: 'useLiteral',
          fix(fixer) {
            const argsText = getArgumentsText(node);

            return fixer.replaceText(node, `[${argsText}]`);
          },
        });
      }
    }

    return {
      CallExpression: check,
      NewExpression: check,
    };
  }

Internal helpers

Declared inside another function in this file.

getArgumentsText(node: TSESTree.CallExpression | TSESTree.NewE…): any

Parameters:

  • node TSESTree.CallExpression | TSESTree.NewExpression

Returns: any

Calls:

  • sourceCode.getLastToken
  • isClosingParenToken (from @typescript-eslint/utils/ast-utils)
  • sourceCode.getTokenAfter
  • isOpeningParenToken (from @typescript-eslint/utils/ast-utils)
  • sourceCode.text.slice
Code
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]);
    }

check(node: TSESTree.CallExpression | TSESTree.NewE…): void

Disallow construction of dense arrays using the Array constructor

Parameters:

  • node any: node to evaluate
Raw JSDoc
/**
     * Disallow construction of dense arrays using the Array constructor
     * @param node node to evaluate
     */

Calls:

  • context.report
  • getArgumentsText
  • fixer.replaceText
Code
function check(
      node: TSESTree.CallExpression | TSESTree.NewExpression,
    ): void {
      if (
        node.arguments.length !== 1 &&
        node.callee.type === AST_NODE_TYPES.Identifier &&
        node.callee.name === 'Array' &&
        !node.typeArguments
      ) {
        context.report({
          node,
          messageId: 'useLiteral',
          fix(fixer) {
            const argsText = getArgumentsText(node);

            return fixer.replaceText(node, `[${argsText}]`);
          },
        });
      }
    }

Generated by Syntax Scribe