UNPKG

knip

Version:

Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects

46 lines (45 loc) 1.72 kB
import { IMPORT_FLAGS } from '../../constants.js'; export const collectTypeScriptPragmaImports = (sourceFile) => { if (!sourceFile.pragmas || sourceFile.pragmas.size === 0) return []; const importNodes = []; const modifiers = IMPORT_FLAGS.TYPE_ONLY; const jsxImportSourcePragmas = sourceFile.pragmas.get('jsximportsource'); if (jsxImportSourcePragmas) { const jsxImportSourcePragma = Array.isArray(jsxImportSourcePragmas) ? jsxImportSourcePragmas[jsxImportSourcePragmas.length - 1] : jsxImportSourcePragmas; const { factory: specifier } = jsxImportSourcePragma?.arguments ?? {}; const pos = jsxImportSourcePragma.range?.pos ?? 0; if (specifier) importNodes.push({ specifier, identifier: undefined, pos, modifiers, alias: undefined, namespace: undefined, symbol: undefined, }); } const referencePragma = sourceFile.pragmas.get('reference'); if (referencePragma) { const refs = [referencePragma].flat(); for (const ref of refs) { if (ref.arguments?.types) { const { value: specifier, pos } = ref.arguments.types; if (specifier) importNodes.push({ specifier, identifier: undefined, pos, modifiers, alias: undefined, namespace: undefined, symbol: undefined, }); } } } return importNodes; };