Skip to content

Commit e8b9ec1

Browse files
test(javascript): record where terser refuses or misreads what the printer reads
Reading a source with webpack's parser rather than terser's ported one leaves 757 differences over the comparison corpora, none of them webpack's. 704 are a source the spec allows and terser refuses: an escape anywhere in an identifier name, a trailing comma in an argument list, a regular expression after the ogham space mark, `let` as a name outside strict mode, `yield` naming a function inside a generator, `export * as default from`, a field named `get` before a generator, and an escape no cooked template value can read. The other 53 are bytes where terser reads or prints the program differently: a `let` or `async` before a line break, which the semicolon inserted after it leaves a name; a line continuation's own break written into the string it holds nothing of; and a binary, octal or hex property key quoted as it is written, naming the property `0b1` where it is `1`. `REFUSED_BY_REFERENCE` lists the parse errors with what makes each source legal and `REFERENCE_MISPRINTS` the cases by name. Both fail where the reference stops differing, and an accepted output still runs against what its test prints.
1 parent bee7768 commit e8b9ec1

2 files changed

Lines changed: 166 additions & 4 deletions

File tree

‎docs/syntax.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ Read this before touching the CSS, HTML or JavaScript parser, printer or minifie
3838

3939
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser for its `parse` phase, which reads an asset with it — building the printer's tree as each statement finishes, rather than converting a tree. `parse.webpackParser` defaults to true and turns that off; terser's own parser reads what it reads its own way (`parse.expression`, `parse.strict`, TypeScript, a line opening an HTML comment) and whatever webpack's parser refuses.
4040
- `syntax-parser.js` — tokenizer, acorn-derived core and every production in one file (any parse reaches the productions, and the coming struct-of-arrays rewrite moves node creation through them). `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.
41-
- `syntax-printer.js` — webpack's JavaScript minifier, terser's design ported to write byte for byte what terser writes; `jsMinify.js` (the `minify` the default minimizer dispatches JS to) goes through it for every asset; terser is a devDependency the tests compare against, and nothing in `lib/` loads it. `createModules` builds what terser's modules exported — the node classes from `nodeClasses()` (`createAst`), the parser (`createLenientParser`), the compressor and the output stream — and each entry of `PHASES` installs one part onto them: add phases there and nowhere else. `correct` wraps the methods with which terser writes a wrong program and changes only that output; `corrections.enabled` switches it off. `improve` writes less where terser's longer program provably runs the same — after compressing, a statement calling a function in place with nothing passed runs its body there — and `improvements.enabled` switches it off. The `parse` phase builds the tree from `syntax-parser.js`, leaving to the lenient parser a source read unlike the spec; `syntax-printer-terser-parser.unittest.js` holds the ported parser to terser's own and `syntax-printer-estree.unittest.js` the ESTree conversion. `test/specCases/minify-corpora.spectest.js` holds the whole printer to terser's output, errors included, over each corpus in its `CORPORA` list (add a new one there) under the option sets in `OPTION_SETS`, and runs each output whose test states what its input prints (terser's `expect_stdout`, swc's `expected.stdout` and `exec.rs`) in terser's sandbox. A byte difference is accepted only where switching the `correct` and `improve` phases off gives terser's bytes back, and fails where `improve` writes more than without it; what it fixes is listed in `CORRECTED` (printing right with the phase, as terser without), and an output misprinting as terser's by an option's own contract in `INHERITED`, each with the reason — an entry that stops matching fails until retired. Each output is also sized against swc's recorded one, a fixture's `output.js` or, for one of terser's cases, what swc's port of it records (gzip decides, raw breaks a tie): one bigger fails unless `test/helpers/swcSmaller.js` lists it under what swc does that webpack does not yet, and a listed one no longer bigger fails until retired. Under the default minimizer's options each source of every corpus but test262 is also minified by `oxc-minify`, held the same way to `test/helpers/oxcSmaller.js`; an oxc output that misprints is no lead. `JS_MINIFY_REPORT=<file>` tallies where webpack's is worse, the same and better. terser's corpus submodule is pinned to the `terser` devDependency — bump both together, and regenerate `data.js`.
41+
- `syntax-printer.js` — webpack's JavaScript minifier, terser's design ported to write byte for byte what terser writes; `jsMinify.js` (the `minify` the default minimizer dispatches JS to) goes through it for every asset; terser is a devDependency the tests compare against, and nothing in `lib/` loads it. `createModules` builds what terser's modules exported — the node classes from `nodeClasses()` (`createAst`), the parser (`createLenientParser`), the compressor and the output stream — and each entry of `PHASES` installs one part onto them: add phases there and nowhere else. `correct` wraps the methods with which terser writes a wrong program and changes only that output; `corrections.enabled` switches it off. `improve` writes less where terser's longer program provably runs the same — after compressing, a statement calling a function in place with nothing passed runs its body there — and `improvements.enabled` switches it off. The `parse` phase builds the tree from `syntax-parser.js`, leaving to the lenient parser a source read unlike the spec; `syntax-printer-terser-parser.unittest.js` holds the ported parser to terser's own and `syntax-printer-estree.unittest.js` the ESTree conversion. `test/specCases/minify-corpora.spectest.js` holds the whole printer to terser's output, errors included, over each corpus in its `CORPORA` list (add a new one there) under the option sets in `OPTION_SETS`, and runs each output whose test states what its input prints (terser's `expect_stdout`, swc's `expected.stdout` and `exec.rs`) in terser's sandbox. A byte difference is accepted only where switching the `correct` and `improve` phases off gives terser's bytes back, and fails where `improve` writes more than without it; what it fixes is listed in `CORRECTED` (printing right with the phase, as terser without), and an output misprinting as terser's by an option's own contract in `INHERITED`, each with the reason — an entry that stops matching fails until retired. Where the reference instead refuses a source the spec allows, the parse error it answers with is listed in `REFUSED_BY_REFERENCE` with what makes the source legal; where it reads or prints one differently, the case is listed in `REFERENCE_MISPRINTS` with what it gets wrong — a listed one webpack must still run correctly, and one the reference stops differing on fails until retired. Each output is also sized against swc's recorded one, a fixture's `output.js` or, for one of terser's cases, what swc's port of it records (gzip decides, raw breaks a tie): one bigger fails unless `test/helpers/swcSmaller.js` lists it under what swc does that webpack does not yet, and a listed one no longer bigger fails until retired. Under the default minimizer's options each source of every corpus but test262 is also minified by `oxc-minify`, held the same way to `test/helpers/oxcSmaller.js`; an oxc output that misprints is no lead. `JS_MINIFY_REPORT=<file>` tallies where webpack's is worse, the same and better. terser's corpus submodule is pinned to the `terser` devDependency — bump both together, and regenerate `data.js`.
4242
- `yarn test:minify-corpora` — webpack's JS minifier vs the published one it replaces, byte for byte (errors included), over every JS corpus: terser's own `test/compress` and `test/input`, test262, swc's minifier tests and oxc's; where a test states what its input prints, the output is also run and must print it. Each output must also be no bigger than swc's recorded one unless `SWC_SMALLER` lists why, and every output outside test262 no bigger than `oxc-minify`'s unless `OXC_SMALLER` does; `JS_MINIFY_REPORT=<file>` tallies worse, the same and better.

‎test/specCases/minify-corpora.spectest.js‎

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,137 @@ const CORRECTED = {
998998
"terser drops destructuring defaults and patterns whose evaluation has effects or throws"
999999
};
10001000

1001+
/* cspell:disable */
1002+
// What the reference gets wrong, or how its output and webpack's agree, where
1003+
// several sources differ for the same reason.
1004+
const ASI_LET =
1005+
"terser reads a `let` before a line break as a declaration, where the semicolon inserted after it leaves a name";
1006+
const ASI_ASYNC =
1007+
"terser reads an `async` before a line break as the modifier, where the semicolon inserted after it leaves a name";
1008+
const CONTINUATION =
1009+
"terser writes a line continuation's own break into the string, which holds nothing";
1010+
const NUMERIC_KEY =
1011+
"terser quotes a binary, octal or hex key as it is written, naming the property `0b1` where it is `1`";
1012+
const RAW_BREAK =
1013+
"a tagged template's raw text reads either break as one line feed, and webpack writes the feed itself";
1014+
const ESCAPED_DIRECTIVE =
1015+
"webpack keeps the escape that stops the string being a directive, where terser unescapes it and detaches it with `;`";
1016+
const EMPTY_IMPORT =
1017+
"webpack writes an import of nothing without the empty list, which imports the same";
1018+
const EMPTY_ATTRIBUTES =
1019+
"webpack writes an import without the empty attributes, which asks for the same";
1020+
const AWAIT_IDENTIFIER =
1021+
"terser reads `await` in a class field as the operator, where a script may hold it as a name";
1022+
1023+
/**
1024+
* Parse errors the reference answers with where the spec allows the source,
1025+
* each with what makes it legal: webpack reads these and prints, and the output
1026+
* is still run against what the test says it prints. A message the reference
1027+
* stops answering with fails until retired.
1028+
* @type {Record<string, string>}
1029+
*/
1030+
const REFUSED_BY_REFERENCE = {
1031+
"Escaped characters are not allowed in keywords":
1032+
"an escape is allowed anywhere in an identifier name; only a keyword read as one may not hold it",
1033+
"Unexpected token: punc ())":
1034+
"an argument list may end in a comma, `import()`'s included",
1035+
"Unexpected token: operator (/)":
1036+
"a regular expression may follow any of the spec's white space, which the reference reads as division",
1037+
"Unexpected character '\u1680'":
1038+
"the ogham space mark is white space, which the reference does not read as any",
1039+
"Name expected":
1040+
"`let` is an identifier outside strict mode, so it names a binding and a property",
1041+
"Yield cannot be used as identifier inside generators":
1042+
"a function expression or declaration inside a generator may be named `yield`",
1043+
"Name or string expected": "`export * as default from` names the namespace",
1044+
"Unexpected token: keyword (if)":
1045+
"a class or function exported as the default ends at the semicolon inserted after it",
1046+
"Unexpected token: name (a)":
1047+
"a field named `get` or `set` ends at the semicolon inserted before a generator",
1048+
"Unicode reference out of bounds":
1049+
"a tagged template may hold an escape no cooked value can read, which only its raw text keeps",
1050+
"Unexpected token: keyword (let)":
1051+
"a rest parameter may be named `yield` outside strict mode",
1052+
"Unexpected token: operator (>)":
1053+
"Annex B reads this as the identifier it is, not an operator",
1054+
"Strict mode may not include a with statement":
1055+
"the corpus reads this module as a script, where `with` is allowed"
1056+
};
1057+
1058+
/**
1059+
* Sources the reference reads or prints differently, each with what it gets
1060+
* wrong or how the two agree; webpack's output stands, and is still run against
1061+
* what the test says it prints. An entry that stops differing fails until
1062+
* retired.
1063+
* @type {Record<string, string>}
1064+
*/
1065+
const REFERENCE_MISPRINTS = {
1066+
"test262: let-identifier-with-newline.js (printing alone)": ASI_LET,
1067+
"test262: let-identifier-with-newline.js (the default minimizer's options)":
1068+
ASI_LET,
1069+
"test262: let-block-with-newline.js (printing alone)": ASI_LET,
1070+
"test262: let-block-with-newline.js (the default minimizer's options)":
1071+
ASI_LET,
1072+
"test262: syntax-declaration-no-line-terminator.js (printing alone)": ASI_ASYNC,
1073+
"test262: syntax-declaration-no-line-terminator.js (the default minimizer's options)":
1074+
ASI_ASYNC,
1075+
"test262: escaped-async-line-terminator.js (printing alone)": ASI_ASYNC,
1076+
"test262: escaped-async-line-terminator.js (the default minimizer's options)":
1077+
ASI_ASYNC,
1078+
"test262: async-lineterminator-identifier-throws.js (printing alone)":
1079+
ASI_ASYNC,
1080+
"test262: async-lineterminator-identifier-throws.js (the default minimizer's options)":
1081+
ASI_ASYNC,
1082+
"test262: line-continuation-double.js (printing alone)": CONTINUATION,
1083+
"test262: line-continuation-double.js (the default minimizer's options)":
1084+
CONTINUATION,
1085+
"test262: line-continuation-single.js (printing alone)": CONTINUATION,
1086+
"test262: line-continuation-single.js (the default minimizer's options)":
1087+
CONTINUATION,
1088+
"test262: property-name.js (printing alone)": NUMERIC_KEY,
1089+
"test262: property-name.js (the default minimizer's options)": NUMERIC_KEY,
1090+
"test262: special-characters.js (printing alone)": RAW_BREAK,
1091+
"test262: special-characters.js (the default minimizer's options)": RAW_BREAK,
1092+
"test262: tv-line-continuation.js (printing alone)": RAW_BREAK,
1093+
"test262: tv-line-continuation.js (the default minimizer's options)": RAW_BREAK,
1094+
"test262: 14.1-4-s.js (printing alone)": ESCAPED_DIRECTIVE,
1095+
"test262: 14.1-5-s.js (printing alone)": ESCAPED_DIRECTIVE,
1096+
"test262: eval-rqstd-order.js (printing alone)": EMPTY_IMPORT,
1097+
"test262: eval-rqstd-order.js (the default minimizer's options)": EMPTY_IMPORT,
1098+
"test262: eval-self-once.js (printing alone)": EMPTY_IMPORT,
1099+
"test262: eval-self-once.js (the default minimizer's options)": EMPTY_IMPORT,
1100+
"test262: instn-once.js (printing alone)": EMPTY_IMPORT,
1101+
"test262: instn-once.js (the default minimizer's options)": EMPTY_IMPORT,
1102+
"test262: instn-resolve-empty-import.js (printing alone)": EMPTY_IMPORT,
1103+
"test262: instn-resolve-empty-import.js (the default minimizer's options)":
1104+
EMPTY_IMPORT,
1105+
"oxc minifier: peephole/remove_unused_declaration.rs:1051 (printing alone)":
1106+
EMPTY_IMPORT,
1107+
"oxc minifier: peephole/remove_unused_declaration.rs:1051 (the default minimizer's options)":
1108+
EMPTY_IMPORT,
1109+
"oxc minifier: peephole/remove_unused_declaration.rs:1058 (printing alone)":
1110+
EMPTY_IMPORT,
1111+
"oxc minifier: peephole/remove_unused_declaration.rs:1058 (the default minimizer's options)":
1112+
EMPTY_IMPORT,
1113+
"test262: import-attribute-empty.js (printing alone)": EMPTY_ATTRIBUTES,
1114+
"test262: import-attribute-empty.js (the default minimizer's options)":
1115+
EMPTY_ATTRIBUTES,
1116+
"test262: await-identifier-script.js (printing alone)": AWAIT_IDENTIFIER,
1117+
"test262: await-identifier-script.js (the default minimizer's options)":
1118+
AWAIT_IDENTIFIER,
1119+
"test262: await-identifier-module-2.js (printing alone)": AWAIT_IDENTIFIER,
1120+
"test262: await-identifier-module-2.js (the default minimizer's options)":
1121+
AWAIT_IDENTIFIER
1122+
};
1123+
/* cspell:enable */
1124+
1125+
// What this run met of the two tables above, so an entry it no longer meets
1126+
// fails instead of lingering.
1127+
/** @type {Set<string>} */
1128+
const refusalsSeen = new Set();
1129+
/** @type {Set<string>} */
1130+
const misprintsSeen = new Set();
1131+
10011132
/**
10021133
* Outputs the `improve` phase writes shorter yet gzip compresses worse, each
10031134
* with why; an entry that stops compressing worse fails until retired.
@@ -1524,9 +1655,29 @@ describe("JavaScript minifier", () => {
15241655
theirs.code !== uncorrected.code ||
15251656
theirs.error !== uncorrected.error
15261657
) {
1527-
differences.push(
1528-
`${source.name} (${setName})\n\treference: ${JSON.stringify(theirs)}\n\twebpack: ${JSON.stringify(uncorrected)}`
1529-
);
1658+
const refused =
1659+
uncorrected.code !== undefined &&
1660+
typeof theirs.error === "string" &&
1661+
Object.prototype.hasOwnProperty.call(
1662+
REFUSED_BY_REFERENCE,
1663+
theirs.error
1664+
);
1665+
if (refused) {
1666+
refusalsSeen.add(
1667+
/** @type {string} */ (theirs.error)
1668+
);
1669+
} else if (
1670+
Object.prototype.hasOwnProperty.call(
1671+
REFERENCE_MISPRINTS,
1672+
key
1673+
)
1674+
) {
1675+
misprintsSeen.add(key);
1676+
} else {
1677+
differences.push(
1678+
`${source.name} (${setName})\n\treference: ${JSON.stringify(theirs)}\n\twebpack: ${JSON.stringify(uncorrected)}`
1679+
);
1680+
}
15301681
}
15311682
if (unimproved.error !== ours.error) {
15321683
differences.push(
@@ -1631,4 +1782,15 @@ describe("JavaScript minifier", () => {
16311782
}
16321783
});
16331784
}
1785+
1786+
it("should list no difference the reference no longer makes", () => {
1787+
expect([
1788+
...Object.keys(REFUSED_BY_REFERENCE)
1789+
.filter((message) => !refusalsSeen.has(message))
1790+
.map((message) => `REFUSED_BY_REFERENCE: ${message}`),
1791+
...Object.keys(REFERENCE_MISPRINTS)
1792+
.filter((key) => !misprintsSeen.has(key))
1793+
.map((key) => `REFERENCE_MISPRINTS: ${key}`)
1794+
]).toEqual([]);
1795+
});
16341796
});

0 commit comments

Comments
 (0)