Skip to content

Commit

Permalink
fixup! fixup! fixup! esm: fix hint on invalid module specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 21, 2023
1 parent 1731f5d commit afafad2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,12 @@ function resolveAsCommonJS(specifier, parentURL) {
// module
const i = StringPrototypeIndexOf(specifier, '/');
const pkg = i === -1 ? specifier : StringPrototypeSlice(specifier, 0, i);
const index = StringPrototypeIndexOf(found, `node_modules${sep}${pkg}${sep}`);
const needle = `node_modules${sep}${pkg}${sep}`;
const index = StringPrototypeIndexOf(found, needle);
if (index !== -1) {
found = ArrayPrototypeJoin(
found = pkg + '/' + ArrayPrototypeJoin(
ArrayPrototypeMap(
StringPrototypeSplit(StringPrototypeSlice(found, index + `node_modules${sep}`.length), sep),
StringPrototypeSplit(StringPrototypeSlice(found, index + needle.length), sep),
// Escape URL-special characters to avoid generating a incorrect suggestion
encodeURIComponent,
),
Expand Down
4 changes: 4 additions & 0 deletions test/es-module/test-esm-module-not-found-commonjs-hint.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('ESM: module not found hint', { concurrency: true }, () => {
input: 'import obj from "some_module/folder%25with percentage?/index.js"',
expected: / "some_module\/folder%2525with%20percentage%3F\/index\.js"\?/,
},
{
input: 'import "@nodejsscope/pkg/index"',
expected: / "@nodejsscope\/pkg\/index\.js"\?/,
},
{
input: 'import obj from "lone_file.js"',
expected: /node_modules\/lone_file\.js"\?/,
Expand Down

0 comments on commit afafad2

Please sign in to comment.