GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor(javascript): remove the parser's object mode (#23345) · webpack/webpack@a715c38

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • .changeset
    • 085-javascript-parse-program-estree-option.md
  • docs
    • syntax.md
  • lib
    • html
      • moduleSyntax.js
    • javascript
      • JavascriptParser.js
      • data.js
      • syntax-parser.js
  • test
    • configCases/parsing/no-estree-objects
      • webpack.config.js
    • fixtures/javascript
      • invariants-runner-declared-runs-and-named-renderers
        • parse.json
      • javascript-minifier-printing-a-syntax-tree-should-compress-in-the-one-pass-748
        • fixture.js
      • parse-program-reads-es5-property-clashes
        • index.js
        • parse.json
      • parse-program-refuses-a-string-import-without-as
        • index.js
        • parse.json
      • parser-from-the-objects-to-estree-object-built-a-program-and-a-function-read-lazily
        • fixture.js
      • parser-from-the-objects-to-estree-object-built-should-write-the-fields-an-older-edition-s-objects-leave-out
        • index.js
      • parser-from-the-objects-to-estree-object-built-should-write-the-tree-its-objects-hold-once-it-was-let-go
        • index.js
      • parser-module-fallback-at-old-editions
        • parse.json
      • parser-paths-only-the-tree-s-own-syntax-reaches-grows-a-new-tree-s-columns-from-a-node-with-no-field-2
        • index.js
      • parser-paths-only-the-tree-s-own-syntax-reaches-grows-a-new-tree-s-columns-from-a-node-with-no-field
        • fixture.js
      • parser-paths-only-the-tree-s-own-syntax-reaches-logs-the-scope-of-a-block-holding-an-import
        • index.js
      • parser-paths-only-the-tree-s-own-syntax-reaches-reads-whitespace-and-a-comment-after-an-html-style-comment
        • index.js
      • parser-paths-only-the-tree-s-own-syntax-reaches-refuses-a-rest-parameter-binding-a-pattern-in-es2015-only
        • index.js
      • parser-paths-only-the-tree-s-own-syntax-reaches-reports-each-parenthesized-binding-an-arrow-s-parameters-hold
        • index.js
      • parser-reads-as-acorn-does-a-pattern-with-a-hole
        • index.js
      • parser-reads-as-acorn-does-a-private-name-without-its-name
        • index.js
      • parser-reads-as-acorn-does-a-template-with-two-parts
        • index.js
      • parser-reads-as-acorn-does-an-export-under-a-string-name
        • index.js
      • parser-reads-as-acorn-does-an-object-s-computed-short-and-literal-keys
        • index.js
      • parser-reads-as-acorn-does-proto-keys-that-do-not-clash
        • index.js
      • parser-to-estree-object-reads-each-option-as-acorn-builds-a-module
        • index.js
      • parser-to-estree-object-reads-each-option-as-acorn-builds-an-older-edition
        • index.js
      • parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue-2
        • index.js
      • parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue-3
        • index.js
      • parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue-4
        • index.js
      • parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue
        • index.js
      • parser-tokenizer-directive-prologues-should-report-tokens-into-an-array-as-acorn-does
        • index.js
      • syntax-tree-keeps-its-records-when-a-token-callback-parses-again-2
        • index.js
      • syntax-tree-keeps-its-records-when-a-token-callback-parses-again
        • index.js
      • syntax-tree-of-another-parsers-estree
        • fixture.js
      • webpack-parser-reports-the-lines-and-columns-of-comments-on-many-lines
        • fixture.js
      • webpack-parser-reports-the-lines-and-columns-of-tokens-and-comments
        • index.js
      • webpack-parser-reports-the-lines-and-columns-of-tokens-on-one-long-line
        • fixture.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-2
        • index.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-3
        • index.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-4
        • index.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-5
        • index.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-6
        • index.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-7
        • index.js
      • webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at
        • index.js
    • helpers
      • javascriptParse.js
      • syntaxInvariants
        • javascript.js
    • specCases
      • js-fuzz.spectest.js
      • test262-parser.spectest.js
    • unitCases
      • JavascriptParser.unittest.js
      • javascript
        • __snapshots__
          • invariants.unittest.js.snap
        • invariants.unittest.js
        • parser.unittest.js
        • printer.unittest.js
        • scope.unittest.js
        • tokenizer.unittest.js
      • tooling
        • compare-js.unittest.js
  • tooling
    • compare-js-tools.js
    • generate-js-data.js
  • types.d.ts

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎.changeset/085-javascript-parse-program-estree-option.md‎

Copy file name to clipboard
+5Lines changed: 5 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Build ESTree objects only with `toEstreeObject` and `fromEstreeObject`.
Collapse file

‎docs/syntax.md‎

Copy file name to clipboardExpand all lines: docs/syntax.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ Conventions every language keeps, and a new member follows:
159159

160160
## JavaScript
161161

162-
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser to read an asset into a `SyntaxTree`. It is the only parser: a source it refuses is refused, and terser's parse options for what is not a program (`parse.expression`, `parse.strict`, `parse.experimental_typescript`) are refused too. `toEstreeObject(node, fields?)` builds the ESTree object of a node of the last parse (`SyntaxTree#estree` of any tree), each call returning the same object: every field, or only those listed and the rest on a later call; a whole program asked for first is built in one pass in id order. It bridges hooks and plugins while the parser moves to that struct-of-arrays tree. A reader the next parse must not disturb takes the tree with `takeParsedTree()` and releases it once read. The tree already reads as [the node path](#the-node-path) does, a node passed last: `type`, `start`, `end`, `range`, `name`, `value`, `field`, `fieldCount`, `flag` and `textOf` with the `NodeType`, `Field`, `Flag` and `Part` that `syntax.parser` exports. `SourceProcessor` walks the tree a parse writes as the CSS and HTML ones walk theirs, each visitor handed that path and the children taken in source order (a template's quasis and expressions in turn). Asked for `mode: "minify"`, the same walk prints: `syntax-printer.js`'s `printer` returns each node's text from its children's, as `minify` prints a source it neither compresses nor mangles, comments left out and terser's format options read from `format`. A minify walks the tree its own parse wrote (`walkSyntaxTree`) with the comments it keeps, a name printed inside its parent's text where none of its own is read: each is taken as terser's printer reaches it, by the node holding its token, and printed once the text before it is, which a text opening with it leaves to the node printing that text. What terser's stream reads off the text before a node — `has_parens` before a function or object starting a statement, `!--`, `</script` — it reads off the tree instead, the one `parsedTree()` returns while the walk runs, and through the one member only the JavaScript path has, `ancestor(level)`, a node enclosing the current one; and each child's text is read once (`PrintContext#consume`), so a long one is made flat and its pieces let go as it is printed.
163-
- `syntax-parser.js` — tokenizer, acorn-derived core and every production in one file (any parse reaches the productions). `parseProgram(input, pos, options)` is the one entry, a build's included: it writes a new `SyntaxTree`, read through `A`, and returns the program's node, unless `estree: true` asks for ESTree objects, the program returned, as `JavascriptParser` and `WebManifestParser` do (rebuilding a whole program with `toEstreeObject` costs 30–55% more). Beside `sourceType` (and `auto`, a module turning into a script at a top-level `return`), `ecmaVersion` and `parseError`, it takes acorn's grammar and token options, `comments` (an array to collect into) and `importPhases`. `JavascriptParser`, the concatenation, the minifier, the `SourceProcessor` and the tooling all parse through it; only a custom `parse` function replaces it. The tree path writes each node into the tree, or the one a caller passes as `syntaxTree`, as it finishes, children before their parent, an id in the tree's columns rather than an object; the few the parser reads back by name (a key, a module name, an exported declaration, a function before its body ends) stay objects until their parent is written, and an expression read as a pattern after all is retyped in place. Every edition takes a tree, each owned production checking the edition where acorn does; parser plugins do not, since acorn's own productions read their nodes back. A parse into ESTree objects keeps acorn's productions for an older edition, whose objects lack the newer fields. `regexp.js` (pattern validator) is the one on-demand piece, reached only for a pattern the host engine rejected — never `require` it from a build path.
162+
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser to read an asset into a `SyntaxTree`. It is the only parser: a source it refuses is refused, and terser's parse options for what is not a program (`parse.expression`, `parse.strict`, `parse.experimental_typescript`) are refused too. `toEstreeObject(node, options?)` builds the ESTree object of a node of the last parse (`SyntaxTree#estreeObject` of any tree), the same object for the same options: `ecmaVersion` (the edition whose fields it holds, the parse's by default), `ranges`, `locations` and `sourceFile`, as acorn's options of those names. A node spanning at most 1024 characters comes with its whole subtree built at once, as plain data properties; a longer one holds each child as an own accessor that reads it from the tree the first time it is read, so a long subtree nothing reads has no objects; the next parse copies the columns the objects made so far read. It bridges hooks and plugins while the parser moves to that struct-of-arrays tree. A reader the next parse must not disturb takes the tree with `takeParsedTree()` and releases it once read. The tree already reads as [the node path](#the-node-path) does, a node passed last: `type`, `start`, `end`, `range`, `name`, `value`, `field`, `fieldCount`, `flag` and `textOf` with the `NodeType`, `Field`, `Flag` and `Part` that `syntax.parser` exports. `SourceProcessor` walks the tree a parse writes as the CSS and HTML ones walk theirs, each visitor handed that path and the children taken in source order (a template's quasis and expressions in turn). Asked for `mode: "minify"`, the same walk prints: `syntax-printer.js`'s `printer` returns each node's text from its children's, as `minify` prints a source it neither compresses nor mangles, comments left out and terser's format options read from `format`. A minify walks the tree its own parse wrote (`walkSyntaxTree`) with the comments it keeps, a name printed inside its parent's text where none of its own is read: each is taken as terser's printer reaches it, by the node holding its token, and printed once the text before it is, which a text opening with it leaves to the node printing that text. What terser's stream reads off the text before a node — `has_parens` before a function or object starting a statement, `!--`, `</script` — it reads off the tree instead, the one `parsedTree()` returns while the walk runs, and through the one member only the JavaScript path has, `ancestor(level)`, a node enclosing the current one; and each child's text is read once (`PrintContext#consume`), so a long one is made flat and its pieces let go as it is printed.
163+
- `syntax-parser.js` — tokenizer, acorn-derived core and every production in one file (any parse reaches the productions). `parseProgram(input, pos, options)` is the one entry, a build's included: it writes a new `SyntaxTree`, read through `A`, and returns the program's node. `toEstreeObject` builds a node's ESTree object from the tree, leaving out the fields acorn's objects lack at the parse's edition. Beside `sourceType` (and `auto`, a module turning into a script at a top-level `return`), `ecmaVersion` and `parseError`, it takes acorn's grammar and token options, `comments` (an array to collect into) and `importPhases`. `JavascriptParser`, the concatenation, the minifier, the `SourceProcessor` and the tooling all parse through it; only a custom `parse` function replaces it. The tree path writes each node into the tree, or the one a caller passes as `syntaxTree`, as it finishes, children before their parent, an id in the tree's columns rather than an object; a key, a module name and an exported declaration are written as they are read and the checks read their names back by node, a modifier read as a key first (`get`, `set`, `async`) taken back off the tree; a function before its body ends stays an object until it is written, and an expression read as a pattern after all is retyped in place. Every edition takes a tree, each owned production checking the edition where acorn does; parser plugins do not, since acorn's own productions read their nodes back. A parse into ESTree objects keeps acorn's productions for an older edition, whose objects lack the newer fields. `regexp.js` (pattern validator) is the one on-demand piece, reached only for a pattern the host engine rejected — never `require` it from a build path.
164164
- **Scopes:** `syntax.scopes` (`webpack.javascript.syntax.scopes`) is `ScopeAnalyzer.js`'s analysis, loaded on first use. A parse with `scopes: true`, or a `ScopeLog` it empties and refills, records names and scopes as it goes, and `analyzeScope(program)` builds the scope tree from that log in one pass. A tree with no log, such as one `fromEstreeObject` wrote, is walked instead. Call it before the next parse: it reads the tree `A` reads. Concatenation reuses one log through `JavascriptParser._parseScopes`, so a build without `optimization.concatenateModules` or `optimization.avoidEntryIife` never records.
165165

166166
```js
Collapse file

‎lib/html/moduleSyntax.js‎

Copy file name to clipboardExpand all lines: lib/html/moduleSyntax.js
+17-20Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,24 @@ const getJavascriptSyntaxParser = memoize(() =>
1717
* @returns {boolean} true, when it uses `import`, `export`, `import.meta` or top-level `await`
1818
*/
1919
const usesModuleSyntax = (code) => {
20-
const { parse } = getJavascriptSyntaxParser();
21-
try {
22-
parse(code, {
23-
ecmaVersion: "latest",
24-
sourceType: "script",
25-
allowHashBang: true
26-
});
27-
return false;
28-
} catch (_err) {
29-
// a syntax error of any kind is kept as written, like before
30-
}
31-
try {
32-
parse(code, {
33-
ecmaVersion: "latest",
34-
sourceType: "module",
35-
allowHashBang: true
36-
});
20+
const { parseProgram, takeParsedTree } = getJavascriptSyntaxParser();
21+
/**
22+
* @param {"script" | "module"} sourceType what to read the code as
23+
* @returns {boolean} whether it parses as that, the tree let go once read
24+
*/
25+
const parsesAs = (sourceType) => {
26+
try {
27+
parseProgram(code, 0, { sourceType, allowHashBang: true });
28+
} catch (_err) {
29+
// a syntax error of any kind is kept as written, like before
30+
return false;
31+
}
32+
/** @type {import("../javascript/syntax-parser").SyntaxTree} */ (
33+
takeParsedTree()
34+
).release();
3735
return true;
38-
} catch (_err) {
39-
return false;
40-
}
36+
};
37+
return !parsesAs("script") && parsesAs("module");
4138
};
4239

4340
const NOMODULE_MODULE_SYNTAX_MESSAGE =
Collapse file

‎lib/javascript/JavascriptParser.js‎

Copy file name to clipboardExpand all lines: lib/javascript/JavascriptParser.js
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ const {
2323
Field,
2424
Flag,
2525
LEGACY_ASSERT_ATTRIBUTES,
26-
MAX_MASKED_INDEX,
2726
NodeType,
2827
Part,
2928
ScopeLog,
3029
fromEstreeObject,
3130
hasOctalEscape,
3231
isIdentifierChar,
33-
overflowFrom,
3432
parseProgram,
3533
syntaxTreeOfEstree,
3634
takeParsedTree
@@ -1313,6 +1311,29 @@ const holdsStatementNode = (tree, owner, statement) => {
13131311
);
13141312
};
13151313

1314+
// WHY: the indices live in one number rather than an array because 59.4% of the
1315+
// lists that declare anything declare exactly once, and a 4-element array costs
1316+
// ~200 bytes of V8 overhead — 2.33 MB of an 84 MB AST measured over the
1317+
// `js-parser-unit` sources. Only an index past this one needs the array.
1318+
const MAX_MASKED_INDEX = 30;
1319+
1320+
/**
1321+
* Turn a mask that can no longer hold every index into the list it falls back
1322+
* to, holding what the mask held plus the index that overflowed it.
1323+
* @param {number} mask the indices recorded so far
1324+
* @param {number} index the index that does not fit
1325+
* @returns {number[]} every index, in source order
1326+
*/
1327+
const overflowFrom = (mask, index) => {
1328+
/** @type {number[]} */
1329+
const indices = [];
1330+
for (let bits = mask; bits !== 0; bits &= bits - 1) {
1331+
indices.push(31 - Math.clz32(bits & -bits));
1332+
}
1333+
indices.push(index);
1334+
return indices;
1335+
};
1336+
13161337
/**
13171338
* `collectBlockDeclarations` for a list of statements of the tree, by id.
13181339
* @param {SyntaxTree} tree the tree
Collapse file

‎lib/javascript/data.js‎

Copy file name to clipboardExpand all lines: lib/javascript/data.js
+15-14Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -477,22 +477,23 @@ const Part = {
477477
phase: 6
478478
};
479479

480-
// Each node type's fields as `name:kind:slot`, in the order its objects hold
481-
// them; a flag's slot is its `Flag` bit, any other field's its place in the record.
480+
// Each node type's fields as `name:kind:slot[:from[:last]]`, in its objects'
481+
// order: a flag's slot is its `Flag` bit, else its place in the record. Acorn's
482+
// objects hold it from edition `from`, after every other field below `last`.
482483
/** @type {Record<string, string>} */
483484
const NODE_FIELDS = {
484485
Identifier: "name:string:0",
485486
PrivateIdentifier: "name:string:0",
486487
Literal: "value:value:0 raw:string:1",
487488
MemberExpression:
488-
"object:node:0 property:node:1 computed:flag:1 optional:flag:2",
489-
CallExpression: "callee:node:0 arguments:list:1 optional:flag:2",
489+
"object:node:0 property:node:1 computed:flag:1 optional:flag:2:11",
490+
CallExpression: "callee:node:0 arguments:list:1 optional:flag:2:11",
490491
NewExpression: "callee:node:0 arguments:list:1",
491-
ExpressionStatement: "expression:node:0 directive:optional:1",
492+
ExpressionStatement: "expression:node:0 directive:optional:1:5",
492493
VariableDeclarator: "id:node:0 init:node:1",
493494
VariableDeclaration: "declarations:list:0 kind:string:1",
494495
Property:
495-
"method:flag:4 shorthand:flag:8 computed:flag:1 key:node:0 value:node:1 kind:string:2",
496+
"method:flag:4:6 shorthand:flag:8:6 computed:flag:1:6 key:node:0 value:node:1 kind:string:2",
496497
BinaryExpression: "left:node:0 operator:string:1 right:node:2",
497498
LogicalExpression: "left:node:0 operator:string:1 right:node:2",
498499
AssignmentExpression: "operator:string:2 left:node:0 right:node:1",
@@ -518,11 +519,11 @@ const NODE_FIELDS = {
518519
EmptyStatement: "",
519520
DebuggerStatement: "",
520521
FunctionExpression:
521-
"id:node:0 expression:flag:64 generator:flag:128 async:flag:256 params:list:1 body:node:2",
522+
"id:node:0 expression:flag:64:0:6 generator:flag:128:6 async:flag:256:8 params:list:1 body:node:2",
522523
FunctionDeclaration:
523-
"id:node:0 expression:flag:64 generator:flag:128 async:flag:256 params:list:1 body:node:2",
524+
"id:node:0 expression:flag:64:0:6 generator:flag:128:6 async:flag:256:8 params:list:1 body:node:2",
524525
ArrowFunctionExpression:
525-
"id:node:0 expression:flag:64 generator:flag:128 async:flag:256 params:list:1 body:node:2",
526+
"id:node:0 expression:flag:64:0:6 generator:flag:128:6 async:flag:256:8 params:list:1 body:node:2",
526527
IfStatement: "test:node:0 consequent:node:1 alternate:node:2",
527528
ConditionalExpression: "test:node:0 consequent:node:1 alternate:node:2",
528529
ParenthesizedExpression: "expression:node:0",
@@ -537,7 +538,7 @@ const NODE_FIELDS = {
537538
SwitchStatement: "discriminant:node:0 cases:list:1",
538539
ForStatement: "init:node:0 test:node:1 update:node:2 body:node:3",
539540
ForInStatement: "left:node:0 right:node:1 body:node:2",
540-
ForOfStatement: "await:flag:2048 left:node:0 right:node:1 body:node:2",
541+
ForOfStatement: "await:flag:2048:9 left:node:0 right:node:1 body:node:2",
541542
WhileStatement: "test:node:0 body:node:1",
542543
DoWhileStatement: "body:node:0 test:node:1",
543544
WithStatement: "object:node:0 body:node:1",
@@ -550,16 +551,16 @@ const NODE_FIELDS = {
550551
ClassExpression: "id:node:0 superClass:node:1 body:node:2",
551552
ChainExpression: "expression:node:0",
552553
ImportDeclaration:
553-
"specifiers:list:0 source:node:1 attributes:list:2 phase:optional:3",
554+
"specifiers:list:0 source:node:1 attributes:list:2:16 phase:optional:3",
554555
ImportSpecifier: "imported:node:0 local:node:1",
555556
ImportDefaultSpecifier: "local:node:0",
556557
ImportNamespaceSpecifier: "local:node:0",
557558
ImportAttribute: "key:node:0 value:node:1",
558-
ImportExpression: "source:node:0 options:node:1 phase:optional:2",
559+
ImportExpression: "source:node:0 options:node:1:16 phase:optional:2",
559560
ExportNamedDeclaration:
560-
"declaration:node:0 specifiers:list:1 source:node:2 attributes:list:3",
561+
"declaration:node:0 specifiers:list:1 source:node:2 attributes:list:3:16",
561562
ExportDefaultDeclaration: "declaration:node:0",
562-
ExportAllDeclaration: "exported:node:0 source:node:1 attributes:list:2",
563+
ExportAllDeclaration: "exported:node:0:11 source:node:1 attributes:list:2:16",
563564
ExportSpecifier: "local:node:0 exported:node:1",
564565
MetaProperty: "meta:node:0 property:node:1"
565566
};

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.