@tylix/compiler 0.6.0 → 1.0.0
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/LICENSE +21 -0
- package/package.json +30 -2
- package/src/ast/nodes.js +145 -23
- package/src/ast/substituteCapabilityRefs.js +52 -5
- package/src/codegen/emitStatementLines.js +60 -10
- package/src/codegen/generateExpression.js +294 -35
- package/src/codegen/generateStatement.js +125 -20
- package/src/codegen/generateTemplate.js +107 -96
- package/src/codegen/generateTemplateExpression.js +63 -10
- package/src/codegen/jsGlobals.js +62 -2
- package/src/compileComponent.js +37 -6
- package/src/lexer/Lexer.js +128 -28
- package/src/parser/Parser.js +1840 -500
- package/src/parser/parseExpressionString.js +5 -0
- package/src/parser/parseTemplate.js +153 -41
- package/src/renderPageDocument.js +71 -8
- package/src/runtime/capability.js +8 -1
- package/src/runtime/reactive.js +289 -61
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +11 -1
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +90 -27
- package/src/codegen/generateExpression.array.test.js +0 -13
- package/src/codegen/generateExpression.callexpr.test.js +0 -19
- package/src/codegen/generateExpression.test.js +0 -59
- package/src/codegen/generateMethod.asyncawait.test.js +0 -37
- package/src/codegen/generateMethod.ifstatement.test.js +0 -20
- package/src/codegen/generateMethod.test.js +0 -81
- package/src/codegen/generatePage.onmount.test.js +0 -46
- package/src/codegen/generatePage.test.js +0 -138
- package/src/codegen/generateStatement.test.js +0 -31
- package/src/codegen/generateTemplate.ifeach.test.js +0 -138
- package/src/codegen/generateTemplate.test.js +0 -149
- package/src/codegen/generateTemplateExpression.test.js +0 -34
- package/src/compileComponent.test.js +0 -121
- package/src/lexer/Lexer.test.js +0 -151
- package/src/parser/Parser.array.test.js +0 -37
- package/src/parser/Parser.asyncawait.test.js +0 -33
- package/src/parser/Parser.ifstatement.test.js +0 -20
- package/src/parser/Parser.onmount.test.js +0 -18
- package/src/parser/Parser.test.js +0 -315
- package/src/parser/parseComponent.test.js +0 -53
- package/src/parser/parsePageFile.test.js +0 -101
- package/src/parser/parseTemplate.ifeach.test.js +0 -33
- package/src/parser/parseTemplate.pre.test.js +0 -39
- package/src/parser/parseTemplate.test.js +0 -112
- package/src/renderPageDocument.test.js +0 -125
- package/src/runtime/reactive.test.js +0 -106
- package/test/dashboard-syntax.test.js +0 -116
|
@@ -16,6 +16,11 @@ export function parseExpressionString(source, options = {}) {
|
|
|
16
16
|
const expression = parser.parseExpression()
|
|
17
17
|
if (!parser.check(TokenType.EOF)) {
|
|
18
18
|
const next = parser.peek()
|
|
19
|
+
if (next.type === TokenType.SEMICOLON) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Only one expression is allowed in a template binding ("${source.trim()}"); to run several statements, put them in an action and call it`,
|
|
22
|
+
)
|
|
23
|
+
}
|
|
19
24
|
throw new Error(
|
|
20
25
|
`Unexpected "${next.value ?? next.type}" in expression "${source.trim()}"`,
|
|
21
26
|
)
|
|
@@ -9,8 +9,28 @@ import {
|
|
|
9
9
|
} from '../ast/nodes.js'
|
|
10
10
|
import { parseExpressionString } from './parseExpressionString.js'
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
const
|
|
12
|
+
// Sticky regexes: matched at TemplateParser.pos via matchAt().
|
|
13
|
+
const TAG_NAME_RE = /[a-zA-Z][a-zA-Z0-9-]*/y
|
|
14
|
+
const ATTR_NAME_RE = /[a-zA-Z_:][a-zA-Z0-9_:.-]*/y
|
|
15
|
+
|
|
16
|
+
// Elements that never have children or a closing tag, so
|
|
17
|
+
// `<br>`, `<img src="x">` and `<input>` work without a trailing "/".
|
|
18
|
+
const VOID_ELEMENTS = new Set([
|
|
19
|
+
'area',
|
|
20
|
+
'base',
|
|
21
|
+
'br',
|
|
22
|
+
'col',
|
|
23
|
+
'embed',
|
|
24
|
+
'hr',
|
|
25
|
+
'img',
|
|
26
|
+
'input',
|
|
27
|
+
'link',
|
|
28
|
+
'meta',
|
|
29
|
+
'param',
|
|
30
|
+
'source',
|
|
31
|
+
'track',
|
|
32
|
+
'wbr',
|
|
33
|
+
])
|
|
14
34
|
|
|
15
35
|
// HTML whitespace only -- NOT \s, which also matches U+00A0 and would
|
|
16
36
|
// swallow a literal non-breaking space.
|
|
@@ -31,6 +51,9 @@ const NAMED_ENTITIES = {
|
|
|
31
51
|
quot: '"',
|
|
32
52
|
apos: "'",
|
|
33
53
|
nbsp: '\u00A0',
|
|
54
|
+
ensp: '\u2002',
|
|
55
|
+
emsp: '\u2003',
|
|
56
|
+
thinsp: '\u2009',
|
|
34
57
|
larr: '\u2190',
|
|
35
58
|
rarr: '\u2192',
|
|
36
59
|
uarr: '\u2191',
|
|
@@ -38,6 +61,26 @@ const NAMED_ENTITIES = {
|
|
|
38
61
|
mdash: '\u2014',
|
|
39
62
|
ndash: '\u2013',
|
|
40
63
|
hellip: '\u2026',
|
|
64
|
+
bull: '\u2022',
|
|
65
|
+
middot: '\u00B7',
|
|
66
|
+
times: '\u00D7',
|
|
67
|
+
divide: '\u00F7',
|
|
68
|
+
plusmn: '\u00B1',
|
|
69
|
+
deg: '\u00B0',
|
|
70
|
+
laquo: '\u00AB',
|
|
71
|
+
raquo: '\u00BB',
|
|
72
|
+
lsquo: '\u2018',
|
|
73
|
+
rsquo: '\u2019',
|
|
74
|
+
ldquo: '\u201C',
|
|
75
|
+
rdquo: '\u201D',
|
|
76
|
+
euro: '\u20AC',
|
|
77
|
+
pound: '\u00A3',
|
|
78
|
+
yen: '\u00A5',
|
|
79
|
+
cent: '\u00A2',
|
|
80
|
+
sect: '\u00A7',
|
|
81
|
+
para: '\u00B6',
|
|
82
|
+
hearts: '\u2665',
|
|
83
|
+
check: '\u2713',
|
|
41
84
|
copy: '\u00A9',
|
|
42
85
|
reg: '\u00AE',
|
|
43
86
|
trade: '\u2122',
|
|
@@ -97,6 +140,9 @@ function findInterpEnd(source, from) {
|
|
|
97
140
|
// parser/languageOptions.js), e.g. { not: 'loose' }. They are passed
|
|
98
141
|
// to every expression parsed out of the template, so `{{ not a has b }}`
|
|
99
142
|
// follows the same `not` mode as script code.
|
|
143
|
+
//
|
|
144
|
+
// HTML comments (<!-- ... -->) are allowed anywhere a node is allowed
|
|
145
|
+
// and produce no output.
|
|
100
146
|
export function parseTemplate(source, options = {}) {
|
|
101
147
|
const parser = new TemplateParser(source, options)
|
|
102
148
|
const nodes = parser.parseNodes(null)
|
|
@@ -117,11 +163,7 @@ class TemplateParser {
|
|
|
117
163
|
// one of alsoStopAt, or null for a closing tag).
|
|
118
164
|
this.stopHit = null
|
|
119
165
|
// Precomputed once so lineAt() is a binary search instead of
|
|
120
|
-
// rescanning from the start on every node
|
|
121
|
-
// via indexOf() in several places (interpolations, {{#if}}/
|
|
122
|
-
// {{#each}} headers), so a running line counter threaded
|
|
123
|
-
// incrementally (the way Lexer does it) would miss newlines that
|
|
124
|
-
// fall inside a skipped range.
|
|
166
|
+
// rescanning from the start on every node.
|
|
125
167
|
this.lineStarts = [0]
|
|
126
168
|
for (let i = 0; i < source.length; i++) {
|
|
127
169
|
if (source[i] === '\n') this.lineStarts.push(i + 1)
|
|
@@ -132,9 +174,28 @@ class TemplateParser {
|
|
|
132
174
|
return this.source.slice(this.pos)
|
|
133
175
|
}
|
|
134
176
|
|
|
135
|
-
//
|
|
136
|
-
//
|
|
137
|
-
//
|
|
177
|
+
// True when the source at the current position starts with `str`.
|
|
178
|
+
// Same as remaining().startsWith(str) without copying the rest of
|
|
179
|
+
// the template on every check.
|
|
180
|
+
at(str) {
|
|
181
|
+
return this.source.startsWith(str, this.pos)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
matchAt(re) {
|
|
185
|
+
re.lastIndex = this.pos
|
|
186
|
+
return re.exec(this.source)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// A "<" starts markup only before a letter, "/" or "!--". Anything
|
|
190
|
+
// else ("a < b", "<3") is literal text.
|
|
191
|
+
isTagStart() {
|
|
192
|
+
if (this.source[this.pos] !== '<') return false
|
|
193
|
+
const next = this.source[this.pos + 1]
|
|
194
|
+
if (next === undefined) return false
|
|
195
|
+
return /[a-zA-Z/]/.test(next) || this.at('<!--')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 1-based line number containing `pos`.
|
|
138
199
|
lineAt(pos) {
|
|
139
200
|
let lo = 0
|
|
140
201
|
let hi = this.lineStarts.length - 1
|
|
@@ -150,6 +211,19 @@ class TemplateParser {
|
|
|
150
211
|
return findInterpEnd(this.source, from)
|
|
151
212
|
}
|
|
152
213
|
|
|
214
|
+
// <!-- ... --> is skipped entirely: nothing inside it is parsed,
|
|
215
|
+
// so {{ }} and tags inside a comment are inert.
|
|
216
|
+
skipComment() {
|
|
217
|
+
const line = this.lineAt(this.pos)
|
|
218
|
+
const end = this.source.indexOf('-->', this.pos + 4)
|
|
219
|
+
if (end === -1) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`Unterminated HTML comment starting at line ${line}: missing '-->'`,
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
this.pos = end + 3
|
|
225
|
+
}
|
|
226
|
+
|
|
153
227
|
// Parses sibling nodes. `stopTag` (an HTML tag name) or `stopMarker`
|
|
154
228
|
// (a literal like "{{/if}}") tell this call when to return; nested
|
|
155
229
|
// control blocks / elements consume their own closing marker before
|
|
@@ -160,41 +234,46 @@ class TemplateParser {
|
|
|
160
234
|
parseNodes(stopTag, stopMarker = null, alsoStopAt = []) {
|
|
161
235
|
const nodes = []
|
|
162
236
|
while (this.pos < this.source.length) {
|
|
163
|
-
if (stopMarker && this.
|
|
237
|
+
if (stopMarker && this.at(stopMarker)) {
|
|
164
238
|
this.pos += stopMarker.length
|
|
165
239
|
this.stopHit = stopMarker
|
|
166
240
|
return nodes
|
|
167
241
|
}
|
|
168
242
|
|
|
169
|
-
const also = alsoStopAt.find((m) => this.
|
|
243
|
+
const also = alsoStopAt.find((m) => this.at(m))
|
|
170
244
|
if (also) {
|
|
171
245
|
this.stopHit = also
|
|
172
246
|
return nodes
|
|
173
247
|
}
|
|
174
248
|
|
|
175
|
-
if (this.
|
|
249
|
+
if (this.at('<!--')) {
|
|
250
|
+
this.skipComment()
|
|
251
|
+
continue
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (this.at('</')) {
|
|
176
255
|
this.stopHit = null
|
|
177
256
|
if (stopTag === null) return nodes
|
|
178
257
|
this.consumeClosingTag(stopTag)
|
|
179
258
|
return nodes
|
|
180
259
|
}
|
|
181
260
|
|
|
182
|
-
if (this.
|
|
261
|
+
if (this.at('{{#if')) {
|
|
183
262
|
nodes.push(this.parseIfBlock())
|
|
184
263
|
continue
|
|
185
264
|
}
|
|
186
265
|
|
|
187
|
-
if (this.
|
|
266
|
+
if (this.at('{{#each')) {
|
|
188
267
|
nodes.push(this.parseEachBlock())
|
|
189
268
|
continue
|
|
190
269
|
}
|
|
191
270
|
|
|
192
|
-
if (this.
|
|
271
|
+
if (this.at('{{')) {
|
|
193
272
|
nodes.push(this.parseInterpolation())
|
|
194
273
|
continue
|
|
195
274
|
}
|
|
196
275
|
|
|
197
|
-
if (this.
|
|
276
|
+
if (this.isTagStart()) {
|
|
198
277
|
nodes.push(this.parseElement())
|
|
199
278
|
continue
|
|
200
279
|
}
|
|
@@ -224,12 +303,16 @@ class TemplateParser {
|
|
|
224
303
|
return nodes
|
|
225
304
|
}
|
|
226
305
|
|
|
306
|
+
// Always consumes at least one character: when parseNodes lands
|
|
307
|
+
// here the position is not at "{{" or real markup, so a "<" at the
|
|
308
|
+
// very start is a literal "<" (as in "a < b").
|
|
227
309
|
parseText() {
|
|
228
310
|
const start = this.pos
|
|
311
|
+
this.pos++
|
|
229
312
|
while (
|
|
230
313
|
this.pos < this.source.length &&
|
|
231
|
-
this.
|
|
232
|
-
!this.
|
|
314
|
+
!this.isTagStart() &&
|
|
315
|
+
!this.at('{{')
|
|
233
316
|
) {
|
|
234
317
|
this.pos++
|
|
235
318
|
}
|
|
@@ -371,7 +454,7 @@ class TemplateParser {
|
|
|
371
454
|
const attributes = this.parseAttributes()
|
|
372
455
|
|
|
373
456
|
this.skipWhitespace()
|
|
374
|
-
if (this.
|
|
457
|
+
if (this.at('/>')) {
|
|
375
458
|
this.pos += 2
|
|
376
459
|
return { ...ElementNode(tag, attributes, []), line }
|
|
377
460
|
}
|
|
@@ -381,6 +464,11 @@ class TemplateParser {
|
|
|
381
464
|
}
|
|
382
465
|
this.pos++
|
|
383
466
|
|
|
467
|
+
// <br>, <img ...>, <input ...>: no children, no closing tag.
|
|
468
|
+
if (VOID_ELEMENTS.has(tag)) {
|
|
469
|
+
return { ...ElementNode(tag, attributes, []), line }
|
|
470
|
+
}
|
|
471
|
+
|
|
384
472
|
// <pre> is a raw-text element, same idea as <script>/<style> in
|
|
385
473
|
// real HTML: its content is captured verbatim -- no whitespace
|
|
386
474
|
// collapsing, and critically no {{ }} interpolation/control-flow
|
|
@@ -421,7 +509,7 @@ class TemplateParser {
|
|
|
421
509
|
}
|
|
422
510
|
|
|
423
511
|
readTagName() {
|
|
424
|
-
const match =
|
|
512
|
+
const match = this.matchAt(TAG_NAME_RE)
|
|
425
513
|
if (!match) {
|
|
426
514
|
throw new Error(`Expected a tag name at position ${this.pos}`)
|
|
427
515
|
}
|
|
@@ -435,7 +523,7 @@ class TemplateParser {
|
|
|
435
523
|
while (
|
|
436
524
|
this.pos < this.source.length &&
|
|
437
525
|
this.source[this.pos] !== '>' &&
|
|
438
|
-
!this.
|
|
526
|
+
!this.at('/>')
|
|
439
527
|
) {
|
|
440
528
|
attributes.push(this.parseAttribute())
|
|
441
529
|
this.skipWhitespace()
|
|
@@ -445,7 +533,7 @@ class TemplateParser {
|
|
|
445
533
|
|
|
446
534
|
parseAttribute() {
|
|
447
535
|
const line = this.lineAt(this.pos)
|
|
448
|
-
const nameMatch =
|
|
536
|
+
const nameMatch = this.matchAt(ATTR_NAME_RE)
|
|
449
537
|
if (!nameMatch) {
|
|
450
538
|
throw new Error(`Expected an attribute name at position ${this.pos}`)
|
|
451
539
|
}
|
|
@@ -454,10 +542,7 @@ class TemplateParser {
|
|
|
454
542
|
this.skipWhitespace()
|
|
455
543
|
|
|
456
544
|
// No '=' following the name means a bare boolean attribute
|
|
457
|
-
// (e.g. <input disabled>, <div data-tylix-slot>)
|
|
458
|
-
// parse error -- matches real HTML's own boolean-attribute
|
|
459
|
-
// convention, so devs coming from plain HTML don't hit a
|
|
460
|
-
// surprising "Expected '=' after attribute name" wall.
|
|
545
|
+
// (e.g. <input disabled>, <div data-tylix-slot>).
|
|
461
546
|
if (this.source[this.pos] !== '=') {
|
|
462
547
|
return { ...AttributeNode(name, true, false), line }
|
|
463
548
|
}
|
|
@@ -466,19 +551,45 @@ class TemplateParser {
|
|
|
466
551
|
this.skipWhitespace()
|
|
467
552
|
|
|
468
553
|
const quote = this.source[this.pos]
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
this.pos++
|
|
473
|
-
const valueStart = this.pos
|
|
474
|
-
while (this.pos < this.source.length && this.source[this.pos] !== quote) {
|
|
554
|
+
let rawValue
|
|
555
|
+
|
|
556
|
+
if (quote === '"' || quote === "'") {
|
|
475
557
|
this.pos++
|
|
558
|
+
const valueStart = this.pos
|
|
559
|
+
while (this.pos < this.source.length && this.source[this.pos] !== quote) {
|
|
560
|
+
this.pos++
|
|
561
|
+
}
|
|
562
|
+
if (this.pos >= this.source.length) {
|
|
563
|
+
throw new Error(`Unterminated attribute value for '${name}'`)
|
|
564
|
+
}
|
|
565
|
+
rawValue = this.source.slice(valueStart, this.pos)
|
|
566
|
+
this.pos++
|
|
567
|
+
} else {
|
|
568
|
+
// Unquoted value (<input type=text>): runs to whitespace or
|
|
569
|
+
// '>'. A {{ ... }} inside it is scanned as a unit, so
|
|
570
|
+
// `value={{ a + b }}` keeps its spaces.
|
|
571
|
+
const valueStart = this.pos
|
|
572
|
+
while (
|
|
573
|
+
this.pos < this.source.length &&
|
|
574
|
+
!/[\s>]/.test(this.source[this.pos])
|
|
575
|
+
) {
|
|
576
|
+
if (this.at('{{')) {
|
|
577
|
+
const end = this.findInterpEnd(this.pos + 2)
|
|
578
|
+
if (end === -1) {
|
|
579
|
+
throw new Error(
|
|
580
|
+
`Unterminated '{{' in attribute '${name}' at line ${line}`,
|
|
581
|
+
)
|
|
582
|
+
}
|
|
583
|
+
this.pos = end + 2
|
|
584
|
+
} else {
|
|
585
|
+
this.pos++
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
rawValue = this.source.slice(valueStart, this.pos)
|
|
589
|
+
if (rawValue === '') {
|
|
590
|
+
throw new Error(`Expected a value for attribute '${name}'`)
|
|
591
|
+
}
|
|
476
592
|
}
|
|
477
|
-
if (this.pos >= this.source.length) {
|
|
478
|
-
throw new Error(`Unterminated attribute value for '${name}'`)
|
|
479
|
-
}
|
|
480
|
-
const rawValue = this.source.slice(valueStart, this.pos)
|
|
481
|
-
this.pos++
|
|
482
593
|
|
|
483
594
|
return { ...this.buildAttribute(name, rawValue, line), line }
|
|
484
595
|
}
|
|
@@ -488,9 +599,10 @@ class TemplateParser {
|
|
|
488
599
|
// "{{ expr }}" -> the expression itself (type kept:
|
|
489
600
|
// numbers stay numbers, etc.)
|
|
490
601
|
// "btn {{ a }} x-{{ b }}" -> a template literal
|
|
602
|
+
// Literal text has HTML entities decoded (title="a & b").
|
|
491
603
|
buildAttribute(name, rawValue, line) {
|
|
492
604
|
if (!rawValue.includes('{{')) {
|
|
493
|
-
return AttributeNode(name, rawValue, false)
|
|
605
|
+
return AttributeNode(name, decodeHtmlEntities(rawValue), false)
|
|
494
606
|
}
|
|
495
607
|
|
|
496
608
|
const parts = []
|
|
@@ -505,7 +617,7 @@ class TemplateParser {
|
|
|
505
617
|
)
|
|
506
618
|
}
|
|
507
619
|
if (text) {
|
|
508
|
-
parts.push({ type: 'text', value: text })
|
|
620
|
+
parts.push({ type: 'text', value: decodeHtmlEntities(text) })
|
|
509
621
|
text = ''
|
|
510
622
|
}
|
|
511
623
|
parts.push({
|
|
@@ -520,7 +632,7 @@ class TemplateParser {
|
|
|
520
632
|
text += rawValue[i++]
|
|
521
633
|
}
|
|
522
634
|
}
|
|
523
|
-
if (text) parts.push({ type: 'text', value: text })
|
|
635
|
+
if (text) parts.push({ type: 'text', value: decodeHtmlEntities(text) })
|
|
524
636
|
|
|
525
637
|
if (parts.length === 1 && parts[0].type === 'expr') {
|
|
526
638
|
return AttributeNode(name, parts[0].expression, true)
|
|
@@ -11,6 +11,10 @@ import { normalizeLanguageOptions } from './parser/languageOptions.js'
|
|
|
11
11
|
import { generatePage } from './codegen/generatePage.js'
|
|
12
12
|
import { generateTemplate } from './codegen/generateTemplate.js'
|
|
13
13
|
import { generateCapability } from './codegen/generateCapability.js'
|
|
14
|
+
import {
|
|
15
|
+
generateStatement,
|
|
16
|
+
declaredNames,
|
|
17
|
+
} from './codegen/generateStatement.js'
|
|
14
18
|
import { substituteCapabilityRefs } from './ast/substituteCapabilityRefs.js'
|
|
15
19
|
|
|
16
20
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
@@ -38,6 +42,18 @@ const NOT_FOUND_SOURCE = fs
|
|
|
38
42
|
.readFileSync(path.join(__dirname, 'runtime', 'notFound.js'), 'utf-8')
|
|
39
43
|
.replace(/export\s+/g, '')
|
|
40
44
|
|
|
45
|
+
// Attaches a class's module-level helpers (see withHelpers) to a fresh
|
|
46
|
+
// instance, so templates can call them: a bare `formatPrice(x)` in a
|
|
47
|
+
// template compiles to `instance.formatPrice(x)`.
|
|
48
|
+
const EXPOSE_HELPERS_SOURCE = `
|
|
49
|
+
function __tylixExpose(instance, cls) {
|
|
50
|
+
var helpers = cls && cls.__helpers;
|
|
51
|
+
if (helpers) {
|
|
52
|
+
for (var key of Object.keys(helpers)) instance[key] = helpers[key];
|
|
53
|
+
}
|
|
54
|
+
return instance;
|
|
55
|
+
}`
|
|
56
|
+
|
|
41
57
|
function escapeHtml(text) {
|
|
42
58
|
return String(text)
|
|
43
59
|
.replace(/&/g, '&')
|
|
@@ -197,7 +213,14 @@ async function loadLoadingPage(loadingFile) {
|
|
|
197
213
|
return source
|
|
198
214
|
}
|
|
199
215
|
|
|
200
|
-
const EMPTY_PAGE = {
|
|
216
|
+
const EMPTY_PAGE = {
|
|
217
|
+
props: [],
|
|
218
|
+
state: [],
|
|
219
|
+
computed: [],
|
|
220
|
+
actions: [],
|
|
221
|
+
uses: [],
|
|
222
|
+
helpers: [],
|
|
223
|
+
}
|
|
201
224
|
|
|
202
225
|
// Turns a registry key (a tag name such as "AdminCard" or "my-card")
|
|
203
226
|
// into a class name that is always a valid, unique identifier. The
|
|
@@ -208,6 +231,32 @@ function childClassName(name, index) {
|
|
|
208
231
|
return `__C${index}_${String(name).replace(/[^A-Za-z0-9_$]/g, '_')}`
|
|
209
232
|
}
|
|
210
233
|
|
|
234
|
+
// Module-level declarations of a page or component (`function`,
|
|
235
|
+
// `const`, `let`, `class`, `enum`; `type`/`interface` are erased) are
|
|
236
|
+
// emitted in a function scope of their own around that class, so two
|
|
237
|
+
// components can both define a helper with the same name. The names are
|
|
238
|
+
// stored on the class (`__helpers`) and attached to every instance by
|
|
239
|
+
// __tylixExpose, which is how templates reach them. With no runtime
|
|
240
|
+
// helpers the class source is returned untouched.
|
|
241
|
+
function withHelpers(classSource, className, helpers) {
|
|
242
|
+
const source = (helpers ?? [])
|
|
243
|
+
.map((h) => generateStatement(h, false))
|
|
244
|
+
.filter((s) => s.trim().length > 0)
|
|
245
|
+
.join('\n')
|
|
246
|
+
if (source.length === 0) return classSource
|
|
247
|
+
|
|
248
|
+
const exposed = helpers
|
|
249
|
+
.flatMap(declaredNames)
|
|
250
|
+
.map((n) => `${JSON.stringify(n)}: ${n}`)
|
|
251
|
+
.join(', ')
|
|
252
|
+
return `const ${className} = (() => {
|
|
253
|
+
${source}
|
|
254
|
+
const __Class = ${classSource};
|
|
255
|
+
__Class.__helpers = { ${exposed} };
|
|
256
|
+
return __Class;
|
|
257
|
+
})();`
|
|
258
|
+
}
|
|
259
|
+
|
|
211
260
|
// `fallbackClassName` ALWAYS decides the class name -- for native
|
|
212
261
|
// `component Card` files too -- so two files that both declare Card
|
|
213
262
|
// can live in one page.
|
|
@@ -222,7 +271,11 @@ function compileComponentSource(source, fallbackClassName, language) {
|
|
|
222
271
|
: EMPTY_PAGE,
|
|
223
272
|
)
|
|
224
273
|
|
|
225
|
-
const classSource =
|
|
274
|
+
const classSource = withHelpers(
|
|
275
|
+
generatePage(pageNode, fallbackClassName),
|
|
276
|
+
fallbackClassName,
|
|
277
|
+
pageNode.helpers,
|
|
278
|
+
)
|
|
226
279
|
const templateNodes = parseTemplate(template, language)
|
|
227
280
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
228
281
|
|
|
@@ -237,7 +290,11 @@ function compileComponentSource(source, fallbackClassName, language) {
|
|
|
237
290
|
: EMPTY_PAGE,
|
|
238
291
|
)
|
|
239
292
|
|
|
240
|
-
const classSource =
|
|
293
|
+
const classSource = withHelpers(
|
|
294
|
+
generatePage(pageNode, fallbackClassName),
|
|
295
|
+
fallbackClassName,
|
|
296
|
+
pageNode.helpers,
|
|
297
|
+
)
|
|
241
298
|
const templateNodes = parseTemplate(template, language)
|
|
242
299
|
const { code, rootVar } = generateTemplate(templateNodes)
|
|
243
300
|
|
|
@@ -327,7 +384,11 @@ export async function renderPageDocument(
|
|
|
327
384
|
: EMPTY_PAGE,
|
|
328
385
|
)
|
|
329
386
|
|
|
330
|
-
const classSource =
|
|
387
|
+
const classSource = withHelpers(
|
|
388
|
+
generatePage(pageNode, pageName),
|
|
389
|
+
pageName,
|
|
390
|
+
pageNode.helpers,
|
|
391
|
+
)
|
|
331
392
|
|
|
332
393
|
const templateNodes = parseTemplateWithLineOffset(
|
|
333
394
|
template,
|
|
@@ -366,7 +427,7 @@ export async function renderPageDocument(
|
|
|
366
427
|
loadingClassSource = loadingCompiled.classSource
|
|
367
428
|
loadingMountCode = `
|
|
368
429
|
try {
|
|
369
|
-
const loadingInstance = new ${loadingCompiled.className}({ loading: __tylixLoading });
|
|
430
|
+
const loadingInstance = __tylixExpose(new ${loadingCompiled.className}({ loading: __tylixLoading }), ${loadingCompiled.className});
|
|
370
431
|
(function (document, instance) {
|
|
371
432
|
${loadingCompiled.code}
|
|
372
433
|
document.body.appendChild(${loadingCompiled.rootVar});
|
|
@@ -408,7 +469,7 @@ ${loadingCompiled.code}
|
|
|
408
469
|
const { code: childCode, rootVar: childRootVar, className } = compiled
|
|
409
470
|
return ` components[${label}] = {
|
|
410
471
|
mount(document, props = {}) {
|
|
411
|
-
const instance = new ${className}(props);
|
|
472
|
+
const instance = __tylixExpose(new ${className}(props), ${className});
|
|
412
473
|
const node = (function (document, instance) {
|
|
413
474
|
${childCode}
|
|
414
475
|
return ${childRootVar};
|
|
@@ -439,7 +500,7 @@ ${childCode}
|
|
|
439
500
|
}
|
|
440
501
|
layoutClassSource = layoutCompiled.classSource
|
|
441
502
|
layoutMountCode = `
|
|
442
|
-
const layoutInstance = new ${layoutCompiled.className}();
|
|
503
|
+
const layoutInstance = __tylixExpose(new ${layoutCompiled.className}(), ${layoutCompiled.className});
|
|
443
504
|
if (typeof layoutInstance.__ready === "function") {
|
|
444
505
|
try {
|
|
445
506
|
await layoutInstance.__ready();
|
|
@@ -477,6 +538,8 @@ ${loadingClassSource || !loadingBar ? 'window.__TYLIX_CUSTOM_LOADING__ = true;'
|
|
|
477
538
|
|
|
478
539
|
${RUNTIME_SOURCE}
|
|
479
540
|
|
|
541
|
+
${EXPOSE_HELPERS_SOURCE}
|
|
542
|
+
|
|
480
543
|
${CAPABILITY_SOURCE}
|
|
481
544
|
|
|
482
545
|
${capabilityDefinitions}
|
|
@@ -500,7 +563,7 @@ ${layoutClassSource}
|
|
|
500
563
|
${loadingClassSource}
|
|
501
564
|
|
|
502
565
|
document.addEventListener("DOMContentLoaded", () => {
|
|
503
|
-
const instance = new ${pageName}(${JSON.stringify(props)});
|
|
566
|
+
const instance = __tylixExpose(new ${pageName}(${JSON.stringify(props)}), ${pageName});
|
|
504
567
|
const components = {};
|
|
505
568
|
${childRegistrations}
|
|
506
569
|
(async function (document, instance, components) {
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
// async init() { ... }, // optional, runs once; `this`
|
|
16
16
|
// // bound the same way
|
|
17
17
|
// }
|
|
18
|
+
//
|
|
19
|
+
// `reactive` and `untracked` come from runtime/reactive.js, which is
|
|
20
|
+
// concatenated ahead of this file in the inline script.
|
|
18
21
|
|
|
19
22
|
const registry = new Map()
|
|
20
23
|
|
|
@@ -58,8 +61,12 @@ export function resolveCapability(name) {
|
|
|
58
61
|
entry.initStarted = true
|
|
59
62
|
if (entry.def.init) {
|
|
60
63
|
try {
|
|
64
|
+
// Resolution usually happens inside a template effect (a
|
|
65
|
+
// `uses` getter read during render). Run init untracked, so
|
|
66
|
+
// the state it reads before its first `await` does not
|
|
67
|
+
// subscribe that unrelated effect.
|
|
61
68
|
entry.initPromise = Promise.resolve(
|
|
62
|
-
entry.def.init.call(entry.instance),
|
|
69
|
+
untracked(() => entry.def.init.call(entry.instance)),
|
|
63
70
|
)
|
|
64
71
|
} catch (err) {
|
|
65
72
|
entry.initPromise = Promise.reject(err)
|