Repository navigation
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
Open
jamesconwaydev wants to merge 2 commits into
jamesconwaydev wants to merge 2 commits into
Conversation
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.
Brings the branch onto its new base after the pull request below it merged.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
importandexportnode, but turning them into that means redoing the spec's ParseModule by hand:export default 42exports a local named*default*,export * from "m"andexport * as ns from "m"are different records, and inimport { a } from "m"; export { a }the export is a re-export ofm'sa, not of a local binding, whileimport * as ns from "m"; export { ns }stays local.Change. A new module,
parsec_js::module, withModuleRecord::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. AModuleRequesthas its decodedspecifier, itsattributes(with { type: "json" }) sorted by key in UTF-16 order as WithClauseToAttributes sorts them, and thespanof its first specifier. Two requests are one when specifier and attributes both match, whichever order the attributes were written in. Dynamicimport()isn't a request; it's an expression.import_entries: Vec<ImportEntry>:module_request(an index intorequested_modules),import_name(ImportName::Name,"default"for a default import, orImportName::NamespaceObject),local_name,span.local_export_entries,indirect_export_entries,star_export_entries: Vec<ExportEntry>:export_name,module_request,import_name(ExportImportName::Name,AllorAllButDefault),local_name,span, sorted as ParseModule sorts them, including the two rewrites above.disd, and string names likeexport { c as "e f" }are their values. A specifier with a lone surrogate gets U+FFFD, since a RustStringcan't hold one; the module docs say so.parsefuzz target builds each tree's record and checks every index and span.Tests.
tests/module.rs, 9 tests:import_forms_map_to_import_entries: the spec's table of import forms, all four rows, andimport "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 locallet,export *, a named re-export andexport * as, each in the list ParseModule puts it in.every_declared_name_is_exported:export constwith 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 warningsand the bench's,cargo fmt --checkfor both,cargo docfor each crate with-D warnings,cargo deny check licenses bans sourcesfor both, the fuzz manifests'cargo metadataandscripts/check-headers.shpass locally.