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 #1107
  • Loading branch information
brettz9 committed Jun 3, 2023
1 parent d7ec6e0 commit 785fb26
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/rules/importsAsDependencies.js
Expand Up @@ -37,6 +37,8 @@ try {
/* eslint-enable no-console -- Inform user */
}

const moduleCheck = new Map();

export default iterateJsdoc(({
jsdoc,
settings,
Expand All @@ -60,13 +62,34 @@ 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',
);
if (!moduleCheck.has(mod)) {
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}`;
}

moduleCheck.set(mod, !deps.has(mod));
}

if (moduleCheck.get(mod)) {
utils.reportJSDoc(
'import points to package which is not found in dependencies',
tag,
);
}
}
});
}
Expand Down
15 changes: 15 additions & 0 deletions test/rules/assertions/importsAsDependencies.js
Expand Up @@ -87,5 +87,20 @@ export default {
*/
`,
},
{
code: `
/**
* @type {null|import('esquery').ESQueryOptions}
*/
`,
},
{
code: `
/**
* @type {null|import('@es-joy/jsdoccomment').InlineTag|
* import('@es-joy/jsdoccomment').JsdocBlock}
*/
`,
},
],
};

0 comments on commit 785fb26

Please sign in to comment.