Skip to content

Commit ad31b14

Browse files
committed
feat(check-values): checks for valid @import syntax
1 parent 54ac4fd commit ad31b14

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

.README/rules/check-values.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This rule checks the values for a handful of tags:
1818
6. `@kind` - Insists that it be one of the allowed values: 'class',
1919
'constant', 'event', 'external', 'file', 'function', 'member', 'mixin',
2020
'module', 'namespace', 'typedef',
21+
7. `@import` - For TypeScript `mode` only. Enforces valid ES import syntax.
2122

2223
## Options
2324

docs/rules/check-values.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This rule checks the values for a handful of tags:
2828
6. `@kind` - Insists that it be one of the allowed values: 'class',
2929
'constant', 'event', 'external', 'file', 'function', 'member', 'mixin',
3030
'module', 'namespace', 'typedef',
31+
7. `@import` - For TypeScript `mode` only. Enforces valid ES import syntax.
3132

3233
<a name="user-content-check-values-options"></a>
3334
<a name="check-values-options"></a>
@@ -251,6 +252,14 @@ function quux (foo) {
251252
}
252253
// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)\nCopyright"}]
253254
// Message: Invalid JSDoc @license: "Oops"; expected SPDX expression: https://spdx.org/licenses/.
255+
256+
/**
257+
* @import BadImportIgnoredByThisRule
258+
*/
259+
/**
260+
* @import {AnotherBadImportIgnoredByThisRule} from
261+
*/
262+
// Message: Bad @import tag
254263
````
255264
256265
@@ -405,5 +414,21 @@ function quux (foo) {
405414
406415
}
407416
// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)\nCopyright"}]
417+
418+
/**
419+
* @import LinterDef, { Sth as Something, Another as Another2 } from "eslint"
420+
*/
421+
/**
422+
* @import { Linter } from "eslint"
423+
*/
424+
/**
425+
* @import LinterDefault from "eslint"
426+
*/
427+
/**
428+
* @import {Linter as Lintee} from "eslint"
429+
*/
430+
/**
431+
* @import * as Linters from "eslint"
432+
*/
408433
````
409434

src/rules/checkValues.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import iterateJsdoc from '../iterateJsdoc.js';
1+
import { parseImportsSync } from 'parse-imports';
22
import semver from 'semver';
33
import spdxExpressionParse from 'spdx-expression-parse';
4+
import iterateJsdoc from '../iterateJsdoc.js';
45

56
const allowedKinds = new Set([
67
'class',
@@ -20,6 +21,7 @@ export default iterateJsdoc(({
2021
utils,
2122
report,
2223
context,
24+
settings,
2325
}) => {
2426
const options = context.options[0] || {};
2527
const {
@@ -157,6 +159,30 @@ export default iterateJsdoc(({
157159
}
158160
});
159161

162+
if (settings.mode === 'typescript') {
163+
utils.forEachPreferredTag('import', (tag) => {
164+
const {
165+
type, name, description
166+
} = tag;
167+
const typePart = type ? `{${type}} `: '';
168+
const imprt = 'import ' + (description
169+
? `${typePart}${name} ${description}`
170+
: `${typePart}${name}`);
171+
172+
try {
173+
// Should technically await non-sync, but ESLint doesn't support async rules;
174+
// thankfully, the Wasm load time is safely fast
175+
parseImportsSync(imprt);
176+
} catch (err) {
177+
report(
178+
`Bad @import tag`,
179+
null,
180+
tag,
181+
);
182+
}
183+
});
184+
}
185+
160186
utils.forEachPreferredTag('author', (jsdocParameter, targetTagName) => {
161187
const author = /** @type {string} */ (
162188
utils.getTagDescription(jsdocParameter)

test/rules/assertions/checkValues.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ export default {
353353
},
354354
],
355355
},
356+
{
357+
code: `
358+
/**
359+
* @import BadImportIgnoredByThisRule
360+
*/
361+
/**
362+
* @import {AnotherBadImportIgnoredByThisRule} from
363+
*/
364+
`,
365+
errors: [
366+
{
367+
line: 3,
368+
message: 'Bad @import tag',
369+
},
370+
{
371+
line: 6,
372+
message: 'Bad @import tag',
373+
},
374+
],
375+
},
356376
],
357377
valid: [
358378
{
@@ -592,5 +612,24 @@ export default {
592612
},
593613
],
594614
},
615+
{
616+
code: `
617+
/**
618+
* @import LinterDef, { Sth as Something, Another as Another2 } from "eslint"
619+
*/
620+
/**
621+
* @import { Linter } from "eslint"
622+
*/
623+
/**
624+
* @import LinterDefault from "eslint"
625+
*/
626+
/**
627+
* @import {Linter as Lintee} from "eslint"
628+
*/
629+
/**
630+
* @import * as Linters from "eslint"
631+
*/
632+
`,
633+
},
595634
],
596635
};

0 commit comments

Comments
 (0)