-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(
imports-as-dependencies
): add new rule to detect missing depen…
…dencies for import statements; fixes #896
- Loading branch information
Showing
9 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### `imports-as-dependencies` | ||
|
||
This rule will report an issue if JSDoc `import()` statements point to a package | ||
which is not listed in `dependencies` or `devDependencies`. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|``| | ||
|Recommended|false| | ||
|Settings|| | ||
|Options|| | ||
|
||
<!-- assertions importsAsDependencies --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<a name="user-content-imports-as-dependencies"></a> | ||
<a name="imports-as-dependencies"></a> | ||
### <code>imports-as-dependencies</code> | ||
|
||
This rule will report an issue if JSDoc `import()` statements point to a package | ||
which is not listed in `dependencies` or `devDependencies`. | ||
|
||
||| | ||
|---|---| | ||
|Context|everywhere| | ||
|Tags|``| | ||
|Recommended|false| | ||
|Settings|| | ||
|Options|| | ||
|
||
<!-- assertions importsAsDependencies --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import iterateJsdoc from '../iterateJsdoc'; | ||
import { | ||
parse, | ||
traverse, | ||
tryParse, | ||
} from '@es-joy/jsdoccomment'; | ||
import { | ||
readFileSync, | ||
} from 'fs'; | ||
import { | ||
join, | ||
} from 'path'; | ||
|
||
/** | ||
* @type {Set<string>} | ||
*/ | ||
let deps; | ||
try { | ||
const pkg = JSON.parse( | ||
// @ts-expect-error It's ok | ||
readFileSync(join(process.cwd(), './package.json')), | ||
); | ||
deps = new Set([ | ||
...(pkg.dependencies ? | ||
Object.keys(pkg.dependencies) : | ||
// istanbul ignore next | ||
[]), | ||
...(pkg.devDependencies ? | ||
Object.keys(pkg.devDependencies) : | ||
// istanbul ignore next | ||
[]), | ||
]); | ||
} catch (error) { | ||
/* eslint-disable no-console -- Inform user */ | ||
// istanbul ignore next | ||
console.log(error); | ||
/* eslint-enable no-console -- Inform user */ | ||
} | ||
|
||
export default iterateJsdoc(({ | ||
jsdoc, | ||
settings, | ||
utils, | ||
}) => { | ||
// istanbul ignore if | ||
if (!deps) { | ||
return; | ||
} | ||
|
||
const { | ||
mode, | ||
} = settings; | ||
|
||
for (const tag of jsdoc.tags) { | ||
let typeAst; | ||
try { | ||
typeAst = mode === 'permissive' ? tryParse(tag.type) : parse(tag.type, mode); | ||
} catch { | ||
continue; | ||
} | ||
|
||
traverse(typeAst, (nde) => { | ||
if (nde.type === 'JsdocTypeImport' && !deps.has(nde.element.value.replace( | ||
/^(@[^/]+\/[^/]+|[^/]+).*$/u, '$1', | ||
))) { | ||
utils.reportJSDoc( | ||
'import points to package which is not found in dependencies', | ||
tag, | ||
); | ||
} | ||
}); | ||
} | ||
}, { | ||
iterateAllJsdocs: true, | ||
meta: { | ||
docs: { | ||
description: 'Reports if JSDoc `import()` statements point to a package which is not listed in `dependencies` or `devDependencies`', | ||
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/imports-as-dependencies.md#repos-sticky-header', | ||
}, | ||
type: 'suggestion', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('sth').SomeApi} | ||
*/ | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
message: 'import points to package which is not found in dependencies', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('sth').SomeApi} | ||
*/ | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
message: 'import points to package which is not found in dependencies', | ||
}, | ||
], | ||
settings: { | ||
jsdoc: { | ||
mode: 'permissive', | ||
}, | ||
}, | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('missingpackage/subpackage').SomeApi} | ||
*/ | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
message: 'import points to package which is not found in dependencies', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('@sth/pkg').SomeApi} | ||
*/ | ||
`, | ||
errors: [ | ||
{ | ||
line: 3, | ||
message: 'import points to package which is not found in dependencies', | ||
}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('eslint').ESLint} | ||
*/ | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('eslint/use-at-your-own-risk').ESLint} | ||
*/ | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import('@es-joy/jsdoccomment').InlineTag} | ||
*/ | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
/** | ||
* @type {null|import(} | ||
*/ | ||
`, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters