Skip to content

Commit

Permalink
more nits
Browse files Browse the repository at this point in the history
  • Loading branch information
sverweij committed Dec 1, 2024
1 parent 6c98818 commit 41d2e71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/extract/tsc/extract-typescript-deps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function isJSDocImport(pTypeNode) {
);
}

function keyIsBoring(pKey) {
function keyInJSDocIsIgnorable(pKey) {
return [
"parent",
"pos",
Expand All @@ -303,27 +303,23 @@ function keyIsBoring(pKey) {
].includes(pKey);
}

/**
* Walks the given object, that can have both arrays and objects as values, and returns a new object with the same structure, but with all the values replaced by the result of the given function.
* @param {Object} obj The object to walk.
*/
export function walkJSDoc(pObject, pCollection = new Set()) {
if (isJSDocImport(pObject)) {
pCollection.add(pObject.argument.literal.text);
} else if (Array.isArray(pObject)) {
pObject.forEach((pValue) => walkJSDoc(pValue, pCollection));
} else if (typeof pObject === "object") {
for (const lKey in pObject) {
if (!keyIsBoring(lKey) && pObject[lKey]) {
if (!keyInJSDocIsIgnorable(lKey) && pObject[lKey]) {
walkJSDoc(pObject[lKey], pCollection);
}
}
}
}

export function getJSDocImports(pObject) {
export function getJSDocImports(pTagNode) {
const lCollection = new Set();
walkJSDoc(pObject, lCollection);
walkJSDoc(pTagNode, lCollection);
return Array.from(lCollection);
}

Expand Down
24 changes: 23 additions & 1 deletion test/extract/tsc/jsdoc-bracket-imports.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ describe("[U] ast-extractors/extract-typescript - jsdoc 'bracket' imports", () =
);
});

/* eslint mocha/no-skipped-tests: "off" */
it("extracts @type whole module even when wrapped in type shenanigans (Partial)", () => {
deepEqual(
extractTypescript(
Expand Down Expand Up @@ -222,6 +221,29 @@ describe("[U] ast-extractors/extract-typescript - jsdoc 'bracket' imports", () =
],
);
});
it("extracts @return wrapped in type shenanigans)", () => {
deepEqual(
extractTypescript(
"/** @return {Promise<import('./types.js').IPartialInitConfig>} */",
[],
true,
),
[
{
module: "./types.js",
moduleSystem: "es6",
dynamic: false,
exoticallyRequired: false,
dependencyTypes: [
"type-only",
"import",
"jsdoc",
"jsdoc-bracket-import",
],
},
],
);
});

it("leaves @type things alone that are not imports (but that look a bit like them)", () => {
deepEqual(
Expand Down

0 comments on commit 41d2e71

Please sign in to comment.