@tylix/compiler 0.5.2 → 0.6.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/package.json +1 -1
- package/src/ast/nodes.js +91 -10
- package/src/ast/substituteCapabilityRefs.js +135 -58
- package/src/codegen/emitStatementLines.js +42 -48
- package/src/codegen/generateExpression.js +68 -24
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +56 -12
- package/src/codegen/generateTemplate.js +124 -53
- package/src/codegen/generateTemplateExpression.js +40 -50
- package/src/codegen/jsGlobals.js +151 -0
- package/src/compileComponent.js +2 -2
- package/src/imports/extractImports.js +112 -26
- package/src/imports/resolveImports.js +186 -0
- package/src/index.js +3 -2
- package/src/lexer/Lexer.js +308 -213
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +680 -169
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +18 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +202 -26
- package/src/renderPageDocument.js +270 -51
- package/src/runtime/capability.js +32 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +128 -58
- package/src/typecheck/buildVirtualTs.js +13 -12
- package/src/typecheck/typedTemplateExpr.js +115 -36
- package/src/typecheckPage.js +28 -4
- package/src/imports/resolvePackageComponents.js +0 -41
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Per-project language switches. Defaults keep legacy behaviour.
|
|
2
|
+
// not: 'tight' -- `not` binds like `!` (legacy, default)
|
|
3
|
+
// not: 'loose' -- the WORD `not` binds looser than is / has / exists
|
|
4
|
+
// (`not items has x`, `not user exists`). The symbol
|
|
5
|
+
// `!` stays tight in both modes.
|
|
6
|
+
export const DEFAULT_LANGUAGE_OPTIONS = Object.freeze({ not: 'tight' })
|
|
7
|
+
|
|
8
|
+
export function normalizeLanguageOptions(options = {}) {
|
|
9
|
+
const not = options?.not ?? DEFAULT_LANGUAGE_OPTIONS.not
|
|
10
|
+
if (not !== 'tight' && not !== 'loose') {
|
|
11
|
+
throw new Error(
|
|
12
|
+
`Invalid language option not: "${not}" (expected "tight" or "loose")`,
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
return { ...DEFAULT_LANGUAGE_OPTIONS, ...options, not }
|
|
16
|
+
}
|
|
@@ -1,13 +1,24 @@
|
|
|
1
|
-
import { Lexer } from
|
|
2
|
-
import { Parser } from
|
|
1
|
+
import { Lexer } from '../lexer/Lexer.js'
|
|
2
|
+
import { Parser } from './Parser.js'
|
|
3
|
+
import { TokenType } from '../lexer/tokenTypes.js'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Parses a single expression (no statements) from a raw string, e.g.
|
|
6
7
|
* the inside of a template interpolation: "count * 2" or "increment".
|
|
7
8
|
* Reuses Parser's expression grammar directly rather than duplicating it.
|
|
9
|
+
*
|
|
10
|
+
* Anything left over after the expression is an error rather than
|
|
11
|
+
* being silently dropped (`{{ a b }}`, `{{ a(); b() }}`).
|
|
8
12
|
*/
|
|
9
|
-
export function parseExpressionString(source) {
|
|
10
|
-
const tokens = new Lexer(source).tokenize()
|
|
11
|
-
const parser = new Parser(tokens)
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
export function parseExpressionString(source, options = {}) {
|
|
14
|
+
const tokens = new Lexer(source).tokenize()
|
|
15
|
+
const parser = new Parser(tokens, options)
|
|
16
|
+
const expression = parser.parseExpression()
|
|
17
|
+
if (!parser.check(TokenType.EOF)) {
|
|
18
|
+
const next = parser.peek()
|
|
19
|
+
throw new Error(
|
|
20
|
+
`Unexpected "${next.value ?? next.type}" in expression "${source.trim()}"`,
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
return expression
|
|
24
|
+
}
|
|
@@ -1,4 +1,102 @@
|
|
|
1
|
-
|
|
1
|
+
// Locates the top-level `uses | state | computed | action | template |
|
|
2
|
+
// style` section keywords. A keyword only counts when:
|
|
3
|
+
// - it is the first thing on its line, and
|
|
4
|
+
// - it is not followed by `:` `(` `=` or `,` (that makes it a field
|
|
5
|
+
// or attribute name, e.g. `style: "dark"` or `style="..."`), and
|
|
6
|
+
// - before the template: it sits at brace/paren depth 0, so keys
|
|
7
|
+
// inside action bodies and object literals are ignored. Strings,
|
|
8
|
+
// template literals and comments are skipped while tracking depth.
|
|
9
|
+
// - after the template starts: only `style` is looked for (the
|
|
10
|
+
// template body is HTML, where quotes and braces mean nothing).
|
|
11
|
+
const SECTION_RE = /^(uses|state|computed|action|template|style)(?![\w$])/
|
|
12
|
+
|
|
13
|
+
export function findSectionBoundaries(source) {
|
|
14
|
+
const boundaries = []
|
|
15
|
+
const n = source.length
|
|
16
|
+
let i = 0
|
|
17
|
+
let depth = 0
|
|
18
|
+
let lineStart = true
|
|
19
|
+
let inTemplate = false
|
|
20
|
+
|
|
21
|
+
while (i < n) {
|
|
22
|
+
const c = source[i]
|
|
23
|
+
|
|
24
|
+
if (c === '\n') {
|
|
25
|
+
lineStart = true
|
|
26
|
+
i++
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
if (c === ' ' || c === '\t' || c === '\r') {
|
|
30
|
+
i++
|
|
31
|
+
continue
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (lineStart) {
|
|
35
|
+
lineStart = false
|
|
36
|
+
const m = SECTION_RE.exec(source.slice(i, i + 16))
|
|
37
|
+
if (m) {
|
|
38
|
+
const keyword = m[1]
|
|
39
|
+
let j = i + keyword.length
|
|
40
|
+
while (source[j] === ' ' || source[j] === '\t') j++
|
|
41
|
+
const next = source[j]
|
|
42
|
+
const isFieldName =
|
|
43
|
+
next === ':' || next === '(' || next === '=' || next === ','
|
|
44
|
+
const allowed = inTemplate ? keyword === 'style' : depth === 0
|
|
45
|
+
if (!isFieldName && allowed) {
|
|
46
|
+
boundaries.push({
|
|
47
|
+
keyword,
|
|
48
|
+
start: i,
|
|
49
|
+
contentStart: i + keyword.length,
|
|
50
|
+
})
|
|
51
|
+
if (keyword === 'template') inTemplate = true
|
|
52
|
+
i += keyword.length
|
|
53
|
+
continue
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (inTemplate) {
|
|
59
|
+
i++
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// --- script-mode scanning ---
|
|
64
|
+
if (c === '/' && source[i + 1] === '/') {
|
|
65
|
+
while (i < n && source[i] !== '\n') i++
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
if (c === '/' && source[i + 1] === '*') {
|
|
69
|
+
const close = source.indexOf('*/', i + 2)
|
|
70
|
+
i = close === -1 ? n : close + 2
|
|
71
|
+
continue
|
|
72
|
+
}
|
|
73
|
+
if (c === '"' || c === "'") {
|
|
74
|
+
i++
|
|
75
|
+
// A quote never spans lines, so a stray one (inside a regex
|
|
76
|
+
// literal, say) can't swallow the rest of the file.
|
|
77
|
+
while (i < n && source[i] !== c && source[i] !== '\n') {
|
|
78
|
+
if (source[i] === '\\') i++
|
|
79
|
+
i++
|
|
80
|
+
}
|
|
81
|
+
if (source[i] === c) i++
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
if (c === '`') {
|
|
85
|
+
i++
|
|
86
|
+
while (i < n && source[i] !== '`') {
|
|
87
|
+
if (source[i] === '\\') i++
|
|
88
|
+
i++
|
|
89
|
+
}
|
|
90
|
+
i++
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
if (c === '{' || c === '(' || c === '[') depth++
|
|
94
|
+
else if (c === '}' || c === ')' || c === ']') depth = Math.max(0, depth - 1)
|
|
95
|
+
i++
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return boundaries
|
|
99
|
+
}
|
|
2
100
|
|
|
3
101
|
export function parsePageFile(source) {
|
|
4
102
|
const pageMatch = /^\s*page\s+([A-Za-z_$][A-Za-z0-9_$]*)/.exec(source)
|
|
@@ -12,10 +110,24 @@ export function parsePageFile(source) {
|
|
|
12
110
|
)
|
|
13
111
|
|
|
14
112
|
const boundaries = findSectionBoundaries(afterPageDeclaration)
|
|
113
|
+
const templateBoundary = boundaries.find((b) => b.keyword === 'template')
|
|
114
|
+
const styleBoundary = boundaries.find((b) => b.keyword === 'style')
|
|
115
|
+
|
|
116
|
+
if (!templateBoundary) {
|
|
117
|
+
throw new Error('.tyx file is missing a required "template" section.')
|
|
118
|
+
}
|
|
15
119
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
120
|
+
// A section's content runs until the next template/style keyword,
|
|
121
|
+
// or the end of the file -- so `style` written before `template`
|
|
122
|
+
// no longer swallows the template.
|
|
123
|
+
const contentBoundaries = [templateBoundary, styleBoundary].filter(Boolean)
|
|
124
|
+
const endOf = (b) =>
|
|
125
|
+
contentBoundaries
|
|
126
|
+
.filter((x) => x.start > b.start)
|
|
127
|
+
.map((x) => x.start)
|
|
128
|
+
.sort((a, c) => a - c)[0] ?? afterPageDeclaration.length
|
|
129
|
+
|
|
130
|
+
const scriptEnd = Math.min(...contentBoundaries.map((b) => b.start))
|
|
19
131
|
const rawScriptSource = afterPageDeclaration.slice(0, scriptEnd)
|
|
20
132
|
const scriptSource = rawScriptSource.trim()
|
|
21
133
|
|
|
@@ -27,44 +139,30 @@ export function parsePageFile(source) {
|
|
|
27
139
|
1
|
|
28
140
|
const scriptStartLine = linesBeforeScript + trimmedPrefix.split('\n').length
|
|
29
141
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (templateBoundary) {
|
|
36
|
-
const templateEnd =
|
|
37
|
-
styleBoundary && styleBoundary.start > templateBoundary.start
|
|
38
|
-
? styleBoundary.start
|
|
39
|
-
: afterPageDeclaration.length
|
|
40
|
-
const rawTemplateSource = afterPageDeclaration.slice(
|
|
41
|
-
templateBoundary.contentStart,
|
|
42
|
-
templateEnd,
|
|
43
|
-
)
|
|
44
|
-
template = rawTemplateSource.trim()
|
|
45
|
-
|
|
46
|
-
const templateLeadingWhitespaceLength =
|
|
47
|
-
rawTemplateSource.length - rawTemplateSource.trimStart().length
|
|
48
|
-
const templateTrimmedPrefix = rawTemplateSource.slice(
|
|
49
|
-
0,
|
|
50
|
-
templateLeadingWhitespaceLength,
|
|
51
|
-
)
|
|
52
|
-
const absoluteContentStart =
|
|
53
|
-
pageMatch.index + pageMatch[0].length + templateBoundary.contentStart
|
|
54
|
-
const linesBeforeTemplate =
|
|
55
|
-
source.slice(0, absoluteContentStart).split('\n').length - 1
|
|
56
|
-
templateStartLine =
|
|
57
|
-
linesBeforeTemplate + templateTrimmedPrefix.split('\n').length
|
|
58
|
-
}
|
|
142
|
+
const rawTemplateSource = afterPageDeclaration.slice(
|
|
143
|
+
templateBoundary.contentStart,
|
|
144
|
+
endOf(templateBoundary),
|
|
145
|
+
)
|
|
146
|
+
const template = rawTemplateSource.trim()
|
|
59
147
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
148
|
+
const templateLeadingWhitespaceLength =
|
|
149
|
+
rawTemplateSource.length - rawTemplateSource.trimStart().length
|
|
150
|
+
const templateTrimmedPrefix = rawTemplateSource.slice(
|
|
151
|
+
0,
|
|
152
|
+
templateLeadingWhitespaceLength,
|
|
153
|
+
)
|
|
154
|
+
const absoluteContentStart =
|
|
155
|
+
pageMatch.index + pageMatch[0].length + templateBoundary.contentStart
|
|
156
|
+
const linesBeforeTemplate =
|
|
157
|
+
source.slice(0, absoluteContentStart).split('\n').length - 1
|
|
158
|
+
const templateStartLine =
|
|
159
|
+
linesBeforeTemplate + templateTrimmedPrefix.split('\n').length
|
|
64
160
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
161
|
+
const style = styleBoundary
|
|
162
|
+
? afterPageDeclaration
|
|
163
|
+
.slice(styleBoundary.contentStart, endOf(styleBoundary))
|
|
164
|
+
.trim()
|
|
165
|
+
: ''
|
|
68
166
|
|
|
69
167
|
return {
|
|
70
168
|
pageName,
|
|
@@ -74,21 +172,4 @@ export function parsePageFile(source) {
|
|
|
74
172
|
scriptStartLine,
|
|
75
173
|
templateStartLine,
|
|
76
174
|
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export function findSectionBoundaries(source) {
|
|
80
|
-
const boundaries = []
|
|
81
|
-
const lineStartPattern =
|
|
82
|
-
/(^|\n)[ \t]*(uses|state|computed|action|template|style)\b/g
|
|
83
|
-
let match
|
|
84
|
-
while ((match = lineStartPattern.exec(source)) !== null) {
|
|
85
|
-
const keyword = match[2]
|
|
86
|
-
const keywordStart = match.index + match[0].indexOf(keyword)
|
|
87
|
-
boundaries.push({
|
|
88
|
-
keyword,
|
|
89
|
-
start: keywordStart,
|
|
90
|
-
contentStart: keywordStart + keyword.length,
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
return boundaries
|
|
94
|
-
}
|
|
175
|
+
}
|
|
@@ -5,13 +5,24 @@ import {
|
|
|
5
5
|
InterpolationNode,
|
|
6
6
|
IfNode,
|
|
7
7
|
EachNode,
|
|
8
|
+
TemplateLiteralExpr,
|
|
8
9
|
} from '../ast/nodes.js'
|
|
9
10
|
import { parseExpressionString } from './parseExpressionString.js'
|
|
10
11
|
|
|
11
12
|
const TAG_NAME_RE = /^[a-zA-Z][a-zA-Z0-9-]*/
|
|
12
13
|
const ATTR_NAME_RE = /^[a-zA-Z][a-zA-Z0-9-]*/
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
// HTML whitespace only -- NOT \s, which also matches U+00A0 and would
|
|
16
|
+
// swallow a literal non-breaking space.
|
|
17
|
+
const WS_ONLY_RE = /^[ \t\r\n\f]*$/
|
|
18
|
+
const WS_RUN_RE = /[ \t\r\n\f]+/g
|
|
19
|
+
|
|
20
|
+
// {{#each item in items}} {{#each item of items}}
|
|
21
|
+
// {{#each item, i in items}} {{#each [k, v] in Object.entries(o)}}
|
|
22
|
+
// {{#each { id, name } in rows}}
|
|
23
|
+
// group 1: item (name or destructuring pattern), 2: index name, 3: iterable
|
|
24
|
+
const EACH_HEADER_RE =
|
|
25
|
+
/^(\[[^\]]*\]|\{[^}]*\}|[A-Za-z_$][A-Za-z0-9_$]*)(?:\s*,\s*([A-Za-z_$][A-Za-z0-9_$]*))?\s+(?:in|of)\s+([\s\S]+)$/
|
|
15
26
|
|
|
16
27
|
const NAMED_ENTITIES = {
|
|
17
28
|
amp: '&',
|
|
@@ -54,8 +65,40 @@ function decodeHtmlEntities(text) {
|
|
|
54
65
|
})
|
|
55
66
|
}
|
|
56
67
|
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
// Finds the "}}" that closes a {{ ... }} whose content starts at
|
|
69
|
+
// `from` in `source`. Skips nested braces and string/template
|
|
70
|
+
// literals, so {{ fn({a:{b:1}}) }} and {{ "}}" }} work, where a plain
|
|
71
|
+
// indexOf('}}') would cut the expression short. A free function so
|
|
72
|
+
// attribute values can be scanned the same way as template text.
|
|
73
|
+
function findInterpEnd(source, from) {
|
|
74
|
+
let depth = 0
|
|
75
|
+
let quote = null
|
|
76
|
+
for (let i = from; i < source.length; i++) {
|
|
77
|
+
const c = source[i]
|
|
78
|
+
if (quote) {
|
|
79
|
+
if (c === '\\') i++
|
|
80
|
+
else if (c === quote) quote = null
|
|
81
|
+
continue
|
|
82
|
+
}
|
|
83
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
84
|
+
quote = c
|
|
85
|
+
continue
|
|
86
|
+
}
|
|
87
|
+
if (c === '{') depth++
|
|
88
|
+
else if (c === '}') {
|
|
89
|
+
if (depth === 0 && source[i + 1] === '}') return i
|
|
90
|
+
if (depth > 0) depth--
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return -1
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// `options` are the project's language options (see
|
|
97
|
+
// parser/languageOptions.js), e.g. { not: 'loose' }. They are passed
|
|
98
|
+
// to every expression parsed out of the template, so `{{ not a has b }}`
|
|
99
|
+
// follows the same `not` mode as script code.
|
|
100
|
+
export function parseTemplate(source, options = {}) {
|
|
101
|
+
const parser = new TemplateParser(source, options)
|
|
59
102
|
const nodes = parser.parseNodes(null)
|
|
60
103
|
if (parser.pos < parser.source.length) {
|
|
61
104
|
throw new Error(
|
|
@@ -66,9 +109,13 @@ export function parseTemplate(source) {
|
|
|
66
109
|
}
|
|
67
110
|
|
|
68
111
|
class TemplateParser {
|
|
69
|
-
constructor(source) {
|
|
112
|
+
constructor(source, options = {}) {
|
|
113
|
+
this.options = options
|
|
70
114
|
this.source = source
|
|
71
115
|
this.pos = 0
|
|
116
|
+
// Set by parseNodes: which marker it stopped at (the stopMarker,
|
|
117
|
+
// one of alsoStopAt, or null for a closing tag).
|
|
118
|
+
this.stopHit = null
|
|
72
119
|
// Precomputed once so lineAt() is a binary search instead of
|
|
73
120
|
// rescanning from the start on every node -- parsing jumps around
|
|
74
121
|
// via indexOf() in several places (interpolations, {{#if}}/
|
|
@@ -99,19 +146,34 @@ class TemplateParser {
|
|
|
99
146
|
return lo + 1
|
|
100
147
|
}
|
|
101
148
|
|
|
149
|
+
findInterpEnd(from) {
|
|
150
|
+
return findInterpEnd(this.source, from)
|
|
151
|
+
}
|
|
152
|
+
|
|
102
153
|
// Parses sibling nodes. `stopTag` (an HTML tag name) or `stopMarker`
|
|
103
154
|
// (a literal like "{{/if}}") tell this call when to return; nested
|
|
104
155
|
// control blocks / elements consume their own closing marker before
|
|
105
156
|
// returning, so recursion handles arbitrary nesting automatically.
|
|
106
|
-
|
|
157
|
+
// `alsoStopAt` lists marker PREFIXES (like "{{#else") that end this
|
|
158
|
+
// call WITHOUT being consumed, so the caller can parse them itself.
|
|
159
|
+
// After returning, this.stopHit says which one ended the call.
|
|
160
|
+
parseNodes(stopTag, stopMarker = null, alsoStopAt = []) {
|
|
107
161
|
const nodes = []
|
|
108
162
|
while (this.pos < this.source.length) {
|
|
109
163
|
if (stopMarker && this.remaining().startsWith(stopMarker)) {
|
|
110
164
|
this.pos += stopMarker.length
|
|
165
|
+
this.stopHit = stopMarker
|
|
166
|
+
return nodes
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const also = alsoStopAt.find((m) => this.remaining().startsWith(m))
|
|
170
|
+
if (also) {
|
|
171
|
+
this.stopHit = also
|
|
111
172
|
return nodes
|
|
112
173
|
}
|
|
113
174
|
|
|
114
175
|
if (this.remaining().startsWith('</')) {
|
|
176
|
+
this.stopHit = null
|
|
115
177
|
if (stopTag === null) return nodes
|
|
116
178
|
this.consumeClosingTag(stopTag)
|
|
117
179
|
return nodes
|
|
@@ -138,9 +200,15 @@ class TemplateParser {
|
|
|
138
200
|
}
|
|
139
201
|
|
|
140
202
|
const text = this.parseText()
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
203
|
+
if (WS_ONLY_RE.test(text)) {
|
|
204
|
+
// JSX rule: whitespace-only runs containing a newline are
|
|
205
|
+
// formatting and get dropped; a run on a single line sits
|
|
206
|
+
// BETWEEN inline content (`{{ first }} {{ last }}`,
|
|
207
|
+
// `<b>a</b> <i>b</i>`) and collapses to one real space.
|
|
208
|
+
if (!text.includes('\n')) nodes.push(TextNode(' '))
|
|
209
|
+
} else {
|
|
210
|
+
// Collapse first, decode after, so ` ` survives.
|
|
211
|
+
nodes.push(TextNode(decodeHtmlEntities(text.replace(WS_RUN_RE, ' '))))
|
|
144
212
|
}
|
|
145
213
|
}
|
|
146
214
|
|
|
@@ -152,6 +220,7 @@ class TemplateParser {
|
|
|
152
220
|
if (stopTag !== null) {
|
|
153
221
|
throw new Error(`Unclosed tag <${stopTag}>: reached end of template`)
|
|
154
222
|
}
|
|
223
|
+
this.stopHit = null
|
|
155
224
|
return nodes
|
|
156
225
|
}
|
|
157
226
|
|
|
@@ -170,13 +239,21 @@ class TemplateParser {
|
|
|
170
239
|
parseInterpolation() {
|
|
171
240
|
const line = this.lineAt(this.pos)
|
|
172
241
|
const start = this.pos + 2
|
|
173
|
-
const end = this.
|
|
242
|
+
const end = this.findInterpEnd(start)
|
|
174
243
|
if (end === -1) {
|
|
175
244
|
throw new Error("Unterminated interpolation: missing '}}'")
|
|
176
245
|
}
|
|
177
246
|
const exprSource = this.source.slice(start, end).trim()
|
|
247
|
+
if (exprSource.startsWith('#') || exprSource.startsWith('/')) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`Unexpected block tag "{{${exprSource}}}" at line ${line}: it has no matching opening or closing tag (check {{#if}} / {{#each}} / {{#else}} / {{/if}} / {{/each}})`,
|
|
250
|
+
)
|
|
251
|
+
}
|
|
178
252
|
this.pos = end + 2
|
|
179
|
-
return {
|
|
253
|
+
return {
|
|
254
|
+
...InterpolationNode(parseExpressionString(exprSource, this.options)),
|
|
255
|
+
line,
|
|
256
|
+
}
|
|
180
257
|
}
|
|
181
258
|
|
|
182
259
|
parseIfBlock() {
|
|
@@ -184,16 +261,51 @@ class TemplateParser {
|
|
|
184
261
|
this.pos += 2 // "{{"
|
|
185
262
|
this.pos += 3 // "#if"
|
|
186
263
|
this.skipWhitespace()
|
|
187
|
-
const end = this.
|
|
264
|
+
const end = this.findInterpEnd(this.pos)
|
|
188
265
|
if (end === -1) {
|
|
189
266
|
throw new Error("Unterminated {{#if}} header: missing '}}'")
|
|
190
267
|
}
|
|
191
268
|
const conditionSource = this.source.slice(this.pos, end).trim()
|
|
192
269
|
this.pos = end + 2
|
|
270
|
+
return this.parseIfRest(conditionSource, line)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Parses the body of an {{#if}} (header already consumed), plus an
|
|
274
|
+
// optional {{#else}} or {{#else if cond}} chain. An else-if is a
|
|
275
|
+
// nested IfNode in elseChildren that shares the single {{/if}}.
|
|
276
|
+
parseIfRest(conditionSource, line) {
|
|
277
|
+
const children = this.parseNodes(null, '{{/if}}', ['{{#else'])
|
|
278
|
+
|
|
279
|
+
let elseChildren = null
|
|
280
|
+
if (this.stopHit === '{{#else') {
|
|
281
|
+
this.pos += '{{#else'.length
|
|
282
|
+
this.skipWhitespace()
|
|
283
|
+
const elseLine = this.lineAt(this.pos)
|
|
284
|
+
const end = this.findInterpEnd(this.pos)
|
|
285
|
+
if (end === -1) {
|
|
286
|
+
throw new Error("Unterminated {{#else}} header: missing '}}'")
|
|
287
|
+
}
|
|
288
|
+
const header = this.source.slice(this.pos, end).trim()
|
|
289
|
+
this.pos = end + 2
|
|
290
|
+
|
|
291
|
+
const elseIf = /^if\b\s*([\s\S]+)$/.exec(header)
|
|
292
|
+
if (header === '') {
|
|
293
|
+
elseChildren = this.parseNodes(null, '{{/if}}')
|
|
294
|
+
} else if (elseIf) {
|
|
295
|
+
elseChildren = [this.parseIfRest(elseIf[1].trim(), elseLine)]
|
|
296
|
+
} else {
|
|
297
|
+
throw new Error(
|
|
298
|
+
`Invalid {{#else}} header "${header}" at line ${elseLine}: expected {{#else}} or {{#else if condition}}`,
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
193
302
|
|
|
194
|
-
const children = this.parseNodes(null, '{{/if}}')
|
|
195
303
|
return {
|
|
196
|
-
...IfNode(
|
|
304
|
+
...IfNode(
|
|
305
|
+
parseExpressionString(conditionSource, this.options),
|
|
306
|
+
children,
|
|
307
|
+
elseChildren,
|
|
308
|
+
),
|
|
197
309
|
line,
|
|
198
310
|
}
|
|
199
311
|
}
|
|
@@ -203,7 +315,7 @@ class TemplateParser {
|
|
|
203
315
|
this.pos += 2 // "{{"
|
|
204
316
|
this.pos += 5 // "#each"
|
|
205
317
|
this.skipWhitespace()
|
|
206
|
-
const end = this.
|
|
318
|
+
const end = this.findInterpEnd(this.pos)
|
|
207
319
|
if (end === -1) {
|
|
208
320
|
throw new Error("Unterminated {{#each}} header: missing '}}'")
|
|
209
321
|
}
|
|
@@ -213,15 +325,41 @@ class TemplateParser {
|
|
|
213
325
|
const match = EACH_HEADER_RE.exec(header)
|
|
214
326
|
if (!match) {
|
|
215
327
|
throw new Error(
|
|
216
|
-
`Invalid {{#each}} header "${header}": expected "item in items"`,
|
|
328
|
+
`Invalid {{#each}} header "${header}": expected "item in items", "item, index in items" or "[a, b] in pairs"`,
|
|
217
329
|
)
|
|
218
330
|
}
|
|
219
331
|
const itemName = match[1]
|
|
220
|
-
const
|
|
332
|
+
const indexName = match[2] ?? null
|
|
333
|
+
const iterableSource = match[3].trim()
|
|
334
|
+
|
|
335
|
+
const children = this.parseNodes(null, '{{/each}}', ['{{#else'])
|
|
336
|
+
|
|
337
|
+
let elseChildren = null
|
|
338
|
+
if (this.stopHit === '{{#else') {
|
|
339
|
+
this.pos += '{{#else'.length
|
|
340
|
+
this.skipWhitespace()
|
|
341
|
+
const elseEnd = this.findInterpEnd(this.pos)
|
|
342
|
+
if (elseEnd === -1) {
|
|
343
|
+
throw new Error("Unterminated {{#else}} header: missing '}}'")
|
|
344
|
+
}
|
|
345
|
+
const elseHeader = this.source.slice(this.pos, elseEnd).trim()
|
|
346
|
+
if (elseHeader !== '') {
|
|
347
|
+
throw new Error(
|
|
348
|
+
`{{#else}} inside {{#each}} takes no condition (got "${elseHeader}")`,
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
this.pos = elseEnd + 2
|
|
352
|
+
elseChildren = this.parseNodes(null, '{{/each}}')
|
|
353
|
+
}
|
|
221
354
|
|
|
222
|
-
const children = this.parseNodes(null, '{{/each}}')
|
|
223
355
|
return {
|
|
224
|
-
...EachNode(
|
|
356
|
+
...EachNode(
|
|
357
|
+
itemName,
|
|
358
|
+
parseExpressionString(iterableSource, this.options),
|
|
359
|
+
children,
|
|
360
|
+
indexName,
|
|
361
|
+
elseChildren,
|
|
362
|
+
),
|
|
225
363
|
line,
|
|
226
364
|
}
|
|
227
365
|
}
|
|
@@ -342,14 +480,52 @@ class TemplateParser {
|
|
|
342
480
|
const rawValue = this.source.slice(valueStart, this.pos)
|
|
343
481
|
this.pos++
|
|
344
482
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
483
|
+
return { ...this.buildAttribute(name, rawValue, line), line }
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Splits an attribute value into literal text and {{ }} parts.
|
|
487
|
+
// "plain" -> static string
|
|
488
|
+
// "{{ expr }}" -> the expression itself (type kept:
|
|
489
|
+
// numbers stay numbers, etc.)
|
|
490
|
+
// "btn {{ a }} x-{{ b }}" -> a template literal
|
|
491
|
+
buildAttribute(name, rawValue, line) {
|
|
492
|
+
if (!rawValue.includes('{{')) {
|
|
493
|
+
return AttributeNode(name, rawValue, false)
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const parts = []
|
|
497
|
+
let text = ''
|
|
498
|
+
let i = 0
|
|
499
|
+
while (i < rawValue.length) {
|
|
500
|
+
if (rawValue.startsWith('{{', i)) {
|
|
501
|
+
const end = findInterpEnd(rawValue, i + 2)
|
|
502
|
+
if (end === -1) {
|
|
503
|
+
throw new Error(
|
|
504
|
+
`Unterminated '{{' in attribute '${name}' at line ${line}`,
|
|
505
|
+
)
|
|
506
|
+
}
|
|
507
|
+
if (text) {
|
|
508
|
+
parts.push({ type: 'text', value: text })
|
|
509
|
+
text = ''
|
|
510
|
+
}
|
|
511
|
+
parts.push({
|
|
512
|
+
type: 'expr',
|
|
513
|
+
expression: parseExpressionString(
|
|
514
|
+
rawValue.slice(i + 2, end).trim(),
|
|
515
|
+
this.options,
|
|
516
|
+
),
|
|
517
|
+
})
|
|
518
|
+
i = end + 2
|
|
519
|
+
} else {
|
|
520
|
+
text += rawValue[i++]
|
|
350
521
|
}
|
|
351
522
|
}
|
|
352
|
-
|
|
523
|
+
if (text) parts.push({ type: 'text', value: text })
|
|
524
|
+
|
|
525
|
+
if (parts.length === 1 && parts[0].type === 'expr') {
|
|
526
|
+
return AttributeNode(name, parts[0].expression, true)
|
|
527
|
+
}
|
|
528
|
+
return AttributeNode(name, TemplateLiteralExpr(parts), true)
|
|
353
529
|
}
|
|
354
530
|
|
|
355
531
|
skipWhitespace() {
|
|
@@ -357,4 +533,4 @@ class TemplateParser {
|
|
|
357
533
|
this.pos++
|
|
358
534
|
}
|
|
359
535
|
}
|
|
360
|
-
}
|
|
536
|
+
}
|