Skip to content

WIP: feat(lexer): report glob n for interpolated template dynamic imports - #1

Closed
BridgeAR wants to merge 5 commits into
mainfrom
BridgeAR/2026-06-21-dynamic-import-template-glob
Closed

BridgeAR wants to merge 5 commits into
mainfrom
BridgeAR/2026-06-21-dynamic-import-template-glob

Conversation

@BridgeAR

Copy link
Copy Markdown
Owner

Summary

A dynamic import whose entire argument is a template literal with substitutions returned n: undefined, so a consumer resolving the specifier (a bundler or glob importer) had nothing to work with. It now reports the static skeleton as a glob with each ${...} collapsed to a single *: import(`./locales/${locale}.js`) yields ./locales/*.js. Only a lone template literal qualifies — a template concatenated with anything else (import(`a` + b)) has no static skeleton and still returns undefined.

The change lives entirely in the three decoders (WASM src/lexer.ts, asm.js src/lexer.asm.js, pure-JS lexer.js); src/lexer.c is untouched, since the parser already produces the correct specifier range and still descends into ${ ... } for nested-import detection.

Test plan

  • chomp test green on both shipped builds (wasm + asm.js), 141 passing each.
  • New Dynamic import interpolated template specifier glob test covers multi-substitution, leading/trailing/whole substitution, escapes, string/comment/nested-template interpolation bodies, attribute-following, and the concatenation negatives.
  • CI on all supported Node majors / platforms.

Notes

WIP / draft: based on origin/main, so the PR currently also carries three sibling commits not yet in main — guybedford#200 (dev-deps), guybedford#201 (no-substitution template specifiers), and the export binding-detection fix — which this work depends on. The lib/ artifact rebuild is a separate chore commit.

Fixes: guybedford#137

BridgeAR and others added 5 commits June 21, 2026 00:11
* build(deps-dev): update dev dependencies to latest

The TypeScript and mocha majors each break a build step unless paired with a
change here:

1. `build:types` errored on `Buffer` — TypeScript 6 no longer auto-includes
   @types/node for a file passed on the tsc command line, so the call now
   passes `--types node`.
2. The unit suite threw `ReferenceError: beforeEach is not defined` at load —
   mocha 11's tdd UI dropped the bdd `beforeEach`, so its two hooks use the
   tdd `setup` alias.

* fix(bench): update kleur to v4 function-call chaining API

---------

Co-authored-by: Guy Bedford <guybedford@gmail.com>
…d#201)

* fix: report n for no-substitution template dynamic imports

A dynamic import whose specifier is a no-substitution template literal
(import(`./x.js`)) returned n: undefined, while the quoted forms returned the
specifier. A template with no ${...} is a constant string, so it now records a
safe specifier the same as a quoted one; an interpolated template has no
constant value and still returns undefined. This surfaces with minified
Rolldown bundles, which rewrite dynamic-import specifiers to template literals.

readString tolerates raw line breaks for a backtick quote, since template
literals permit them and string literals do not.

Drive-by fix:

* The JS fallback recorded the specifier start before skipping leading
  whitespace/comments, so import(/* c */ "x") mis-decoded n; it now records the
  start after, matching the C lexer.

Fixes guybedford#198

* fix: tolerate template line breaks in asm.js specifier decoder

The asm.js wrapper decodes the dynamic-import specifier with its own
readString copy, which still rejected raw line breaks. A multi-line
no-substitution template threw a parse error on the asm.js build while
WASM and the JS fallback decoded it. Apply the same backtick exception.

---------

Co-authored-by: Guy Bedford <guybedford@gmail.com>
The export lexer stopped at the first `= initializer`, so later declarators
were dropped (`export const a = 1, b = 2` reported only `a`), and the
destructuring path reported property keys instead of bound names
(`export const { a: b } = x` reported `a`) and dropped defaults, nested
patterns, rest elements and trailing declarators.

Initializers and defaults are now skipped expression-aware, so a comma inside
one never splits the binding list, and the list ends at `;`, EOF or an ASI
line break — the statement after an automatic semicolon is never read as
another binding.

A binding list that reaches EOF mid-declaration (`export const`,
`export let a,`) no longer scans past the source terminator into the analysis
heap and reports a phantom export: the var/let/const loop now stops at `end`,
matching the destructuring-pattern loop.

Drive-by fix:

* The JS fallback matched char code 109 (`m`) for the `let` keyword instead of
  108 (`l`), so `export let` parsed no names there; the C lexer was correct.
The tracked lib/ artifacts last matched src/lexer.c at cc29c5b (guybedford#195). Two
later commits changed the C source without rebuilding them, leaving the
committed artifacts inconsistent with the sources they are generated from:

1. 0259739 (guybedford#201) — no-substitution template dynamic-import specifiers.
2. e139948 — export var/let/const binding detection.

Regenerate with the pinned wasi-sdk 12 + emscripten 1.40.1-fastcomp toolchain.
A dynamic import whose entire argument is a template literal with substitutions
returned n: undefined, so a consumer resolving the specifier (a bundler or glob
importer) had nothing to work with. It now reports the static skeleton as a
glob with each ${...} collapsed to a single "*": import(`./locales/${x}.js`)
yields "./locales/*.js".

Only a lone template literal qualifies. A template concatenated with anything
else (import(`a` + b)) has no static skeleton and still returns undefined.

Fixes: guybedford#137
@BridgeAR BridgeAR closed this Jun 27, 2026
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.

Glob imports don't parse

1 participant