UNPKG

knip

Version:

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

34 lines (33 loc) 1.34 kB
import { IMPORT_FLAGS } from '../../constants.js'; import { getEnvSpecifier } from '../../plugins/vitest/helpers.js'; import { isAbsolute, isInternal } from '../../util/path.js'; import { getLeadingComments, stripQuotes } from '../ast-helpers.js'; const VITEST_ENV = /@(vitest|jest)-environment\s+(\S+)/g; export const collectCustomImports = (sourceFile) => { const comments = getLeadingComments(sourceFile); if (!comments.length) return []; const importNodes = []; for (const comment of comments) { let match; while ((match = VITEST_ENV.exec(comment.text)) !== null) { const id = stripQuotes(match[2].trim()); if (!id) continue; const isLocal = isInternal(id) || isAbsolute(id); const modifiers = isLocal ? IMPORT_FLAGS.ENTRY : IMPORT_FLAGS.NONE; const offset = match[0].length - match[2].length; const specifier = isLocal || id === 'node' ? id : getEnvSpecifier(id); importNodes.push({ specifier, identifier: undefined, pos: comment.pos + match.index + offset, modifiers, alias: undefined, namespace: undefined, symbol: undefined, }); } } return importNodes; };