GeezerVison is showing github.com — refactor(javascript): remove the parser's object mode (#23345) · webpack/webpack@a715c38
File treeExpand file treeCollapse file tree Open diff view settings configCases/parsing/no-estree-objects invariants-runner-declared-runs-and-named-renderers javascript-minifier-printing-a-syntax-tree-should-compress-in-the-one-pass-748 parse-program-reads-es5-property-clashes parse-program-refuses-a-string-import-without-as parser-from-the-objects-to-estree-object-built-a-program-and-a-function-read-lazily parser-from-the-objects-to-estree-object-built-should-write-the-fields-an-older-edition-s-objects-leave-out parser-from-the-objects-to-estree-object-built-should-write-the-tree-its-objects-hold-once-it-was-let-go parser-module-fallback-at-old-editions parser-paths-only-the-tree-s-own-syntax-reaches-grows-a-new-tree-s-columns-from-a-node-with-no-field-2 parser-paths-only-the-tree-s-own-syntax-reaches-grows-a-new-tree-s-columns-from-a-node-with-no-field parser-paths-only-the-tree-s-own-syntax-reaches-logs-the-scope-of-a-block-holding-an-import parser-paths-only-the-tree-s-own-syntax-reaches-reads-whitespace-and-a-comment-after-an-html-style-comment parser-paths-only-the-tree-s-own-syntax-reaches-refuses-a-rest-parameter-binding-a-pattern-in-es2015-only parser-paths-only-the-tree-s-own-syntax-reaches-reports-each-parenthesized-binding-an-arrow-s-parameters-hold parser-reads-as-acorn-does-a-pattern-with-a-hole parser-reads-as-acorn-does-a-private-name-without-its-name parser-reads-as-acorn-does-a-template-with-two-parts parser-reads-as-acorn-does-an-export-under-a-string-name parser-reads-as-acorn-does-an-object-s-computed-short-and-literal-keys parser-reads-as-acorn-does-proto-keys-that-do-not-clash parser-to-estree-object-reads-each-option-as-acorn-builds-a-module parser-to-estree-object-reads-each-option-as-acorn-builds-an-older-edition parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue-2 parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue-3 parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue-4 parser-tokenizer-directive-prologues-should-read-a-script-strict-as-acorn-does-from-its-prologue parser-tokenizer-directive-prologues-should-report-tokens-into-an-array-as-acorn-does syntax-tree-keeps-its-records-when-a-token-callback-parses-again-2 syntax-tree-keeps-its-records-when-a-token-callback-parses-again syntax-tree-of-another-parsers-estree webpack-parser-reports-the-lines-and-columns-of-comments-on-many-lines webpack-parser-reports-the-lines-and-columns-of-tokens-and-comments webpack-parser-reports-the-lines-and-columns-of-tokens-on-one-long-line webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-2 webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-3 webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-4 webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-5 webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-6 webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at-7 webpack-parser-writing-a-syntax-tree-builds-acorn-s-objects-for-the-edition-a-tree-was-read-at
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +5Lines changed: 5 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +"webpack": patch | | 3 | +--- | | 4 | + | | 5 | +Build ESTree objects only with `toEstreeObject` and `fromEstreeObject`. |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -159,8 +159,8 @@ Conventions every language keeps, and a new member follows: | 159 | 159 |
| 160 | 160 | ## JavaScript | 161 | 161 |
| 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. | 164 | 164 | - **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. | 165 | 165 |
| 166 | 166 | ```js | |
Collapse file Copy file name to clipboardExpand all lines: lib/html/moduleSyntax.js +17-20Lines changed: 17 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,27 +17,24 @@ const getJavascriptSyntaxParser = memoize(() => | 17 | 17 | * @returns {boolean} true, when it uses `import`, `export`, `import.meta` or top-level `await` | 18 | 18 | */ | 19 | 19 | 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(); | 37 | 35 | return true; | 38 | | - } catch (_err) { | 39 | | - return false; | 40 | | - } | | 36 | + }; | | 37 | + return !parsesAs("script") && parsesAs("module"); | 41 | 38 | }; | 42 | 39 |
| 43 | 40 | const NOMODULE_MODULE_SYNTAX_MESSAGE = | |
Collapse file Copy file name to clipboardExpand all lines: lib/javascript/JavascriptParser.js +23-2Lines changed: 23 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,14 +23,12 @@ const { | 23 | 23 | Field, | 24 | 24 | Flag, | 25 | 25 | LEGACY_ASSERT_ATTRIBUTES, | 26 | | - MAX_MASKED_INDEX, | 27 | 26 | NodeType, | 28 | 27 | Part, | 29 | 28 | ScopeLog, | 30 | 29 | fromEstreeObject, | 31 | 30 | hasOctalEscape, | 32 | 31 | isIdentifierChar, | 33 | | - overflowFrom, | 34 | 32 | parseProgram, | 35 | 33 | syntaxTreeOfEstree, | 36 | 34 | takeParsedTree | @@ -1313,6 +1311,29 @@ const holdsStatementNode = (tree, owner, statement) => { | 1313 | 1311 | ); | 1314 | 1312 | }; | 1315 | 1313 |
| | 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 | + | 1316 | 1337 | /** | 1317 | 1338 | * `collectBlockDeclarations` for a list of statements of the tree, by id. | 1318 | 1339 | * @param {SyntaxTree} tree the tree | |
Collapse file Copy file name to clipboardExpand all lines: lib/javascript/data.js +15-14Lines changed: 15 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -477,22 +477,23 @@ const Part = { | 477 | 477 | phase: 6 | 478 | 478 | }; | 479 | 479 |
| 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`. | 482 | 483 | /** @type {Record<string, string>} */ | 483 | 484 | const NODE_FIELDS = { | 484 | 485 | Identifier: "name:string:0", | 485 | 486 | PrivateIdentifier: "name:string:0", | 486 | 487 | Literal: "value:value:0 raw:string:1", | 487 | 488 | 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", | 490 | 491 | NewExpression: "callee:node:0 arguments:list:1", | 491 | | - ExpressionStatement: "expression:node:0 directive:optional:1", | | 492 | + ExpressionStatement: "expression:node:0 directive:optional:1:5", | 492 | 493 | VariableDeclarator: "id:node:0 init:node:1", | 493 | 494 | VariableDeclaration: "declarations:list:0 kind:string:1", | 494 | 495 | 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", | 496 | 497 | BinaryExpression: "left:node:0 operator:string:1 right:node:2", | 497 | 498 | LogicalExpression: "left:node:0 operator:string:1 right:node:2", | 498 | 499 | AssignmentExpression: "operator:string:2 left:node:0 right:node:1", | @@ -518,11 +519,11 @@ const NODE_FIELDS = { | 518 | 519 | EmptyStatement: "", | 519 | 520 | DebuggerStatement: "", | 520 | 521 | 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", | 522 | 523 | 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", | 524 | 525 | 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", | 526 | 527 | IfStatement: "test:node:0 consequent:node:1 alternate:node:2", | 527 | 528 | ConditionalExpression: "test:node:0 consequent:node:1 alternate:node:2", | 528 | 529 | ParenthesizedExpression: "expression:node:0", | @@ -537,7 +538,7 @@ const NODE_FIELDS = { | 537 | 538 | SwitchStatement: "discriminant:node:0 cases:list:1", | 538 | 539 | ForStatement: "init:node:0 test:node:1 update:node:2 body:node:3", | 539 | 540 | 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", | 541 | 542 | WhileStatement: "test:node:0 body:node:1", | 542 | 543 | DoWhileStatement: "body:node:0 test:node:1", | 543 | 544 | WithStatement: "object:node:0 body:node:1", | @@ -550,16 +551,16 @@ const NODE_FIELDS = { | 550 | 551 | ClassExpression: "id:node:0 superClass:node:1 body:node:2", | 551 | 552 | ChainExpression: "expression:node:0", | 552 | 553 | 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", | 554 | 555 | ImportSpecifier: "imported:node:0 local:node:1", | 555 | 556 | ImportDefaultSpecifier: "local:node:0", | 556 | 557 | ImportNamespaceSpecifier: "local:node:0", | 557 | 558 | 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", | 559 | 560 | 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", | 561 | 562 | 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", | 563 | 564 | ExportSpecifier: "local:node:0 exported:node:1", | 564 | 565 | MetaProperty: "meta:node:0 property:node:1" | 565 | 566 | }; | |
|