diff --git a/.config/vitest.config.mts b/.config/vitest.config.mts index eefa13d63..106bcea95 100644 --- a/.config/vitest.config.mts +++ b/.config/vitest.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -59,4 +59,4 @@ export default defineConfig({ ], }, }, -}) +} satisfies UserConfig diff --git a/.config/vitest.e2e.config.mts b/.config/vitest.e2e.config.mts index 0ed28d4b0..668a97bdf 100644 --- a/.config/vitest.e2e.config.mts +++ b/.config/vitest.e2e.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -37,4 +37,4 @@ export default defineConfig({ ], }, }, -}) +} satisfies UserConfig diff --git a/.config/vitest.integration.config.mts b/.config/vitest.integration.config.mts index 5b37d0861..20c092a2b 100644 --- a/.config/vitest.integration.config.mts +++ b/.config/vitest.integration.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -29,4 +29,4 @@ export default defineConfig({ 'test/fixtures/**', ], }, -}) +} satisfies UserConfig diff --git a/.config/vitest.native.config.mts b/.config/vitest.native.config.mts index 994807a1a..bf7b0eede 100644 --- a/.config/vitest.native.config.mts +++ b/.config/vitest.native.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false, @@ -32,4 +32,4 @@ export default defineConfig({ 'test/fixtures/**', ], }, -}) +} satisfies UserConfig diff --git a/.config/vitest.terminal.config.mts b/.config/vitest.terminal.config.mts index 063691615..3b40b7130 100644 --- a/.config/vitest.terminal.config.mts +++ b/.config/vitest.terminal.config.mts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url' -import { defineConfig } from 'vitest/config' +import type { UserConfig } from 'vitest/config' import { resolveSnapshotPath } from './snapshot-path.mts' -export default defineConfig({ +export default { root: fileURLToPath(new URL('../', import.meta.url)), resolve: { preserveSymlinks: false }, test: { @@ -23,4 +23,4 @@ export default defineConfig({ ], exclude: ['**/node_modules/**', '**/dist/**', '.claude/worktrees/**'], }, -}) +} satisfies UserConfig diff --git a/src/shadow/common.test.mts b/src/shadow/common.test.mts new file mode 100644 index 000000000..1f5ec40bf --- /dev/null +++ b/src/shadow/common.test.mts @@ -0,0 +1,28 @@ +import { describe, expect, it, vi } from 'vitest' + +const mockGetAlertsMapFromPurls = vi.hoisted(() => vi.fn()) + +vi.mock('../utils/alerts-map.mts', () => ({ + getAlertsMapFromPurls: mockGetAlertsMapFromPurls, +})) + +import { scanPackagesAndLogAlerts } from './common.mts' + +describe('scanPackagesAndLogAlerts', () => { + it('scans npm alias arguments using the target package PURL', async () => { + mockGetAlertsMapFromPurls.mockResolvedValue(new Map()) + + await scanPackagesAndLogAlerts({ + acceptRisks: false, + command: 'add', + installCommands: new Set(['add']), + managerName: 'npm', + rawArgs: ['npm', 'alias@npm:real-package@1.2.3'], + }) + + expect(mockGetAlertsMapFromPurls).toHaveBeenCalledWith( + ['pkg:npm/real-package@1.2.3'], + expect.any(Object), + ) + }) +}) diff --git a/src/utils/npm-spec.mts b/src/utils/npm-spec.mts index 48df2d7b9..974709800 100644 --- a/src/utils/npm-spec.mts +++ b/src/utils/npm-spec.mts @@ -22,6 +22,7 @@ */ import npmPackageArg from 'npm-package-arg' +import type { AliasResult } from 'npm-package-arg' import { NPM } from '../constants.mts' import { createPurlObject } from './purl.mts' @@ -98,38 +99,37 @@ export function safeParseNpmSpec( } } - // Extract name and version from parsed spec. - const name = parsed.name || pkgSpec + // npm aliases carry the target package in subSpec. Use it for PURLs and + // version extraction so the alias label does not become the package name. + const spec = + parsed.type === 'alias' ? (parsed as AliasResult).subSpec : parsed + const name = spec.name || pkgSpec let version: string | undefined // Handle different spec types from npm-package-arg. - if ( - parsed.type === 'tag' || - parsed.type === 'version' || - parsed.type === 'range' - ) { + if (spec.type === 'tag' || spec.type === 'version' || spec.type === 'range') { // For npm registry packages: // - type 'tag': latest, beta, etc. // - type 'version': exact version like 1.0.0 // - type 'range': version range like ^1.0.0, ~1.0.0, or * for bare names // Don't include '*' as a version - it means "any version". - if (parsed.fetchSpec && parsed.fetchSpec !== '*') { - version = parsed.fetchSpec + if (spec.fetchSpec && spec.fetchSpec !== '*') { + version = spec.fetchSpec } else if ( - parsed.rawSpec && - parsed.rawSpec !== '*' && - parsed.rawSpec !== parsed.name + spec.rawSpec && + spec.rawSpec !== '*' && + spec.rawSpec !== spec.name ) { - version = parsed.rawSpec + version = spec.rawSpec } } else if ( - parsed.type === 'git' || - parsed.type === 'remote' || - parsed.type === 'file' + spec.type === 'git' || + spec.type === 'remote' || + spec.type === 'file' ) { // For non-registry specs, use rawSpec if different from name. - if (parsed.rawSpec && parsed.rawSpec !== parsed.name) { - version = parsed.rawSpec + if (spec.rawSpec && spec.rawSpec !== spec.name) { + version = spec.rawSpec } } diff --git a/src/utils/npm-spec.test.mts b/src/utils/npm-spec.test.mts new file mode 100644 index 000000000..009ccccb8 --- /dev/null +++ b/src/utils/npm-spec.test.mts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest' + +import { safeParseNpmSpec, safeNpmSpecToPurl } from './npm-spec.mts' + +describe('npm aliases', () => { + it.each([ + ['my-alias@npm:real-package@1.2.3', 'real-package', '1.2.3'], + ['my-alias@npm:real-package@^1.2.0', 'real-package', '^1.2.0'], + ['my-alias@npm:real-package@beta', 'real-package', 'beta'], + ['my-alias@npm:real-package', 'real-package', undefined], + ['my-alias@npm:@scope/real-package@2.0.0', '@scope/real-package', '2.0.0'], + ['my-alias@npm:real-package@*', 'real-package', undefined], + ])('parses %s using its target', (input, name, version) => { + expect(safeParseNpmSpec(input)).toEqual({ name, version }) + expect(safeNpmSpecToPurl(input)).toBe( + `pkg:npm/${name}${version ? `@${encodeURIComponent(version)}` : ''}`, + ) + }) +})