Skip to content

⬅️ Back to Table of Contents

πŸ“„ default-param-last

πŸ“Š Analysis Summary

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

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/default-param-last.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'default-param-last'
meta.type 'suggestion'
meta.docs.description 'Enforce default parameters to be last'
meta.docs.extendsBaseRule true
meta.docs.frozen true
meta.messages.shouldBeLast 'Default parameters should be last.'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

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

Functions

create(context: any): { ArrowFunctionExpression: (node: TSESTree.ArrowFunctionExp…

Parameters:

  • context any

Returns: { ArrowFunctionExpression: (node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void; FunctionDeclaration: (node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void; FunctionExpression: (node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression) => void; }

Calls:

  • isOptionalParam
  • isPlainParam
  • context.report

Internal Comments:

/**
     * checks if node is optional parameter
     * @param node the node to be evaluated
     * @private
     */
/**
     * checks if node is plain parameter
     * @param node the node to be evaluated
     * @private
     */

Code
create(context) {
    /**
     * checks if node is optional parameter
     * @param node the node to be evaluated
     * @private
     */
    function isOptionalParam(node: TSESTree.Parameter): boolean {
      return (
        (node.type === AST_NODE_TYPES.ArrayPattern ||
          node.type === AST_NODE_TYPES.AssignmentPattern ||
          node.type === AST_NODE_TYPES.Identifier ||
          node.type === AST_NODE_TYPES.ObjectPattern ||
          node.type === AST_NODE_TYPES.RestElement) &&
        node.optional
      );
    }

    /**
     * checks if node is plain parameter
     * @param node the node to be evaluated
     * @private
     */
    function isPlainParam(node: TSESTree.Parameter): boolean {
      return !(
        node.type === AST_NODE_TYPES.AssignmentPattern ||
        node.type === AST_NODE_TYPES.RestElement ||
        isOptionalParam(node)
      );
    }

    function checkDefaultParamLast(
      node:
        | TSESTree.ArrowFunctionExpression
        | TSESTree.FunctionDeclaration
        | TSESTree.FunctionExpression,
    ): void {
      let hasSeenPlainParam = false;
      for (let i = node.params.length - 1; i >= 0; i--) {
        const current = node.params[i];
        const param =
          current.type === AST_NODE_TYPES.TSParameterProperty
            ? current.parameter
            : current;

        if (isPlainParam(param)) {
          hasSeenPlainParam = true;
          continue;
        }

        if (
          hasSeenPlainParam &&
          (isOptionalParam(param) ||
            param.type === AST_NODE_TYPES.AssignmentPattern)
        ) {
          context.report({ node: current, messageId: 'shouldBeLast' });
        }
      }
    }

    return {
      ArrowFunctionExpression: checkDefaultParamLast,
      FunctionDeclaration: checkDefaultParamLast,
      FunctionExpression: checkDefaultParamLast,
    };
  }

Internal helpers

Declared inside another function in this file.

isOptionalParam(node: TSESTree.Parameter): boolean

checks if node is optional parameter

Parameters:

  • node any: the node to be evaluated

Tags: @private

Raw JSDoc
/**
     * checks if node is optional parameter
     * @param node the node to be evaluated
     * @private
     */
Code
function isOptionalParam(node: TSESTree.Parameter): boolean {
      return (
        (node.type === AST_NODE_TYPES.ArrayPattern ||
          node.type === AST_NODE_TYPES.AssignmentPattern ||
          node.type === AST_NODE_TYPES.Identifier ||
          node.type === AST_NODE_TYPES.ObjectPattern ||
          node.type === AST_NODE_TYPES.RestElement) &&
        node.optional
      );
    }

isPlainParam(node: TSESTree.Parameter): boolean

checks if node is plain parameter

Parameters:

  • node any: the node to be evaluated

Tags: @private

Raw JSDoc
/**
     * checks if node is plain parameter
     * @param node the node to be evaluated
     * @private
     */

Calls:

  • isOptionalParam
Code
function isPlainParam(node: TSESTree.Parameter): boolean {
      return !(
        node.type === AST_NODE_TYPES.AssignmentPattern ||
        node.type === AST_NODE_TYPES.RestElement ||
        isOptionalParam(node)
      );
    }

checkDefaultParamLast(node: | TSESTree.ArrowFunctionExpression | TS…): void

Parameters:

  • node | TSESTree.ArrowFunctionExpression | TSESTree.FunctionDeclaration | TSESTree.FunctionExpression

Returns: void

Calls:

  • isPlainParam
  • isOptionalParam
  • context.report
Code
function checkDefaultParamLast(
      node:
        | TSESTree.ArrowFunctionExpression
        | TSESTree.FunctionDeclaration
        | TSESTree.FunctionExpression,
    ): void {
      let hasSeenPlainParam = false;
      for (let i = node.params.length - 1; i >= 0; i--) {
        const current = node.params[i];
        const param =
          current.type === AST_NODE_TYPES.TSParameterProperty
            ? current.parameter
            : current;

        if (isPlainParam(param)) {
          hasSeenPlainParam = true;
          continue;
        }

        if (
          hasSeenPlainParam &&
          (isOptionalParam(param) ||
            param.type === AST_NODE_TYPES.AssignmentPattern)
        ) {
          context.report({ node: current, messageId: 'shouldBeLast' });
        }
      }
    }

Generated by Syntax Scribe