Repository navigation
Commit 55c9808
authored
refactor(js): own the ECMAScript parser and drop the acorn dependency (#22042)
* refactor(js): own the ECMAScript parser and drop the acorn dependency
`lib/javascript/parser.js` is webpack's own port of acorn 8.18.0, so nothing
under `lib/` requires acorn at runtime and the package ships one dependency
fewer. acorn stays a devDependency: the parity suites and
`tooling/generate-js-data.js` still read it.
Owning the parser pays for itself at load: the `\p{...}` property tables move
to `lib/javascript/regexpData.js` and are required only when a pattern the
engine itself rejected reaches the validator, the identifier ranges decode
lazily from run-length tables, and the destructuring-errors class is exported
rather than captured by parsing through a probe at module load.
* perf(js): load the regexp validator only when a pattern needs it
`readRegexp` builds the literal's `RegExp` and lets the host engine's verdict
stand, so the ported validator is reached only for a pattern that engine
rejected. It moves to `lib/javascript/regexp.js` and is installed on the
parser's prototype the first time that happens, taking its Unicode property
tables and 1,700 lines of `lib/javascript/parser.js` off what every webpack
process reads and compiles.
* perf(js): install the grammar only when something asks to parse
A webpack build is served entirely by `lib/javascript/syntax.js`, so the
187 base productions and the regexp validator are now separate modules
copied onto the parser's prototype on first use.
* perf(js): resolve a member expression once and walk it without closures
The walker re-resolved a member expression when the walk and an evaluate
met on the same node, and built three arrays, three closures and four
objects per hit whether or not anything read them. Resolve into instance
slots, keyed on the node and its scope, and build the public info object
only for callers that ask for it. The binary and unary evaluators no
longer allocate their helpers per evaluation.
* perf(js): dispatch a member-chain hook only when one is registered
The call, assignment and unhandled member-chain dispatches built their
argument arrays and member ranges for every chain, and found a hook for
fewer than one in ten.
* perf(js): probe the word cache in one line and track parameter clashes in a Set
The word cache kept its word, type and owner in three arrays, so a probe
touched three cache lines and verified a hit through a builtin call; one
interleaved array and a char loop serve it from one line. Parameter names
were checked for clashes through hasOwnProperty on a null-prototype
object; a pooled Set does the same lookup without dictionary mode.
* perf(js): memoize a word's reserved kind so plain identifiers skip checkUnreserved
Every identifier ran checkUnreserved's length and first-char gates and, for
the short lowercase majority, a Map lookup. The word cache already
classifies each word once; it now records the reserved kind beside the
token type, and parseIdent skips the check when it is zero.
* fix(js): give the `import` of import.meta its start location
The owned parseExprImport built the meta identifier without the start
position acorn passes along, so with locations on it carried only an end.
The build never tracks locations, which is why the parity sweeps missed it.
* fix(js): validate a regexp literal below ES2018 without property tables
The base parser built the `\p{...}` name tables for every edition, and
editions before 2018 have none, so any regexp literal threw at ecmaVersion
5 through 2017. acorn keeps them undefined there and never reads them; so
does the port now. The new unit test holds the validator to acorn's verdict
over a pattern corpus at every edition.
* test(js): hold the base parser to acorn over a program corpus
Drive Parser.parse, parseExpressionAt, tokenizer, extend and the line,
identifier and option helpers against acorn across editions, source
types and option sets, so the paths a build never reaches stay covered.
* test(js): teach cspell the script names in the regexp corpus
* test(js): malform the pattern in the flag-order regexp test
With a valid pattern the assertions passed whichever check ran first;
an unterminated group makes the flag error prove its precedence, and a
valid flag set shows the pattern error surfacing afterwards.
* test(js): reach the grammar's remaining branches from the parity corpus
Explicit resource management, pattern and export edge cases, template and
number tokens, the start-location option and the semicolon and trailing
comma callbacks are now held to acorn as well, which leaves grammar.js and
parser.js at 99.5% line coverage from the unit suites alone.
* test(side-effects): probe symlink support at a per-process path
ConfigTestCases, ConfigCacheTestCases and RoundTripConfigCases run this
filter in separate workers at the same instant, and all three raced on
one probe path between removing and recreating it, which surfaced as an
EEXIST that failed the whole suite.
* perf(js): hold the grammar module once it is installed
The four grammar stubs resolved the module with require() on every call,
so the tokenizer entry point paid the loader's lookup per token and read
seven times slower than main. The module is kept after the first install
and the stubs delegate through it.
* perf(js): let the grammar take the stubs' slots once installed
`install()` skips members the parser prototype already owns, so the four
stubs stayed in place and every token still paid a stub frame, the install
check and a `Function.prototype.call` on its way to the grammar's
`getToken`, which read 10% slower than main on the tokenize benchmarks.
The stubs now hand their slots to the grammar's own methods after the
first install.
* chore(js): drop the grammar's unreferenced typedef imports
eslint-config-webpack 4.12.0 rejects import tags nothing references (webpack/no-unused-import-tag).1 parent e1ab6f2 commit 55c9808
23 files changed
Lines changed: 13381 additions & 2314 deletions
File tree
- .changeset
- lib/javascript
- test
- configCases/side-effects/nested-package-json-sideEffects-symlinked
- fixtures/parser-no-acorn
- tooling
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| |||
573 | 573 | | |
574 | 574 | | |
575 | 575 | | |
| 576 | + | |
576 | 577 | | |
577 | | - | |
| 578 | + | |
578 | 579 | | |
579 | 580 | | |
580 | 581 | | |
| |||
586 | 587 | | |
587 | 588 | | |
588 | 589 | | |
589 | | - | |
| 590 | + | |
590 | 591 | | |
591 | 592 | | |
592 | 593 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
577 | 577 | | |
578 | 578 | | |
579 | 579 | | |
| 580 | + | |
580 | 581 | | |
581 | 582 | | |
582 | 583 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
305 | 305 | | |
306 | 306 | | |
307 | 307 | | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
308 | 320 | | |
309 | 321 | | |
310 | 322 | | |
| |||
0 commit comments