(
importPath: string,
language: Language,
context?: ResolutionContext,
options: { aliasPrefixes?: boolean; fromFile?: string } = {}
)
| 1295 | * files under the manifest that declares it (#2456). |
| 1296 | */ |
| 1297 | export function isExternalImport( |
| 1298 | importPath: string, |
| 1299 | language: Language, |
| 1300 | context?: ResolutionContext, |
| 1301 | options: { aliasPrefixes?: boolean; fromFile?: string } = {} |
| 1302 | ): boolean { |
| 1303 | // Relative imports are not external |
| 1304 | if (importPath.startsWith('.')) { |
| 1305 | return false; |
| 1306 | } |
| 1307 | |
| 1308 | // Workspace-member imports (`@scope/ui`, `@scope/ui/widgets`) are LOCAL to |
| 1309 | // a monorepo even though they look like bare npm specifiers. Consult the |
| 1310 | // workspace map first so they aren't misclassified as external (#629). The |
| 1311 | // map is null for single-package repos, so this is a no-op there. |
| 1312 | const workspaces = context?.getWorkspacePackages?.(); |
| 1313 | if (workspaces && resolveWorkspaceImport(importPath, workspaces, options.fromFile)) { |
| 1314 | return false; |
| 1315 | } |
| 1316 | |
| 1317 | // Common external patterns |
| 1318 | if (ESM_IMPORT_LANGUAGES.has(language)) { |
| 1319 | // Node built-ins |
| 1320 | if (['fs', 'path', 'os', 'crypto', 'http', 'https', 'url', 'util', 'events', 'stream', 'child_process', 'buffer'].includes(importPath)) { |
| 1321 | return true; |
| 1322 | } |
| 1323 | // Project-defined alias prefix? Treat as local. A catch-all `"*"` has no |
| 1324 | // prefix and matches `react` too: what it names is left to |
| 1325 | // resolveFromOwnConfig, which keeps the app's packages out. |
| 1326 | const aliases = options.aliasPrefixes === false ? null : context?.getProjectAliases?.(); |
| 1327 | if (aliases) { |
| 1328 | for (const pat of aliases.patterns) { |
| 1329 | if (pat.prefix !== '' && importPath.startsWith(pat.prefix)) return false; |
| 1330 | } |
| 1331 | } |
| 1332 | // Scoped packages or bare specifiers that don't start with aliases |
| 1333 | if (!importPath.startsWith('@/') && !importPath.startsWith('~/') && !importPath.startsWith('src/')) { |
| 1334 | // Likely an npm package |
| 1335 | return true; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | if (language === 'python') { |
| 1340 | // Standard library modules |
| 1341 | const stdLibs = ['os', 'sys', 'json', 're', 'math', 'datetime', 'collections', 'typing', 'pathlib', 'logging']; |
| 1342 | if (stdLibs.includes(importPath.split('.')[0]!)) { |
| 1343 | return true; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | if (language === 'go') { |
| 1348 | // Relative imports (rare in idiomatic Go but the grammar allows them). |
| 1349 | if (importPath.startsWith('.')) { |
| 1350 | return false; |
| 1351 | } |
| 1352 | // In-module imports look like `<module-path>/sub/pkg` — local to |
| 1353 | // this project. Without the module-path check we'd flag every |
| 1354 | // cross-package call in a Go monorepo as external (issue #388) — and, |
no test coverage detected