@kongyo2/ts-comment-scanner 1.8.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +14 -9
- package/README.md +14 -9
- package/dist/directives.d.ts +27 -9
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +351 -153
- package/dist/directives.js.map +1 -1
- package/dist/jsx.d.ts +54 -0
- package/dist/jsx.d.ts.map +1 -0
- package/dist/jsx.js +193 -0
- package/dist/jsx.js.map +1 -0
- package/dist/ranges.d.ts +10 -0
- package/dist/ranges.d.ts.map +1 -0
- package/dist/ranges.js +19 -0
- package/dist/ranges.js.map +1 -0
- package/dist/remove.d.ts +18 -5
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +460 -169
- package/dist/remove.js.map +1 -1
- package/dist/scanner.d.ts +7 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +26 -5
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts +48 -0
- package/dist/splice.d.ts.map +1 -0
- package/dist/splice.js +279 -0
- package/dist/splice.js.map +1 -0
- package/package.json +1 -1
- package/src/directives.ts +379 -155
- package/src/jsx.ts +263 -0
- package/src/ranges.ts +23 -0
- package/src/remove.ts +539 -174
- package/src/scanner.ts +33 -5
- package/src/splice.ts +317 -0
package/src/directives.ts
CHANGED
|
@@ -14,6 +14,11 @@ interface DirectiveRule {
|
|
|
14
14
|
blockOnly?: boolean;
|
|
15
15
|
/** Only match line comments (e.g. JetBrains suppressions in JS/TS). */
|
|
16
16
|
lineOnly?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Only match comments whose text holds no line feed — `//` comments and
|
|
19
|
+
* single-line block comments, the comments CodeQL reads suppressions from.
|
|
20
|
+
*/
|
|
21
|
+
singleLine?: boolean;
|
|
17
22
|
/** Match any content line, not just the first (for docblock pragmas). */
|
|
18
23
|
anyLine?: boolean;
|
|
19
24
|
/**
|
|
@@ -30,9 +35,8 @@ interface DirectiveRule {
|
|
|
30
35
|
*/
|
|
31
36
|
rawText?: boolean;
|
|
32
37
|
/**
|
|
33
|
-
* Skip `/**`-opened block comments:
|
|
34
|
-
*
|
|
35
|
-
* whitespace before their marker, so a leading `*` defeats them.
|
|
38
|
+
* Skip `/**`-opened block comments: the datadog analyzer skips only
|
|
39
|
+
* whitespace before its marker, so a leading `*` defeats it.
|
|
36
40
|
*/
|
|
37
41
|
notDocblock?: boolean;
|
|
38
42
|
/**
|
|
@@ -67,60 +71,110 @@ function stylelintDirective(token: string): string | undefined {
|
|
|
67
71
|
// --require-pragma looks for and --check-ignore-pragma stops on.
|
|
68
72
|
const FORMAT_PRAGMA_KEYS: ReadonlySet<string> = new Set(["format", "prettier", "noformat", "noprettier"]);
|
|
69
73
|
|
|
74
|
+
// The canonical spelling of each JSX pragma, keyed by its lowercased name.
|
|
75
|
+
const JSX_PRAGMAS: ReadonlyMap<string, string> = new Map(
|
|
76
|
+
["jsx", "jsxFrag", "jsxRuntime", "jsxImportSource", "jsxImportSourceTypes"].map((name) => [
|
|
77
|
+
name.toLowerCase(),
|
|
78
|
+
`@${name}`,
|
|
79
|
+
]),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// typescript-strict-plugin's two pragmas; the capture is the `-ignore` suffix.
|
|
83
|
+
const tsStrictName = (match: RegExpExecArray): string => (match[1] === undefined ? "@ts-strict" : "@ts-strict-ignore");
|
|
84
|
+
|
|
70
85
|
const RULES: DirectiveRule[] = [
|
|
71
|
-
// ESLint
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
86
|
+
// ESLint and oxlint. ESLint 10 takes the label from the trimmed comment value
|
|
87
|
+
// (`[a-z]+(?:-[a-z]+)*` followed by whitespace or the end) and honours only
|
|
88
|
+
// `eslint-disable-line`/`-next-line` in `//` comments; oxlint reads all four
|
|
89
|
+
// commands, and its own prefix, from comments of either kind, under the
|
|
90
|
+
// same boundary. A JSDoc star in front of the label defeats both, and so
|
|
91
|
+
// does a glued suffix (`eslint-disable-foo`, `eslint-disable:foo`).
|
|
92
|
+
{ pattern: /^(?:eslint|oxlint)-(?:disable(?:-next-line|-line)?|enable)(?=\s|$)/, keepStars: true },
|
|
93
|
+
// React Compiler (babel-plugin-react-compiler, and Bun's port of it) skips a
|
|
94
|
+
// component whose comments suppress its react-hooks rules: it searches each
|
|
95
|
+
// comment for `eslint-disable`/`-next-line`/`eslint-enable` followed by one
|
|
96
|
+
// of the rules, anywhere in the text — JSDoc blocks and mid-text mentions
|
|
97
|
+
// included.
|
|
98
|
+
{
|
|
99
|
+
pattern: /eslint-(disable(?:-next-line)?|enable)\s[^]*react-hooks\/(?:exhaustive-deps|rules-of-hooks)/,
|
|
100
|
+
rawText: true,
|
|
101
|
+
name: (match) => `eslint-${match[1] as string}`,
|
|
102
|
+
},
|
|
103
|
+
// ESLint 10 reports `/* eslint-env */` comments as no longer supported, and
|
|
104
|
+
// ESLint 9's eslintrc mode still applies them; both read block comments
|
|
105
|
+
// only, like the configuration comments below.
|
|
106
|
+
{ pattern: /^eslint-env(?=\s|$)/, blockOnly: true, keepStars: true },
|
|
107
|
+
{ pattern: /^eslint\s+\S/, name: "eslint", blockOnly: true, joinLines: true, keepStars: true },
|
|
75
108
|
{
|
|
76
109
|
pattern: /^(globals?|exported)\s+\S/,
|
|
77
110
|
name: (match) => match[1] as string,
|
|
78
111
|
blockOnly: true,
|
|
79
112
|
joinLines: true,
|
|
113
|
+
keepStars: true,
|
|
80
114
|
},
|
|
81
|
-
// TSLint (legacy)
|
|
82
|
-
|
|
115
|
+
// TSLint (legacy): tslint:(enable|disable)(-line|-next-line)? ending at a
|
|
116
|
+
// colon, whitespace or the end of the comment value.
|
|
117
|
+
{ pattern: /^tslint:(?:enable|disable)(?:-line|-next-line)?(?=:|\s|$)/, keepStars: true },
|
|
83
118
|
// JSHint (legacy): `key: value` options and ignore markers (`jshint
|
|
84
119
|
// esversion: 6`, `jshint ignore:line`) plus `-W###`/`+W###` warning toggles.
|
|
85
120
|
// Requiring those shapes keeps prose like `jshint is unused` ordinary.
|
|
86
121
|
{ pattern: /^jshint\s+(?:[A-Za-z$_][\w$]*\s*:|[-+]W\d+\b)/, name: "jshint", joinLines: true },
|
|
87
|
-
// JSCS (legacy)
|
|
88
|
-
// so the name is normalised to the compact form.
|
|
89
|
-
{
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
122
|
+
// JSCS (legacy) allows whitespace on both sides of the colon (`jscs :
|
|
123
|
+
// enable`), so the name is normalised to the compact form.
|
|
124
|
+
{
|
|
125
|
+
pattern: /^jscs\s*:\s*(disable|enable|ignore)/,
|
|
126
|
+
name: (match) => `jscs:${match[1]}`,
|
|
127
|
+
joinLines: true,
|
|
128
|
+
keepStars: true,
|
|
129
|
+
},
|
|
130
|
+
// JSLint: the directive word must sit immediately after the comment marker
|
|
131
|
+
// with no space (`/*jslint devel*/` works, `/* jslint */` does not) and
|
|
132
|
+
// needs a body, so these match the raw text; `//global` counts too. The
|
|
133
|
+
// whole-line pragmas (`/*jslint-disable*/`, `//jslint-ignore-line`) are
|
|
134
|
+
// exact strings.
|
|
135
|
+
{ pattern: /^\/[/*](jslint|property|global)\s+\S/, rawText: true, name: (match) => match[1] as string },
|
|
95
136
|
{ pattern: /^\/\*jslint-(disable|enable)\*\/$/, rawText: true, name: (match) => `jslint-${match[1]}` },
|
|
96
137
|
{ pattern: /^\/\/jslint-(ignore-line|quiet)$/, rawText: true, name: (match) => `jslint-${match[1]}` },
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
//
|
|
100
|
-
{
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
138
|
+
// Biome parses every line of a comment (leading `*`s stripped) that opens
|
|
139
|
+
// with `biome-ignore`; `-all` applies only before the file's first token
|
|
140
|
+
// (anything later is rejected as a misplaced top-level suppression).
|
|
141
|
+
{
|
|
142
|
+
pattern: /^biome-ignore(-all|-start|-end)?/,
|
|
143
|
+
name: (match) => `biome-ignore${match[1] ?? ""}`,
|
|
144
|
+
anyLine: true,
|
|
145
|
+
},
|
|
146
|
+
// Rome (Biome's predecessor) used the same line-by-line parser: a
|
|
147
|
+
// case-insensitive keyword with `-` or `_`, then categories (`lint`,
|
|
148
|
+
// `lint/<group>/<rule>`, `format`, `parse`, ...) and a mandatory colon —
|
|
149
|
+
// without the colon Rome reported a parse diagnostic and the suppression
|
|
150
|
+
// stayed inert.
|
|
104
151
|
{
|
|
105
|
-
pattern:
|
|
152
|
+
pattern:
|
|
153
|
+
/^rome[-_]ignore\s*(?:(?:lint(?:\/[A-Za-z0-9]+){0,2}|parse(?:\/[A-Za-z0-9]+)?|format|organizeImports)(?:\([^)]*\))?\s*)+:/i,
|
|
106
154
|
name: "rome-ignore",
|
|
107
155
|
anyLine: true,
|
|
108
156
|
},
|
|
109
|
-
// Deno
|
|
110
|
-
|
|
157
|
+
// Deno. deno_lint compares the first whitespace-delimited token of the
|
|
158
|
+
// trimmed comment text with the directive, so a JSDoc star in front defeats
|
|
159
|
+
// it; `deno-lint-ignore` may be a block comment (the only form inside JSX
|
|
160
|
+
// children), the file form only a line comment in the header. The coverage
|
|
161
|
+
// directives are line comments compared the same way, and
|
|
162
|
+
// `deno-coverage-ignore-file` only counts as the file's first comment.
|
|
163
|
+
{ pattern: /^deno-lint-ignore(?=\s|$)/, keepStars: true },
|
|
164
|
+
{ pattern: /^deno-(?:lint-ignore-file|coverage-ignore(?:-file|-start|-stop)?)(?=\s|$)/, lineOnly: true },
|
|
111
165
|
// Deno type directives. deno_graph matches them case-insensitively at the
|
|
112
|
-
// comment
|
|
113
|
-
// require quotes.
|
|
114
|
-
{ pattern: /^@deno-types\s*=\s*(?:"[^"
|
|
115
|
-
{ pattern: /^@ts-types\s*=\s*
|
|
116
|
-
{ pattern: /^@ts-self-types\s*=\s*
|
|
166
|
+
// start of the comment text; `@deno-types` accepts a bare specifier, the
|
|
167
|
+
// newer names require quotes. A JSDoc star in front defeats them.
|
|
168
|
+
{ pattern: /^@deno-types\s*=\s*(?:["'][^"']+["']|\S+)/i, keepStars: true, name: "@deno-types" },
|
|
169
|
+
{ pattern: /^@ts-types\s*=\s*["'][^"']+["']/i, keepStars: true, name: "@ts-types" },
|
|
170
|
+
{ pattern: /^@ts-self-types\s*=\s*["'][^"']+["']/i, keepStars: true, name: "@ts-self-types" },
|
|
117
171
|
// stylelint. A configuration comment is recognised from the first
|
|
118
172
|
// whitespace-delimited token of the trimmed comment body alone (see
|
|
119
173
|
// stylelintDirective), so a glued rule list, a suffix, prose before the
|
|
120
174
|
// marker or a different case leave the comment inert — as does a JSDoc star
|
|
121
175
|
// after `/**`, which postcss keeps as comment text (hence keepStars). `//`
|
|
122
176
|
// comments count too: postcss-scss and postcss-less hand stylelint the same
|
|
123
|
-
// trimmed text. Verified against stylelint 17.
|
|
177
|
+
// trimmed text. Verified against stylelint 17.16. A project-specific
|
|
124
178
|
// `configurationComment` prefix cannot be known here, so only the default
|
|
125
179
|
// marker is matched.
|
|
126
180
|
{ pattern: /^\S+/, keepStars: true, name: (match) => stylelintDirective(match[0]) },
|
|
@@ -134,14 +188,21 @@ const RULES: DirectiveRule[] = [
|
|
|
134
188
|
// the marker (`/** prettier-ignore */`) leaves the comment ordinary.
|
|
135
189
|
{ pattern: /^prettier-ignore$/, joinLines: true, keepStars: true },
|
|
136
190
|
{ pattern: /^oxfmt-ignore$/, joinLines: true, keepStars: true },
|
|
137
|
-
// dprint
|
|
138
|
-
//
|
|
139
|
-
//
|
|
140
|
-
//
|
|
141
|
-
|
|
191
|
+
// dprint, and `deno fmt`, which runs dprint's TypeScript plugin with
|
|
192
|
+
// `deno-fmt-ignore`/`deno-fmt-ignore-file` as its comment texts. The file
|
|
193
|
+
// pragma is a starts-with check on the file's leading comments, after only
|
|
194
|
+
// spaces in a `//` comment and any whitespace in a block one, so a tab or a
|
|
195
|
+
// `/**` docblock star defeats it. The node pragma is a substring bounded by
|
|
196
|
+
// non-alphanumerics anywhere in the comment, so a reason may follow — and
|
|
197
|
+
// below the header a stray file pragma still counts as one.
|
|
142
198
|
{
|
|
143
|
-
pattern:
|
|
144
|
-
|
|
199
|
+
pattern: /^\/(?:\/ *|\*\s*)(dprint|deno-fmt)-ignore-file/,
|
|
200
|
+
rawText: true,
|
|
201
|
+
name: (match) => `${match[1] as string}-ignore-file`,
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
pattern: /(?:^|[^\p{L}\p{N}])(dprint|deno-fmt)-ignore(?![\p{L}\p{N}])/u,
|
|
205
|
+
name: (match) => `${match[1] as string}-ignore`,
|
|
145
206
|
anyLine: true,
|
|
146
207
|
},
|
|
147
208
|
// Prettier pragma mode (--require-pragma / --check-ignore-pragma). Prettier
|
|
@@ -164,13 +225,24 @@ const RULES: DirectiveRule[] = [
|
|
|
164
225
|
// the expression that follows it (`/** @type {Foo} */ (bar)`), the cast
|
|
165
226
|
// tsc itself honours in JavaScript. A `/*` comment or `@typedef` is not one.
|
|
166
227
|
{ pattern: /^\/\*\*[^]*?@(type|satisfies)\b/, rawText: true, blockOnly: true, name: (match) => `@${match[1]}` },
|
|
167
|
-
// prettier-plugin-organize-imports
|
|
168
|
-
//
|
|
169
|
-
|
|
228
|
+
// Import sorting. prettier-plugin-organize-imports skips a file whose text
|
|
229
|
+
// contains `// organize-imports-ignore` or `// tslint:disable:ordered-imports`
|
|
230
|
+
// (single space included, so it matches the raw comment text);
|
|
231
|
+
// @trivago/prettier-plugin-sort-imports, and Bun's port of it, skip one
|
|
232
|
+
// whose leading or import comments start with `sort-imports-ignore` once
|
|
233
|
+
// leading whitespace is trimmed.
|
|
234
|
+
{
|
|
235
|
+
pattern: /\/\/ (?:organize-imports-ignore|tslint:disable:ordered-imports)/,
|
|
236
|
+
rawText: true,
|
|
237
|
+
name: "organize-imports-ignore",
|
|
238
|
+
},
|
|
239
|
+
{ pattern: /^sort-imports-ignore/, keepStars: true, name: "sort-imports-ignore" },
|
|
170
240
|
// js-beautify (legacy): exact-shape block comments with single spaces; only
|
|
171
|
-
// the ignore/preserve pairs have an effect.
|
|
241
|
+
// the ignore/preserve pairs have an effect. The comment ending an ignored
|
|
242
|
+
// range is searched with `\s` instead, so any one whitespace character
|
|
243
|
+
// separates its words.
|
|
172
244
|
{
|
|
173
|
-
pattern: /^\/\* beautify(?: (?:ignore|preserve):(?:start|end))+ \*\/$/,
|
|
245
|
+
pattern: /^\/\*(?: beautify(?: (?:ignore|preserve):(?:start|end))+ |\sbeautify\signore:end\s)\*\/$/,
|
|
174
246
|
rawText: true,
|
|
175
247
|
blockOnly: true,
|
|
176
248
|
name: "beautify",
|
|
@@ -186,9 +258,23 @@ const RULES: DirectiveRule[] = [
|
|
|
186
258
|
{ pattern: /^c8\s+ignore\b/, name: "c8-ignore" },
|
|
187
259
|
{ pattern: /^v8\s+ignore\s+([a-z]+)/, name: (match) => `v8-ignore-${match[1]}` },
|
|
188
260
|
{ pattern: /^v8\s+ignore\b/, name: "v8-ignore" },
|
|
261
|
+
// Node core reads `node:coverage` with `ignore next [N]`, `disable` and
|
|
262
|
+
// `enable`; Vitest's v8 provider also takes its `ignore` with `start`/`stop`
|
|
263
|
+
// as a range, which gets a range name like the other tools'
|
|
264
|
+
// (`node:coverage-ignore` stays the counted next-line pragma). Source text
|
|
265
|
+
// must never spell a range pragma out: the v8 provider finds one on any line
|
|
266
|
+
// of this file too, and would drop the rest of it from coverage.
|
|
189
267
|
{
|
|
190
|
-
pattern: /^node:coverage\s+(disable|enable|ignore)\b/,
|
|
191
|
-
name: (match) =>
|
|
268
|
+
pattern: /^node:coverage\s+(?:ignore\s+(start|stop)(?=\W|$)|(disable|enable|ignore)\b)/,
|
|
269
|
+
name: (match) =>
|
|
270
|
+
match[1] === undefined ? `node:coverage-${match[2] as string}` : `node:coverage-ignore-${match[1]}`,
|
|
271
|
+
},
|
|
272
|
+
// ast-v8-to-istanbul finds `ignore start`/`stop` ranges with a plain text
|
|
273
|
+
// search over every line, so those count after any other text as well.
|
|
274
|
+
{
|
|
275
|
+
pattern: /(istanbul|[cv]8|node:coverage)\s+ignore\s+(start|stop)(?=\W|$)/,
|
|
276
|
+
name: (match) => `${match[1] as string}-ignore-${match[2] as string}`,
|
|
277
|
+
anyLine: true,
|
|
192
278
|
},
|
|
193
279
|
// type-coverage: substring match in any comment. There is no ignore-file
|
|
194
280
|
// comment — file-level ignores are a CLI option only.
|
|
@@ -198,32 +284,34 @@ const RULES: DirectiveRule[] = [
|
|
|
198
284
|
anyLine: true,
|
|
199
285
|
},
|
|
200
286
|
// StrykerJS mutation testing. The instrumenter runs
|
|
201
|
-
// /^\s?Stryker (disable|restore)( next-line)?
|
|
202
|
-
// comment value: at most one space before `Stryker`, single literal
|
|
203
|
-
// and a mandatory mutator list (`all` or names
|
|
287
|
+
// /^\s?Stryker (disable|restore)(?: (next-line))? ([a-zA-Z, ]+)/ against the
|
|
288
|
+
// raw comment value: at most one space before `Stryker`, single literal
|
|
289
|
+
// spaces, and a mandatory mutator list (`all` or names, which may itself
|
|
290
|
+
// open with blanks or a comma) — hence the rawText match.
|
|
204
291
|
{
|
|
205
|
-
pattern: /^\/[/*]\s?Stryker (disable|restore)( next-line)? [a-zA-Z]/,
|
|
292
|
+
pattern: /^\/[/*]\s?Stryker (disable|restore)( next-line)? [a-zA-Z, ]/,
|
|
206
293
|
rawText: true,
|
|
207
294
|
name: (match) => `stryker-${match[1]}${match[2] === undefined ? "" : "-next-line"}`,
|
|
208
295
|
},
|
|
209
296
|
// ts-prune: substring of the closest leading comment above an export.
|
|
210
297
|
{ pattern: /ts-prune-ignore-next/, name: "ts-prune-ignore-next", anyLine: true },
|
|
211
|
-
// typescript-strict-plugin
|
|
212
|
-
//
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
},
|
|
298
|
+
// typescript-strict-plugin. Its CLI splits `//` lines on spaces and compares
|
|
299
|
+
// the tokens, so the pragma may sit anywhere on such a line; its IDE plugin
|
|
300
|
+
// finds it with tsc's TODO-comment scan instead — case-insensitively, right
|
|
301
|
+
// after the comment opener or a line's leading stars, and followed by
|
|
302
|
+
// anything but an ASCII letter or digit (`@ts-strict-ignore: legacy`).
|
|
303
|
+
{ pattern: /(?:^|\s)@ts-strict(-ignore)?(?=\s|$)/, name: tsStrictName, anyLine: true },
|
|
304
|
+
{ pattern: /(?:^[\s*]*|\/\/+\s*|\/\*+\s*)@ts-strict(-ignore)?(?![A-Za-z0-9])/im, rawText: true, name: tsStrictName },
|
|
218
305
|
// Flow. The pragmas live in the file's leading comments. Prettier's babel
|
|
219
306
|
// parser switches to Flow syntax when /@(?:no)?flow\b/ matches anywhere in
|
|
220
307
|
// the text before the first token (isFlowFile in its parse/babel.js), so
|
|
221
308
|
// `foo@flow.com` or `@flow-strict` in a header comment is live to it; Flow's
|
|
222
309
|
// own tokenised pragma (`@flow` as a whole word) is a subset of that.
|
|
223
|
-
// Suppressions must open the comment and take an optional [error-code]
|
|
224
|
-
//
|
|
225
|
-
//
|
|
226
|
-
// rule:severity
|
|
310
|
+
// Suppressions must open the comment and take an optional [error-code]
|
|
311
|
+
// (Flow now requires one; older releases did not); all four historical
|
|
312
|
+
// suppressors are recognised ($FlowIssue/$FlowIgnore stopped working in
|
|
313
|
+
// Flow 0.281 but still gate older setups). flowlint takes rule:severity
|
|
314
|
+
// pairs.
|
|
227
315
|
{
|
|
228
316
|
pattern: /@(no)?flow\b/,
|
|
229
317
|
name: (match) => (match[1] === undefined ? "@flow" : "@noflow"),
|
|
@@ -233,6 +321,13 @@ const RULES: DirectiveRule[] = [
|
|
|
233
321
|
pattern: /^\$Flow(FixMe|ExpectedError|Issue|Ignore)(?:\[[a-z-]+\])?(?![\w-])/,
|
|
234
322
|
name: (match) => `$Flow${match[1]}`,
|
|
235
323
|
},
|
|
324
|
+
// React Compiler also skips a component under a Flow suppression of its
|
|
325
|
+
// `react-rule-*` errors, found anywhere in a comment.
|
|
326
|
+
{
|
|
327
|
+
pattern: /\$(?:(FlowFixMe)\w*|(FlowExpectedError|FlowIssue))\[react-rule/,
|
|
328
|
+
rawText: true,
|
|
329
|
+
name: (match) => `$${match[1] ?? match[2]}`,
|
|
330
|
+
},
|
|
236
331
|
{
|
|
237
332
|
pattern: /^flowlint(-line|-next-line)?\s+[a-z][a-z-]*\s*:\s*(?:off|warn|error)/,
|
|
238
333
|
name: (match) => `flowlint${match[1] ?? ""}`,
|
|
@@ -240,9 +335,15 @@ const RULES: DirectiveRule[] = [
|
|
|
240
335
|
},
|
|
241
336
|
// Bundlers. webpack's gate regex requires the colon to follow the key
|
|
242
337
|
// immediately and evaluates the body as an object literal, which JSDoc
|
|
243
|
-
// stars break
|
|
244
|
-
|
|
245
|
-
|
|
338
|
+
// stars break. Turbopack reads the whole trimmed comment as one `key:
|
|
339
|
+
// value` pair instead — blanks before the colon allowed — and honours
|
|
340
|
+
// webpackIgnore and webpackExports as well as its own keys.
|
|
341
|
+
{ pattern: /^webpack[A-Z][A-Za-z]+:/, name: "webpack-magic-comment", keepStars: true },
|
|
342
|
+
{
|
|
343
|
+
pattern: /^(?:turbopack[A-Z][A-Za-z]+|webpack(?:Ignore|Exports))\s*:/,
|
|
344
|
+
name: "turbopack-magic-comment",
|
|
345
|
+
keepStars: true,
|
|
346
|
+
},
|
|
246
347
|
// Vite matches the literal block comment (`/\/\*\s*@vite-ignore\s*\*\//` in
|
|
247
348
|
// importAnalysis), so line comments, suffixes and trailing words stay
|
|
248
349
|
// ordinary.
|
|
@@ -250,40 +351,73 @@ const RULES: DirectiveRule[] = [
|
|
|
250
351
|
// Terser/Rollup annotations: a bare substring search over any comment kind,
|
|
251
352
|
// so leading or trailing words keep the annotation active.
|
|
252
353
|
{ pattern: /[#@]__(?:PURE|NO_SIDE_EFFECTS|INLINE|NOINLINE|KEY|MANGLE_PROP)__/, anyLine: true },
|
|
253
|
-
// Million.js: substring of the leading comments of a component
|
|
354
|
+
// Million.js: substring of the leading comments of a component; the skip
|
|
355
|
+
// annotations are compared with the whole trimmed comment value.
|
|
254
356
|
{ pattern: /million-ignore/, name: "million-ignore", anyLine: true },
|
|
255
357
|
{
|
|
256
358
|
pattern: /^@million (jsx-)?skip$/,
|
|
257
359
|
joinLines: true,
|
|
360
|
+
keepStars: true,
|
|
258
361
|
name: (match) => (match[1] === undefined ? "@million-skip" : "@million-jsx-skip"),
|
|
259
362
|
},
|
|
260
363
|
// Nx project graph: substring; the comment has to end on the line right
|
|
261
364
|
// above the import it hides.
|
|
262
365
|
{ pattern: /nx-ignore-next-line/, name: "nx-ignore-next-line", anyLine: true },
|
|
263
|
-
// UnoCSS: include/ignore
|
|
264
|
-
// markers must be their own comment.
|
|
265
|
-
{
|
|
366
|
+
// UnoCSS: include/ignore (and the language server's ide-include) are raw
|
|
367
|
+
// whole-file substring checks; the skip markers must be their own comment.
|
|
368
|
+
{
|
|
369
|
+
pattern: /@unocss-(include|ignore|ide-include)/,
|
|
370
|
+
name: (match) => `@unocss-${match[1]}`,
|
|
371
|
+
anyLine: true,
|
|
372
|
+
},
|
|
266
373
|
{ pattern: /^@unocss-skip-(start|end)\b/, name: (match) => `@unocss-skip-${match[1]}` },
|
|
267
374
|
// Next.js codemod markers: `next build` fails while an unresolved
|
|
268
375
|
// @next-codemod-error comment is attached to the call.
|
|
269
376
|
{ pattern: /@next-codemod-(error|ignore)/, name: (match) => `@next-codemod-${match[1]}`, anyLine: true },
|
|
270
|
-
// Bun: the runtime treats a file
|
|
271
|
-
//
|
|
377
|
+
// Bun: the runtime treats a file whose first line, or whose line right after
|
|
378
|
+
// a `#!` line, starts with the literal bytes `// @bun` (optionally followed
|
|
379
|
+
// by flags like `@bytecode`) as already transpiled.
|
|
272
380
|
{ pattern: /^\/\/ @bun/, rawText: true, lineOnly: true, name: "@bun" },
|
|
273
381
|
// React Fast Refresh / solid-refresh. react-refresh looks for the
|
|
274
382
|
// substring `@refresh reset` in any comment; solid-refresh requires its
|
|
275
|
-
// pragma to be the entire comment
|
|
276
|
-
{
|
|
383
|
+
// pragma to be the entire trimmed comment value.
|
|
384
|
+
{
|
|
385
|
+
pattern: /^@refresh (skip|reload|jsx-skip)$/,
|
|
386
|
+
joinLines: true,
|
|
387
|
+
keepStars: true,
|
|
388
|
+
name: (match) => `@refresh-${match[1]}`,
|
|
389
|
+
},
|
|
277
390
|
{ pattern: /@refresh reset/, name: "@refresh-reset", anyLine: true },
|
|
278
391
|
// Embedded-language tag comments: prettier formats the following template
|
|
279
|
-
// literal when the whole comment is `GraphQL` (graphql-tag-pluck
|
|
280
|
-
// case-insensitively) or `HTML` (prettier's exact ` HTML `
|
|
281
|
-
|
|
392
|
+
// literal when the whole comment is `GraphQL` (graphql-tag-pluck compares
|
|
393
|
+
// the trimmed value case-insensitively) or `HTML` (prettier's exact ` HTML `
|
|
394
|
+
// value).
|
|
395
|
+
{ pattern: /^graphql$/i, joinLines: true, keepStars: true, name: "GraphQL" },
|
|
282
396
|
{ pattern: /^\/\* HTML \*\/$/, rawText: true, blockOnly: true, name: "HTML" },
|
|
283
|
-
// Source maps (`//# sourceMappingURL=`, legacy `//@`, and the block form)
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
397
|
+
// Source maps (`//# sourceMappingURL=`, legacy `//@`, and the block form).
|
|
398
|
+
// deno_graph matches the name in any case and allows blanks around the `=`.
|
|
399
|
+
{
|
|
400
|
+
pattern: /^[#@]\s*(source(?:Mapping)?URL)\s*=/i,
|
|
401
|
+
name: (match) => ((match[1] as string).toLowerCase() === "sourceurl" ? "sourceURL" : "sourceMappingURL"),
|
|
402
|
+
},
|
|
403
|
+
// JSX pragmas. esbuild and Bun look at every `@` of every comment, Babel at
|
|
404
|
+
// every comment line (its TypeScript plugin anywhere in the text), and the
|
|
405
|
+
// oxc transformer at the leading comments — line comments included, all
|
|
406
|
+
// case-sensitive, all needing whitespace and an argument after the name.
|
|
407
|
+
// tsc lowercases the name of a block-comment pragma, and deno_graph matches
|
|
408
|
+
// `@jsxImportSource(Types)` block comments in any case, so block comments
|
|
409
|
+
// also match case-insensitively.
|
|
410
|
+
{
|
|
411
|
+
pattern: /@(jsx(?:Frag|Runtime|ImportSource)?)\s+\S/,
|
|
412
|
+
joinLines: true,
|
|
413
|
+
name: (match) => `@${match[1] as string}`,
|
|
414
|
+
},
|
|
415
|
+
{
|
|
416
|
+
pattern: /@(jsx(?:frag|runtime|importsource(?:types)?)?)\s+\S/i,
|
|
417
|
+
blockOnly: true,
|
|
418
|
+
joinLines: true,
|
|
419
|
+
name: (match) => JSX_PRAGMAS.get((match[1] as string).toLowerCase()),
|
|
420
|
+
},
|
|
287
421
|
// Test runners. Jest reads these from the file's leading docblock, but
|
|
288
422
|
// Vitest (which also honours the @jest- spellings) matches the pragma with
|
|
289
423
|
// a bare regex over the whole file, so any comment kind and position can be
|
|
@@ -298,24 +432,27 @@ const RULES: DirectiveRule[] = [
|
|
|
298
432
|
name: (match) => `@${match[1]}`,
|
|
299
433
|
anyLine: true,
|
|
300
434
|
},
|
|
301
|
-
// Vitest module tags
|
|
302
|
-
//
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
//
|
|
306
|
-
|
|
435
|
+
// Vitest module tags: /(\/\/|\*)\s*@module-tag\s+([\w\-/]+)\b/ over the
|
|
436
|
+
// whole file, so the tag has to follow a `//` or a `*` — any of the
|
|
437
|
+
// comment's — with only whitespace between.
|
|
438
|
+
{ pattern: /(?:\/\/|\*)\s*@module-tag\s+[\w/-]*\w/, rawText: true, name: "@module-tag" },
|
|
439
|
+
// SonarQube/SonarCloud (SonarJS): the comment value, minus ONE leading `*`,
|
|
440
|
+
// trimmed, has to START with NOSONAR, case-insensitively; any suffix keeps
|
|
441
|
+
// it active. A star-prefixed line inside a block comment does not count.
|
|
442
|
+
{ pattern: /^\/[/*]\*?\s*NOSONAR/i, rawText: true, name: "NOSONAR" },
|
|
307
443
|
// Semgrep: ` nosemgrep`/` nosem` (case-insensitive) on the finding line or
|
|
308
444
|
// standalone above it, with optional `:`/`=` rule ids. Semgrep itself needs
|
|
309
445
|
// no trailing boundary; the lookahead just keeps words like `nosemantic`
|
|
310
446
|
// ordinary.
|
|
311
447
|
{ pattern: /(?:^|\s)nosem(?:grep)?(?![\w-])/i, name: "nosemgrep", anyLine: true },
|
|
312
|
-
// CodeQL / LGTM alert suppressions: the AlertSuppression
|
|
313
|
-
//
|
|
314
|
-
// `lgtm[...]` matches anywhere in the
|
|
315
|
-
// start or after a `;`; `codeql[...]` requires
|
|
316
|
-
|
|
317
|
-
{ pattern:
|
|
318
|
-
{ pattern:
|
|
448
|
+
// CodeQL / LGTM alert suppressions: the JavaScript AlertSuppression query
|
|
449
|
+
// reads every comment whose text holds no line feed — `//` comments and
|
|
450
|
+
// single-line block comments alike. `lgtm[...]` matches anywhere in the
|
|
451
|
+
// text; bare `lgtm` only at its start or after a `;`; `codeql[...]` requires
|
|
452
|
+
// the bracketed query id.
|
|
453
|
+
{ pattern: /\blgtm\s*\[[^\]]*\]/i, rawText: true, singleLine: true, name: "lgtm" },
|
|
454
|
+
{ pattern: /(?:^\/[/*]|;)\s*lgtm(?!\w)(?!\s*\[)/i, rawText: true, singleLine: true, name: "lgtm" },
|
|
455
|
+
{ pattern: /\bcodeql\s*\[[^\]]*\]/i, rawText: true, singleLine: true, name: "codeql" },
|
|
319
456
|
// DeepSource: bare `skipcq` or `skipcq: CODE1, CODE2`, on the flagged line
|
|
320
457
|
// or standalone above it (it may trail other comment text).
|
|
321
458
|
{ pattern: /\bskipcq(?![\w-])/, name: "skipcq", anyLine: true },
|
|
@@ -345,17 +482,18 @@ const RULES: DirectiveRule[] = [
|
|
|
345
482
|
// cspell's legacy emacs-style word list (case-sensitive, colon optional).
|
|
346
483
|
{ pattern: /\bLocalWords(?::|\s+\S)/, name: "LocalWords", anyLine: true },
|
|
347
484
|
// codespell: the tag must follow a punctuation character (the comment
|
|
348
|
-
// marker itself at line starts) with
|
|
485
|
+
// marker itself at line starts), with any whitespace between.
|
|
349
486
|
{
|
|
350
|
-
pattern: /(?:^|[^\w\s]\s
|
|
487
|
+
pattern: /(?:^|[^\w\s]\s*)codespell:ignore(?:(-next-line)\b|(?![\w-]))/,
|
|
351
488
|
name: (match) => `codespell:ignore${match[1] ?? ""}`,
|
|
352
489
|
anyLine: true,
|
|
353
490
|
},
|
|
354
491
|
// JetBrains IDEs. `noinspection` suppressions are line comments in JS/TS
|
|
492
|
+
// (`//\s*noinspection\s+ids`, or `//file:noinspection` for the whole file)
|
|
355
493
|
// and take comma-separated inspection ids; editor-fold markers are
|
|
356
494
|
// substring checks; a `language=` comment injects a language into the
|
|
357
495
|
// literal that follows.
|
|
358
|
-
{ pattern:
|
|
496
|
+
{ pattern: /^\/\/(?:file:)?\s*noinspection\s+[\w.-]+/, rawText: true, lineOnly: true, name: "noinspection" },
|
|
359
497
|
{
|
|
360
498
|
pattern: /<(\/?)editor-fold/,
|
|
361
499
|
name: (match) => (match[1] === "/" ? "editor-fold-end" : "editor-fold"),
|
|
@@ -364,29 +502,45 @@ const RULES: DirectiveRule[] = [
|
|
|
364
502
|
{ pattern: /^language=\S/, name: "language-injection" },
|
|
365
503
|
// ReSharper/Rider suppressions (`ReSharper disable once RuleName`, ...).
|
|
366
504
|
{ pattern: /^ReSharper (disable|restore)\b/, lineOnly: true, name: (match) => `resharper-${match[1]}` },
|
|
367
|
-
// Editor folding
|
|
368
|
-
|
|
505
|
+
// Editor folding regions: VS Code folds `// #region` and `// region`
|
|
506
|
+
// comments (`^\s*//\s*#?region\b`), IntelliJ any comment that opens with
|
|
507
|
+
// marker characters, blanks and `region` (`[/*#-]*\s*region.*`).
|
|
508
|
+
{ pattern: /^[/*#-]*\s*#?(region|endregion)\b/, rawText: true, name: (match) => `#${match[1] as string}` },
|
|
509
|
+
// ESLint's no-fallthrough (in eslint:recommended) accepts a case that falls
|
|
510
|
+
// through when the last comment before the next case matches
|
|
511
|
+
// /falls?\s?through/iu anywhere in its value; oxlint's port and JSHint's
|
|
512
|
+
// `/* falls through */` accept narrower forms of the same words. ESLint's and
|
|
513
|
+
// oxlint's default-case accept a final comment that is exactly `no default`,
|
|
514
|
+
// in any case. Both only count in front of the code those rules read them
|
|
515
|
+
// before (see SWITCH_CLAUSE_TARGETS), so the same words elsewhere are prose.
|
|
516
|
+
{ pattern: /falls?\s?through/iu, rawText: true, name: "fallthrough" },
|
|
517
|
+
{ pattern: /^\/(?:\/\s*no default\s*|\*\s*no default\s*\*\/)$/iu, rawText: true, name: "no-default" },
|
|
369
518
|
];
|
|
370
519
|
|
|
371
|
-
//
|
|
520
|
+
// tsc 6 (the JavaScript compiler) reads triple-slash directives with
|
|
521
|
+
// tripleSlashXMLCommentStartRegEx: the directive name (matched
|
|
372
522
|
// case-insensitively) must be followed by whitespace and the tag has to close
|
|
373
|
-
// with
|
|
374
|
-
//
|
|
375
|
-
//
|
|
523
|
+
// with `/>`, and the AMD directives additionally need their mandatory quoted
|
|
524
|
+
// argument, or the pragma is dropped altogether. tsc 7 (the Go port) has no
|
|
525
|
+
// AMD directives left and reads `reference` as a name of letters and hyphens
|
|
526
|
+
// that needs neither: `/// <reference types="node">` is a live type reference
|
|
527
|
+
// to it, and a bare `/// <reference>` a directive it rejects (TS1084).
|
|
376
528
|
const TRIPLE_SLASH = /^\/\/\/\s*<(reference|amd-dependency|amd-module)\s.*?\/>/i;
|
|
377
529
|
const TRIPLE_SLASH_REQUIRED_ARGUMENT: Readonly<Record<string, RegExp>> = {
|
|
378
530
|
"amd-dependency": /\spath\s*=\s*(?:'[^']*'|"[^"]*")/i,
|
|
379
531
|
"amd-module": /\sname\s*=\s*(?:'[^']*'|"[^"]*")/i,
|
|
380
532
|
};
|
|
533
|
+
const TS7_TRIPLE_SLASH_REFERENCE = /^\/\/\/[ \t]*<reference(?![A-Za-z-])/i;
|
|
381
534
|
|
|
382
|
-
/** The canonical triple-slash directive name the line comment carries, if tsc would honour it. */
|
|
535
|
+
/** The canonical triple-slash directive name the line comment carries, if either tsc would honour it. */
|
|
383
536
|
function tripleSlashDirective(text: string): string | undefined {
|
|
384
537
|
const match = TRIPLE_SLASH.exec(text);
|
|
385
|
-
if (
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
538
|
+
if (match) {
|
|
539
|
+
const name = (match[1] as string).toLowerCase();
|
|
540
|
+
const requiredArgument = TRIPLE_SLASH_REQUIRED_ARGUMENT[name];
|
|
541
|
+
if (requiredArgument === undefined || requiredArgument.test(text)) return `triple-slash-${name}`;
|
|
542
|
+
}
|
|
543
|
+
return TS7_TRIPLE_SLASH_REFERENCE.test(text) ? "triple-slash-reference" : undefined;
|
|
390
544
|
}
|
|
391
545
|
|
|
392
546
|
/**
|
|
@@ -399,21 +553,43 @@ export interface DirectivePlacement {
|
|
|
399
553
|
header: boolean;
|
|
400
554
|
/** The comment is the file's first comment, preceded only by whitespace (or a shebang). */
|
|
401
555
|
firstComment: boolean;
|
|
402
|
-
/**
|
|
556
|
+
/**
|
|
557
|
+
* The comment starts where Bun looks for its `// @bun` marker: the very
|
|
558
|
+
* first character of the file, or the first character after the line break
|
|
559
|
+
* that ends a leading `#!` line (see bunMarkerOffset).
|
|
560
|
+
*/
|
|
403
561
|
fileStart: boolean;
|
|
562
|
+
/**
|
|
563
|
+
* The token that follows the comment past any further comments (`case`,
|
|
564
|
+
* `}`, an identifier, ...), or `""` when only comments follow. ESLint's
|
|
565
|
+
* switch-clause comments (`// falls through`, `// no default`) count only in
|
|
566
|
+
* front of certain tokens. Left out, any token is assumed.
|
|
567
|
+
*/
|
|
568
|
+
nextToken?: string;
|
|
404
569
|
}
|
|
405
570
|
|
|
406
571
|
const ANY_PLACEMENT: DirectivePlacement = { header: true, firstComment: true, fileStart: true };
|
|
407
572
|
|
|
573
|
+
/**
|
|
574
|
+
* The offset at which Bun reads its `// @bun` marker (`has_bun_pragma` in its
|
|
575
|
+
* parser): the start of the text, or, when the text opens with a `#!` line,
|
|
576
|
+
* the character after that line's first `\n` — -1 when the shebang line is
|
|
577
|
+
* the whole text, leaving no place for the marker.
|
|
578
|
+
*/
|
|
579
|
+
export function bunMarkerOffset(text: string): number {
|
|
580
|
+
if (!text.startsWith("#!")) return 0;
|
|
581
|
+
const lineFeed = text.indexOf("\n");
|
|
582
|
+
return lineFeed === -1 ? -1 : lineFeed + 1;
|
|
583
|
+
}
|
|
584
|
+
|
|
408
585
|
// File-wide pragmas (check pragmas, triple-slash directives, Flow/JSLint
|
|
409
|
-
// header pragmas,
|
|
410
|
-
//
|
|
411
|
-
//
|
|
412
|
-
//
|
|
413
|
-
//
|
|
414
|
-
//
|
|
415
|
-
//
|
|
416
|
-
// (ast-v8-to-istanbul) honour them on any comment in the file. Test
|
|
586
|
+
// header pragmas, Biome's top-level suppression, Deno's lint file pragma,
|
|
587
|
+
// dprint/deno fmt file pragmas and @ts-self-types) only count before the first
|
|
588
|
+
// token; anywhere later the tools treat them as ordinary text — or, for a
|
|
589
|
+
// stray dprint/deno fmt file pragma, as the node pragma it contains, which the
|
|
590
|
+
// next rule then reports. Coverage file pragmas (`istanbul ignore file`,
|
|
591
|
+
// `c8/v8 ignore file`) are NOT gated: istanbul-lib-instrument and Vitest's v8
|
|
592
|
+
// provider (ast-v8-to-istanbul) honour them on any comment in the file. Test
|
|
417
593
|
// environment pragmas are not gated either: Vitest matches them (including
|
|
418
594
|
// the @jest- spellings) with a regex over the whole file, and neither are
|
|
419
595
|
// @ts-strict pragmas (typescript-strict-plugin scans every line). @jsx
|
|
@@ -427,44 +603,75 @@ const HEADER_ONLY_DIRECTIVES = new Set([
|
|
|
427
603
|
"@ts-self-types",
|
|
428
604
|
"jslint",
|
|
429
605
|
"property",
|
|
606
|
+
"biome-ignore-all",
|
|
607
|
+
"deno-lint-ignore-file",
|
|
608
|
+
"dprint-ignore-file",
|
|
609
|
+
"deno-fmt-ignore-file",
|
|
430
610
|
]);
|
|
431
611
|
|
|
432
612
|
// Prettier's pragma mode reads the pragma through jest-docblock, which only
|
|
433
613
|
// ever extracts the file's FIRST comment (a shebang may precede it) — a
|
|
434
614
|
// docblock behind any other comment is ignored. Cross-checked against the
|
|
435
|
-
// real prettier in directives.prettier.test.ts.
|
|
436
|
-
|
|
615
|
+
// real prettier in directives.prettier.test.ts. Deno's coverage reads its
|
|
616
|
+
// file pragma from the first comment alone, too.
|
|
617
|
+
const FIRST_COMMENT_ONLY_DIRECTIVES = new Set([
|
|
618
|
+
"@format",
|
|
619
|
+
"@noformat",
|
|
620
|
+
"@prettier",
|
|
621
|
+
"@noprettier",
|
|
622
|
+
"deno-coverage-ignore-file",
|
|
623
|
+
]);
|
|
437
624
|
|
|
438
|
-
// Bun treats a file as pre-transpiled only when it
|
|
439
|
-
//
|
|
625
|
+
// Bun treats a file as pre-transpiled only when `// @bun` opens it or the
|
|
626
|
+
// line after its shebang; anywhere else the marker is inert.
|
|
440
627
|
const FILE_START_ONLY_DIRECTIVES = new Set(["@bun"]);
|
|
441
628
|
|
|
629
|
+
// ESLint's no-fallthrough reads the last comment before the next `case` or
|
|
630
|
+
// `default` (or before a case block's closing brace), and default-case the
|
|
631
|
+
// last one before the switch's closing brace; in front of anything else the
|
|
632
|
+
// same words are prose.
|
|
633
|
+
const SWITCH_CLAUSE_TARGETS: ReadonlyMap<string, ReadonlySet<string>> = new Map([
|
|
634
|
+
["fallthrough", new Set(["case", "default", "}"])],
|
|
635
|
+
["no-default", new Set(["}"])],
|
|
636
|
+
]);
|
|
637
|
+
|
|
442
638
|
function isActiveAt(name: string, placement: DirectivePlacement): boolean {
|
|
443
639
|
if (FILE_START_ONLY_DIRECTIVES.has(name)) return placement.fileStart;
|
|
444
640
|
if (FIRST_COMMENT_ONLY_DIRECTIVES.has(name)) return placement.firstComment;
|
|
445
|
-
if (
|
|
446
|
-
HEADER_ONLY_DIRECTIVES.has(name) ||
|
|
447
|
-
(name.startsWith("deno-") && name.endsWith("-ignore-file")) ||
|
|
448
|
-
name.startsWith("triple-slash-")
|
|
449
|
-
) {
|
|
641
|
+
if (HEADER_ONLY_DIRECTIVES.has(name) || name.startsWith("triple-slash-")) {
|
|
450
642
|
return placement.header;
|
|
451
643
|
}
|
|
644
|
+
const targets = SWITCH_CLAUSE_TARGETS.get(name);
|
|
645
|
+
if (targets !== undefined) return placement.nextToken === undefined || targets.has(placement.nextToken);
|
|
452
646
|
return true;
|
|
453
647
|
}
|
|
454
648
|
|
|
455
|
-
// Mirrors the TypeScript
|
|
456
|
-
// against tsc
|
|
457
|
-
//
|
|
458
|
-
//
|
|
459
|
-
// whitespace
|
|
460
|
-
//
|
|
461
|
-
const TS_LINE_SUPPRESSION =
|
|
462
|
-
|
|
649
|
+
// Mirrors the TypeScript compilers' own comment-directive matching, verified
|
|
650
|
+
// against tsc 6.0 and 7.0 (a comment counts when either honours it). The
|
|
651
|
+
// suppression directives are case-sensitive PREFIX matches (`@ts-ignoreTODO`
|
|
652
|
+
// is active). In `//` comments tsc 6 allows one extra slash and any
|
|
653
|
+
// whitespace before the `@`, tsc 7 any number of extra slashes but only
|
|
654
|
+
// spaces and tabs — so `//// @ts-ignore` is live to tsc 7 alone.
|
|
655
|
+
const TS_LINE_SUPPRESSION = /^\/\/(?:\/?\s*|\/*[ \t]*)@ts-(ignore|expect-error)/;
|
|
656
|
+
// The file-wide check pragmas go through each compiler's case-insensitive
|
|
657
|
+
// pragma parser. tsc 6 reads the name up to the next whitespace or colon
|
|
658
|
+
// (`@ts-nocheck:reason` is active, `@ts-nocheckfoo` and `@ts-nocheck-foo` are
|
|
659
|
+
// not); tsc 7 reads it as a run of ASCII letters and hyphens after spaces and
|
|
660
|
+
// tabs only, so `@ts-nocheck.` and `@ts-nocheck1` are active to it.
|
|
661
|
+
const TS6_LINE_CHECK_PRAGMA = /^\/\/\/?\s*@ts-(nocheck|check)(?=[^\S\r\n]|:|$)/i;
|
|
662
|
+
const TS7_LINE_CHECK_PRAGMA = /^\/\/\/?[ \t]*@ts-(nocheck|check)(?![A-Za-z-])/i;
|
|
463
663
|
// Block comments: only the suppression directives, and only on the comment's
|
|
464
664
|
// literal last line (`*/` included), even when that line is otherwise blank.
|
|
465
665
|
// tsc trims the line and then allows one run of `/`/`*` characters before the
|
|
466
666
|
// directive, so `* @ts-ignore */` counts but `/ * @ts-ignore */` does not.
|
|
467
667
|
const TS_BLOCK_SUPPRESSION = /^\s*[/*]*\s*@ts-(ignore|expect-error)/;
|
|
668
|
+
// Flow, which also checks TypeScript files, honours the same suppressions
|
|
669
|
+
// there with its own matching: the directive must OPEN the comment value,
|
|
670
|
+
// after any spaces, tabs, line breaks and stars — in a block comment
|
|
671
|
+
// whatever its last line holds — and end where an identifier would
|
|
672
|
+
// (`@ts-ignore: reason` counts, `@ts-ignoree` does not).
|
|
673
|
+
const FLOW_LINE_SUPPRESSION = /^\/\/[ \t*]*@ts-(ignore|expect-error)(?![\w-])/;
|
|
674
|
+
const FLOW_BLOCK_SUPPRESSION = /^\/\*[ \t\r\n*]*@ts-(ignore|expect-error)(?![\w-])/;
|
|
468
675
|
|
|
469
676
|
/**
|
|
470
677
|
* Returns the canonical name of the compiler/linter/tooling directive the
|
|
@@ -481,35 +688,52 @@ export function detectDirective(kind: CommentKind, text: string, placement?: Dir
|
|
|
481
688
|
if (kind === "line") {
|
|
482
689
|
const tripleSlash = tripleSlashDirective(text);
|
|
483
690
|
if (tripleSlash !== undefined && isActiveAt(tripleSlash, at)) return tripleSlash;
|
|
484
|
-
const suppression = TS_LINE_SUPPRESSION.exec(text);
|
|
691
|
+
const suppression = TS_LINE_SUPPRESSION.exec(text) ?? FLOW_LINE_SUPPRESSION.exec(text);
|
|
485
692
|
if (suppression) {
|
|
486
693
|
return `@ts-${suppression[1]}`;
|
|
487
694
|
}
|
|
488
|
-
const checkPragma =
|
|
695
|
+
const checkPragma = TS6_LINE_CHECK_PRAGMA.exec(text) ?? TS7_LINE_CHECK_PRAGMA.exec(text);
|
|
489
696
|
if (checkPragma) {
|
|
490
697
|
const name = `@ts-${checkPragma[1]?.toLowerCase()}`;
|
|
491
698
|
if (isActiveAt(name, at)) return name;
|
|
492
699
|
}
|
|
493
700
|
} else {
|
|
494
|
-
const suppression = TS_BLOCK_SUPPRESSION.exec(lastLine(text));
|
|
701
|
+
const suppression = TS_BLOCK_SUPPRESSION.exec(lastLine(text)) ?? FLOW_BLOCK_SUPPRESSION.exec(text);
|
|
495
702
|
if (suppression) {
|
|
496
703
|
return `@ts-${suppression[1]}`;
|
|
497
704
|
}
|
|
498
705
|
}
|
|
499
706
|
|
|
500
|
-
|
|
501
|
-
|
|
707
|
+
// A directive that opens the comment names it, however the comment goes on
|
|
708
|
+
// to mention other tools' markers (`// nosemgrep: ... @jsx h` stays a
|
|
709
|
+
// Semgrep suppression, with the next-line shield that goes with it); only
|
|
710
|
+
// without one does a marker found after other text name the comment.
|
|
711
|
+
let mention: string | undefined;
|
|
712
|
+
for (const { name, opening } of ruleMatches(kind, text)) {
|
|
713
|
+
if (!isActiveAt(name, at)) continue;
|
|
714
|
+
if (opening) return name;
|
|
715
|
+
mention ??= name;
|
|
502
716
|
}
|
|
503
|
-
return
|
|
717
|
+
return mention;
|
|
504
718
|
}
|
|
505
719
|
|
|
506
720
|
/** Directive names of every rule the comment matches, in rule order. */
|
|
507
|
-
|
|
721
|
+
/** A rule's directive name, and whether its match opens the comment rather than following other text. */
|
|
722
|
+
interface RuleMatch {
|
|
723
|
+
name: string;
|
|
724
|
+
opening: boolean;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
function* ruleMatches(kind: CommentKind, text: string): Generator<RuleMatch> {
|
|
508
728
|
const lines = contentLines(kind, text, false);
|
|
509
729
|
const literalLines = kind === "block" ? contentLines(kind, text, true) : lines;
|
|
730
|
+
// Where the raw text's content begins, past the comment opener, stars and
|
|
731
|
+
// blanks.
|
|
732
|
+
const contentStart = (/^[/*\s]*/.exec(text) as RegExpExecArray)[0].length;
|
|
510
733
|
for (const rule of RULES) {
|
|
511
734
|
if (rule.blockOnly === true && kind !== "block") continue;
|
|
512
735
|
if (rule.lineOnly === true && kind !== "line") continue;
|
|
736
|
+
if (rule.singleLine === true && text.includes("\n")) continue;
|
|
513
737
|
if (rule.notDocblock === true && kind === "block" && text.startsWith("/**")) continue;
|
|
514
738
|
const ruleLines = rule.keepStars === true ? literalLines : lines;
|
|
515
739
|
const candidates =
|
|
@@ -520,31 +744,31 @@ function* ruleMatches(kind: CommentKind, text: string): Generator<string> {
|
|
|
520
744
|
: rule.joinLines === true
|
|
521
745
|
? [ruleLines.join(" ")]
|
|
522
746
|
: ruleLines.slice(0, 1);
|
|
523
|
-
for (const line of candidates) {
|
|
747
|
+
for (const [index, line] of candidates.entries()) {
|
|
524
748
|
const match = rule.pattern.exec(line);
|
|
525
749
|
if (!match) continue;
|
|
750
|
+
const opening = rule.rawText === true ? match.index <= contentStart : index === 0 && match.index === 0;
|
|
526
751
|
const name =
|
|
527
752
|
typeof rule.name === "string" ? rule.name : typeof rule.name === "function" ? rule.name(match) : match[0];
|
|
528
753
|
if (name === undefined) continue;
|
|
529
|
-
|
|
530
|
-
else yield* name;
|
|
754
|
+
for (const each of typeof name === "string" ? [name] : name) yield { name: each, opening };
|
|
531
755
|
}
|
|
532
756
|
}
|
|
533
757
|
}
|
|
534
758
|
|
|
535
759
|
/**
|
|
536
|
-
* The position-dependent directives (prettier's first-comment pragmas,
|
|
537
|
-
* file-start marker) the comment carries that are
|
|
538
|
-
* placement, sorted. Unlike detectDirective — which
|
|
539
|
-
* name — this sees through masking: an eslint config
|
|
540
|
-
* `@format` pragma, and whether that pragma is live
|
|
541
|
-
* name an earlier rule already claimed. Only
|
|
542
|
-
* gates are consulted; header-gated directives
|
|
543
|
-
* the comments around them change.
|
|
760
|
+
* The position-dependent directives (prettier's first-comment pragmas, Deno's
|
|
761
|
+
* coverage file pragma, Bun's file-start marker) the comment carries that are
|
|
762
|
+
* active at the given placement, sorted. Unlike detectDirective — which
|
|
763
|
+
* reports one canonical name — this sees through masking: an eslint config
|
|
764
|
+
* block can also hold a `@format` pragma, and whether that pragma is live
|
|
765
|
+
* must not hide behind the name an earlier rule already claimed. Only
|
|
766
|
+
* first-comment and file-start gates are consulted; header-gated directives
|
|
767
|
+
* cannot change activation when the comments around them change.
|
|
544
768
|
*/
|
|
545
769
|
export function activePositionalDirectives(kind: CommentKind, text: string, placement: DirectivePlacement): string[] {
|
|
546
770
|
const names = new Set<string>();
|
|
547
|
-
for (const name of ruleMatches(kind, text)) {
|
|
771
|
+
for (const { name } of ruleMatches(kind, text)) {
|
|
548
772
|
if (!FIRST_COMMENT_ONLY_DIRECTIVES.has(name) && !FILE_START_ONLY_DIRECTIVES.has(name)) continue;
|
|
549
773
|
if (isActiveAt(name, placement)) names.add(name);
|
|
550
774
|
}
|
|
@@ -566,8 +790,8 @@ export function isLegalComment(text: string): boolean {
|
|
|
566
790
|
/**
|
|
567
791
|
* Non-empty content lines of the comment, with comment markers stripped.
|
|
568
792
|
* For line comments only the `//` marker itself is removed: extra slashes or
|
|
569
|
-
* stars are content, so
|
|
570
|
-
* ordinary. JSDoc-style `*` prefixes are stripped for block comments unless
|
|
793
|
+
* stars are content, so `/// eslint-disable-line` and `// * prettier-ignore`
|
|
794
|
+
* stay ordinary. JSDoc-style `*` prefixes are stripped for block comments unless
|
|
571
795
|
* `keepStars` asks for the literal lines (for tools that compare the raw
|
|
572
796
|
* comment value, where a leading `*` defeats the directive).
|
|
573
797
|
*/
|