Skip to content

feat(js): a module's imports and exports had to be dug out of its tree - #196

Open
jamesconwaydev wants to merge 2 commits into
js-earlyfrom
js-records
Open

jamesconwaydev wants to merge 2 commits into
js-earlyfrom
js-records

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Fixes #160.

Builds on #195, which this is based on; once the stack below it merges, this is rebased onto its base. With it, M11 (#157) has all three parts.

Problem. A bundler, a linter or a code-search tool building a module graph needs what each module requests and which names cross each edge. Since #188 the tree has every import and export node, but turning them into that means redoing the spec's ParseModule by hand: export default 42 exports a local named *default*, export * from "m" and export * as ns from "m" are different records, and in import { a } from "m"; export { a } the export is a re-export of m's a, not of a local binding, while import * as ns from "m"; export { ns } stays local.

Change. A new module, parsec_js::module, with ModuleRecord::new(&ast, src). Nothing is resolved or loaded.

  • requested_modules: Vec<ModuleRequest>: each request once, in the order first requested, as the spec's ModuleRequests dedupes them. A ModuleRequest has its decoded specifier, its attributes (with { type: "json" }) sorted by key in UTF-16 order as WithClauseToAttributes sorts them, and the span of its first specifier. Two requests are one when specifier and attributes both match, whichever order the attributes were written in. Dynamic import() isn't a request; it's an expression.
  • import_entries: Vec<ImportEntry>: module_request (an index into requested_modules), import_name (ImportName::Name, "default" for a default import, or ImportName::NamespaceObject), local_name, span.
  • local_export_entries, indirect_export_entries, star_export_entries: Vec<ExportEntry>: export_name, module_request, import_name (ExportImportName::Name, All or AllButDefault), local_name, span, sorted as ParseModule sorts them, including the two rewrites above.
  • Names are decoded: d is d, and string names like export { c as "e f" } are their values. A specifier with a lone surrogate gets U+FFFD, since a Rust String can't hold one; the module docs say so.
  • The parse fuzz target builds each tree's record and checks every index and span.
  • README gets a row, ROADMAP's M11 a bullet, and the changelog an entry.

Tests. tests/module.rs, 9 tests:

  • import_forms_map_to_import_entries: the spec's table of import forms, all four rows, and import "mod" with no entry.
  • export_forms_map_to_export_entries: the spec's table of export forms, all ten rows, each written as the table writes it (null, *default*, all, all-but-default).
  • parse_module_sorts_exports: default, named and namespace imports exported again, a local let, export *, a named re-export and export * as, each in the list ParseModule puts it in.
  • every_declared_name_is_exported: export const with nested object and array patterns, defaults, holes and rest, then a function and a class.
  • requested_modules_are_listed_once_in_order, import_attributes_are_sorted_by_key, strings_and_escapes_are_decoded, spans_point_at_each_entry, a_script_has_no_records.

cargo +nightly fuzz run parse -- -max_total_time=120 -max_len=4096: 1,203,859 runs, no crash. cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings and the bench's, cargo fmt --check for both, cargo doc for each crate with -D warnings, cargo deny check licenses bans sources for both, the fuzz manifests' cargo metadata and scripts/check-headers.sh pass locally.

A bundler or code-search tool building a module graph needs what each
module requests and which names cross each edge. The tree has every
import and export node, but turning them into that meant redoing the
spec's ParseModule: export default's *default* name, export * against
export * as, and an imported name exported again, which is a
re-export of the other module's name and not a local one.

parsec_js::module::ModuleRecord::new(&ast, src) gives the records as
ParseModule makes them:

- requested_modules: each ModuleRequest once, in first-request order,
  its specifier decoded and its import attributes sorted by key as the
  spec sorts them; two are one when both match;
- import_entries: ImportName (a name, default, or the namespace
  object) and LocalName, with the index of the request;
- local_export_entries, indirect_export_entries, star_export_entries:
  ExportEntry records with ExportName, ModuleRequest, ImportName (a
  name, all or all-but-default) and LocalName, an imported binding
  exported again moved to indirect, an imported namespace kept local.

Each entry has a span. tests/module.rs checks the spec's tables of
import and export forms row by row, and the parse fuzz target checks
every record's indices and spans.
@jamesconwaydev jamesconwaydev added the javascript The JavaScript parser, parsec-js label Nov 21, 2026
Brings the branch onto its new base after the pull request below it merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

javascript The JavaScript parser, parsec-js

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant