Skip to content

Commit

Permalink
fix(imports-as-dependencies): do not log missing package.json when …
Browse files Browse the repository at this point in the history
…rule is not active; fixes #1117
  • Loading branch information
brettz9 committed Jun 7, 2023
1 parent ab00592 commit 3a5dd7d
Showing 1 changed file with 39 additions and 23 deletions.
62 changes: 39 additions & 23 deletions src/rules/importsAsDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@ import {
} from 'path';

/**
* @type {Set<string>}
* @type {Set<string>|null}
*/
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 */
}

const setDeps = function () {
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) {
// istanbul ignore next -- our package.json exists
deps = null;
/* eslint-disable no-console -- Inform user */
// istanbul ignore next -- our package.json exists
console.log(error);
/* eslint-enable no-console -- Inform user */
}
};

const moduleCheck = new Map();

Expand All @@ -46,7 +51,12 @@ export default iterateJsdoc(({
utils,
}) => {
// istanbul ignore if
if (!deps) {
if (deps === undefined) {
setDeps();
}

// istanbul ignore if -- our package.json exists
if (deps === null) {
return;
}

Expand All @@ -62,7 +72,13 @@ export default iterateJsdoc(({
continue;
}

// eslint-disable-next-line no-loop-func -- Safe
traverse(typeAst, (nde) => {
// istanbul ignore if -- TS guard
if (deps === null) {
return;
}

if (nde.type === 'JsdocTypeImport') {
let mod = nde.element.value.replace(
/^(@[^/]+\/[^/]+|[^/]+).*$/u, '$1',
Expand Down

0 comments on commit 3a5dd7d

Please sign in to comment.