@xaendar/compiler 0.6.12 → 0.6.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/xaendar-compiler.es.js +157 -103
- package/package.json +3 -3
|
@@ -409,14 +409,15 @@ function consumeEvent(cursor, _context) {
|
|
|
409
409
|
break;
|
|
410
410
|
case 40:
|
|
411
411
|
deep++;
|
|
412
|
+
cursor.advance();
|
|
412
413
|
break;
|
|
413
414
|
case 41:
|
|
414
415
|
deep--;
|
|
416
|
+
cursor.advance();
|
|
415
417
|
break;
|
|
416
|
-
default:
|
|
418
|
+
default:
|
|
417
419
|
cursor.advance();
|
|
418
|
-
event = `${event}${cursor.currentChar.value}`;
|
|
419
|
-
}
|
|
420
|
+
if (deep === 0) event = `${event}${cursor.currentChar.value}`;
|
|
420
421
|
}
|
|
421
422
|
return retVal;
|
|
422
423
|
}
|
|
@@ -1935,77 +1936,6 @@ var Parser = class {
|
|
|
1935
1936
|
}
|
|
1936
1937
|
};
|
|
1937
1938
|
//#endregion
|
|
1938
|
-
//#region ../packages/compiler/src/render-generator/states/process-const-declaration.state.ts
|
|
1939
|
-
/**
|
|
1940
|
-
* Generates code for a `@const` declaration node.
|
|
1941
|
-
* Emits a `const name = expression;` JavaScript statement.
|
|
1942
|
-
*
|
|
1943
|
-
* @param node The `ConstDeclarationNode` to process.
|
|
1944
|
-
* @param _nodeName Unused node name.
|
|
1945
|
-
* @param _parentNode Unused parent node name.
|
|
1946
|
-
* @returns Array containing the generated const statement string.
|
|
1947
|
-
*/
|
|
1948
|
-
function processConstDeclaration(node, _nodeName, _parentNode) {
|
|
1949
|
-
return [`const ${node.varName} = ${node.varName};`];
|
|
1950
|
-
}
|
|
1951
|
-
//#endregion
|
|
1952
|
-
//#region ../packages/compiler/src/render-generator/states/process-element.state.ts
|
|
1953
|
-
/**
|
|
1954
|
-
* Generates code for an HTML element node: creates the DOM element, sets attributes,
|
|
1955
|
-
* attaches event listeners, appends it to the parent, and recursively processes children.
|
|
1956
|
-
*
|
|
1957
|
-
* @param node The `ElementNode` to process.
|
|
1958
|
-
* @param nodeName Variable name to use for the created DOM element.
|
|
1959
|
-
* @param parentNode Variable name of the parent DOM node to append to.
|
|
1960
|
-
* @returns Array of generated code lines.
|
|
1961
|
-
*/
|
|
1962
|
-
function processElement(node, nodeName, parentNode) {
|
|
1963
|
-
const attributes = mapAttributes(node.attributes);
|
|
1964
|
-
const events = mapEvents(node.events);
|
|
1965
|
-
const code = [`const ${nodeName} = _renderElement(${parentNode}, context, '${node.tagName}',`];
|
|
1966
|
-
attributes.length ? code.push(...indent([
|
|
1967
|
-
"[",
|
|
1968
|
-
...indent(attributes),
|
|
1969
|
-
"],"
|
|
1970
|
-
])) : code[code.length - 1] = `${code[code.length - 1]} [],`;
|
|
1971
|
-
events.length ? code.push(...indent([
|
|
1972
|
-
"[",
|
|
1973
|
-
...indent(events),
|
|
1974
|
-
"]"
|
|
1975
|
-
]), ")") : code[code.length - 1] = `${code[code.length - 1]} [])`;
|
|
1976
|
-
code.push(...node.children.map((child, i) => processNode(child, i.toString(), nodeName)).flat());
|
|
1977
|
-
return code;
|
|
1978
|
-
}
|
|
1979
|
-
/**
|
|
1980
|
-
* Generates code that assigns attributes to a DOM element.
|
|
1981
|
-
*
|
|
1982
|
-
* Static (string) attribute values are set once via a direct `setAttribute` call,
|
|
1983
|
-
* while dynamic attribute values are wrapped in an `effect` so the attribute is
|
|
1984
|
-
* re-evaluated and updated whenever its underlying signal dependencies change.
|
|
1985
|
-
* The disposer returned by the `effect` is registered in `unwatchFns` for cleanup.
|
|
1986
|
-
*
|
|
1987
|
-
* @param attributes The attribute nodes to map onto the element.
|
|
1988
|
-
* @returns Array of generated code lines, one per attribute.
|
|
1989
|
-
*/
|
|
1990
|
-
function mapAttributes(attributes) {
|
|
1991
|
-
return attributes?.map(({ name, value }) => {
|
|
1992
|
-
const isLiteral = typeof value === "string";
|
|
1993
|
-
return `{ name: '${name}', value: '${isLiteral ? value : value.expression.text}', literal: ${isLiteral} },`;
|
|
1994
|
-
});
|
|
1995
|
-
}
|
|
1996
|
-
/**
|
|
1997
|
-
* Generates code that attaches event listeners to a DOM element.
|
|
1998
|
-
*
|
|
1999
|
-
* For each event node an `addEventListener` call is emitted, binding the event
|
|
2000
|
-
* to the component instance handler and exposing the native event as `$event`.
|
|
2001
|
-
*
|
|
2002
|
-
* @param events The event nodes to bind to the element.
|
|
2003
|
-
* @returns Array of generated code lines, one per event listener.
|
|
2004
|
-
*/
|
|
2005
|
-
function mapEvents(events) {
|
|
2006
|
-
return events?.map((event) => `{ name: '${event.name}', handler: '${event.handler}' }`);
|
|
2007
|
-
}
|
|
2008
|
-
//#endregion
|
|
2009
1939
|
//#region ../packages/compiler/src/render-generator/utils/render-generator.utils.ts
|
|
2010
1940
|
/**
|
|
2011
1941
|
* Complete set of JavaScript global identifiers up to ES2026.
|
|
@@ -2203,7 +2133,7 @@ var ROOT_NODE = "root";
|
|
|
2203
2133
|
*
|
|
2204
2134
|
* @param expression - Either a raw identifier string or a validated
|
|
2205
2135
|
* `ts.Expression` node produced by `validateExpression`.
|
|
2206
|
-
* @param
|
|
2136
|
+
* @param compilerContext - The active template scope context.
|
|
2207
2137
|
* @returns The resolved expression as a JavaScript string ready for codegen.
|
|
2208
2138
|
*
|
|
2209
2139
|
* @example
|
|
@@ -2216,8 +2146,8 @@ var ROOT_NODE = "root";
|
|
|
2216
2146
|
* // typeof id !== 'boolean' || pippo instanceof HTMLElement
|
|
2217
2147
|
* // → typeof this.id !== 'boolean' || this.pippo instanceof HTMLElement
|
|
2218
2148
|
*/
|
|
2219
|
-
function resolveExpression(expression) {
|
|
2220
|
-
return emitNode(expression, expression);
|
|
2149
|
+
function resolveExpression(expression, compilerContext) {
|
|
2150
|
+
return emitNode(expression, expression, compilerContext);
|
|
2221
2151
|
}
|
|
2222
2152
|
/**
|
|
2223
2153
|
* Emits the resolved text for a node.
|
|
@@ -2228,13 +2158,14 @@ function resolveExpression(expression) {
|
|
|
2228
2158
|
* - If the node is a resolvable Identifier, emits the resolved name.
|
|
2229
2159
|
* - Otherwise recurses into children and concatenates their output.
|
|
2230
2160
|
*/
|
|
2231
|
-
function emitNode(node, parent) {
|
|
2161
|
+
function emitNode(node, parent, compilerContext) {
|
|
2162
|
+
if (ts.isIdentifier(node) && needsResolution(node, parent)) return compilerContext.hasIdentifier(node.text) ? node.text : `this.${node.text}`;
|
|
2232
2163
|
if (!containsResolvableIdentifier(node, parent)) return node.getText();
|
|
2233
2164
|
const sourceText = node.getSourceFile().text;
|
|
2234
2165
|
let result = "";
|
|
2235
2166
|
let lastEnd = node.getStart();
|
|
2236
2167
|
ts.forEachChild(node, (child) => {
|
|
2237
|
-
result = `${result}${sourceText.slice(lastEnd, child.getStart())}${emitNode(child, node)}`;
|
|
2168
|
+
result = `${result}${sourceText.slice(lastEnd, child.getStart())}${emitNode(child, node, compilerContext)}`;
|
|
2238
2169
|
lastEnd = child.getEnd();
|
|
2239
2170
|
});
|
|
2240
2171
|
return `${result}${sourceText.slice(lastEnd, node.getEnd())}`;
|
|
@@ -2279,6 +2210,114 @@ function getBlockIdentifier(parentNode, index, prefix) {
|
|
|
2279
2210
|
return (parentNode !== "root" ? `${parentNode}_${prefix}${index}` : `${prefix}${index}`).replace(/-/g, "_");
|
|
2280
2211
|
}
|
|
2281
2212
|
//#endregion
|
|
2213
|
+
//#region ../packages/compiler/src/render-generator/states/process-const-declaration.state.ts
|
|
2214
|
+
/**
|
|
2215
|
+
* Generates code for a `@const` declaration node.
|
|
2216
|
+
* Emits a `const name = expression;` JavaScript statement.
|
|
2217
|
+
*
|
|
2218
|
+
* @param node The `ConstDeclarationNode` to process.
|
|
2219
|
+
* @param _nodeName Unused node name.
|
|
2220
|
+
* @param _parentNode Unused parent node name.
|
|
2221
|
+
* @param _context Unused render context.
|
|
2222
|
+
* @returns Array containing the generated const statement string.
|
|
2223
|
+
*/
|
|
2224
|
+
function processConstDeclaration(node, _nodeName, _parentNode, compilerContext) {
|
|
2225
|
+
compilerContext.addIdentifier(node.varName);
|
|
2226
|
+
return [`declareConst(context, ${node.varName}, () => ${resolveExpression(node.expression, compilerContext)});`];
|
|
2227
|
+
}
|
|
2228
|
+
//#endregion
|
|
2229
|
+
//#region ../packages/compiler/src/render-generator/states/process-element.state.ts
|
|
2230
|
+
/**
|
|
2231
|
+
* Generates code for an HTML element node: creates the DOM element, sets attributes,
|
|
2232
|
+
* attaches event listeners, appends it to the parent, and recursively processes children.
|
|
2233
|
+
*
|
|
2234
|
+
* @param node The `ElementNode` to process.
|
|
2235
|
+
* @param nodeName Variable name to use for the created DOM element.
|
|
2236
|
+
* @param parentNode Variable name of the parent DOM node to append to.
|
|
2237
|
+
* @param compilerContext Current render scope context.
|
|
2238
|
+
* @returns Array of generated code lines.
|
|
2239
|
+
*/
|
|
2240
|
+
function processElement(node, nodeName, parentNode, compilerContext) {
|
|
2241
|
+
const attributes = mapAttributes(node.attributes, compilerContext);
|
|
2242
|
+
const events = mapEvents(node.events);
|
|
2243
|
+
const code = [`const ${nodeName} = _renderElement(${parentNode}, context, '${node.tagName}',`];
|
|
2244
|
+
attributes.length ? code.push(...indent([
|
|
2245
|
+
"[",
|
|
2246
|
+
...indent(attributes),
|
|
2247
|
+
"],"
|
|
2248
|
+
])) : code[code.length - 1] = `${code[code.length - 1]} [],`;
|
|
2249
|
+
events.length ? code.push(...indent([
|
|
2250
|
+
"[",
|
|
2251
|
+
...indent(events),
|
|
2252
|
+
"]"
|
|
2253
|
+
]), ")") : code[code.length - 1] = `${code[code.length - 1]} [])`;
|
|
2254
|
+
code.push(...node.children.map((child, i) => processNode(child, i.toString(), nodeName, compilerContext)).flat());
|
|
2255
|
+
return code;
|
|
2256
|
+
}
|
|
2257
|
+
/**
|
|
2258
|
+
* Generates code that assigns attributes to a DOM element.
|
|
2259
|
+
*
|
|
2260
|
+
* Static (string) attribute values are set once via a direct `setAttribute` call,
|
|
2261
|
+
* while dynamic attribute values are wrapped in an `effect` so the attribute is
|
|
2262
|
+
* re-evaluated and updated whenever its underlying signal dependencies change.
|
|
2263
|
+
* The disposer returned by the `effect` is registered in `unwatchFns` for cleanup.
|
|
2264
|
+
*
|
|
2265
|
+
* @param attributes The attribute nodes to map onto the element.
|
|
2266
|
+
* @returns Array of generated code lines, one per attribute.
|
|
2267
|
+
*/
|
|
2268
|
+
function mapAttributes(attributes, compilerContext) {
|
|
2269
|
+
return attributes?.map(({ name, value }) => {
|
|
2270
|
+
const isLiteral = typeof value === "string";
|
|
2271
|
+
return `{ name: '${name}', value: () => ${isLiteral ? `'${value}'` : resolveExpression(value.expression, compilerContext)}, literal: ${isLiteral} },`;
|
|
2272
|
+
});
|
|
2273
|
+
}
|
|
2274
|
+
/**
|
|
2275
|
+
* Generates code that attaches event listeners to a DOM element.
|
|
2276
|
+
*
|
|
2277
|
+
* For each event node an `addEventListener` call is emitted, binding the event
|
|
2278
|
+
* to the component instance handler and exposing the native event as `$event`.
|
|
2279
|
+
*
|
|
2280
|
+
* @param events The event nodes to bind to the element.
|
|
2281
|
+
* @returns Array of generated code lines, one per event listener.
|
|
2282
|
+
*/
|
|
2283
|
+
function mapEvents(events) {
|
|
2284
|
+
return events?.map((event) => `{ name: '${event.name}', handler: '${event.handler}' }`);
|
|
2285
|
+
}
|
|
2286
|
+
//#endregion
|
|
2287
|
+
//#region ../packages/compiler/src/render-generator/models/compiler-context.model.ts
|
|
2288
|
+
/**
|
|
2289
|
+
* Tracks identifier scope during render code generation.
|
|
2290
|
+
* Each `Context` instance represents one lexical scope (e.g. a `@for` loop body)
|
|
2291
|
+
* and can be chained to a parent context for outer-scope resolution.
|
|
2292
|
+
*/
|
|
2293
|
+
var CompilerContext = class {
|
|
2294
|
+
_identifiers;
|
|
2295
|
+
_parent;
|
|
2296
|
+
/**
|
|
2297
|
+
* Creates a new scope context.
|
|
2298
|
+
*
|
|
2299
|
+
* @param _identifiers List of loop variable names declared in this scope.
|
|
2300
|
+
* @param _parent Optional parent context representing the enclosing scope.
|
|
2301
|
+
*/
|
|
2302
|
+
constructor(_identifiers = new Array(), _parent) {
|
|
2303
|
+
this._identifiers = _identifiers;
|
|
2304
|
+
this._parent = _parent;
|
|
2305
|
+
}
|
|
2306
|
+
addIdentifier(name) {
|
|
2307
|
+
if (this.hasIdentifier(name)) throw new Error(`Identifier "${name}" is already declared in this scope.`);
|
|
2308
|
+
this._identifiers.push(name);
|
|
2309
|
+
}
|
|
2310
|
+
/**
|
|
2311
|
+
* Returns the innermost identifier in the current scope chain, or
|
|
2312
|
+
* delegates to the parent context if none is found in this scope.
|
|
2313
|
+
*
|
|
2314
|
+
* @returns The most recently declared identifier name, or `undefined` if none exists.
|
|
2315
|
+
*/
|
|
2316
|
+
hasIdentifier(name) {
|
|
2317
|
+
return this._identifiers.includes(name) || (this._parent?.hasIdentifier(name) ?? false);
|
|
2318
|
+
}
|
|
2319
|
+
};
|
|
2320
|
+
//#endregion
|
|
2282
2321
|
//#region ../packages/compiler/src/render-generator/states/process-for.state.ts
|
|
2283
2322
|
/**
|
|
2284
2323
|
* Generates code for a `@for` iteration node.
|
|
@@ -2308,12 +2347,14 @@ function getBlockIdentifier(parentNode, index, prefix) {
|
|
|
2308
2347
|
* @param nodeName - Base variable name prefix used for child nodes and
|
|
2309
2348
|
* to produce a unique loop counter identifier.
|
|
2310
2349
|
* @param parentNode - Variable name of the parent DOM node.
|
|
2350
|
+
* @param compilerContext - The enclosing scope context.
|
|
2311
2351
|
* @returns Array of generated code lines.
|
|
2312
2352
|
*/
|
|
2313
|
-
function processFor(node, nodeName, parentNode) {
|
|
2353
|
+
function processFor(node, nodeName, parentNode, compilerContext) {
|
|
2314
2354
|
const mainBlock = new Array();
|
|
2315
2355
|
const functionsToProcess = /* @__PURE__ */ new Map();
|
|
2316
2356
|
const iterableSource = node.iterableSource;
|
|
2357
|
+
compilerContext.hasIdentifier(iterableSource) || `${iterableSource}`;
|
|
2317
2358
|
const itemsName = getTextIdentifier(parentNode, nodeName, "items");
|
|
2318
2359
|
const counterName = getTextIdentifier(parentNode, nodeName, "i");
|
|
2319
2360
|
const indexName = resolveImplicit(node, "$index");
|
|
@@ -2321,9 +2362,17 @@ function processFor(node, nodeName, parentNode) {
|
|
|
2321
2362
|
const lastName = resolveImplicit(node, "$last");
|
|
2322
2363
|
const evenName = resolveImplicit(node, "$even");
|
|
2323
2364
|
const oddName = resolveImplicit(node, "$odd");
|
|
2365
|
+
const forContext = new CompilerContext([
|
|
2366
|
+
node.itemAlias,
|
|
2367
|
+
indexName,
|
|
2368
|
+
firstName,
|
|
2369
|
+
lastName,
|
|
2370
|
+
evenName,
|
|
2371
|
+
oddName
|
|
2372
|
+
], compilerContext);
|
|
2324
2373
|
const forKey = getBlockIdentifier(parentNode, nodeName, "for");
|
|
2325
2374
|
functionsToProcess.set(forKey, {
|
|
2326
|
-
code: [`const { ${node.itemAlias}, ${indexName}, ${firstName}, ${lastName}, ${evenName}, ${oddName} } = _iterationVariables(${itemsName}, ${counterName});`, ...node.children.flatMap((child, i) => processNode(child, `${nodeName}_${i}`, parentNode))],
|
|
2375
|
+
code: [`const { ${node.itemAlias}, ${indexName}, ${firstName}, ${lastName}, ${evenName}, ${oddName} } = _iterationVariables(${itemsName}, ${counterName});`, ...node.children.flatMap((child, i) => processNode(child, `${nodeName}_${i}`, parentNode, forContext))],
|
|
2327
2376
|
args: [
|
|
2328
2377
|
parentNode,
|
|
2329
2378
|
"parentContext",
|
|
@@ -2331,7 +2380,7 @@ function processFor(node, nodeName, parentNode) {
|
|
|
2331
2380
|
counterName
|
|
2332
2381
|
]
|
|
2333
2382
|
});
|
|
2334
|
-
mainBlock.push(`_for(${parentNode}, context, '${iterableSource}', this.${forKey}.bind(this));`);
|
|
2383
|
+
mainBlock.push(`_for(${parentNode}, context, context.getIdentifier('${iterableSource}'), this.${forKey}.bind(this));`);
|
|
2335
2384
|
return {
|
|
2336
2385
|
mainBlock,
|
|
2337
2386
|
fns: functionsToProcess
|
|
@@ -2363,38 +2412,41 @@ function resolveImplicit(node, implicit) {
|
|
|
2363
2412
|
* @param parentNode Variable name of the parent DOM node.
|
|
2364
2413
|
* @returns Array of generated code lines.
|
|
2365
2414
|
*/
|
|
2366
|
-
function processIf(node, nodeName, parentNode) {
|
|
2415
|
+
function processIf(node, nodeName, parentNode, compilerContext) {
|
|
2416
|
+
const ifContext = new CompilerContext([], compilerContext);
|
|
2367
2417
|
const functionsToProcess = /* @__PURE__ */ new Map();
|
|
2368
2418
|
const mainBlock = new Array();
|
|
2369
2419
|
const ifKey = getBlockIdentifier(parentNode, nodeName, "if");
|
|
2370
2420
|
mainBlock.push({
|
|
2371
|
-
condition: resolveExpression(node.conditionNode),
|
|
2421
|
+
condition: resolveExpression(node.conditionNode, compilerContext),
|
|
2372
2422
|
block: `this.${ifKey}.bind(this)`
|
|
2373
2423
|
});
|
|
2374
2424
|
functionsToProcess.set(ifKey, {
|
|
2375
|
-
code: processConsequent(node, nodeName, parentNode),
|
|
2425
|
+
code: processConsequent(node, nodeName, parentNode, ifContext),
|
|
2376
2426
|
args: [parentNode, "parentContext"]
|
|
2377
2427
|
});
|
|
2378
2428
|
let alt = node.alternate;
|
|
2379
2429
|
let index = 0;
|
|
2380
2430
|
while (alt?.type === ASTNodeType.ElseIf) {
|
|
2431
|
+
const elseIfContext = new CompilerContext([], compilerContext);
|
|
2381
2432
|
const keyElseIf = getBlockIdentifier(parentNode, `${nodeName}_${index}`, "elseIf");
|
|
2382
2433
|
const conditionNode = alt.conditionNode;
|
|
2383
2434
|
mainBlock.push({
|
|
2384
|
-
condition: resolveExpression(conditionNode),
|
|
2435
|
+
condition: resolveExpression(conditionNode, compilerContext),
|
|
2385
2436
|
block: `this.${keyElseIf}.bind(this)`
|
|
2386
2437
|
});
|
|
2387
2438
|
functionsToProcess.set(keyElseIf, {
|
|
2388
|
-
code: processConsequent(alt, nodeName, parentNode),
|
|
2439
|
+
code: processConsequent(alt, nodeName, parentNode, elseIfContext),
|
|
2389
2440
|
args: [parentNode, "parentContext"]
|
|
2390
2441
|
});
|
|
2391
2442
|
alt = alt.alternate;
|
|
2392
2443
|
}
|
|
2393
2444
|
if (alt) {
|
|
2445
|
+
const elseContext = new CompilerContext([], compilerContext);
|
|
2394
2446
|
const keyElse = getBlockIdentifier(parentNode, nodeName, "else");
|
|
2395
2447
|
mainBlock.push({ block: `this.${keyElse}.bind(this)` });
|
|
2396
2448
|
functionsToProcess.set(keyElse, {
|
|
2397
|
-
code: processConsequent(alt, nodeName, parentNode),
|
|
2449
|
+
code: processConsequent(alt, nodeName, parentNode, elseContext),
|
|
2398
2450
|
args: [parentNode, "parentContext"]
|
|
2399
2451
|
});
|
|
2400
2452
|
}
|
|
@@ -2413,8 +2465,8 @@ function processIf(node, nodeName, parentNode) {
|
|
|
2413
2465
|
fns: functionsToProcess
|
|
2414
2466
|
};
|
|
2415
2467
|
}
|
|
2416
|
-
function processConsequent(node, nodeName, parentNode) {
|
|
2417
|
-
return node.consequent.map((child, i) => processNode(child, `${nodeName}_${i}`, parentNode)).flat();
|
|
2468
|
+
function processConsequent(node, nodeName, parentNode, compilerContext) {
|
|
2469
|
+
return node.consequent.map((child, i) => processNode(child, `${nodeName}_${i}`, parentNode, compilerContext)).flat();
|
|
2418
2470
|
}
|
|
2419
2471
|
//#endregion
|
|
2420
2472
|
//#region ../packages/compiler/src/render-generator/states/process-switch.state.ts
|
|
@@ -2426,15 +2478,17 @@ function processConsequent(node, nodeName, parentNode) {
|
|
|
2426
2478
|
* @param node The `SwitchNode` to process.
|
|
2427
2479
|
* @param nodeName Base variable name prefix for child nodes.
|
|
2428
2480
|
* @param parentNode Variable name of the parent DOM node.
|
|
2481
|
+
* @param compilerContext - Current render scope context.
|
|
2429
2482
|
* @returns Array of generated code lines.
|
|
2430
2483
|
*/
|
|
2431
|
-
function processSwitch(node, nodeName, parentNode) {
|
|
2484
|
+
function processSwitch(node, nodeName, parentNode, compilerContext) {
|
|
2432
2485
|
const functionsToProcess = /* @__PURE__ */ new Map();
|
|
2433
2486
|
const blocks = new Array();
|
|
2434
2487
|
node.cases.forEach((caseNode, i) => {
|
|
2488
|
+
const caseContext = new CompilerContext([], compilerContext);
|
|
2435
2489
|
const caseName = caseNode.condition ? getBlockIdentifier(parentNode, `${nodeName}_${i}`, "case") : getBlockIdentifier(parentNode, nodeName, "default");
|
|
2436
2490
|
functionsToProcess.set(caseName, {
|
|
2437
|
-
code: caseNode.children.map((child, i) => processNode(child, `${nodeName}_${i}_${i}`, parentNode)).flat(),
|
|
2491
|
+
code: caseNode.children.map((child, i) => processNode(child, `${nodeName}_${i}_${i}`, parentNode, caseContext)).flat(),
|
|
2438
2492
|
args: [parentNode, "parentContext"]
|
|
2439
2493
|
});
|
|
2440
2494
|
blocks.push({
|
|
@@ -2444,7 +2498,7 @@ function processSwitch(node, nodeName, parentNode) {
|
|
|
2444
2498
|
});
|
|
2445
2499
|
return {
|
|
2446
2500
|
mainBlock: [
|
|
2447
|
-
`_switch(${parentNode}, context, () => ${resolveExpression(node.expression)}, [`,
|
|
2501
|
+
`_switch(${parentNode}, context, () => ${resolveExpression(node.expression, compilerContext)}, [`,
|
|
2448
2502
|
...indent(blocks.map(({ condition, block }) => {
|
|
2449
2503
|
return [
|
|
2450
2504
|
"{",
|
|
@@ -2465,11 +2519,10 @@ function processSwitch(node, nodeName, parentNode) {
|
|
|
2465
2519
|
* then appends it to the parent DOM node.
|
|
2466
2520
|
*
|
|
2467
2521
|
* @param node A `TextNode` or `InterpolationNode` to process.
|
|
2468
|
-
* @param nodeName - The identifier for the Text Node created
|
|
2469
2522
|
* @param parentNode Variable name of the parent DOM node.
|
|
2470
2523
|
* @returns Array of two generated code lines: the text node creation and the appendChild call.
|
|
2471
2524
|
*/
|
|
2472
|
-
function processTextAndInterpolation(node,
|
|
2525
|
+
function processTextAndInterpolation(node, parentNode) {
|
|
2473
2526
|
return [`${node.type === ASTNodeType.Text ? `_renderLiteralText(${parentNode}, context, '${node.value}')` : `_renderText(${parentNode}, context, '${node.expression.text}')`}`];
|
|
2474
2527
|
}
|
|
2475
2528
|
//#endregion
|
|
@@ -2483,9 +2536,10 @@ var nodeToProcess = /* @__PURE__ */ new Map();
|
|
|
2483
2536
|
*/
|
|
2484
2537
|
function generateRenderFunction(ast, cssVariableName) {
|
|
2485
2538
|
nodeToProcess.clear();
|
|
2539
|
+
const compilerContext = new CompilerContext();
|
|
2486
2540
|
const renderFunctions = ["_render() {", ...indent([`const ${ROOT_NODE} = this._root;`, "const context = new Context(this)"])];
|
|
2487
2541
|
if (cssVariableName) renderFunctions.push(indent(`${ROOT_NODE}.adoptedStyleSheets = [${cssVariableName}];`));
|
|
2488
|
-
renderFunctions.push(...indent([...ast.map((node, i) => [...processNode(node, i.toString(), ROOT_NODE)]).flat(), "return context;"]), "}");
|
|
2542
|
+
renderFunctions.push(...indent([...ast.map((node, i) => [...processNode(node, i.toString(), ROOT_NODE, compilerContext)]).flat(), "return context;"]), "}");
|
|
2489
2543
|
while (nodeToProcess.size) {
|
|
2490
2544
|
const [key, fnData] = nodeToProcess.entries().next().value;
|
|
2491
2545
|
renderFunctions.push(`${key}(${fnData.args.join(", ")}) {`, ...indent([
|
|
@@ -2502,33 +2556,33 @@ function generateRenderFunction(ast, cssVariableName) {
|
|
|
2502
2556
|
* For flow control nodes no single var is produced; instead multiple children
|
|
2503
2557
|
* are appended directly inside the control flow block.
|
|
2504
2558
|
*/
|
|
2505
|
-
function processNode(node, nodeName, parentNode) {
|
|
2559
|
+
function processNode(node, nodeName, parentNode, compilerContext) {
|
|
2506
2560
|
switch (node.type) {
|
|
2507
2561
|
case ASTNodeType.Text:
|
|
2508
|
-
case ASTNodeType.Interpolation: return processTextAndInterpolation(node,
|
|
2509
|
-
case ASTNodeType.Element: return processElement(node, getElementIdentifier(node, parentNode, nodeName), parentNode);
|
|
2562
|
+
case ASTNodeType.Interpolation: return processTextAndInterpolation(node, parentNode);
|
|
2563
|
+
case ASTNodeType.Element: return processElement(node, getElementIdentifier(node, parentNode, nodeName), parentNode, compilerContext);
|
|
2510
2564
|
case ASTNodeType.If:
|
|
2511
|
-
const conditionalBlockData = processIf(node, nodeName, parentNode);
|
|
2565
|
+
const conditionalBlockData = processIf(node, nodeName, parentNode, compilerContext);
|
|
2512
2566
|
conditionalBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, {
|
|
2513
2567
|
fn: () => fnBody.code,
|
|
2514
2568
|
args: fnBody.args
|
|
2515
2569
|
}));
|
|
2516
2570
|
return conditionalBlockData.mainBlock;
|
|
2517
2571
|
case ASTNodeType.For:
|
|
2518
|
-
const forBlockData = processFor(node, nodeName, parentNode);
|
|
2572
|
+
const forBlockData = processFor(node, nodeName, parentNode, compilerContext);
|
|
2519
2573
|
forBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, {
|
|
2520
2574
|
fn: () => fnBody.code,
|
|
2521
2575
|
args: fnBody.args
|
|
2522
2576
|
}));
|
|
2523
2577
|
return forBlockData.mainBlock;
|
|
2524
2578
|
case ASTNodeType.Switch:
|
|
2525
|
-
const switchBlockData = processSwitch(node, nodeName, parentNode);
|
|
2579
|
+
const switchBlockData = processSwitch(node, nodeName, parentNode, compilerContext);
|
|
2526
2580
|
switchBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, {
|
|
2527
2581
|
fn: () => fnBody.code,
|
|
2528
2582
|
args: fnBody.args
|
|
2529
2583
|
}));
|
|
2530
2584
|
return switchBlockData.mainBlock;
|
|
2531
|
-
case ASTNodeType.ConstDeclaration: return processConstDeclaration(node, nodeName, parentNode);
|
|
2585
|
+
case ASTNodeType.ConstDeclaration: return processConstDeclaration(node, nodeName, parentNode, compilerContext);
|
|
2532
2586
|
default: return [];
|
|
2533
2587
|
}
|
|
2534
2588
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xaendar/compiler",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.13",
|
|
4
4
|
"description": "A library for transpiling Xaendar Templates into JavaScript code",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@xaendar/common": "0.6.
|
|
20
|
-
"@xaendar/types": "0.6.
|
|
19
|
+
"@xaendar/common": "0.6.13",
|
|
20
|
+
"@xaendar/types": "0.6.13",
|
|
21
21
|
"typescript": "^6.0.3"
|
|
22
22
|
}
|
|
23
23
|
}
|