Skip to content

feat(js): modules parsed only as far as their first import or export - #188

Open
jamesconwaydev wants to merge 3 commits into
js-parserfrom
js-modules
Open

jamesconwaydev wants to merge 3 commits into
js-parserfrom
js-modules

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Fixes #154.

Builds on #187, which this is based on; once that merges, this is rebased onto its base.

Problem. With #187, a module parsed as a strict script with top-level await and nothing else, so import a from "a" stopped at a with unexpected-token, and the 713 test262 module tests were skipped. three.js, which ships as a module, didn't parse at all.

Change.

  • src/parser/module.rs: the module grammar of clause 16.2.
    • import: a default binding, * as ns, named imports (a as b, string names like "a-b" as c), side-effect imports, and a with { type: "json" } clause, with duplicate keys an error. A name imported without as has to be one a binding could be, so import {if} from "a" and import {"a"} from "a" fail with import-needs-as.
    • export: var, let, const, functions, generators, async functions and classes; default with a declaration (whose name is optional) or an expression; { ... } with and without from; * and * as name, with attributes. A local export (export {a}, no from) must name what a reference could, so export {if} and export {"a"} fail.
    • Exported names are checked for duplicates, default included. String names with a lone surrogate are an error, as IsStringWellFormedUnicode says.
    • Both declarations only at a module's top level. In a script, a block or a function, they fail with module-item-not-allowed, which says where they're allowed; import(...) and import.meta stay expressions everywhere.
  • ast.rs and estree.rs: nine node types, written as acorn writes them: ImportDeclaration with attributes, ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, ImportAttribute, ExportNamedDeclaration, ExportSpecifier, ExportDefaultDeclaration and ExportAllDeclaration.
  • tests/test262.rs parses flags: [module] files once, as modules. The 294 _FIXTURE.js files are skipped and counted: they're what module tests import, some are meant not to parse or use syntax past ES2025, and only the tests importing them say which. Module tests that declare a name twice or export a name never declared join the early errors left for scope analysis.
  • tests/estree/modules.mjs and its acorn fixture. gen-estree-fixtures.sh parses .mjs files as modules, and compare-acorn.sh compares test262's modules too.
  • README's JavaScript row, ROADMAP's M10, the changelog, the crate description and Options::source_type's doc say modules are in.

Tests.

  • test262: 52,232 passed, 0 failed, 1,863 skipped. All 48,408 files that should parse do, 513 of them modules. Of the 4,374 parse negatives, 3,824 fail to parse; the 550 left are early errors for M11 (358 regular expression patterns, 192 redeclarations and unresolvable exports, 15 of those in modules). 294 module fixtures and 1,019 files past ES2025 or not UTF-8 are skipped.
  • tests/estree.rs: modules.mjs, with every import and export form, attributes, string names, import.meta and top-level await, matches acorn 8.19.0's tree.
  • scripts/compare-acorn.sh, run locally: 48,051 test262 files the same as acorn's, 509 of them modules, and so are three.module.js and three.module.min.js 0.181.0.
  • tests/parser.rs: imports, exports and modules_are_strict_and_top_level. Without the change, each fails on its first case: import 'a', export var a = 1, b, and { import 'a' }, which needs module-item-not-allowed.

cargo test --workspace, cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --all --check, cargo doc with -D warnings, cargo deny check licenses bans sources and scripts/check-headers.sh pass locally.

A module parsed as a strict script with top-level await, so every
`import` and `export` declaration was a syntax error, and test262's
module tests were skipped.

- `import` in every form: a default binding, `* as ns`, named imports
  with `as` and string names, side-effect imports, and `with { ... }`
  attributes, their keys checked for duplicates.
- `export` in every form: declarations, `default` with a declaration or
  an expression, named exports with and without a source, `*` and
  `* as name`, with attributes. Exported names are checked for
  duplicates, and a local export must name what a reference could.
- Both only at a module's top level; in a script, or in a block, they
  are an error that says so.
- ESTree output in acorn's shape for all nine node types.
- test262 runs module tests as modules; `_FIXTURE.js` files, which only
  the tests importing them judge, are skipped and counted.
- A module fixture from acorn, and compare-acorn.sh compares modules.
@jamesconwaydev jamesconwaydev added the javascript The JavaScript parser, parsec-js label Nov 21, 2026
early_error matches paths with `/`, and on Windows each test's name had
`\`, so the 535 early errors it names failed there instead of being
skipped. The name now uses `/` on every OS.
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