Skip to content

Commit

Permalink
fix: specifier === '#' or '#/'
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Jan 1, 2023
1 parent 5b742f7 commit 9368d41
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-lizards-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'resolve.imports': patch
---

Returns `undefined` if specifier is exactly '#' or '#/'.
5 changes: 3 additions & 2 deletions .changeset/metal-planets-end.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

Do not support recursive resolution.

There is no spec for it.
It will return `undefined` if the import is recursive.
The spec does not support recursion: https://nodejs.org/api/esm.html#esm_resolver_algorithm_specification

`resolve()` will return `undefined` if the import is recursive.
4 changes: 2 additions & 2 deletions packages/resolve.imports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ Here are the key notes:
> Return PACKAGE_RESOLVE(target with every instance of "*" replaced by patternMatch, packageURL + "/").

The phrase `target with every instance of "*" replaced by patternMatch` indicates it can contain multiple `*`s.
However, in `PATTERN_KEY_COMPARE`, it is asserted that the pattern only contains one `*`.
So this module only supports one `*` in the pattern.
This module supports multiple `*`s in the replacer pattern as described,
but it is likely a bug in the spec, as the resulting string likely does not make sense.

### `PATTERN_KEY_COMPARE`

Expand Down
10 changes: 10 additions & 0 deletions packages/resolve.imports/ts/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ describe('subpath imports', () => {
expect(r).toBeUndefined()
})

it('returns undefined when specifier does not start with #', () => {
expect(resolve({ imports: { x: 'y' } }, 'x')).toBeUndefined()
})

it('returns undfined if specifier is exactly # or #/', () => {
const pkg = { imports: { '#': 'y', '#/': 'x' } }
expect(resolve(pkg, '#')).toBeUndefined()
expect(resolve(pkg, '#/')).toBeUndefined()
})

it('returns undefined when entry does not start with #', () => {
const r = resolve(
{
Expand Down
13 changes: 7 additions & 6 deletions packages/resolve.imports/ts/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ export type ResolveOptions = {
* Resolve an import specifier based on package.json#imports.
*
* @param pkg contents of package.json
* @param entry import specifier
* @param specifier import specifier
* @return resolved specifier or undefined if not found
*/
export function resolve(pkg: any, entry: string, options?: ResolveOptions) {
export function resolve(pkg: any, specifier: string, options?: ResolveOptions) {
if (!pkg.imports) return undefined
if (!entry.startsWith('#')) return undefined
if (!specifier.startsWith('#')) return undefined
if (specifier === '#' || specifier === '#/') return undefined

const matched = pkg.imports[entry]
const matched = pkg.imports[specifier]
if (matched) {
return noRecursive(lookupReplacer(matched, options?.conditions?.slice()))
}
Expand All @@ -26,7 +27,7 @@ export function resolve(pkg: any, entry: string, options?: ResolveOptions) {
const keyParts = key.split('*')

const [prefix, suffix] = keyParts
if (entry.startsWith(prefix)) {
if (specifier.startsWith(prefix)) {
const replacer = lookupReplacer(pkg.imports[key], options?.conditions?.slice())

if (replacer) return noRecursive(
Expand All @@ -35,7 +36,7 @@ export function resolve(pkg: any, entry: string, options?: ResolveOptions) {
}

function replacePattern(replacer: string) {
const toKeep = suffix ? entry.slice(prefix.length, -suffix.length) : entry.slice(prefix.length)
const toKeep = suffix ? specifier.slice(prefix.length, -suffix.length) : specifier.slice(prefix.length)
return replacer.replace(/\*/g, toKeep)
}
}
Expand Down

0 comments on commit 9368d41

Please sign in to comment.