Skip to content

Commit

Permalink
fix(imports-as-dependencies): check for types in package.json a…
Browse files Browse the repository at this point in the history
…nd if not present, check `@types`; fixes gajus#1107
  • Loading branch information
brettz9 committed Jun 3, 2023
1 parent d7ec6e0 commit 3621a61
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/rules/importsAsDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,30 @@ export default iterateJsdoc(({
}

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,
if (nde.type === 'JsdocTypeImport') {
let mod = nde.element.value.replace(
/^(@[^/]+\/[^/]+|[^/]+).*$/u, '$1',
);
let pkg;
try {
pkg = JSON.parse(
// @ts-expect-error It's ok
readFileSync(join(process.cwd(), 'node_modules', mod, './package.json')),
);
} catch {
// Ignore
}

if (!pkg || !pkg.types) {
mod = `@types/${mod}`;
}

if (!deps.has(mod)) {
utils.reportJSDoc(
'import points to package which is not found in dependencies',
tag,
);
}
}
});
}
Expand Down
7 changes: 7 additions & 0 deletions test/rules/assertions/importsAsDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,12 @@ export default {
*/
`,
},
{
code: `
/**
* @type {null|import('esquery').ESQueryOptions}
*/
`,
},
],
};

0 comments on commit 3621a61

Please sign in to comment.