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