@tylix/compiler 0.3.5 → 0.3.7
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 +3 -3
- package/src/ast/nodes.js +34 -1
- package/src/ast/substituteCapabilityRefs.js +162 -0
- package/src/codegen/generateCapability.js +49 -0
- package/src/codegen/generateMethod.test.js +4 -2
- package/src/codegen/generatePage.js +73 -7
- package/src/codegen/generatePage.onmount.test.js +17 -4
- package/src/index.js +2 -0
- package/src/lexer/Lexer.js +12 -0
- package/src/lexer/tokenTypes.js +21 -0
- package/src/parser/Parser.js +210 -28
- package/src/parser/Parser.test.js +94 -6
- package/src/parser/parsePageFile.js +1 -1
- package/src/parser/parseTemplate.test.js +20 -4
- package/src/renderPageDocument.js +90 -10
- package/src/renderPageDocument.test.js +7 -7
- package/src/runtime/capability.js +61 -0
- package/src/typecheckPage.js +4 -2
- package/test/dashboard-syntax.test.js +116 -0
package/src/parser/Parser.js
CHANGED
|
@@ -32,6 +32,8 @@ import {
|
|
|
32
32
|
RepeatStatement,
|
|
33
33
|
BreakStatement,
|
|
34
34
|
ContinueStatement,
|
|
35
|
+
CapabilityRefNode,
|
|
36
|
+
MetaEntryNode,
|
|
35
37
|
} from '../ast/nodes.js'
|
|
36
38
|
import { parseExpressionString } from './parseExpressionString.js'
|
|
37
39
|
|
|
@@ -101,6 +103,7 @@ export class Parser {
|
|
|
101
103
|
state: [],
|
|
102
104
|
computed: [],
|
|
103
105
|
actions: [],
|
|
106
|
+
uses: [],
|
|
104
107
|
onMount: null,
|
|
105
108
|
}
|
|
106
109
|
|
|
@@ -111,12 +114,38 @@ export class Parser {
|
|
|
111
114
|
page.state = this.parseSectionBlock(this.parseStateEntry.bind(this))
|
|
112
115
|
} else if (this.match(TokenType.COMPUTED)) {
|
|
113
116
|
page.computed = this.parseSectionBlock(this.parseMethod.bind(this))
|
|
117
|
+
} else if (this.match(TokenType.USES)) {
|
|
118
|
+
page.uses = this.parseSectionBlock(this.parseUsesEntry.bind(this))
|
|
114
119
|
} else if (this.match(TokenType.ACTION)) {
|
|
115
120
|
page.actions = this.parseSectionBlock(this.parseMethod.bind(this))
|
|
116
121
|
} else if (this.check(TokenType.ONMOUNT)) {
|
|
117
122
|
const startLine = this.peek().line
|
|
118
123
|
this.advance()
|
|
119
124
|
page.onMount = { ...this.parseOnMountBody(), line: startLine }
|
|
125
|
+
} else if (this.match(TokenType.TITLE)) {
|
|
126
|
+
page.title = this.expect(
|
|
127
|
+
TokenType.STRING,
|
|
128
|
+
"Expected a string after 'title'",
|
|
129
|
+
).value
|
|
130
|
+
} else if (this.match(TokenType.LAYOUT)) {
|
|
131
|
+
page.layout = this.expect(
|
|
132
|
+
TokenType.IDENTIFIER,
|
|
133
|
+
'Expected a layout component name',
|
|
134
|
+
).value
|
|
135
|
+
} else if (this.match(TokenType.NEEDS)) {
|
|
136
|
+
page.needs = this.parseSectionBlock(this.parseUsesEntry.bind(this))
|
|
137
|
+
} else if (this.match(TokenType.PREFETCH)) {
|
|
138
|
+
page.prefetch = this.parseSectionBlock(
|
|
139
|
+
this.parseCapabilityRefEntry.bind(this),
|
|
140
|
+
)
|
|
141
|
+
} else if (this.match(TokenType.BACKGROUND)) {
|
|
142
|
+
page.background = this.parseSectionBlock(this.parseUsesEntry.bind(this))
|
|
143
|
+
} else if (this.match(TokenType.PERMISSIONS)) {
|
|
144
|
+
page.permissions = this.parseSectionBlock(
|
|
145
|
+
this.parsePermissionEntry.bind(this),
|
|
146
|
+
)
|
|
147
|
+
} else if (this.match(TokenType.META)) {
|
|
148
|
+
page.meta = this.parseSectionBlock(this.parseMetaEntry.bind(this))
|
|
120
149
|
} else {
|
|
121
150
|
throw new Error(
|
|
122
151
|
`Unexpected token ${this.peek().type} at line ${this.peek().line}`,
|
|
@@ -139,7 +168,7 @@ export class Parser {
|
|
|
139
168
|
return { body, isAsync }
|
|
140
169
|
}
|
|
141
170
|
const body = []
|
|
142
|
-
while (!this.
|
|
171
|
+
while (!this.isAtSectionBoundary()) {
|
|
143
172
|
body.push(this.parseStatement())
|
|
144
173
|
}
|
|
145
174
|
return { body, isAsync }
|
|
@@ -166,34 +195,147 @@ export class Parser {
|
|
|
166
195
|
isSectionKeyword(type) {
|
|
167
196
|
return (
|
|
168
197
|
type === TokenType.PROPS ||
|
|
198
|
+
type === TokenType.USES ||
|
|
169
199
|
type === TokenType.STATE ||
|
|
170
200
|
type === TokenType.COMPUTED ||
|
|
171
201
|
type === TokenType.ACTION ||
|
|
172
202
|
type === TokenType.ONMOUNT ||
|
|
203
|
+
type === TokenType.INIT ||
|
|
173
204
|
type === TokenType.EOF
|
|
174
205
|
)
|
|
175
206
|
}
|
|
176
207
|
|
|
208
|
+
// These five double as ordinary field names in practice (title
|
|
209
|
+
// especially), so unlike the keywords above they can't be trusted as
|
|
210
|
+
// section boundaries on token type alone -- isAtSectionBoundary below
|
|
211
|
+
// checks the following token to disambiguate.
|
|
212
|
+
isAmbiguousSectionKeyword(type) {
|
|
213
|
+
return (
|
|
214
|
+
type === TokenType.TITLE ||
|
|
215
|
+
type === TokenType.LAYOUT ||
|
|
216
|
+
type === TokenType.NEEDS ||
|
|
217
|
+
type === TokenType.PREFETCH ||
|
|
218
|
+
type === TokenType.BACKGROUND ||
|
|
219
|
+
type === TokenType.PERMISSIONS ||
|
|
220
|
+
type === TokenType.META
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
isAtSectionBoundary() {
|
|
225
|
+
const type = this.peek().type
|
|
226
|
+
if (this.isSectionKeyword(type)) return true
|
|
227
|
+
if (this.isAmbiguousSectionKeyword(type)) {
|
|
228
|
+
const next = this.tokens[this.pos + 1]
|
|
229
|
+
// Followed by ':' or '(' means it's a field/prop/method name in
|
|
230
|
+
// the CURRENT block (e.g. `title: ""`, `title(Type): ""`), not
|
|
231
|
+
// the start of a new section.
|
|
232
|
+
return !(
|
|
233
|
+
next &&
|
|
234
|
+
(next.type === TokenType.COLON || next.type === TokenType.LPAREN)
|
|
235
|
+
)
|
|
236
|
+
}
|
|
237
|
+
return false
|
|
238
|
+
}
|
|
239
|
+
|
|
177
240
|
parseBareBlock(parseEntry) {
|
|
178
241
|
const entries = []
|
|
179
|
-
while (!this.
|
|
242
|
+
while (!this.isAtSectionBoundary()) {
|
|
180
243
|
entries.push(parseEntry())
|
|
181
244
|
this.match(TokenType.COMMA)
|
|
182
245
|
}
|
|
183
246
|
return entries
|
|
184
247
|
}
|
|
185
248
|
|
|
186
|
-
parsePropEntry
|
|
249
|
+
// Shared by parseStateEntry/parsePropEntry/parseMethod: a keyword
|
|
250
|
+
// token (title, layout, needs, etc.) is a perfectly valid field/prop/
|
|
251
|
+
// method name once we're past the section-boundary check above --
|
|
252
|
+
// same allowance parsePropertyName already makes for `.title` access.
|
|
253
|
+
parseNameToken(message) {
|
|
254
|
+
const token = this.peek()
|
|
255
|
+
if (
|
|
256
|
+
token.type === TokenType.IDENTIFIER ||
|
|
257
|
+
KEYWORD_TOKEN_TYPES.has(token.type)
|
|
258
|
+
) {
|
|
259
|
+
this.advance()
|
|
260
|
+
return token.value
|
|
261
|
+
}
|
|
262
|
+
throw new Error(`${message} at line ${token.line}, got ${token.type}`)
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
parsePropEntry() {
|
|
187
266
|
const startLine = this.peek().line
|
|
188
|
-
const name = this.
|
|
267
|
+
const name = this.parseNameToken('Expected prop name')
|
|
189
268
|
const optional = this.match(TokenType.QUESTION)
|
|
190
269
|
this.expect(TokenType.COLON, "Expected ':' after prop name")
|
|
191
270
|
const propType = this.parseTypeExpression()
|
|
192
271
|
return { ...PropNode(name, propType, optional), line: startLine }
|
|
193
272
|
}
|
|
273
|
+
|
|
274
|
+
parseUsesEntry() {
|
|
275
|
+
const startLine = this.peek().line
|
|
276
|
+
const token = this.expect(TokenType.IDENTIFIER, 'Expected capability name')
|
|
277
|
+
const name = token.value
|
|
278
|
+
if (!/^[A-Z]/.test(name)) {
|
|
279
|
+
throw new Error(
|
|
280
|
+
`Capability name "${name}" must start with an uppercase letter (line ${startLine})`,
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
return { name, line: startLine }
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
parseCapabilityRefEntry() {
|
|
287
|
+
const startLine = this.peek().line
|
|
288
|
+
const capability = this.expect(
|
|
289
|
+
TokenType.IDENTIFIER,
|
|
290
|
+
'Expected a capability name',
|
|
291
|
+
).value
|
|
292
|
+
this.expect(
|
|
293
|
+
TokenType.DOT,
|
|
294
|
+
"Expected '.' after capability name in prefetch entry",
|
|
295
|
+
)
|
|
296
|
+
const member = this.parsePropertyName("after '.'")
|
|
297
|
+
let args = null
|
|
298
|
+
if (this.check(TokenType.LPAREN)) {
|
|
299
|
+
args = this.parseCallArguments()
|
|
300
|
+
}
|
|
301
|
+
return { ...CapabilityRefNode(capability, member, args), line: startLine }
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
parsePermissionEntry() {
|
|
305
|
+
const startLine = this.peek().line
|
|
306
|
+
let name = this.expect(
|
|
307
|
+
TokenType.IDENTIFIER,
|
|
308
|
+
'Expected a permission name',
|
|
309
|
+
).value
|
|
310
|
+
while (this.match(TokenType.DOT)) {
|
|
311
|
+
name += '.' + this.parsePropertyName("after '.'")
|
|
312
|
+
}
|
|
313
|
+
return { name, line: startLine }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
parseMetaEntry() {
|
|
317
|
+
const startLine = this.peek().line
|
|
318
|
+
const key = this.expect(TokenType.IDENTIFIER, 'Expected a meta key').value
|
|
319
|
+
let value
|
|
320
|
+
if (
|
|
321
|
+
this.check(TokenType.STRING) ||
|
|
322
|
+
this.check(TokenType.NUMBER) ||
|
|
323
|
+
this.check(TokenType.DURATION)
|
|
324
|
+
) {
|
|
325
|
+
value = this.advance().value
|
|
326
|
+
} else if (this.check(TokenType.IDENTIFIER)) {
|
|
327
|
+
// true / false, lexed as identifiers per parsePrimary's convention
|
|
328
|
+
const raw = this.advance().value
|
|
329
|
+
value = raw === 'true' ? true : raw === 'false' ? false : raw
|
|
330
|
+
} else {
|
|
331
|
+
throw new Error(`Expected a meta value at line ${this.peek().line}`)
|
|
332
|
+
}
|
|
333
|
+
return { ...MetaEntryNode(key, value), line: startLine }
|
|
334
|
+
}
|
|
335
|
+
|
|
194
336
|
parseStateEntry() {
|
|
195
337
|
const startLine = this.peek().line
|
|
196
|
-
const name = this.
|
|
338
|
+
const name = this.parseNameToken('Expected state name')
|
|
197
339
|
|
|
198
340
|
let typeAnnotation = null
|
|
199
341
|
if (this.match(TokenType.LPAREN)) {
|
|
@@ -218,18 +360,16 @@ export class Parser {
|
|
|
218
360
|
const value = this.parsePrimary()
|
|
219
361
|
return { ...StateNode(name, value, typeAnnotation), line: startLine }
|
|
220
362
|
}
|
|
221
|
-
|
|
363
|
+
|
|
364
|
+
parseMethod() {
|
|
222
365
|
const startLine = this.peek().line
|
|
223
366
|
const isAsync = this.match(TokenType.ASYNC)
|
|
224
|
-
const name = this.
|
|
367
|
+
const name = this.parseNameToken('Expected method name')
|
|
225
368
|
this.expect(TokenType.LPAREN, "Expected '(' after method name")
|
|
226
369
|
|
|
227
370
|
const params = []
|
|
228
371
|
while (!this.check(TokenType.RPAREN)) {
|
|
229
|
-
const paramName = this.
|
|
230
|
-
TokenType.IDENTIFIER,
|
|
231
|
-
'Expected parameter name',
|
|
232
|
-
).value
|
|
372
|
+
const paramName = this.parseNameToken('Expected parameter name')
|
|
233
373
|
let paramType = null
|
|
234
374
|
if (this.match(TokenType.COLON)) {
|
|
235
375
|
paramType = this.parseTypeExpression()
|
|
@@ -263,13 +403,13 @@ export class Parser {
|
|
|
263
403
|
// property values, and array pattern elements, so `const { a: { b } }
|
|
264
404
|
// = x` and `const [[a, b]] = x` both fall out for free instead of
|
|
265
405
|
// needing separate one-level-deep parsing logic in three places.
|
|
266
|
-
|
|
406
|
+
parseBindingTarget() {
|
|
267
407
|
if (this.check(TokenType.LBRACE)) return this.parseObjectPattern()
|
|
268
408
|
if (this.check(TokenType.LBRACKET)) return this.parseArrayPattern()
|
|
269
|
-
return this.
|
|
409
|
+
return this.parseNameToken('Expected a binding name')
|
|
270
410
|
}
|
|
271
411
|
|
|
272
|
-
|
|
412
|
+
parseObjectPattern() {
|
|
273
413
|
this.expect(TokenType.LBRACE, "Expected '{' to start destructuring pattern")
|
|
274
414
|
const properties = []
|
|
275
415
|
let rest = null
|
|
@@ -282,10 +422,7 @@ export class Parser {
|
|
|
282
422
|
this.match(TokenType.COMMA)
|
|
283
423
|
continue
|
|
284
424
|
}
|
|
285
|
-
const key = this.
|
|
286
|
-
TokenType.IDENTIFIER,
|
|
287
|
-
'Expected a property name',
|
|
288
|
-
).value
|
|
425
|
+
const key = this.parseNameToken('Expected a property name')
|
|
289
426
|
let binding = key
|
|
290
427
|
if (this.match(TokenType.COLON)) {
|
|
291
428
|
binding = this.parseBindingTarget()
|
|
@@ -300,7 +437,6 @@ export class Parser {
|
|
|
300
437
|
this.expect(TokenType.RBRACE, "Expected '}' to close destructuring pattern")
|
|
301
438
|
return ObjectPattern(properties, rest)
|
|
302
439
|
}
|
|
303
|
-
|
|
304
440
|
parseArrayPattern() {
|
|
305
441
|
this.expect(
|
|
306
442
|
TokenType.LBRACKET,
|
|
@@ -362,6 +498,42 @@ export class Parser {
|
|
|
362
498
|
return node
|
|
363
499
|
}
|
|
364
500
|
|
|
501
|
+
// Parses a capability.tyx file: `capability Name` header, then
|
|
502
|
+
// state/action/init sections. Structurally similar to parse(), but
|
|
503
|
+
// deliberately separate rather than unified -- a capability has no
|
|
504
|
+
// props, no template, and `init` (not `onMount`) is its only
|
|
505
|
+
// lifecycle hook, so sharing parse()'s isSectionKeyword/loop would
|
|
506
|
+
// mean threading capability-only exceptions through page-only logic.
|
|
507
|
+
parseCapability() {
|
|
508
|
+
this.expect(TokenType.CAPABILITY, "Expected 'capability'")
|
|
509
|
+
const name = this.expect(
|
|
510
|
+
TokenType.IDENTIFIER,
|
|
511
|
+
'Expected capability name',
|
|
512
|
+
).value
|
|
513
|
+
|
|
514
|
+
const capability = { name, state: [], actions: [], init: null }
|
|
515
|
+
|
|
516
|
+
while (!this.check(TokenType.EOF)) {
|
|
517
|
+
if (this.match(TokenType.STATE)) {
|
|
518
|
+
capability.state = this.parseSectionBlock(
|
|
519
|
+
this.parseStateEntry.bind(this),
|
|
520
|
+
)
|
|
521
|
+
} else if (this.match(TokenType.ACTION)) {
|
|
522
|
+
capability.actions = this.parseSectionBlock(this.parseMethod.bind(this))
|
|
523
|
+
} else if (this.check(TokenType.INIT)) {
|
|
524
|
+
const startLine = this.peek().line
|
|
525
|
+
this.advance()
|
|
526
|
+
capability.init = { ...this.parseOnMountBody(), line: startLine }
|
|
527
|
+
} else {
|
|
528
|
+
throw new Error(
|
|
529
|
+
`Unexpected token ${this.peek().type} at line ${this.peek().line}`,
|
|
530
|
+
)
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return capability
|
|
535
|
+
}
|
|
536
|
+
|
|
365
537
|
parseStatementInner() {
|
|
366
538
|
if (this.match(TokenType.RETURN)) {
|
|
367
539
|
const hasValue =
|
|
@@ -568,7 +740,10 @@ export class Parser {
|
|
|
568
740
|
// position.
|
|
569
741
|
tryParseArrowFunction() {
|
|
570
742
|
// single-param form: x => expr or x: Type => expr
|
|
571
|
-
if (
|
|
743
|
+
if (
|
|
744
|
+
this.check(TokenType.IDENTIFIER) ||
|
|
745
|
+
KEYWORD_TOKEN_TYPES.has(this.peek().type)
|
|
746
|
+
) {
|
|
572
747
|
const next = this.tokens[this.pos + 1]
|
|
573
748
|
if (next && next.type === TokenType.ARROW) {
|
|
574
749
|
const param = this.advance().value
|
|
@@ -603,7 +778,10 @@ export class Parser {
|
|
|
603
778
|
|
|
604
779
|
if (!this.check(TokenType.RPAREN)) {
|
|
605
780
|
while (true) {
|
|
606
|
-
if (
|
|
781
|
+
if (
|
|
782
|
+
!this.check(TokenType.IDENTIFIER) &&
|
|
783
|
+
!KEYWORD_TOKEN_TYPES.has(this.peek().type)
|
|
784
|
+
) {
|
|
607
785
|
isValidParamList = false
|
|
608
786
|
break
|
|
609
787
|
}
|
|
@@ -828,7 +1006,10 @@ export class Parser {
|
|
|
828
1006
|
return TemplateLiteralExpr(parts)
|
|
829
1007
|
}
|
|
830
1008
|
|
|
831
|
-
if (
|
|
1009
|
+
if (
|
|
1010
|
+
this.check(TokenType.IDENTIFIER) ||
|
|
1011
|
+
KEYWORD_TOKEN_TYPES.has(this.peek().type)
|
|
1012
|
+
) {
|
|
832
1013
|
const name = this.advance().value
|
|
833
1014
|
// "null"/"true"/"false"/"undefined" are lexed as plain identifier
|
|
834
1015
|
// tokens (there's no separate keyword TokenType for them), but
|
|
@@ -919,14 +1100,11 @@ export class Parser {
|
|
|
919
1100
|
return name
|
|
920
1101
|
}
|
|
921
1102
|
|
|
922
|
-
|
|
1103
|
+
parseObjectTypeLiteral() {
|
|
923
1104
|
this.expect(TokenType.LBRACE, "Expected '{' to start object type")
|
|
924
1105
|
const members = []
|
|
925
1106
|
while (!this.check(TokenType.RBRACE)) {
|
|
926
|
-
const key = this.
|
|
927
|
-
TokenType.IDENTIFIER,
|
|
928
|
-
'Expected a property name',
|
|
929
|
-
).value
|
|
1107
|
+
const key = this.parseNameToken('Expected a property name')
|
|
930
1108
|
const optional = this.match(TokenType.QUESTION)
|
|
931
1109
|
this.expect(
|
|
932
1110
|
TokenType.COLON,
|
|
@@ -958,9 +1136,13 @@ export class Parser {
|
|
|
958
1136
|
const properties = []
|
|
959
1137
|
while (!this.check(TokenType.RBRACE)) {
|
|
960
1138
|
const isString = this.check(TokenType.STRING)
|
|
1139
|
+
const isKeywordKey =
|
|
1140
|
+
!isString && KEYWORD_TOKEN_TYPES.has(this.peek().type)
|
|
961
1141
|
const key = isString
|
|
962
1142
|
? this.advance().value
|
|
963
|
-
:
|
|
1143
|
+
: isKeywordKey
|
|
1144
|
+
? this.advance().value
|
|
1145
|
+
: this.expect(TokenType.IDENTIFIER, 'Expected an object key').value
|
|
964
1146
|
|
|
965
1147
|
if (!isString && !this.check(TokenType.COLON)) {
|
|
966
1148
|
// Shorthand: { name } means { name: name }
|
|
@@ -8,6 +8,39 @@ function parse(source) {
|
|
|
8
8
|
return new Parser(tokens).parse()
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
// AST nodes carry extra bookkeeping fields (line, typeAnnotation,
|
|
12
|
+
// computed, optional) that most of these tests don't care about.
|
|
13
|
+
// pickShape keeps only the keys named in `shape`, so a test asserts
|
|
14
|
+
// on what it means to check and doesn't break when a node gains a
|
|
15
|
+
// field.
|
|
16
|
+
function pickShape(actual, shape) {
|
|
17
|
+
if (Array.isArray(shape)) {
|
|
18
|
+
return Array.isArray(actual)
|
|
19
|
+
? actual.map((item, i) => pickShape(item, shape[i]))
|
|
20
|
+
: actual
|
|
21
|
+
}
|
|
22
|
+
if (shape && typeof shape === 'object') {
|
|
23
|
+
const out = {}
|
|
24
|
+
for (const key of Object.keys(shape)) {
|
|
25
|
+
out[key] = pickShape(actual?.[key], shape[key])
|
|
26
|
+
}
|
|
27
|
+
return out
|
|
28
|
+
}
|
|
29
|
+
return actual
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function assertMatches(actual, shape) {
|
|
33
|
+
assert.deepEqual(pickShape(actual, shape), shape)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The same content written braced or bare lands on different lines,
|
|
37
|
+
// so line numbers are dropped before comparing the two ASTs.
|
|
38
|
+
function withoutLines(node) {
|
|
39
|
+
return JSON.parse(
|
|
40
|
+
JSON.stringify(node, (key, value) => (key === 'line' ? undefined : value)),
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
11
44
|
test('respects operator precedence: multiplication before addition', () => {
|
|
12
45
|
const source = `action\n compute() {\n return 1 + 2 * 3\n }`
|
|
13
46
|
const ast = new Parser(new Lexer(source).tokenize()).parse()
|
|
@@ -57,14 +90,34 @@ test('parses a template literal with a single interpolation', () => {
|
|
|
57
90
|
|
|
58
91
|
test('parses a props block', () => {
|
|
59
92
|
const ast = parse(`props { title: String }`)
|
|
60
|
-
|
|
93
|
+
assertMatches(ast.props, [
|
|
94
|
+
{ type: 'Prop', name: 'title', propType: 'String' },
|
|
95
|
+
])
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('parses a bare props block with a prop named title', () => {
|
|
99
|
+
const ast = parse(`props\ntitle: String`)
|
|
100
|
+
assertMatches(ast.props, [
|
|
61
101
|
{ type: 'Prop', name: 'title', propType: 'String' },
|
|
62
102
|
])
|
|
63
103
|
})
|
|
64
104
|
|
|
105
|
+
test('accepts other keyword tokens as prop names', () => {
|
|
106
|
+
const ast = parse(`props { title: String, layout: String }`)
|
|
107
|
+
assert.equal(ast.props.length, 2)
|
|
108
|
+
assert.equal(ast.props[0].name, 'title')
|
|
109
|
+
assert.equal(ast.props[1].name, 'layout')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('a page-level title section still parses after a bare props block', () => {
|
|
113
|
+
const ast = parse(`props\nname: String\ntitle "Home"`)
|
|
114
|
+
assert.equal(ast.props.length, 1)
|
|
115
|
+
assert.equal(ast.title, 'Home')
|
|
116
|
+
})
|
|
117
|
+
|
|
65
118
|
test('parses a state block with number and string values', () => {
|
|
66
119
|
const ast = parse(`state { count: 0, label: "hi" }`)
|
|
67
|
-
|
|
120
|
+
assertMatches(ast.state, [
|
|
68
121
|
{ type: 'State', name: 'count', value: { type: 'Literal', value: 0 } },
|
|
69
122
|
{ type: 'State', name: 'label', value: { type: 'Literal', value: 'hi' } },
|
|
70
123
|
])
|
|
@@ -82,7 +135,7 @@ test('parses a computed block with return + member/binary expressions', () => {
|
|
|
82
135
|
assert.equal(method.name, 'doubled')
|
|
83
136
|
assert.deepEqual(method.params, [])
|
|
84
137
|
assert.equal(method.body[0].type, 'ReturnStatement')
|
|
85
|
-
|
|
138
|
+
assertMatches(method.body[0].argument, {
|
|
86
139
|
type: 'BinaryExpression',
|
|
87
140
|
operator: '*',
|
|
88
141
|
left: {
|
|
@@ -124,7 +177,40 @@ test('parses a method with parameters', () => {
|
|
|
124
177
|
}
|
|
125
178
|
}
|
|
126
179
|
`)
|
|
127
|
-
|
|
180
|
+
// Params carry an optional type annotation, so they are objects.
|
|
181
|
+
assert.deepEqual(ast.actions[0].params, [
|
|
182
|
+
{ name: 'newValue', typeAnnotation: null },
|
|
183
|
+
])
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
test('accepts a keyword token as a method parameter name', () => {
|
|
187
|
+
const ast = parse(`
|
|
188
|
+
action {
|
|
189
|
+
setTitle(title) {
|
|
190
|
+
this.title = title
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
`)
|
|
194
|
+
assert.deepEqual(ast.actions[0].params, [
|
|
195
|
+
{ name: 'title', typeAnnotation: null },
|
|
196
|
+
])
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
test('accepts a keyword token as a destructured property and binding name', () => {
|
|
200
|
+
assert.doesNotThrow(() =>
|
|
201
|
+
parse(`
|
|
202
|
+
action {
|
|
203
|
+
load() {
|
|
204
|
+
const { title } = this.data
|
|
205
|
+
const layout = title
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
`),
|
|
209
|
+
)
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
test('accepts a keyword token as a key in an object type annotation', () => {
|
|
213
|
+
assert.doesNotThrow(() => parse(`state { post({ title: string }): null }`))
|
|
128
214
|
})
|
|
129
215
|
|
|
130
216
|
test('parses a full realistic page script', () => {
|
|
@@ -195,7 +281,9 @@ test('braced and bare syntax produce identical ASTs for the same content', () =>
|
|
|
195
281
|
const bracedPage = new Parser(new Lexer(braced).tokenize()).parse()
|
|
196
282
|
const barePage = new Parser(new Lexer(bare).tokenize()).parse()
|
|
197
283
|
|
|
198
|
-
|
|
284
|
+
// Same content, different line numbers (1 vs 2), so compare
|
|
285
|
+
// everything except `line`.
|
|
286
|
+
assert.deepEqual(withoutLines(bracedPage.state), withoutLines(barePage.state))
|
|
199
287
|
})
|
|
200
288
|
|
|
201
289
|
test('parses a method call with no arguments inside an action body', () => {
|
|
@@ -224,4 +312,4 @@ test('parses chained member access followed by a call', () => {
|
|
|
224
312
|
assert.equal(call.type, 'CallExpression')
|
|
225
313
|
assert.equal(call.callee.type, 'MemberExpression')
|
|
226
314
|
assert.equal(call.callee.property, 'remove')
|
|
227
|
-
})
|
|
315
|
+
})
|
|
@@ -79,7 +79,7 @@ export function parsePageFile(source) {
|
|
|
79
79
|
export function findSectionBoundaries(source) {
|
|
80
80
|
const boundaries = []
|
|
81
81
|
const lineStartPattern =
|
|
82
|
-
/(^|\n)[ \t]*(state|computed|action|template|style)\b/g
|
|
82
|
+
/(^|\n)[ \t]*(uses|state|computed|action|template|style)\b/g
|
|
83
83
|
let match
|
|
84
84
|
while ((match = lineStartPattern.exec(source)) !== null) {
|
|
85
85
|
const keyword = match[2]
|
|
@@ -2,13 +2,27 @@ import { test } from "node:test";
|
|
|
2
2
|
import assert from "node:assert/strict";
|
|
3
3
|
import { parseTemplate } from "./parseTemplate.js";
|
|
4
4
|
|
|
5
|
+
// Element and Attribute nodes carry a `line` for error reporting.
|
|
6
|
+
// Tests that compare whole nodes drop it first, so they assert on
|
|
7
|
+
// structure and don't break whenever position tracking changes.
|
|
8
|
+
function withoutLines(node) {
|
|
9
|
+
return JSON.parse(
|
|
10
|
+
JSON.stringify(node, (key, value) => (key === "line" ? undefined : value)),
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
5
14
|
test("parses a single element with text content", () => {
|
|
6
15
|
const nodes = parseTemplate(`<h1>Hello</h1>`);
|
|
7
|
-
assert.deepEqual(nodes, [
|
|
16
|
+
assert.deepEqual(withoutLines(nodes), [
|
|
8
17
|
{ type: "Element", tag: "h1", attributes: [], children: [{ type: "Text", value: "Hello" }] },
|
|
9
18
|
]);
|
|
10
19
|
});
|
|
11
20
|
|
|
21
|
+
test("records the source line on elements", () => {
|
|
22
|
+
const nodes = parseTemplate(`<h1>Hello</h1>`);
|
|
23
|
+
assert.equal(nodes[0].line, 1);
|
|
24
|
+
});
|
|
25
|
+
|
|
12
26
|
test("parses an interpolation node", () => {
|
|
13
27
|
const nodes = parseTemplate(`{{ count }}`);
|
|
14
28
|
assert.equal(nodes.length, 1);
|
|
@@ -34,7 +48,7 @@ test("parses nested elements", () => {
|
|
|
34
48
|
|
|
35
49
|
test("parses a static attribute", () => {
|
|
36
50
|
const nodes = parseTemplate(`<div class="wrapper"></div>`);
|
|
37
|
-
assert.deepEqual(nodes[0].attributes, [
|
|
51
|
+
assert.deepEqual(withoutLines(nodes[0].attributes), [
|
|
38
52
|
{ type: "Attribute", name: "class", value: "wrapper", dynamic: false },
|
|
39
53
|
]);
|
|
40
54
|
});
|
|
@@ -49,7 +63,9 @@ test("parses a dynamic event-binding attribute using the expression parser", ()
|
|
|
49
63
|
|
|
50
64
|
test("parses a self-closing element", () => {
|
|
51
65
|
const nodes = parseTemplate(`<input />`);
|
|
52
|
-
assert.deepEqual(nodes, [
|
|
66
|
+
assert.deepEqual(withoutLines(nodes), [
|
|
67
|
+
{ type: "Element", tag: "input", attributes: [], children: [] },
|
|
68
|
+
]);
|
|
53
69
|
});
|
|
54
70
|
|
|
55
71
|
test("parses the realistic counter template from the design doc", () => {
|
|
@@ -93,4 +109,4 @@ test("collapses indentation-only whitespace between tags to nothing", () => {
|
|
|
93
109
|
const nodes = parseTemplate(`<div>\n <h1>Hi</h1>\n</div>`);
|
|
94
110
|
assert.equal(nodes[0].children.length, 1, "pure-whitespace indentation should not become a Text node");
|
|
95
111
|
assert.equal(nodes[0].children[0].tag, "h1");
|
|
96
|
-
});
|
|
112
|
+
});
|