UNPKG

3.44 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const util = __importStar(require("../util"));
23const utils_1 = require("@typescript-eslint/utils");
24exports.default = util.createRule({
25 name: 'prefer-ts-expect-error',
26 meta: {
27 type: 'problem',
28 docs: {
29 description: 'Recommends using `@ts-expect-error` over `@ts-ignore`',
30 recommended: false,
31 },
32 fixable: 'code',
33 messages: {
34 preferExpectErrorComment: 'Use "@ts-expect-error" to ensure an error is actually being suppressed.',
35 },
36 schema: [],
37 },
38 defaultOptions: [],
39 create(context) {
40 const tsIgnoreRegExpSingleLine = /^\s*\/?\s*@ts-ignore/;
41 const tsIgnoreRegExpMultiLine = /^\s*(?:\/|\*)*\s*@ts-ignore/;
42 const sourceCode = context.getSourceCode();
43 function isLineComment(comment) {
44 return comment.type === utils_1.AST_TOKEN_TYPES.Line;
45 }
46 function getLastCommentLine(comment) {
47 if (isLineComment(comment)) {
48 return comment.value;
49 }
50 // For multiline comments - we look at only the last line.
51 const commentlines = comment.value.split('\n');
52 return commentlines[commentlines.length - 1];
53 }
54 function isValidTsIgnorePresent(comment) {
55 const line = getLastCommentLine(comment);
56 return isLineComment(comment)
57 ? tsIgnoreRegExpSingleLine.test(line)
58 : tsIgnoreRegExpMultiLine.test(line);
59 }
60 return {
61 Program() {
62 const comments = sourceCode.getAllComments();
63 comments.forEach(comment => {
64 if (isValidTsIgnorePresent(comment)) {
65 const lineCommentRuleFixer = (fixer) => fixer.replaceText(comment, `//${comment.value.replace('@ts-ignore', '@ts-expect-error')}`);
66 const blockCommentRuleFixer = (fixer) => fixer.replaceText(comment, `/*${comment.value.replace('@ts-ignore', '@ts-expect-error')}*/`);
67 context.report({
68 node: comment,
69 messageId: 'preferExpectErrorComment',
70 fix: isLineComment(comment)
71 ? lineCommentRuleFixer
72 : blockCommentRuleFixer,
73 });
74 }
75 });
76 },
77 };
78 },
79});
80//# sourceMappingURL=prefer-ts-expect-error.js.map
\No newline at end of file