fix(resolution): reject wildcard alias when prefix and suffix overlap#882
Open
Dashsoap wants to merge 1 commit into
Open
fix(resolution): reject wildcard alias when prefix and suffix overlap#882Dashsoap wants to merge 1 commit into
Dashsoap wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
applyAliasesinsrc/resolution/path-aliases.tsmatched a tsconfigpathswildcard 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, becausestartsWith(prefix)andendsWith(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
@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'](withcaptured === ''), silently mis-resolving the import.Fix
Guard the wildcard branch so prefix and suffix must occupy disjoint regions before a match is accepted:
Literal (non-wildcard) patterns are unaffected — they already require an exact match.
Test
Adds a regression test in the existing
tsconfig path aliasesdescribe block (__tests__/resolution.test.ts). It indexes a project withpaths: { '@x/*/index': ['widgets/*/index'] }and a bareimport { trap } from '@x/index', then asserts thewidgets/index.tssymbol does not receive a caller via the false match. Verified the test fails onmainand passes with this fix;npm run buildis clean.