-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix up JSDoc visitor and try harder to find import types
- Loading branch information
Showing
4 changed files
with
49 additions
and
11 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
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
47 changes: 41 additions & 6 deletions
47
packages/knip/src/typescript/visitors/imports/jsDocType.ts
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 |
---|---|---|
@@ -1,16 +1,51 @@ | ||
import ts from 'typescript'; | ||
import { isValidImportTypeNode } from '../../ast-helpers.js'; | ||
import { importVisitor as visit } from '../index.js'; | ||
|
||
const extractImportSpecifiers = (node: ts.JSDocTag) => { | ||
const importSpecifiers: string[] = []; | ||
|
||
function visit(node: ts.Node) { | ||
if (ts.isJSDocTypeExpression(node)) { | ||
const typeNode = node.type; | ||
if (ts.isTypeReferenceNode(typeNode) && typeNode.typeArguments) { | ||
typeNode.typeArguments.forEach(arg => { | ||
if (ts.isImportTypeNode(arg)) { | ||
const importClause = arg.argument; | ||
if (ts.isLiteralTypeNode(importClause) && ts.isStringLiteral(importClause.literal)) { | ||
importSpecifiers.push(importClause.literal.text); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
if (ts.isJSDocTypeTag(node)) { | ||
const typeNode = node.typeExpression?.type; | ||
if (ts.isImportTypeNode(typeNode)) { | ||
const importClause = typeNode.argument; | ||
if (ts.isLiteralTypeNode(importClause) && ts.isStringLiteral(importClause.literal)) { | ||
importSpecifiers.push(importClause.literal.text); | ||
} | ||
} | ||
} | ||
ts.forEachChild(node, visit); | ||
} | ||
|
||
visit(node); | ||
|
||
return importSpecifiers; | ||
}; | ||
|
||
export default visit( | ||
() => true, | ||
node => { | ||
if ('jsDoc' in node) { | ||
const type = ts.getJSDocType(node); | ||
if (type && isValidImportTypeNode(type)) { | ||
// TODO Odd to assume this is an `import()` call? | ||
return { specifier: type.argument.literal.text }; | ||
if ('jsDoc' in node && node.jsDoc) { | ||
const jsDoc = node.jsDoc as ts.JSDoc[]; | ||
if (jsDoc.length > 0 && jsDoc[0].parent.parent === node.parent) { | ||
return jsDoc | ||
.flatMap(jsDoc => (jsDoc.tags ?? []).flatMap(extractImportSpecifiers)) | ||
.map(specifier => ({ specifier })); | ||
} | ||
} | ||
return []; | ||
} | ||
); |
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