Skip to content

⬅️ Back to Table of Contents

📄 prefer-ts-expect-error

📊 Analysis Summary

Metric Count
🔧 Functions 6
📦 Imports 6
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/prefer-ts-expect-error.ts

📤 Default Export

export default createRule<[], MessageIds>({ ... })
Property Value
name 'prefer-ts-expect-error'
meta.type 'problem'
meta.deprecated.deprecatedSince '7.11.0'
meta.deprecated.replacedBy [ { rule: { name: '@typescript-eslint/ban-ts-comment', url: 'https://typescript-eslint.io/rules/ban-ts-comment', }, }, ]
meta.deprecated.url 'https://github.com/typescript-eslint/typescript-eslint/pull/9081'
meta.docs.description 'Enforce using @ts-expect-error over @ts-ignore'
meta.fixable 'code'
meta.messages.preferExpectErrorComment 'Use "@ts-expect-error" to ensure an error is actually being suppressed.'
meta.replacedBy ['@typescript-eslint/ban-ts-comment']
meta.schema []
defaultOptions []

Entry point: create — documented under Functions.


📦 Imports

Name Source
TSESTree @typescript-eslint/utils
RuleFix @typescript-eslint/utils/ts-eslint
RuleFixer @typescript-eslint/utils/ts-eslint
AST_TOKEN_TYPES @typescript-eslint/utils
ASTUtils @typescript-eslint/utils
createRule ../util

Functions

create(context: any): { Program(): void; }

Parameters:

  • context any

Returns: { Program(): void; }

Calls:

  • isLineComment
  • comment.value.split
  • getLastCommentLine
  • tsIgnoreRegExpSingleLine.test
  • tsIgnoreRegExpMultiLine.test
  • context.sourceCode.getAllComments
  • comments.forEach
  • isValidTsIgnorePresent
  • fixer.replaceText
  • comment.value.replace
  • context.report

Internal Comments:

// For multiline comments - we look at only the last line. (x2)

Code
create(context) {
    const tsIgnoreRegExpSingleLine = /^\s*\/?\s*@ts-ignore/;
    const tsIgnoreRegExpMultiLine = /^\s*(?:\/|\*)*\s*@ts-ignore/;

    function isLineComment(comment: TSESTree.Comment): boolean {
      return comment.type === AST_TOKEN_TYPES.Line;
    }

    function getLastCommentLine(comment: TSESTree.Comment): string {
      if (isLineComment(comment)) {
        return comment.value;
      }

      // For multiline comments - we look at only the last line.
      const commentlines = comment.value.split(ASTUtils.LINEBREAK_MATCHER);
      return commentlines[commentlines.length - 1];
    }

    function isValidTsIgnorePresent(comment: TSESTree.Comment): boolean {
      const line = getLastCommentLine(comment);
      return isLineComment(comment)
        ? tsIgnoreRegExpSingleLine.test(line)
        : tsIgnoreRegExpMultiLine.test(line);
    }

    return {
      Program(): void {
        const comments = context.sourceCode.getAllComments();
        comments.forEach(comment => {
          if (isValidTsIgnorePresent(comment)) {
            const lineCommentRuleFixer = (fixer: RuleFixer): RuleFix =>
              fixer.replaceText(
                comment,
                `//${comment.value.replace('@ts-ignore', '@ts-expect-error')}`,
              );

            const blockCommentRuleFixer = (fixer: RuleFixer): RuleFix =>
              fixer.replaceText(
                comment,
                `/*${comment.value.replace(
                  '@ts-ignore',
                  '@ts-expect-error',
                )}*/`,
              );

            context.report({
              node: comment,
              messageId: 'preferExpectErrorComment',
              fix: isLineComment(comment)
                ? lineCommentRuleFixer
                : blockCommentRuleFixer,
            });
          }
        });
      },
    };
  }

Internal helpers

Declared inside another function in this file.

isLineComment(comment: TSESTree.Comment): boolean

Parameters:

  • comment TSESTree.Comment

Returns: boolean

Code
function isLineComment(comment: TSESTree.Comment): boolean {
      return comment.type === AST_TOKEN_TYPES.Line;
    }

getLastCommentLine(comment: TSESTree.Comment): string

Parameters:

  • comment TSESTree.Comment

Returns: string

Calls:

  • isLineComment
  • comment.value.split

Internal Comments:

// For multiline comments - we look at only the last line. (x2)

Code
function getLastCommentLine(comment: TSESTree.Comment): string {
      if (isLineComment(comment)) {
        return comment.value;
      }

      // For multiline comments - we look at only the last line.
      const commentlines = comment.value.split(ASTUtils.LINEBREAK_MATCHER);
      return commentlines[commentlines.length - 1];
    }

isValidTsIgnorePresent(comment: TSESTree.Comment): boolean

Parameters:

  • comment TSESTree.Comment

Returns: boolean

Calls:

  • getLastCommentLine
  • isLineComment
  • tsIgnoreRegExpSingleLine.test
  • tsIgnoreRegExpMultiLine.test
Code
function isValidTsIgnorePresent(comment: TSESTree.Comment): boolean {
      const line = getLastCommentLine(comment);
      return isLineComment(comment)
        ? tsIgnoreRegExpSingleLine.test(line)
        : tsIgnoreRegExpMultiLine.test(line);
    }

lineCommentRuleFixer(fixer: RuleFixer): RuleFix

Parameters:

  • fixer RuleFixer

Returns: RuleFix

Calls:

  • fixer.replaceText
Code
(fixer: RuleFixer): RuleFix =>
              fixer.replaceText(
                comment,
                `//${comment.value.replace('@ts-ignore', '@ts-expect-error')}`,
              )

blockCommentRuleFixer(fixer: RuleFixer): RuleFix

Parameters:

  • fixer RuleFixer

Returns: RuleFix

Calls:

  • fixer.replaceText
Code
(fixer: RuleFixer): RuleFix =>
              fixer.replaceText(
                comment,
                `/*${comment.value.replace(
                  '@ts-ignore',
                  '@ts-expect-error',
                )}*/`,
              )

Type Aliases

MessageIds

type MessageIds = 'preferExpectErrorComment';

Generated by Syntax Scribe