📄 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¶
| 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:
contextany
Returns: { Program(): void; }
Calls:
isLineCommentcomment.value.splitgetLastCommentLinetsIgnoreRegExpSingleLine.testtsIgnoreRegExpMultiLine.testcontext.sourceCode.getAllCommentscomments.forEachisValidTsIgnorePresentfixer.replaceTextcomment.value.replacecontext.report
Internal Comments:
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:
commentTSESTree.Comment
Returns: boolean
Code
getLastCommentLine(comment: TSESTree.Comment): string¶
Parameters:
commentTSESTree.Comment
Returns: string
Calls:
isLineCommentcomment.value.split
Internal Comments:
Code
isValidTsIgnorePresent(comment: TSESTree.Comment): boolean¶
Parameters:
commentTSESTree.Comment
Returns: boolean
Calls:
getLastCommentLineisLineCommenttsIgnoreRegExpSingleLine.testtsIgnoreRegExpMultiLine.test
Code
lineCommentRuleFixer(fixer: RuleFixer): RuleFix¶
Parameters:
fixerRuleFixer
Returns: RuleFix
Calls:
fixer.replaceText
Code
blockCommentRuleFixer(fixer: RuleFixer): RuleFix¶
Parameters:
fixerRuleFixer
Returns: RuleFix
Calls:
fixer.replaceText
Code
Type Aliases¶
MessageIds¶
Generated by Syntax Scribe