forked from typescript-eslint/typescript-eslint
/
ban-ts-comment.ts
185 lines (169 loc) · 5.63 KB
/
ban-ts-comment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import GraphemeSplitter from 'grapheme-splitter';
import * as util from '../util';
let splitter: GraphemeSplitter;
function isASCII(value: string): boolean {
return /^[\u0020-\u007f]*$/u.test(value);
}
function getStringLength(value: string): number {
if (isASCII(value)) {
return value.length;
}
splitter ??= new GraphemeSplitter();
return splitter.countGraphemes(value);
}
type DirectiveConfig =
| boolean
| 'allow-with-description'
| { descriptionFormat: string };
interface Options {
'ts-expect-error'?: DirectiveConfig;
'ts-ignore'?: DirectiveConfig;
'ts-nocheck'?: DirectiveConfig;
'ts-check'?: DirectiveConfig;
minimumDescriptionLength?: number;
}
export const defaultMinimumDescriptionLength = 3;
type MessageIds =
| 'tsDirectiveComment'
| 'tsDirectiveCommentRequiresDescription'
| 'tsDirectiveCommentDescriptionNotMatchPattern';
export default util.createRule<[Options], MessageIds>({
name: 'ban-ts-comment',
meta: {
type: 'problem',
docs: {
description:
'Disallow `@ts-<directive>` comments or require descriptions after directive',
recommended: 'error',
},
messages: {
tsDirectiveComment:
'Do not use "@ts-{{directive}}" because it alters compilation errors.',
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.',
tsDirectiveCommentDescriptionNotMatchPattern:
'The description for the "@ts-{{directive}}" directive must match the {{format}} format.',
},
schema: {
$defs: {
directiveConfigSchema: {
oneOf: [
{
type: 'boolean',
default: true,
},
{
enum: ['allow-with-description'],
},
{
type: 'object',
properties: {
descriptionFormat: { type: 'string' },
},
},
],
},
},
prefixItems: [
{
properties: {
'ts-expect-error': {
$ref: '#/$defs/directiveConfigSchema',
},
'ts-ignore': { $ref: '#/$defs/directiveConfigSchema' },
'ts-nocheck': { $ref: '#/$defs/directiveConfigSchema' },
'ts-check': { $ref: '#/$defs/directiveConfigSchema' },
minimumDescriptionLength: {
type: 'number',
default: defaultMinimumDescriptionLength,
},
},
additionalProperties: false,
},
],
type: 'array',
},
},
defaultOptions: [
{
'ts-expect-error': 'allow-with-description',
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
minimumDescriptionLength: defaultMinimumDescriptionLength,
},
],
create(context, [options]) {
/*
The regex used are taken from the ones used in the official TypeScript repo -
https://github.com/microsoft/TypeScript/blob/408c760fae66080104bc85c449282c2d207dfe8e/src/compiler/scanner.ts#L288-L296
*/
const commentDirectiveRegExSingleLine =
/^\/*\s*@ts-(?<directive>expect-error|ignore|check|nocheck)(?<description>.*)/;
const commentDirectiveRegExMultiLine =
/^\s*(?:\/|\*)*\s*@ts-(?<directive>expect-error|ignore|check|nocheck)(?<description>.*)/;
const sourceCode = context.getSourceCode();
const descriptionFormats = new Map<string, RegExp>();
for (const directive of [
'ts-expect-error',
'ts-ignore',
'ts-nocheck',
'ts-check',
] as const) {
const option = options[directive];
if (typeof option === 'object' && option.descriptionFormat) {
descriptionFormats.set(directive, new RegExp(option.descriptionFormat));
}
}
return {
Program(): void {
const comments = sourceCode.getAllComments();
comments.forEach(comment => {
const regExp =
comment.type === AST_TOKEN_TYPES.Line
? commentDirectiveRegExSingleLine
: commentDirectiveRegExMultiLine;
const match = regExp.exec(comment.value);
if (!match) {
return;
}
const { directive, description } = match.groups!;
const fullDirective = `ts-${directive}` as keyof Options;
const option = options[fullDirective];
if (option === true) {
context.report({
data: { directive },
node: comment,
messageId: 'tsDirectiveComment',
});
}
if (
option === 'allow-with-description' ||
(typeof option === 'object' && option.descriptionFormat)
) {
const {
minimumDescriptionLength = defaultMinimumDescriptionLength,
} = options;
const format = descriptionFormats.get(fullDirective);
if (
getStringLength(description.trim()) < minimumDescriptionLength
) {
context.report({
data: { directive, minimumDescriptionLength },
node: comment,
messageId: 'tsDirectiveCommentRequiresDescription',
});
} else if (format && !format.test(description)) {
context.report({
data: { directive, format: format.source },
node: comment,
messageId: 'tsDirectiveCommentDescriptionNotMatchPattern',
});
}
}
});
},
};
},
});