-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcodeGen.ts
326 lines (292 loc) · 15.2 KB
/
codeGen.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import {
Program,
EmbeddedCode,
CodeText,
JSXElement,
JSXText,
JSXComment,
JSXInsert,
JSXProperty,
JSXStaticProperty,
JSXDynamicProperty,
JSXStyleProperty,
JSXFunction,
JSXContent,
JSXSpreadProperty,
CodeSegment
} from './AST';
import { LOC } from './parse';
import { locationMark } from './sourcemap';
import { Params } from './compile';
import { SvgOnlyTagRx, SvgForeignTag, IsAttribute } from './domRef';
export { codeGen, codeStr };
// pre-compiled regular expressions
const rx = {
backslashes : /\\/g,
newlines : /\r?\n/g,
hasParen : /\(/,
loneFunction: /^function |^\(\w*\) =>|^\w+ =>/,
endsInParen : /\)\s*$/,
nonIdChars : /[^a-zA-Z0-9]/,
doubleQuotes: /"/g,
indent : /\n(?=[^\n]+$)([ \t]*)/
};
class DOMExpression {
constructor(
public readonly ids : string[],
public readonly statements : string[],
public readonly computations : Computation[]
) { }
}
class Computation {
constructor(
public readonly statements : string[],
public readonly loc : LOC,
public readonly stateVar : string | null,
public readonly seed : string | null,
) { }
}
class SubComponent {
constructor(
public readonly name : string,
public readonly refs : string[],
public readonly fns : string[],
public readonly properties : (string | { [ name : string ] : string })[],
public readonly children : string[],
public readonly loc : LOC
) { }
}
const codeGen = (ctl : Program, opts : Params) => {
const compileSegments = (node : Program | EmbeddedCode) => {
return node.segments.reduce((res, s) => res + compileSegment(s, res), "");
},
compileSegment = (node : CodeSegment, previousCode : string) =>
node instanceof CodeText ? compileCodeText(node) : compileHtmlElement(node, indent(previousCode)),
compileCodeText = (node : CodeText) =>
markBlockLocs(node.text, node.loc, opts),
compileHtmlElement = (node : JSXElement, indent : Indents) : string => {
const code =
!node.isHTML ?
emitSubComponent(buildSubComponent(node), indent) :
(node.properties.length === 0 && node.functions.length === 0 && node.content.length === 0) ?
// optimization: don't need IIFE for simple single nodes
node.references.map(r => compileSegments(r.code) + ' = ').join('')
+ `Surplus.createElement('${node.tag}', null, null)` :
(node.properties.length === 1
&& node.properties[0] instanceof JSXStaticProperty
&& (node.properties[0] as JSXStaticProperty).name === "className"
&& node.functions.length === 1
&& node.content.length === 0) ?
// optimization: don't need IIFE for simple single nodes
node.references.map(r => compileSegments(r.code) + ' = ').join('')
+ `Surplus.createElement('${node.tag}', ${(node.properties[0] as JSXStaticProperty).value}, null)` :
emitDOMExpression(buildDOMExpression(node), indent);
return markLoc(code, node.loc, opts);
},
buildSubComponent = (node : JSXElement) => {
const
refs = node.references.map(r => compileSegments(r.code)),
fns = node.functions.map(r => compileSegments(r.code)),
// group successive properties into property objects, but spreads stand alone
// e.g. a="1" b={foo} {...spread} c="3" gets combined into [{a: "1", b: foo}, spread, {c: "3"}]
properties = node.properties.reduce((props : (string | { [name : string] : string })[], p) => {
const lastSegment = props.length > 0 ? props[props.length - 1] : null,
value = p instanceof JSXStaticProperty ? p.value : compileSegments(p.code);
if (p instanceof JSXSpreadProperty) {
props.push(value);
} else if (lastSegment === null
|| typeof lastSegment === 'string'
|| (p instanceof JSXStyleProperty && lastSegment["style"])
) {
props.push({ [p.name]: value });
} else {
lastSegment[p.name] = value;
}
return props;
}, []),
children = node.content.map(c =>
c instanceof JSXElement ? compileHtmlElement(c, indent("")) :
c instanceof JSXText ? codeStr(c.text.trim()) :
c instanceof JSXInsert ? compileSegments(c.code) :
`document.createComment(${codeStr(c.text)})`
);
return new SubComponent(node.tag, refs, fns, properties, children, node.loc);
},
emitSubComponent = (sub : SubComponent, indent : Indents) => {
const { nl, nli, nlii } = indent;
// build properties expression
const
// convert children to an array expression
children = sub.children.length === 0 ? '[]' : '[' + nlii
+ sub.children.join(',' + nlii) + nli
+ ']',
property0 = sub.properties.length === 0 ? null : sub.properties[0],
propertiesWithChildren = property0 === null || typeof property0 === 'string'
// add children to first property object if we can, otherwise add an initial property object with just children
? [{ children: children }, ...sub.properties ]
: [{ ...property0, children: children }, ...sub.properties.splice(1)],
propertyExprs = propertiesWithChildren.map(obj =>
typeof obj === 'string' ? obj :
'{' + Object.keys(obj).map(p => `${nli}${p}: ${obj[p]}`).join(',') + nl + '}'
),
properties = propertyExprs.length === 1 ? propertyExprs[0] :
`Object.assign(${propertyExprs.join(', ')})`;
// main call to sub-component
let expr = `${sub.name}(${properties})`;
// ref assignments
if (sub.refs.length > 0) {
expr = sub.refs.map(r => r + " = ").join("") + expr;
}
// build computations for fns
if (sub.fns.length > 0) {
const comps = sub.fns.map(fn => new Computation([`(${fn})(__, __state);`], sub.loc, '__state', null));
expr = `(function (__) {${
nli}var __ = ${expr};${
nli}${comps.map(comp => emitComputation(comp, indent) + nli)}${
nli}return __;${
nl}})()`;
}
return expr;
},
buildDOMExpression = (top : JSXElement) => {
const ids = [] as string[],
statements = [] as string[],
computations = [] as Computation[];
const buildHtmlElement = (node : JSXElement, parent : string, n : number, svg : boolean) => {
const { tag, properties, references, functions, content, loc } = node;
svg = svg || SvgOnlyTagRx.test(tag);
if (!node.isHTML) {
buildInsertedSubComponent(node, parent, n);
} else {
const
id = addId(parent, tag, n),
propExprs = properties.map(p => p instanceof JSXStaticProperty ? '' : compileSegments(p.code)),
spreads = properties.filter(p => p instanceof JSXSpreadProperty || p instanceof JSXStyleProperty),
classProp = spreads.length === 0 && properties.filter(p => p instanceof JSXStaticProperty && p.name === 'className')[0] as JSXStaticProperty || null,
propsDynamic = propExprs.some(e => !noApparentSignals(e)),
propStmts = properties.map((p, i) =>
p === classProp ? '' :
p instanceof JSXStaticProperty ? buildProperty(id, p.name, p.value, svg) :
p instanceof JSXDynamicProperty ? buildProperty(id, p.name, propExprs[i], svg) :
p instanceof JSXStyleProperty ? buildStyle(p, id, propExprs[i], propsDynamic, spreads) :
buildSpread(id, propExprs[i], svg)
).filter(s => s !== ''),
refStmts = references.map(r => compileSegments(r.code) + ' = ').join(''),
childSvg = svg && tag !== SvgForeignTag;
addStatement(`${id} = ${refStmts}Surplus.create${svg ? 'Svg' : ''}Element('${tag}', ${classProp && classProp.value}, ${parent || null});`);
if (!propsDynamic) {
propStmts.forEach(addStatement);
}
if (content.length === 1 && content[0] instanceof JSXInsert) {
buildJSXContent(content[0] as JSXInsert, id);
} else {
content.forEach((c, i) => buildChild(c, id, i, childSvg));
}
if (propsDynamic) {
addComputation(propStmts, null, null, loc);
}
functions.forEach(f => buildNodeFn(f, id));
}
},
buildProperty = (id : string, prop : string, expr : string, svg : boolean) =>
svg || IsAttribute(prop)
? `${id}.setAttribute(${codeStr(prop)}, ${expr});`
: `${id}.${prop} = ${expr};`,
buildSpread = (id : string, expr : string, svg : boolean) =>
`Surplus.spread(${id}, ${expr}, ${svg});`,
buildNodeFn = (node : JSXFunction, id : string) => {
var expr = compileSegments(node.code);
addComputation([`(${expr})(${id}, __state);`], '__state', null, node.loc);
},
buildStyle = (node : JSXStyleProperty, id : string, expr : string, dynamic : boolean, spreads : JSXProperty[]) =>
`Surplus.assign(${id}.style, ${expr});`,
buildChild = (node : JSXContent, parent : string, n : number, svg : boolean) =>
node instanceof JSXElement ? buildHtmlElement(node, parent, n, svg) :
node instanceof JSXComment ? buildHtmlComment(node, parent) :
node instanceof JSXText ? buildJSXText(node, parent, n) :
buildJSXInsert(node, parent, n),
buildInsertedSubComponent = (node : JSXElement, parent : string, n : number) =>
buildJSXInsert(new JSXInsert(new EmbeddedCode([node]), node.loc), parent, n),
buildHtmlComment = (node : JSXComment, parent : string) =>
addStatement(`Surplus.createComment(${codeStr(node.text)}, ${parent})`),
buildJSXText = (node : JSXText, parent : string, n : number) =>
addStatement(`Surplus.createTextNode(${codeStr(node.text)}, ${parent})`),
buildJSXInsert = (node : JSXInsert, parent : string, n : number) => {
const id = addId(parent, 'insert', n),
ins = compileSegments(node.code),
range = `{ start: ${id}, end: ${id} }`;
addStatement(`${id} = Surplus.createTextNode('', ${parent})`);
addComputation([`Surplus.insert(__range, ${ins});`], "__range", range, node.loc);
},
buildJSXContent = (node : JSXInsert, parent : string) => {
const content = compileSegments(node.code),
dynamic = !noApparentSignals(content);
if (dynamic) addComputation([`Surplus.content(${parent}, ${content}, __current);`], '__current', "''", node.loc);
else addStatement(`Surplus.content(${parent}, ${content}, "");`);
},
addId = (parent : string, tag : string, n : number) => {
tag = tag.replace(rx.nonIdChars, '_');
const id = parent === '' ? '__' : parent + (parent[parent.length - 1] === '_' ? '' : '_') + tag + (n + 1);
ids.push(id);
return id;
},
addStatement = (stmt : string) =>
statements.push(stmt),
addComputation = (body : string[], stateVar : string | null, seed : string | null, loc : LOC) => {
computations.push(new Computation(body, loc, stateVar, seed));
}
buildHtmlElement(top, '', 0, false);
return new DOMExpression(ids, statements, computations);
},
emitDOMExpression = (code : DOMExpression, indent : Indents) => {
const { nl, nli, nlii } = indent;
return '(function () {' + nli
+ 'var ' + code.ids.join(', ') + ';' + nli
+ code.statements.join(nli) + nli
+ code.computations.map(comp => emitComputation(comp, indent))
.join(nli) + (code.computations.length === 0 ? '' : nli)
+ 'return __;' + nl
+ '})()';
},
emitComputation = (comp : Computation, { nli, nlii } : Indents) => {
const { statements, loc, stateVar, seed } = comp;
if (stateVar) statements[statements.length - 1] = 'return ' + statements[statements.length - 1];
const body = statements.length === 1 ? (' ' + statements[0] + ' ') : (nlii + statements.join(nlii) + nli),
code = `Surplus.S(function (${stateVar || ''}) {${body}}${seed !== null ? `, ${seed}` : ''});`;
return markLoc(code, loc, opts);
};
return compileSegments(ctl);
};
const
noApparentSignals = (code : string) =>
!rx.hasParen.test(code) || (rx.loneFunction.test(code) && !rx.endsInParen.test(code)),
indent = (previousCode : string) : Indents => {
const m = rx.indent.exec(previousCode),
pad = m ? m[1] : '',
nl = "\r\n" + pad,
nli = nl + ' ',
nlii = nli + ' ';
return { nl, nli, nlii };
},
codeStr = (str : string) =>
'"' +
str.replace(rx.backslashes, "\\\\")
.replace(rx.doubleQuotes, "\\\"")
.replace(rx.newlines, "\\n") +
'"';
interface Indents { nl : string, nli : string, nlii : string }
const
markLoc = (str : string, loc : LOC, opts : Params) =>
opts.sourcemap ? locationMark(loc) + str : str,
markBlockLocs = (str : string, loc : LOC, opts : Params) => {
if (!opts.sourcemap) return str;
let lines = str.split('\n'),
offset = 0;
for (let i = 1; i < lines.length; i++) {
let line = lines[i];
offset += line.length;
let lineloc = { line: loc.line + i, col: 0, pos: loc.pos + offset + i };
lines[i] = locationMark(lineloc) + line;
}
return locationMark(loc) + lines.join('\n');
};