Skip to content

Commit

Permalink
feat(check-values): checks for valid @import syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Jun 24, 2024
1 parent 54ac4fd commit ad31b14
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions .README/rules/check-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This rule checks the values for a handful of tags:
6. `@kind` - Insists that it be one of the allowed values: 'class',
'constant', 'event', 'external', 'file', 'function', 'member', 'mixin',
'module', 'namespace', 'typedef',
7. `@import` - For TypeScript `mode` only. Enforces valid ES import syntax.

## Options

Expand Down
25 changes: 25 additions & 0 deletions docs/rules/check-values.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This rule checks the values for a handful of tags:
6. `@kind` - Insists that it be one of the allowed values: 'class',
'constant', 'event', 'external', 'file', 'function', 'member', 'mixin',
'module', 'namespace', 'typedef',
7. `@import` - For TypeScript `mode` only. Enforces valid ES import syntax.

<a name="user-content-check-values-options"></a>
<a name="check-values-options"></a>
Expand Down Expand Up @@ -251,6 +252,14 @@ function quux (foo) {
}
// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)\nCopyright"}]
// Message: Invalid JSDoc @license: "Oops"; expected SPDX expression: https://spdx.org/licenses/.
/**
* @import BadImportIgnoredByThisRule
*/
/**
* @import {AnotherBadImportIgnoredByThisRule} from
*/
// Message: Bad @import tag
````
Expand Down Expand Up @@ -405,5 +414,21 @@ function quux (foo) {
}
// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)\nCopyright"}]
/**
* @import LinterDef, { Sth as Something, Another as Another2 } from "eslint"
*/
/**
* @import { Linter } from "eslint"
*/
/**
* @import LinterDefault from "eslint"
*/
/**
* @import {Linter as Lintee} from "eslint"
*/
/**
* @import * as Linters from "eslint"
*/
````
28 changes: 27 additions & 1 deletion src/rules/checkValues.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import iterateJsdoc from '../iterateJsdoc.js';
import { parseImportsSync } from 'parse-imports';
import semver from 'semver';
import spdxExpressionParse from 'spdx-expression-parse';
import iterateJsdoc from '../iterateJsdoc.js';

const allowedKinds = new Set([
'class',
Expand All @@ -20,6 +21,7 @@ export default iterateJsdoc(({
utils,
report,
context,
settings,
}) => {
const options = context.options[0] || {};
const {
Expand Down Expand Up @@ -157,6 +159,30 @@ export default iterateJsdoc(({
}
});

if (settings.mode === 'typescript') {
utils.forEachPreferredTag('import', (tag) => {
const {
type, name, description
} = tag;
const typePart = type ? `{${type}} `: '';
const imprt = 'import ' + (description
? `${typePart}${name} ${description}`
: `${typePart}${name}`);

try {
// Should technically await non-sync, but ESLint doesn't support async rules;
// thankfully, the Wasm load time is safely fast
parseImportsSync(imprt);
} catch (err) {
report(
`Bad @import tag`,
null,
tag,
);
}
});
}

utils.forEachPreferredTag('author', (jsdocParameter, targetTagName) => {
const author = /** @type {string} */ (
utils.getTagDescription(jsdocParameter)
Expand Down
39 changes: 39 additions & 0 deletions test/rules/assertions/checkValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ export default {
},
],
},
{
code: `
/**
* @import BadImportIgnoredByThisRule
*/
/**
* @import {AnotherBadImportIgnoredByThisRule} from
*/
`,
errors: [
{
line: 3,
message: 'Bad @import tag',
},
{
line: 6,
message: 'Bad @import tag',
},
],
},
],
valid: [
{
Expand Down Expand Up @@ -592,5 +612,24 @@ export default {
},
],
},
{
code: `
/**
* @import LinterDef, { Sth as Something, Another as Another2 } from "eslint"
*/
/**
* @import { Linter } from "eslint"
*/
/**
* @import LinterDefault from "eslint"
*/
/**
* @import {Linter as Lintee} from "eslint"
*/
/**
* @import * as Linters from "eslint"
*/
`,
},
],
};

0 comments on commit ad31b14

Please sign in to comment.