Skip to content

fix(resolution): reject wildcard alias when prefix and suffix overlap#882

Open
Dashsoap wants to merge 1 commit into
colbymchenry:mainfrom
Dashsoap:fix/alias-prefix-suffix-overlap
Open

fix(resolution): reject wildcard alias when prefix and suffix overlap#882
Dashsoap wants to merge 1 commit into
colbymchenry:mainfrom
Dashsoap:fix/alias-prefix-suffix-overlap

Conversation

@Dashsoap

Copy link
Copy Markdown

What

applyAliases in src/resolution/path-aliases.ts matched a tsconfig paths wildcard pattern against imports where the pattern's prefix and suffix overlap.

For a pattern like @x/*/index (prefix @x/, suffix /index), an import of @x/index — which has nothing in the * slot — was wrongly accepted, because startsWith(prefix) and endsWith(suffix) can both be satisfied by the same shared /. The captured wildcard came out empty and the import resolved to a bogus mapped file, so call/reference edges were attached to the wrong symbol.

Why

prefix = '@x/'    (len 3)
suffix = '/index' (len 6)
import = '@x/index' (len 8)   // 8 < 3 + 6  → no room for the wildcard

@x/index.startsWith('@x/') ✓ and @x/index.endsWith('/index') ✓, but the two regions overlap on the single /. TypeScript requires the * to match at least one character, so this import must not resolve through the alias.

Before the fix, applyAliases('@x/index', …) returned ['widgets/index'] (with captured === ''), silently mis-resolving the import.

Fix

Guard the wildcard branch so prefix and suffix must occupy disjoint regions before a match is accepted:

if (importPath.length < pat.prefix.length + pat.suffix.length) continue;

Literal (non-wildcard) patterns are unaffected — they already require an exact match.

Test

Adds a regression test in the existing tsconfig path aliases describe block (__tests__/resolution.test.ts). It indexes a project with paths: { '@x/*/index': ['widgets/*/index'] } and a bare import { trap } from '@x/index', then asserts the widgets/index.ts symbol does not receive a caller via the false match. Verified the test fails on main and passes with this fix; npm run build is clean.

A tsconfig `paths` pattern with a wildcard between literals — e.g.
`@x/*/index` (prefix `@x/`, suffix `/index`) — was matched against
imports where the prefix and suffix regions overlap, such as `@x/index`.
`startsWith(prefix)` and `endsWith(suffix)` can both be satisfied by the
same `/` character, so the import falsely matched even though it has no
segment for `*`. The captured wildcard came out empty and the import
resolved to a bogus file, attaching call/reference edges to the wrong
symbol.

Guard the wildcard branch so the prefix and suffix must occupy disjoint
regions (`importPath.length >= prefix.length + suffix.length`) before a
match is accepted. Adds a regression test under the existing
`tsconfig path aliases` suite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant