Skip to content

⬅️ Back to Table of Contents

📄 no-unsafe-unary-minus

📊 Analysis Summary

Metric Count
🔧 Functions 1
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/no-unsafe-unary-minus.ts

📤 Default Export

export default util.createRule<Options, MessageIds>({ ... })
Property Value
name 'no-unsafe-unary-minus'
meta.type 'problem'
meta.docs.description 'Require unary negation to take a number'
meta.docs.recommended 'recommended'
meta.docs.requiresTypeChecking true
meta.messages.unaryMinus 'Argument of unary negation should be assignable to number | bigint but is {{type}} instead.'
meta.schema []
defaultOptions []

Entry point: create — documented under Functions.


Functions

create(context: any): { UnaryExpression(node: any): void; }

Parameters:

  • context any

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

Calls:

  • util.getParserServices
  • util.getConstrainedTypeAtLocation
  • services.program.getTypeChecker
  • tsutils .unionConstituents(argType) .some
  • tsutils.isTypeFlagSet
  • context.report
  • checker.typeToString
Code
create(context) {
    return {
      UnaryExpression(node): void {
        if (node.operator !== '-') {
          return;
        }
        const services = util.getParserServices(context);
        const argType = util.getConstrainedTypeAtLocation(
          services,
          node.argument,
        );
        const checker = services.program.getTypeChecker();
        if (
          tsutils
            .unionConstituents(argType)
            .some(
              type =>
                !tsutils.isTypeFlagSet(
                  type,
                  ts.TypeFlags.Any |
                    ts.TypeFlags.Never |
                    ts.TypeFlags.BigIntLike |
                    ts.TypeFlags.NumberLike,
                ),
            )
        ) {
          context.report({
            node,
            messageId: 'unaryMinus',
            data: { type: checker.typeToString(argType) },
          });
        }
      },
    };
  }

Type Aliases

Options

type Options = [];

MessageIds

type MessageIds = 'unaryMinus';

Generated by Syntax Scribe