Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-non-null-asserted-optional-chain

πŸ“Š Analysis Summary

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

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-non-null-asserted-optional-chain.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'no-non-null-asserted-optional-chain'
meta.type 'problem'
meta.docs.description 'Disallow non-null assertions after an optional chain expression'
meta.docs.recommended 'recommended'
meta.hasSuggestions true
meta.messages.noNonNullOptionalChain 'Optional chain expressions can return undefined by design - using a non-null assertion is unsafe and wrong.'
meta.messages.suggestRemovingNonNull 'You should remove the non-null assertion.'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

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

Functions

create(context: any): { 'TSNonNullExpression > ChainExpression'(node: TSESTree.Ch…

Parameters:

  • context any

Returns: { 'TSNonNullExpression > ChainExpression'(node: TSESTree.ChainExpression): void; 'ChainExpression > TSNonNullExpression'(node: TSESTree.TSNonNullExpression): void; }

Calls:

  • context.report
  • fixer.removeRange

Internal Comments:

// non-nulling a wrapped chain will scrub all nulls introduced by the chain (x2)
// (x?.y)! (x2)
// (x?.())! (x2)
// selector guarantees this assertion (x2)
// use a suggestion instead of a fixer, because this can obviously break type checks (x4)
// non-nulling at the end of a chain will scrub all nulls introduced by the chain (x2)
// x?.y! (x2)
// x?.()! (x2)

Code
create(context) {
    return {
      // non-nulling a wrapped chain will scrub all nulls introduced by the chain
      // (x?.y)!
      // (x?.())!
      'TSNonNullExpression > ChainExpression'(
        node: TSESTree.ChainExpression,
      ): void {
        // selector guarantees this assertion
        const parent = node.parent as TSESTree.TSNonNullExpression;
        context.report({
          node,
          messageId: 'noNonNullOptionalChain',
          // use a suggestion instead of a fixer, because this can obviously break type checks
          suggest: [
            {
              messageId: 'suggestRemovingNonNull',
              fix(fixer): TSESLint.RuleFix {
                return fixer.removeRange([
                  parent.range[1] - 1,
                  parent.range[1],
                ]);
              },
            },
          ],
        });
      },

      // non-nulling at the end of a chain will scrub all nulls introduced by the chain
      // x?.y!
      // x?.()!
      'ChainExpression > TSNonNullExpression'(
        node: TSESTree.TSNonNullExpression,
      ): void {
        context.report({
          node,
          messageId: 'noNonNullOptionalChain',
          // use a suggestion instead of a fixer, because this can obviously break type checks
          suggest: [
            {
              messageId: 'suggestRemovingNonNull',
              fix(fixer): TSESLint.RuleFix {
                return fixer.removeRange([node.range[1] - 1, node.range[1]]);
              },
            },
          ],
        });
      },
    };
  }

Generated by Syntax Scribe