Skip to content

Commit f91f72d

Browse files
feat(syntax): read structure through Field and Flag on the node path (#22579)
* feat(syntax): read structure through Field and Flag on the node path CSS and HTML paths gain field, fieldCount, fieldAt, fieldNamed and flag, read with the Field and Flag enums each language exports beside NodeType. They replace declarations, rules and important on CSS and attributeCount, attribute, findAttribute, templateContent and selfClosing on HTML; the CSS minifier keeps reading a block as an array through internal readers. * fix(syntax): answer fields and flags only for the node that owns them A CSS leaf's id can be a reused declaration's, whose important bit stays set, so flag now answers for containers only. HTML reads the attribute run of an element only, and both fieldAt reject a negative index. * feat(syntax): type each visitor's path by the node types it visits SourceProcessor takes a language's parts table, and a visitor registered for some node types gets a path whose field, fieldCount, fieldAt, fieldNamed and flag accept only the parts those types have. Reading attributes in a Comment visitor, or off an attribute, fails to type-check; a node of unknown type still takes any part. * refactor(syntax): read fields with field(i, F) and part positions with rangeOf(Part) A single-node field reads as a list of at most one, so `field(i, F, n)` replaces `field` and `fieldAt`, matching `child(i, n)`. `rangeOf(Part, n)` replaces `nameRange`, `valueRange`, `contentRange` and `blockRange`, typed by node type like fields and flags. * test(css): cover folding an !important longhand into its shorthand * refactor(syntax): read every grammar-specific detail through generic members A node path has no member of its own per language: an element's namespace and CSS's type flags are flags, an element's tags and a doctype's identifiers are parts read with rangeOf and textOf, a dimension's unit is its name and a simple block's bracket its value, and within(type) replaces inValue for any ancestor type. * test(syntax): cover the parts a node lacks and a spaced ICSS import * feat(syntax): hand every visitor the run's state as path.state, typed by the processor process(src, { state }) sets path.state for the length of the run and puts the caller's back after. The processor's TState types it in every visitor, array, literal map and node printer alike, and process takes only that shape; each language's visitor map is generic in it. * refactor(syntax): read nodes in CssParser and HtmlParser through the public path only Both parsers now use the members a plugin has, nothing from the parser's internals. Positions stay allocation-free through start/end and startOf/endOf(Part), beside acorn's range; children(n) returns a node's children as an array. A streamed rule records its block start before its enter runs, and under skip.text a raw-text body ends at its end tag even when that tag has attributes.
1 parent 6ee97b8 commit f91f72d

27 files changed

Lines changed: 3874 additions & 1308 deletions

‎docs/syntax.md‎

Lines changed: 52 additions & 19 deletions
Large diffs are not rendered by default.

‎lib/css/CssParser.js‎

Lines changed: 308 additions & 317 deletions
Large diffs are not rendered by default.

‎lib/css/syntax-parser.js‎

Lines changed: 385 additions & 145 deletions
Large diffs are not rendered by default.

‎lib/css/syntax-printer.js‎

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,15 @@ const {
171171
_atKeyword,
172172
_atKeywordOf,
173173
_blockSpans,
174+
_blockTokenOf,
174175
_canonicalSelectorList,
175-
_childList,
176176
_closeAtEof,
177177
_collapseBox,
178178
_composeBlockBody,
179179
_customMedia,
180180
_customPropertyValue,
181181
_customSelectors,
182+
_declarationsOf,
182183
_dropFamilyInitials,
183184
_dropImpliedUniversalSelector,
184185
_dropZeroLengthUnit,
@@ -193,6 +194,7 @@ const {
193194
_isHexDigit,
194195
_isIdentCodePoint,
195196
_isIdentLike,
197+
_isImportant,
196198
_isLetter,
197199
_isUnterminatedUrl,
198200
_join,
@@ -217,6 +219,7 @@ const {
217219
_roundSignificant,
218220
_ruleEntryOf,
219221
_rulePrelude,
222+
_rulesOf,
220223
_setHoistedRuns,
221224
_setRuleEntry,
222225
_shortestAuthoredFlex,
@@ -2248,10 +2251,10 @@ let _nestedRuns = [];
22482251
* @returns {boolean} true when no declaration follows a rule
22492252
*/
22502253
const _declarationsPrecedeRules = (path, rule) => {
2251-
const decls = path.declarations(rule);
2254+
const decls = _declarationsOf(rule);
22522255
if (decls === null || decls.length === 0) return true;
22532256
// The rule asking holds this one, so the list it is in is never empty.
2254-
const rules = /** @type {Rule[]} */ (path.rules(rule));
2257+
const rules = /** @type {Rule[]} */ (_rulesOf(rule));
22552258
let lastDecl = -1;
22562259
for (let i = 0; i < decls.length; i++) {
22572260
const at = _startOf(decls[i]);
@@ -4762,7 +4765,7 @@ const printer = (path, writer) => {
47624765
// A declaration value, a media feature or a `@supports` test — elsewhere a
47634766
// prelude number is An+B, as it is in a tested `selector()`.
47644767
return minify &&
4765-
(path.inValue() ||
4768+
(path.within(T_DECLARATION) ||
47664769
_S._inMediaConditionPrelude ||
47674770
(_S._inSupportsPrelude && !_S._inSupportsSelector))
47684771
? _normalizeNumericToken(raw, _isRgbChannel(path))
@@ -4780,7 +4783,7 @@ const printer = (path, writer) => {
47804783
// so a squeezed whitespace run asks another question. Nothing is rewritten.
47814784
if (
47824785
minify &&
4783-
!path.inValue() &&
4786+
!path.within(T_DECLARATION) &&
47844787
_S._inMediaConditionPrelude &&
47854788
equalsLowerCase(name, "style")
47864789
) {
@@ -4809,7 +4812,7 @@ const printer = (path, writer) => {
48094812
const trim =
48104813
_S._mathFunctionDepth !== 0
48114814
? _TRIM_MATH
4812-
: path.inValue()
4815+
: path.within(T_DECLARATION)
48134816
? _TRIM_NOTHING
48144817
: SELECTOR_FUNCTIONS.has(lower)
48154818
? _TRIM_COMBINATORS
@@ -4862,15 +4865,15 @@ const printer = (path, writer) => {
48624865
out = _S._transforms.rewriteEscapes ? escapeName(out) : written;
48634866
}
48644867
// An+B in a selector: `2n+1` is what `odd` names, and shorter written so.
4865-
if (!path.inValue() && NTH_PSEUDO_FUNCTIONS.has(fn)) {
4868+
if (!path.within(T_DECLARATION) && NTH_PSEUDO_FUNCTIONS.has(fn)) {
48664869
return _S._inSupportsSelector
48674870
? `${out}(${inner})`
48684871
: _minifyAnPlusB(out, inner);
48694872
}
48704873
// Every rewrite below is keyed by a name none of these carry.
48714874
if (
48724875
SUBSTITUTION_FUNCTIONS.has(fn) ||
4873-
(!path.inValue() && SELECTOR_FUNCTIONS.has(fn))
4876+
(!path.within(T_DECLARATION) && SELECTOR_FUNCTIONS.has(fn))
48744877
) {
48754878
return `${out}(${inner})`;
48764879
}
@@ -4910,7 +4913,7 @@ const printer = (path, writer) => {
49104913
if (
49114914
_S._loweringLightDark &&
49124915
fn === "light-dark" &&
4913-
path.inValue() &&
4916+
path.within(T_DECLARATION) &&
49144917
!_S._inCustomProperty &&
49154918
!_S._inSupportsPrelude &&
49164919
!_S._inSubstitutedValue
@@ -4965,7 +4968,7 @@ const printer = (path, writer) => {
49654968
if (
49664969
fn === "calc" &&
49674970
_S._mathFunctionDepth > 1 &&
4968-
path.inValue() &&
4971+
path.within(T_DECLARATION) &&
49694972
_S._transforms.reduceFunctions
49704973
) {
49714974
return `(${reduced === null ? inner : reduced})`;
@@ -4975,15 +4978,15 @@ const printer = (path, writer) => {
49754978
// An easing function is only ever a value; `url()` is also an at-rule
49764979
// prelude's (`@import url("a.css")`).
49774980
const easing =
4978-
path.inValue() && !_S._inCustomProperty
4981+
path.within(T_DECLARATION) && !_S._inCustomProperty
49794982
? _minifyEasingFunction(fn, inner)
49804983
: null;
49814984
if (easing !== null) return easing;
49824985
// A translation's percentage is of the element's own box, which always
49834986
// has a size, so a `0%` term beside one length adds nothing to it.
49844987
if (
49854988
_S._transforms.reduceFunctions &&
4986-
path.inValue() &&
4989+
path.within(T_DECLARATION) &&
49874990
!_S._inSubstitutedValue &&
49884991
fn.startsWith("translate") &&
49894992
inner.includes("0%")
@@ -5013,14 +5016,14 @@ const printer = (path, writer) => {
50135016
: undefined;
50145017
if (
50155018
oneAxis !== undefined &&
5016-
path.inValue() &&
5019+
path.within(T_DECLARATION) &&
50175020
!_S._inSubstitutedValue &&
50185021
!inner.includes(",") &&
50195022
!_hasSubstitution(inner)
50205023
) {
50215024
return `${oneAxis}(${inner})`;
50225025
}
5023-
if (path.inValue() && !_S._inSubstitutedValue) {
5026+
if (path.within(T_DECLARATION) && !_S._inSubstitutedValue) {
50245027
// An argument that is the amount an omitted one means says nothing,
50255028
// whether or not the zero still carries the unit its grammar drops.
50265029
const omitted = _S._transforms.reduceFunctions
@@ -5064,7 +5067,7 @@ const printer = (path, writer) => {
50645067
// A zero angle needs no unit where the grammar names `<zero>` beside
50655068
// `<angle>` (CSS Transforms 2, Filter Effects 1).
50665069
if (
5067-
path.inValue() &&
5070+
path.within(T_DECLARATION) &&
50685071
!_S._inSubstitutedValue &&
50695072
_S._transforms.shortenNumbers &&
50705073
ZERO_ANGLE_FUNCTIONS.has(fn)
@@ -5184,7 +5187,7 @@ const printer = (path, writer) => {
51845187
// color, six bytes shorter. The tap-highlight guard still holds: that
51855188
// WebKit bug is about the keyword's *value*, whichever way it is spelled.
51865189
if (
5187-
path.inValue() &&
5190+
path.within(T_DECLARATION) &&
51885191
!_S._inSubstitutedValue &&
51895192
_S._hexAlphaAllowed &&
51905193
_S._transforms.shortenColors &&
@@ -5198,7 +5201,7 @@ const printer = (path, writer) => {
51985201
if (
51995202
!_S._systemUiAllowed &&
52005203
_S._transforms.lowerUnsupported &&
5201-
path.inValue() &&
5204+
path.within(T_DECLARATION) &&
52025205
!_S._inCustomProperty &&
52035206
!_S._inSubstitutedValue &&
52045207
equalsLowerCase(meaning, "system-ui") &&
@@ -5207,7 +5210,7 @@ const printer = (path, writer) => {
52075210
return SYSTEM_UI_STACK;
52085211
}
52095212
if (
5210-
path.inValue() &&
5213+
path.within(T_DECLARATION) &&
52115214
!_S._inCustomProperty &&
52125215
!_S._inSupportsPrelude &&
52135216
!_S._inSubstitutedValue
@@ -5249,16 +5252,16 @@ const printer = (path, writer) => {
52495252
return _S._transforms.rewriteEscapes ? _minifyIdentEscapes(raw) : raw;
52505253
}
52515254
case T_SIMPLE_BLOCK: {
5252-
const open = path.blockToken();
5255+
const open = _blockTokenOf(path.node);
52535256
const close = open === "(" ? ")" : open === "[" ? "]" : "}";
52545257
// Outside a declaration value a `[…]` is an attribute selector and a `(…)`
52555258
// a query condition. Inside one they are a grid line-name list and a
52565259
// `calc()` sub-expression, where neither of those rewrites holds.
5257-
const structural = minify && !path.inValue();
5260+
const structural = minify && !path.within(T_DECLARATION);
52585261
/** @type {string[]} */
52595262
let parts;
52605263
if (structural && open === "[") {
5261-
parts = _printAttributeSelector(path, _childList(path.node), writer);
5264+
parts = _printAttributeSelector(path, path.children(), writer);
52625265
} else {
52635266
// One array, rather than a child list and a mapped copy of it.
52645267
parts = [];
@@ -5312,7 +5315,7 @@ const printer = (path, writer) => {
53125315
// it builds a different CSSOM from the same document.
53135316
(count === 1 &&
53145317
path.type(path.child(0)) === T_SIMPLE_BLOCK &&
5315-
path.blockToken(path.child(0)) === "{");
5318+
_blockTokenOf(path.child(0)) === "{");
53165319
// A value-less declaration is invalid, so it is already ignored — except
53175320
// on a custom property, where the empty value is the guaranteed-invalid
53185321
// one a `var()` fallback reads.
@@ -5418,7 +5421,7 @@ const printer = (path, writer) => {
54185421
}
54195422
}
54205423
}
5421-
const important = path.important() ? `${soft}!important` : "";
5424+
const important = _isImportant(path.node) ? `${soft}!important` : "";
54225425
// Property names match ASCII case-insensitively; a custom property's
54235426
// does not, which is what `custom` already stands for — nor does an
54245427
// `@font-feature-values` sub-rule's, which names a feature value.
@@ -5445,7 +5448,7 @@ const printer = (path, writer) => {
54455448
? /** @type {string[]} */ ([])
54465449
: undefined;
54475450
const prelude = _rulePrelude(path, writer, minify, preludeParts);
5448-
const decls = path.declarations();
5451+
const decls = _declarationsOf(path.node);
54495452
// A qualified rule always has a block; an at-rule has one only when its
54505453
// declaration list is non-null — else it is `@…;`.
54515454
if (decls === null) {
@@ -5487,7 +5490,7 @@ const printer = (path, writer) => {
54875490
// Read before the body: composing it is what leaves the block empty, and
54885491
// the drop below asks the same question again.
54895492
const unusedRule = _takeUnusedRule(/** @type {Node} */ (_S._currentNode));
5490-
const rules = path.rules();
5493+
const rules = _rulesOf(path.node);
54915494
// `nl` prefixes each item: nothing minifying, a line break beautifying
54925495
// (declarations end in `;`, rules don't).
54935496
const nl = minify ? "" : "\n";
@@ -5758,7 +5761,7 @@ const printer = (path, writer) => {
57585761
// A hash is a hex color in a value and an id in a selector, and ids are
57595762
// case-sensitive, so only the former shortens — at any value depth. An id's
57605763
// escapes still shorten, the `#` held back so its first code point is one.
5761-
if (!path.inValue()) {
5764+
if (!path.within(T_DECLARATION)) {
57625765
return _S._transforms.rewriteEscapes
57635766
? `#${_minifyIdentEscapes(raw.slice(1))}`
57645767
: raw;

‎lib/css/syntax.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ const SourceProcessor = require("../util/SourceProcessor");
99

1010
/** @typedef {typeof import("./syntax-parser")} SyntaxParser */
1111
/** @typedef {typeof import("./syntax-printer")} SyntaxPrinter */
12+
/** @typedef {import("./syntax-parser").CssParts} CssParts */
13+
/**
14+
* @template [S=unknown]
15+
* @typedef {import("./syntax-parser").CssVisitorMap<S>} CssVisitorMap
16+
*/
1217
/** @typedef {import("./syntax-parser").Node} Node */
1318
/** @typedef {import("./syntax-parser").CssProcessOptions} CssProcessOptions */
1419
/** @typedef {import("./syntax-parser").CssPath} CssPath */
@@ -41,13 +46,14 @@ const printer = () => _printer || (_printer = require("./syntax-printer"));
4146
* new CssSourceProcessor().use({ [NodeType.AtRule]: (path) => {} }).process(source, { skip });
4247
* ```
4348
* @experimental exposed as `webpack.css.syntax.SourceProcessor`; unstable API
44-
* @extends {SourceProcessor<CssPath, Node, CssProcessOptions>}
49+
* @template [TState=unknown] what `process` hands every visitor as `path.state`
50+
* @extends {SourceProcessor<CssPath, Node, CssProcessOptions, EXPECTED_OBJECT, CssParts, TState, CssVisitorMap<TState>>}
4551
*/
4652
class CssSourceProcessor extends SourceProcessor {
4753
constructor() {
4854
// The printer is not named here but asked for on the first print, so a
4955
// walk that only fires visitors never loads it.
50-
super(parser().grammar, undefined, () => printer().printer);
56+
super(parser().grammar, undefined, () => printer().printer, parser().A);
5157
}
5258
}
5359

‎lib/html/HtmlGenerator.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ class HtmlGenerator extends Generator {
723723
// materializing text nodes; `</script` stays escaped, so CSP hashes match
724724
// the exact bytes the browser sees.
725725
const {
726+
Field,
727+
Flag,
726728
NodeType,
727729
_attrRawValue,
728730
_contentEndOf,
@@ -772,9 +774,9 @@ class HtmlGenerator extends Generator {
772774
}
773775
/** @type {Record<string, string | boolean | undefined>} */
774776
const attrs = {};
775-
const count = path.attributeCount();
777+
const count = path.fieldCount(Field.attributes);
776778
for (let i = 0; i < count; i++) {
777-
const attr = path.attribute(i);
779+
const attr = path.field(i, Field.attributes);
778780
attrs[path.name(attr)] =
779781
_valueStartOf(attr) === -1 ? true : _attrRawValue(attr);
780782
}
@@ -791,7 +793,7 @@ class HtmlGenerator extends Generator {
791793
location: bodyDepth > 0 ? "body" : "head",
792794
nameEnd: isRawText ? _nameEndOf(path.node) : -1,
793795
contentEnd: isRawText ? _contentEndOf(path.node) : -1,
794-
selfClosing: path.selfClosing()
796+
selfClosing: path.flag(Flag.selfClosing)
795797
});
796798
originals.push(snapshotAttrs(attrs));
797799
descriptors.push(null);

0 commit comments

Comments
 (0)