UNPKG

6.19 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 });
22exports.defaultMinimumDescriptionLength = void 0;
23const utils_1 = require("@typescript-eslint/utils");
24const util = __importStar(require("../util"));
25exports.defaultMinimumDescriptionLength = 3;
26exports.default = util.createRule({
27 name: 'ban-ts-comment',
28 meta: {
29 type: 'problem',
30 docs: {
31 description: 'Bans `@ts-<directive>` comments from being used or requires descriptions after directive',
32 recommended: 'error',
33 },
34 messages: {
35 tsDirectiveComment: 'Do not use "@ts-{{directive}}" because it alters compilation errors.',
36 tsDirectiveCommentRequiresDescription: 'Include a description after the "@ts-{{directive}}" directive to explain why the @ts-{{directive}} is necessary. The description must be {{minimumDescriptionLength}} characters or longer.',
37 },
38 schema: [
39 {
40 type: 'object',
41 properties: {
42 'ts-expect-error': {
43 oneOf: [
44 {
45 type: 'boolean',
46 default: true,
47 },
48 {
49 enum: ['allow-with-description'],
50 },
51 ],
52 },
53 'ts-ignore': {
54 oneOf: [
55 {
56 type: 'boolean',
57 default: true,
58 },
59 {
60 enum: ['allow-with-description'],
61 },
62 ],
63 },
64 'ts-nocheck': {
65 oneOf: [
66 {
67 type: 'boolean',
68 default: true,
69 },
70 {
71 enum: ['allow-with-description'],
72 },
73 ],
74 },
75 'ts-check': {
76 oneOf: [
77 {
78 type: 'boolean',
79 default: true,
80 },
81 {
82 enum: ['allow-with-description'],
83 },
84 ],
85 },
86 minimumDescriptionLength: {
87 type: 'number',
88 default: exports.defaultMinimumDescriptionLength,
89 },
90 },
91 additionalProperties: false,
92 },
93 ],
94 },
95 defaultOptions: [
96 {
97 'ts-expect-error': 'allow-with-description',
98 'ts-ignore': true,
99 'ts-nocheck': true,
100 'ts-check': false,
101 minimumDescriptionLength: exports.defaultMinimumDescriptionLength,
102 },
103 ],
104 create(context, [options]) {
105 /*
106 The regex used are taken from the ones used in the official TypeScript repo -
107 https://github.com/microsoft/TypeScript/blob/main/src/compiler/scanner.ts#L281-L289
108 */
109 const commentDirectiveRegExSingleLine = /^\/*\s*@ts-(expect-error|ignore|check|nocheck)(.*)/;
110 const commentDirectiveRegExMultiLine = /^\s*(?:\/|\*)*\s*@ts-(expect-error|ignore|check|nocheck)(.*)/;
111 const sourceCode = context.getSourceCode();
112 return {
113 Program() {
114 const comments = sourceCode.getAllComments();
115 comments.forEach(comment => {
116 var _a;
117 let regExp = commentDirectiveRegExSingleLine;
118 if (comment.type !== utils_1.AST_TOKEN_TYPES.Line) {
119 regExp = commentDirectiveRegExMultiLine;
120 }
121 const [, directive, description] = (_a = regExp.exec(comment.value)) !== null && _a !== void 0 ? _a : [];
122 const fullDirective = `ts-${directive}`;
123 const option = options[fullDirective];
124 if (option === true) {
125 context.report({
126 data: { directive },
127 node: comment,
128 messageId: 'tsDirectiveComment',
129 });
130 }
131 if (option === 'allow-with-description') {
132 const { minimumDescriptionLength = exports.defaultMinimumDescriptionLength, } = options;
133 if (description.trim().length < minimumDescriptionLength) {
134 context.report({
135 data: { directive, minimumDescriptionLength },
136 node: comment,
137 messageId: 'tsDirectiveCommentRequiresDescription',
138 });
139 }
140 }
141 });
142 },
143 };
144 },
145});
146//# sourceMappingURL=ban-ts-comment.js.map
\No newline at end of file