Skip to content

Commit ca10a16

Browse files
committed
feat: Data schema for Aggregate Rewriting.
1 parent 59620ef commit ca10a16

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Every CSS Block style is assigned a unique number that is globally unique
2+
type GlobalStyleIndex = number;
3+
// For clarity as to what type of information the strings are
4+
type Classname = string;
5+
type OptimizedClassname = string;
6+
// this is a really fancy way to alias to `number`
7+
type OutputClassnameIndex = Exclude<keyof AggregateRewriteData["outputClassnames"], string>;
8+
// this is a really fancy way to alias to `number`
9+
type GlobalBlockIndex = Exclude<keyof AggregateRewriteData["blocks"], string>;
10+
// this is a really fancy way to alias to `number`
11+
type OptimizationIndex = Exclude<keyof AggregateRewriteData["optimizations"], string>;
12+
13+
// These 5 lines are a compact AST for boolean expressions.
14+
// Normally an AST would use objects with more expressive keys, but we want a
15+
// more compact data structure in this AST for fewer bytes in our output.
16+
// An array represents a sub-expression and the first element in that array indicates the
17+
// sub-expression type.
18+
//
19+
// When a GlobalStyleIndex is encountered, that part of the boolean expression
20+
// is considered true iff the current input state has that GlobalStyleIndex applied.
21+
enum StyleExpressionType { AND = 1, OR = 2, NOT = 3 }
22+
type StyleExpression = GlobalStyleIndex | AndStyleExpression | OrStyleExpression | NotStyleExpression;
23+
type AndStyleExpression = [StyleExpressionType.AND, ...StyleExpression[]];
24+
type OrStyleExpression = [StyleExpressionType.OR, ...StyleExpression[]];
25+
type NotStyleExpression = [StyleExpressionType.NOT, StyleExpression];
26+
27+
interface AggregateRewriteData {
28+
// Maps a block's unique ID to an index of AggregateRewriteData["blocks"].
29+
blockIds: {
30+
[blockId: string]: GlobalBlockIndex;
31+
};
32+
blocks: Array<BlockInfo>;
33+
34+
// This is a list of all the class names that might be returned by the rewrite helper
35+
// with the exception of public class names (block aliases) that are found in the styleImplications.
36+
// Note: classnames from the original source that are not optimized are also returned here.
37+
// Note: classnames from the original source that the optimizer has flagged as obsolete are not listed here.
38+
outputClassnames: Array<OptimizedClassname>;
39+
styleRequirements: {
40+
// The key is a GlobalStyleIndex.
41+
//
42+
// Value is an unordered set of GlobalStyleIndex values that must all be
43+
// applied to the element in order for the style to be applied.
44+
[styleIndex: number]: Array<GlobalStyleIndex>;
45+
};
46+
impliedStyles: {
47+
// The key is a GlobalStyleIndex
48+
//
49+
// Value is an unordered set.
50+
// - GlobalStyleIndex: a style that is also applied in conjunction with the given style.
51+
// - string: public class name (block alias) that is also applied in conjunction with the given style.
52+
//
53+
// Note: This list is only the directly implied styles. The full implication
54+
// graph is resolved at runtime.
55+
[styleIndex: number]: Array<GlobalStyleIndex | string>;
56+
};
57+
optimizations: [
58+
// Adds the class name to the output if the style expression matches the current input state.
59+
[OutputClassnameIndex, StyleExpression]
60+
];
61+
possibleOptimizations: {
62+
// Key is a GlobalStyleIndex.
63+
// Value is a list of all outputs that might apply for the given GlobalStyleIndex.
64+
[styleIndex: number]: Array<OptimizationIndex>;
65+
};
66+
}
67+
68+
interface BlockInfo {
69+
// The styles of this block
70+
// Note: this includes all the styles that are inherited but not overridden
71+
// but does not include the styles that are inherited but then overridden.
72+
styles: Array<GlobalStyleIndex>;
73+
// Given a block that implements this block's interface or inherits from this interface
74+
// Get an array of GlobalStyleIndex values that directly correspond to the same index as BlockInfo["styles"].
75+
// Note: If an implementation inherits from this block, this array will contain
76+
// GlobalStyleIndex values from this BlockInfo's styles wherever those styles
77+
// are not overridden. Thus it is guaranteed that each value in `implementationStyles`
78+
// has the same length as the `styles` array.
79+
implementationStyles: {
80+
// The key is a GlobalStyleIndex
81+
[blockIndex: number]: Array<GlobalStyleIndex>;
82+
};
83+
}

0 commit comments

Comments
 (0)