Skip to content

⬅️ Back to Table of Contents

📄 no-unused-expressions

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 6
📊 Variables & Constants 1
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/no-unused-expressions.ts

📤 Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-unused-expressions'
meta.type 'suggestion'
meta.docs.description 'Disallow unused expressions'
meta.docs.extendsBaseRule true
meta.docs.recommended 'recommended'
meta.hasSuggestions baseRule.meta.hasSuggestions
meta.messages baseRule.meta.messages
meta.schema baseRule.meta.schema

Entry point: create — documented under Functions.


📦 Imports

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

Variables & Constants

Name Type Kind Value Exported
defaultOptions Options const [ { allowShortCircuit: false, allowTaggedTemplates: false, allowTernary: fals...

Functions

create(context: any, [{ allowShortCircuit = false, a…: any): { ExpressionStatement(node: any): void; }

Parameters:

  • context any
  • [{ allowShortCircuit = false, allowTernary = false }] any

Returns: { ExpressionStatement(node: any): void; }

Calls:

  • baseRule.create
  • isValidExpression
  • rules.ExpressionStatement
Code
create(context, [{ allowShortCircuit = false, allowTernary = false }]) {
    const rules = baseRule.create(context);

    function isValidExpression(node: TSESTree.Node): boolean {
      if (allowShortCircuit && node.type === AST_NODE_TYPES.LogicalExpression) {
        return isValidExpression(node.right);
      }
      if (allowTernary && node.type === AST_NODE_TYPES.ConditionalExpression) {
        return (
          isValidExpression(node.alternate) &&
          isValidExpression(node.consequent)
        );
      }
      return (
        (node.type === AST_NODE_TYPES.ChainExpression &&
          node.expression.type === AST_NODE_TYPES.CallExpression) ||
        node.type === AST_NODE_TYPES.ImportExpression
      );
    }

    return {
      ExpressionStatement(node): void {
        if (node.directive || isValidExpression(node.expression)) {
          return;
        }

        const expressionType = node.expression.type;

        if (
          expressionType ===
            TSESTree.AST_NODE_TYPES.TSInstantiationExpression ||
          expressionType === TSESTree.AST_NODE_TYPES.TSAsExpression ||
          expressionType === TSESTree.AST_NODE_TYPES.TSNonNullExpression ||
          expressionType === TSESTree.AST_NODE_TYPES.TSTypeAssertion
        ) {
          rules.ExpressionStatement({
            ...node,
            expression: node.expression.expression,
          });
          return;
        }

        rules.ExpressionStatement(node);
      },
    };
  }

Internal helpers

Declared inside another function in this file.

isValidExpression(node: TSESTree.Node): boolean

Parameters:

  • node TSESTree.Node

Returns: boolean

Calls:

  • isValidExpression
Code
function isValidExpression(node: TSESTree.Node): boolean {
      if (allowShortCircuit && node.type === AST_NODE_TYPES.LogicalExpression) {
        return isValidExpression(node.right);
      }
      if (allowTernary && node.type === AST_NODE_TYPES.ConditionalExpression) {
        return (
          isValidExpression(node.alternate) &&
          isValidExpression(node.consequent)
        );
      }
      return (
        (node.type === AST_NODE_TYPES.ChainExpression &&
          node.expression.type === AST_NODE_TYPES.CallExpression) ||
        node.type === AST_NODE_TYPES.ImportExpression
      );
    }

Type Aliases

MessageIds

type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;

Options

type Options = InferOptionsTypeFromRule<typeof baseRule>;

Generated by Syntax Scribe