forked from linkedin/css-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RewriteMapping.ts
191 lines (172 loc) · 6.44 KB
/
RewriteMapping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {
BooleanExpression,
RewriteMapping as OptimizedMapping,
SimpleAttribute,
SimpleTagname,
isAndExpression,
isBooleanExpression,
isNotExpression,
isOrExpression,
isSimpleTagname,
} from "@opticss/template-api";
import { Maybe, ObjectDictionary, assertNever, maybe, objectValues } from "@opticss/util";
import { inspect } from "util";
import { Style } from "../BlockTree";
import { ClassRewrite, IndexedClassRewrite } from "./ClassRewrite";
export type ClassExpressionMap = ObjectDictionary<BooleanExpression<number> | undefined>;
export class IndexedClassMapping implements IndexedClassRewrite<Style> {
inputs: Style[];
staticClasses: string[];
private map: ClassExpressionMap;
private _inputMap: Map<Style, number>;
constructor(inputs: Style[], staticClasses: string[], map: ClassExpressionMap) {
this.inputs = inputs;
this.staticClasses = staticClasses;
this._inputMap = new Map<Style, number>();
inputs.forEach((i, n) => this._inputMap.set(i, n));
this.map = map;
}
dynamicClass(name: string): BooleanExpression<number> | undefined {
return this.map[name];
}
get dynamicClasses(): string[] {
return Object.keys(this.map);
}
public indexOf(input: Style): Maybe<number> {
let index = this._inputMap.get(input);
return maybe(index, "internal error: style not found");
}
static fromOptimizer(
classRewrite: OptimizedMapping,
classMap: Map<string, Style>,
): IndexedClassMapping {
// TODO: move this renumbering to opticss?
let indexSet = new Set<number>();
let dynClasses = classRewrite.dynamicAttributes.class;
objectValues(dynClasses).forEach(expr => indexesUsed(indexSet, expr!));
let usedIndexes = [...indexSet].sort((a, b) => a < b ? -1 : 1);
let adjustments = new Array<number>();
usedIndexes.reduce(
([missing, last], n) => {
missing = missing + (n - last - 1);
adjustments[n] = missing;
return [missing, n];
},
[0, -1]);
function renumberer(i: ExprItem, n: number, arr: ExprItem[]) {
if (typeof i === "number") {
arr[n] = i - adjustments[i];
} else {
renumber(renumberer, i);
}
}
let inputs = classRewrite.inputs.filter((_, n) => indexSet.has(n)).map((_, n, inputs) => processExpressionLiteral(n, inputs, classMap));
objectValues(classRewrite.dynamicAttributes.class).forEach(expr => renumber(renumberer, expr!));
return new IndexedClassMapping(
inputs,
classRewrite.staticAttributes.class,
classRewrite.dynamicAttributes.class,
);
}
}
export class RewriteMapping implements ClassRewrite<Style> {
/**
* output attributes that are always on the element independent of any dynamic changes.
*/
staticClasses: string[];
/**
* The numbers in the boolean expressions represents indexes into the inputAttributes array.
*/
private _dynamicClasses: Map<string, BooleanExpression<Style>>;
constructor(staticClasses?: string[], dynamicClasses?: Map<string, BooleanExpression<Style>>) {
this.staticClasses = staticClasses || [];
this._dynamicClasses = dynamicClasses || new Map<string, BooleanExpression<Style>>();
}
get dynamicClasses(): Array<string> {
return [...this._dynamicClasses.keys()];
}
dynamicClass(name: string): BooleanExpression<Style> {
return this._dynamicClasses.get(name)!;
}
static fromOptimizer(
classRewrite: OptimizedMapping,
classMap: Map<string, Style>,
): RewriteMapping {
let staticClasses = classRewrite.staticAttributes.class;
let dynamicClasses = classRewrite.dynamicAttributes.class;
let dynMap = new Map<string, BooleanExpression<Style>>();
if (dynamicClasses) {
for (let className of Object.keys(dynamicClasses)) {
let expression = dynamicClasses[className];
if (expression) {
dynMap.set(
className,
processExpression(expression, classRewrite.inputs, classMap));
}
}
}
let styleRewrite = new RewriteMapping(staticClasses || [], dynMap);
return styleRewrite;
}
}
function indexesUsed(indexes: Set<number>, expression: BooleanExpression<number> | number) {
if (typeof expression === "number") {
indexes.add(expression);
} else if (isAndExpression(expression)) {
expression.and.forEach(e => indexesUsed(indexes, e));
} else if (isOrExpression(expression)) {
expression.or.forEach(e => indexesUsed(indexes, e));
} else if (isNotExpression(expression)) {
indexesUsed(indexes, expression.not);
} else {
assertNever(expression);
}
}
type ExprItem = number | BooleanExpression<number>;
type Renumberer = (i: ExprItem, n: number, arr: ExprItem[]) => void;
function renumber(renumberer: Renumberer, expression: BooleanExpression<number>) {
if (isAndExpression(expression)) {
expression.and.forEach(renumberer);
} else if (isOrExpression(expression)) {
expression.or.forEach(renumberer);
} else if (isNotExpression(expression)) {
let not = [expression.not];
not.forEach(renumberer);
expression.not = not[0];
} else {
assertNever(expression);
}
}
function processExpression(
expression: BooleanExpression<number>,
inputs: Array<SimpleTagname | SimpleAttribute>,
classMap: Map<string, Style>,
): BooleanExpression<Style> {
if (isAndExpression(expression)) {
return {and: expression.and.map(e => isBooleanExpression(e) ? processExpression(e, inputs, classMap) : processExpressionLiteral(e, inputs, classMap))};
} else if (isOrExpression(expression)) {
return {or: expression.or.map(e => isBooleanExpression(e) ? processExpression(e, inputs, classMap) : processExpressionLiteral(e, inputs, classMap))};
} else if (isNotExpression(expression)) {
return {not: isBooleanExpression(expression.not) ? processExpression(expression.not, inputs, classMap) : processExpressionLiteral(expression.not, inputs, classMap)};
} else {
return assertNever(expression);
}
}
function processExpressionLiteral(
expression: number,
inputs: Array<SimpleTagname | SimpleAttribute>,
classMap: Map<string, Style>,
): Style {
let input = inputs[expression];
if (isSimpleTagname(input)) {
throw new Error("i really just can't handle tag names rn thx");
} else {
if (input.name !== "class") {
throw new Error(`expected a class but got ${inspect(input)}, you should have known better.`);
}
if (!classMap.has(input.value)) {
throw new Error(`wth. no class ${input.value} exists on this element.`);
}
return classMap.get(input.value)!;
}
}