@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
package/src/runtime/reactive.js
CHANGED
|
@@ -6,72 +6,99 @@
|
|
|
6
6
|
* that depend on a property when that property is written -- nothing
|
|
7
7
|
* else re-renders.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* reactive() are ever tracked -- writes to a property of an object
|
|
15
|
-
* *assigned into* a state field (e.g. a resource object's .data) are
|
|
16
|
-
* invisible to the dependency graph, because get() would otherwise
|
|
17
|
-
* hand back the raw, unwrapped object.
|
|
9
|
+
* What is wrapped: plain objects and arrays, lazily on read and
|
|
10
|
+
* memoized (so repeated reads return the same Proxy identity). Date,
|
|
11
|
+
* Map, Set, Promise, RegExp, DOM nodes and class instances are left
|
|
12
|
+
* alone: their methods need the real object as `this` and break when
|
|
13
|
+
* called through a Proxy.
|
|
18
14
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
15
|
+
* What is tracked: property reads, `key in obj`, and key enumeration
|
|
16
|
+
* (Object.keys, for...in). Writes that ADD a key also notify readers
|
|
17
|
+
* of the array's length / the object's key list, which is what makes
|
|
18
|
+
* `items.push(x)` re-render a list that iterates `items`.
|
|
19
|
+
*
|
|
20
|
+
* Every effect tracks (a) which dep Sets it's currently a member of,
|
|
21
|
+
* and (b) which child effects were created while it was running
|
|
21
22
|
* (e.g. an #if/#each block's effect re-creating its children's own
|
|
22
|
-
* effects on every rebuild). Before an effect re-runs, it
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* read, and do so against DOM nodes that have already been removed
|
|
28
|
-
* (whose .parentNode is now null), which throws and can abort
|
|
29
|
-
* whatever action triggered the write mid-way through.
|
|
23
|
+
* effects on every rebuild). Before an effect re-runs, it detaches
|
|
24
|
+
* from every dep and deactivates every child, recursively. A
|
|
25
|
+
* deactivated effect is skipped even if it is already queued in the
|
|
26
|
+
* current trigger pass, so a replaced block can never fire against
|
|
27
|
+
* DOM nodes that were already removed.
|
|
30
28
|
*/
|
|
31
29
|
|
|
32
30
|
let activeEffect = null
|
|
33
31
|
const targetMap = new WeakMap()
|
|
34
32
|
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
// Without this, dependencies tracked against one wrapper would never
|
|
39
|
-
// match the wrapper an effect reads on a later pass, and triggers
|
|
40
|
-
// would silently miss their subscribers.
|
|
33
|
+
// target -> proxy, so re-reading the same nested object returns the
|
|
34
|
+
// same Proxy instance (dependencies tracked against one wrapper must
|
|
35
|
+
// match the wrapper read on later passes).
|
|
41
36
|
const reactiveMap = new WeakMap()
|
|
37
|
+
// Every proxy we have handed out, to avoid double-wrapping.
|
|
38
|
+
const proxies = new WeakSet()
|
|
39
|
+
|
|
40
|
+
const ITERATE_KEY = Symbol('iterate')
|
|
42
41
|
|
|
43
|
-
function
|
|
44
|
-
|
|
42
|
+
function isReactiveTarget(val) {
|
|
43
|
+
if (val === null || typeof val !== 'object') return false
|
|
44
|
+
if (proxies.has(val)) return true
|
|
45
|
+
if (Object.isFrozen(val)) return false
|
|
46
|
+
if (Array.isArray(val)) return true
|
|
47
|
+
const proto = Object.getPrototypeOf(val)
|
|
48
|
+
return proto === Object.prototype || proto === null
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
function isIndexKey(key) {
|
|
52
|
+
return typeof key === 'string' && /^(0|[1-9]\d*)$/.test(key)
|
|
53
|
+
}
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (
|
|
55
|
+
export function reactive(target) {
|
|
56
|
+
if (!isReactiveTarget(target)) return target
|
|
57
|
+
if (proxies.has(target)) return target
|
|
58
|
+
const existing = reactiveMap.get(target)
|
|
59
|
+
if (existing) return existing
|
|
55
60
|
|
|
56
61
|
const proxy = new Proxy(target, {
|
|
57
62
|
get(obj, key, receiver) {
|
|
58
|
-
track(obj, key)
|
|
59
63
|
const result = Reflect.get(obj, key, receiver)
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
if (typeof key !== 'symbol') track(obj, key)
|
|
65
|
+
// Lazily wrap nested plain objects/arrays so writes to THEIR
|
|
66
|
+
// properties are tracked too.
|
|
67
|
+
return isReactiveTarget(result) ? reactive(result) : result
|
|
68
|
+
},
|
|
69
|
+
has(obj, key) {
|
|
70
|
+
if (typeof key !== 'symbol') track(obj, key)
|
|
71
|
+
return Reflect.has(obj, key)
|
|
72
|
+
},
|
|
73
|
+
ownKeys(obj) {
|
|
74
|
+
track(obj, Array.isArray(obj) ? 'length' : ITERATE_KEY)
|
|
75
|
+
return Reflect.ownKeys(obj)
|
|
65
76
|
},
|
|
66
77
|
set(obj, key, value, receiver) {
|
|
67
|
-
const
|
|
78
|
+
const isArray = Array.isArray(obj)
|
|
79
|
+
const hadKey =
|
|
80
|
+
isArray && isIndexKey(key)
|
|
81
|
+
? Number(key) < obj.length
|
|
82
|
+
: Object.prototype.hasOwnProperty.call(obj, key)
|
|
83
|
+
const oldValue = obj[key]
|
|
68
84
|
const result = Reflect.set(obj, key, value, receiver)
|
|
69
|
-
if (
|
|
85
|
+
if (!hadKey) {
|
|
86
|
+
trigger(obj, key, 'add')
|
|
87
|
+
} else if (!Object.is(oldValue, value)) {
|
|
88
|
+
trigger(obj, key, 'set', isArray && key === 'length' ? value : undefined)
|
|
89
|
+
}
|
|
90
|
+
return result
|
|
91
|
+
},
|
|
92
|
+
deleteProperty(obj, key) {
|
|
93
|
+
const hadKey = Object.prototype.hasOwnProperty.call(obj, key)
|
|
94
|
+
const result = Reflect.deleteProperty(obj, key)
|
|
95
|
+
if (hadKey && result) trigger(obj, key, 'delete')
|
|
70
96
|
return result
|
|
71
97
|
},
|
|
72
98
|
})
|
|
73
99
|
|
|
74
100
|
reactiveMap.set(target, proxy)
|
|
101
|
+
proxies.add(proxy)
|
|
75
102
|
return proxy
|
|
76
103
|
}
|
|
77
104
|
|
|
@@ -81,39 +108,81 @@ function track(obj, key) {
|
|
|
81
108
|
if (!depsMap) targetMap.set(obj, (depsMap = new Map()))
|
|
82
109
|
let dep = depsMap.get(key)
|
|
83
110
|
if (!dep) depsMap.set(key, (dep = new Set()))
|
|
84
|
-
dep.
|
|
85
|
-
|
|
111
|
+
if (!dep.has(activeEffect)) {
|
|
112
|
+
dep.add(activeEffect)
|
|
113
|
+
activeEffect.deps.push(dep)
|
|
114
|
+
}
|
|
86
115
|
}
|
|
87
116
|
|
|
88
|
-
function trigger(obj, key) {
|
|
117
|
+
function trigger(obj, key, type, newLength) {
|
|
89
118
|
const depsMap = targetMap.get(obj)
|
|
90
119
|
if (!depsMap) return
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
120
|
+
|
|
121
|
+
const queued = new Set()
|
|
122
|
+
const collect = (dep) => {
|
|
123
|
+
if (dep) for (const fn of dep) queued.add(fn)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
collect(depsMap.get(key))
|
|
127
|
+
|
|
128
|
+
if (type === 'add') {
|
|
129
|
+
if (Array.isArray(obj)) {
|
|
130
|
+
if (isIndexKey(key)) collect(depsMap.get('length'))
|
|
131
|
+
} else {
|
|
132
|
+
collect(depsMap.get(ITERATE_KEY))
|
|
133
|
+
}
|
|
134
|
+
} else if (type === 'delete' && !Array.isArray(obj)) {
|
|
135
|
+
collect(depsMap.get(ITERATE_KEY))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// `arr.length = 0` / splice shrinking: readers of the removed
|
|
139
|
+
// indices must hear about it too.
|
|
140
|
+
if (Array.isArray(obj) && key === 'length' && newLength !== undefined) {
|
|
141
|
+
for (const [k, dep] of depsMap) {
|
|
142
|
+
if (isIndexKey(k) && Number(k) >= newLength) collect(dep)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Run every queued effect even if one throws, then rethrow the
|
|
147
|
+
// first error so a failing effect can't starve the others.
|
|
148
|
+
let failed = false
|
|
149
|
+
let firstError
|
|
150
|
+
for (const fn of queued) {
|
|
151
|
+
if (!fn.active || fn === activeEffect) continue
|
|
152
|
+
try {
|
|
153
|
+
fn()
|
|
154
|
+
} catch (err) {
|
|
155
|
+
if (!failed) {
|
|
156
|
+
failed = true
|
|
157
|
+
firstError = err
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (failed) throw firstError
|
|
96
162
|
}
|
|
97
163
|
|
|
98
|
-
// Detaches an effect from every dep Set it was
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
//
|
|
164
|
+
// Detaches an effect from every dep Set it was part of and
|
|
165
|
+
// deactivates every child effect it created during its last run --
|
|
166
|
+
// recursively, so a deeply nested #if inside an #each inside another
|
|
167
|
+
// #if tears down its whole subtree.
|
|
102
168
|
function cleanup(effectFn) {
|
|
103
169
|
for (const dep of effectFn.deps) dep.delete(effectFn)
|
|
104
170
|
effectFn.deps.length = 0
|
|
105
|
-
for (const child of effectFn.children)
|
|
171
|
+
for (const child of effectFn.children) {
|
|
172
|
+
child.active = false
|
|
173
|
+
cleanup(child)
|
|
174
|
+
}
|
|
106
175
|
effectFn.children.length = 0
|
|
107
176
|
}
|
|
108
177
|
|
|
109
178
|
export function effect(fn) {
|
|
110
179
|
const wrapped = () => {
|
|
180
|
+
if (!wrapped.active) return
|
|
111
181
|
cleanup(wrapped)
|
|
112
182
|
const previousEffect = activeEffect
|
|
113
183
|
// Register as a child of whichever effect is currently running
|
|
114
184
|
// (if any), so that when THAT effect re-runs, it disposes this
|
|
115
|
-
// one too
|
|
116
|
-
// children safe to do repeatedly without leaking.
|
|
185
|
+
// one too.
|
|
117
186
|
if (previousEffect) previousEffect.children.push(wrapped)
|
|
118
187
|
activeEffect = wrapped
|
|
119
188
|
try {
|
|
@@ -122,8 +191,9 @@ export function effect(fn) {
|
|
|
122
191
|
activeEffect = previousEffect
|
|
123
192
|
}
|
|
124
193
|
}
|
|
194
|
+
wrapped.active = true
|
|
125
195
|
wrapped.deps = []
|
|
126
196
|
wrapped.children = []
|
|
127
197
|
wrapped()
|
|
128
198
|
return wrapped
|
|
129
|
-
}
|
|
199
|
+
}
|
|
@@ -1,20 +1,15 @@
|
|
|
1
1
|
import { emitStatementLines } from '../codegen/emitStatementLines.js'
|
|
2
|
+
import { generateParams } from '../codegen/generateExpression.js'
|
|
2
3
|
import { collectTemplateChecks } from './typedTemplateExpr.js'
|
|
3
4
|
|
|
4
|
-
function paramListWithTypes(params) {
|
|
5
|
-
return params
|
|
6
|
-
.map(
|
|
7
|
-
(p) => `${p.name}${p.typeAnnotation ? `: ${p.typeAnnotation}` : ': any'}`,
|
|
8
|
-
)
|
|
9
|
-
.join(', ')
|
|
10
|
-
}
|
|
11
|
-
|
|
12
5
|
// Recursively emits a check tree (see collectTemplateChecks) into
|
|
13
6
|
// the virtual __render() body. `each` entries open a real
|
|
14
7
|
// `for (const item of (iterable as any[]))` so the loop variable is
|
|
15
8
|
// an actual TS declaration, not a bare identifier the checker has
|
|
16
9
|
// never seen -- `as any[]` since state/props are typically `any`
|
|
17
10
|
// here anyway, and iterating `any` never fails strict mode regardless.
|
|
11
|
+
// The optional index binding of `{{#each item, i in items}}` is
|
|
12
|
+
// declared as a number.
|
|
18
13
|
function emitChecks(checks, sink, indent) {
|
|
19
14
|
for (const c of checks) {
|
|
20
15
|
if (c.kind === 'each') {
|
|
@@ -22,12 +17,17 @@ function emitChecks(checks, sink, indent) {
|
|
|
22
17
|
`${indent}for (const ${c.itemName} of (${c.iterableExpr} as any[])) {`,
|
|
23
18
|
c.line,
|
|
24
19
|
)
|
|
20
|
+
if (c.indexName) {
|
|
21
|
+
sink.pushLine(`${indent} const ${c.indexName}: number = 0;`, null)
|
|
22
|
+
}
|
|
25
23
|
emitChecks(c.children, sink, indent + ' ')
|
|
26
24
|
sink.pushLine(`${indent}}`, null)
|
|
27
25
|
} else if (c.eventType) {
|
|
26
|
+
const base = c.eventBase ?? 'Event'
|
|
27
|
+
const full = `${base} & { target: ${c.eventType} }`
|
|
28
28
|
sink.pushLine(`${indent}{`, null)
|
|
29
29
|
sink.pushLine(
|
|
30
|
-
`${indent} const event:
|
|
30
|
+
`${indent} const event: ${full} = new Event("synthetic") as unknown as ${full};`,
|
|
31
31
|
null,
|
|
32
32
|
)
|
|
33
33
|
sink.pushLine(`${indent} const $event = event;`, null)
|
|
@@ -91,7 +91,7 @@ export function buildVirtualPageTs(pageNode, className, templateNodes = null) {
|
|
|
91
91
|
: ''
|
|
92
92
|
sink.pushLine(``, null)
|
|
93
93
|
sink.pushLine(
|
|
94
|
-
` ${a.isAsync ? 'async ' : ''}${a.name}(${
|
|
94
|
+
` ${a.isAsync ? 'async ' : ''}${a.name}(${generateParams(a.params, true)})${returnType ? `: ${returnType}` : ''} {`,
|
|
95
95
|
a.line,
|
|
96
96
|
)
|
|
97
97
|
for (const stmt of a.body) emitStatementLines(stmt, sink, ' ')
|
|
@@ -112,7 +112,8 @@ export function buildVirtualPageTs(pageNode, className, templateNodes = null) {
|
|
|
112
112
|
collectTemplateChecks(templateNodes, new Set(), checks)
|
|
113
113
|
if (checks.length > 0) {
|
|
114
114
|
sink.pushLine(``, null)
|
|
115
|
-
|
|
115
|
+
// async so `await` inside event handlers typechecks
|
|
116
|
+
sink.pushLine(` async __render() {`, null)
|
|
116
117
|
emitChecks(checks, sink, ' ')
|
|
117
118
|
sink.pushLine(` }`, null)
|
|
118
119
|
}
|
|
@@ -130,4 +131,4 @@ export function mapVirtualLineToSource(lineMap, virtualLine) {
|
|
|
130
131
|
else break
|
|
131
132
|
}
|
|
132
133
|
return best
|
|
133
|
-
}
|
|
134
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { JS_GLOBALS, paramNames } from '../codegen/jsGlobals.js'
|
|
2
|
+
|
|
1
3
|
// generateTemplateExpression's twin, for the virtual .ts class body
|
|
2
4
|
// built in buildVirtualPageTs. The REAL compiled template (see
|
|
3
5
|
// codegen/generateTemplateExpression.js) prefixes state reads with
|
|
@@ -20,6 +22,8 @@ function unaryPrefix(operator) {
|
|
|
20
22
|
export function typedTemplateExpr(node, scope) {
|
|
21
23
|
if (node.type === 'Identifier') {
|
|
22
24
|
if (node.name === '$event' || scope.has(node.name)) return node.name
|
|
25
|
+
if (node.name === 'this') return 'this'
|
|
26
|
+
if (JS_GLOBALS.has(node.name)) return node.name
|
|
23
27
|
return `this.${node.name}`
|
|
24
28
|
}
|
|
25
29
|
if (node.type === 'MemberExpression') {
|
|
@@ -47,9 +51,9 @@ export function typedTemplateExpr(node, scope) {
|
|
|
47
51
|
return `(${typedTemplateExpr(node.left, scope)} ?? ${typedTemplateExpr(node.right, scope)})`
|
|
48
52
|
}
|
|
49
53
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
if (node.type === 'AssignmentExpression') {
|
|
55
|
+
return `(${typedTemplateExpr(node.target, scope)} = ${typedTemplateExpr(node.value, scope)})`
|
|
56
|
+
}
|
|
53
57
|
if (node.type === 'CallExpression') {
|
|
54
58
|
const args = node.args.map((a) => typedTemplateExpr(a, scope)).join(', ')
|
|
55
59
|
return `${typedTemplateExpr(node.callee, scope)}${node.optional ? '?.' : ''}(${args})`
|
|
@@ -61,12 +65,20 @@ export function typedTemplateExpr(node, scope) {
|
|
|
61
65
|
|
|
62
66
|
// Arrow params are typed `any` unless annotated, matching what
|
|
63
67
|
// generateExpression does in typed mode. Params become locals, so
|
|
64
|
-
// they are referenced bare in the body.
|
|
68
|
+
// they are referenced bare in the body. Destructured/rest params are
|
|
69
|
+
// stored as source strings; paramNames() extracts the names they
|
|
70
|
+
// bind. Defaults are checked in the arrow's own scope.
|
|
65
71
|
if (node.type === 'ArrowFunctionExpression') {
|
|
66
|
-
const innerScope = new Set([...scope, ...node.params])
|
|
72
|
+
const innerScope = new Set([...scope, ...paramNames(node.params)])
|
|
67
73
|
const types = node.paramTypes ?? node.params.map(() => null)
|
|
74
|
+
const defaults = node.paramDefaults ?? []
|
|
68
75
|
const params = node.params
|
|
69
|
-
.map((name, i) =>
|
|
76
|
+
.map((name, i) => {
|
|
77
|
+
const base = `${name}: ${types[i] ?? 'any'}`
|
|
78
|
+
return defaults[i]
|
|
79
|
+
? `${base} = ${typedTemplateExpr(defaults[i], innerScope)}`
|
|
80
|
+
: base
|
|
81
|
+
})
|
|
70
82
|
.join(', ')
|
|
71
83
|
const asyncPrefix = node.isAsync ? 'async ' : ''
|
|
72
84
|
return `(${asyncPrefix}(${params}) => (${typedTemplateExpr(node.body, innerScope)}))`
|
|
@@ -81,11 +93,18 @@ export function typedTemplateExpr(node, scope) {
|
|
|
81
93
|
|
|
82
94
|
if (node.type === 'ObjectExpression') {
|
|
83
95
|
const properties = node.properties
|
|
84
|
-
.map((p) =>
|
|
85
|
-
p.spread
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
.map((p) => {
|
|
97
|
+
if (p.spread) return `...${typedTemplateExpr(p.value, scope)}`
|
|
98
|
+
if (p.method) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
'Method shorthand ({ fn() {} }) is only allowed in script code, not in template expressions',
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
const keySrc = p.keyExpr
|
|
104
|
+
? `[${typedTemplateExpr(p.keyExpr, scope)}]`
|
|
105
|
+
: JSON.stringify(p.key)
|
|
106
|
+
return `${keySrc}: ${typedTemplateExpr(p.value, scope)}`
|
|
107
|
+
})
|
|
89
108
|
.join(', ')
|
|
90
109
|
return `({ ${properties} })`
|
|
91
110
|
}
|
|
@@ -113,33 +132,81 @@ export function typedTemplateExpr(node, scope) {
|
|
|
113
132
|
throw new Error(`typedTemplateExpr: unsupported node type "${node.type}"`)
|
|
114
133
|
}
|
|
115
134
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
135
|
+
// Any on<event> attribute is an event listener (see generateTemplate).
|
|
136
|
+
const EVENT_ATTR_RE = /^on([a-z]+)$/i
|
|
137
|
+
|
|
138
|
+
// [event interface, element type of `event.target`] per DOM event
|
|
139
|
+
// name. Anything not listed falls back to a plain Event on a generic
|
|
140
|
+
// HTMLElement, same as a real framework would for an unknown handler.
|
|
141
|
+
const MOUSE = ['MouseEvent', 'HTMLElement']
|
|
142
|
+
const KEY = ['KeyboardEvent', 'HTMLInputElement']
|
|
143
|
+
const FOCUS = ['FocusEvent', 'HTMLInputElement']
|
|
144
|
+
const POINTER = ['PointerEvent', 'HTMLElement']
|
|
145
|
+
const DRAG = ['DragEvent', 'HTMLElement']
|
|
146
|
+
const TOUCH = ['TouchEvent', 'HTMLElement']
|
|
147
|
+
const EVENT_TYPES = {
|
|
148
|
+
click: MOUSE,
|
|
149
|
+
dblclick: MOUSE,
|
|
150
|
+
auxclick: MOUSE,
|
|
151
|
+
contextmenu: MOUSE,
|
|
152
|
+
mousedown: MOUSE,
|
|
153
|
+
mouseup: MOUSE,
|
|
154
|
+
mousemove: MOUSE,
|
|
155
|
+
mouseenter: MOUSE,
|
|
156
|
+
mouseleave: MOUSE,
|
|
157
|
+
mouseover: MOUSE,
|
|
158
|
+
mouseout: MOUSE,
|
|
159
|
+
keydown: KEY,
|
|
160
|
+
keyup: KEY,
|
|
161
|
+
keypress: KEY,
|
|
162
|
+
input: ['InputEvent', 'HTMLInputElement'],
|
|
163
|
+
change: ['Event', 'HTMLInputElement'],
|
|
164
|
+
submit: ['SubmitEvent', 'HTMLFormElement'],
|
|
165
|
+
reset: ['Event', 'HTMLFormElement'],
|
|
166
|
+
focus: FOCUS,
|
|
167
|
+
blur: FOCUS,
|
|
168
|
+
focusin: FOCUS,
|
|
169
|
+
focusout: FOCUS,
|
|
170
|
+
scroll: ['Event', 'HTMLElement'],
|
|
171
|
+
wheel: ['WheelEvent', 'HTMLElement'],
|
|
172
|
+
pointerdown: POINTER,
|
|
173
|
+
pointerup: POINTER,
|
|
174
|
+
pointermove: POINTER,
|
|
175
|
+
pointerenter: POINTER,
|
|
176
|
+
pointerleave: POINTER,
|
|
177
|
+
pointercancel: POINTER,
|
|
178
|
+
drag: DRAG,
|
|
179
|
+
dragstart: DRAG,
|
|
180
|
+
dragend: DRAG,
|
|
181
|
+
dragenter: DRAG,
|
|
182
|
+
dragleave: DRAG,
|
|
183
|
+
dragover: DRAG,
|
|
184
|
+
drop: DRAG,
|
|
185
|
+
touchstart: TOUCH,
|
|
186
|
+
touchmove: TOUCH,
|
|
187
|
+
touchend: TOUCH,
|
|
188
|
+
touchcancel: TOUCH,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function eventTypesFor(eventName) {
|
|
192
|
+
return EVENT_TYPES[eventName.toLowerCase()] ?? ['Event', 'HTMLElement']
|
|
129
193
|
}
|
|
130
194
|
|
|
131
195
|
// Walks a parsed template AST (from parseTemplate) collecting every
|
|
132
196
|
// bindable expression as a tree of check entries, ready for
|
|
133
197
|
// buildVirtualPageTs to emit into a synthetic __render() method.
|
|
134
198
|
// Entries come in two kinds:
|
|
135
|
-
// { kind: 'check', expr, line, eventType } -- a single
|
|
136
|
-
// to typecheck (interpolation, #if condition, dynamic
|
|
137
|
-
//
|
|
138
|
-
//
|
|
199
|
+
// { kind: 'check', expr, line, eventType, eventBase } -- a single
|
|
200
|
+
// expression to typecheck (interpolation, #if condition, dynamic
|
|
201
|
+
// attribute). eventType/eventBase are set for event handlers.
|
|
202
|
+
// { kind: 'each', itemName, indexName, iterableExpr, line, children }
|
|
203
|
+
// -- an #each block; children is itself a list of these same entries
|
|
139
204
|
// The 'each' kind exists so the emitted TS can wrap its children in
|
|
140
205
|
// a real `for (const f of ...)` -- without an actual declaration for
|
|
141
206
|
// the loop variable, TS has no way to resolve identifiers like `f`
|
|
142
207
|
// inside the block, regardless of what our own scope-tracking says.
|
|
208
|
+
// {{#else}} branches are collected as ordinary entries after their
|
|
209
|
+
// block, in the OUTER scope (an each-else has no loop variable).
|
|
143
210
|
export function collectTemplateChecks(nodes, scope, out) {
|
|
144
211
|
for (const node of nodes) {
|
|
145
212
|
if (node.type === 'Interpolation') {
|
|
@@ -148,6 +215,7 @@ export function collectTemplateChecks(nodes, scope, out) {
|
|
|
148
215
|
expr: typedTemplateExpr(node.expression, scope),
|
|
149
216
|
line: node.line,
|
|
150
217
|
eventType: null,
|
|
218
|
+
eventBase: null,
|
|
151
219
|
})
|
|
152
220
|
} else if (node.type === 'If') {
|
|
153
221
|
out.push({
|
|
@@ -155,23 +223,32 @@ export function collectTemplateChecks(nodes, scope, out) {
|
|
|
155
223
|
expr: typedTemplateExpr(node.condition, scope),
|
|
156
224
|
line: node.line,
|
|
157
225
|
eventType: null,
|
|
226
|
+
eventBase: null,
|
|
158
227
|
})
|
|
159
228
|
collectTemplateChecks(node.children, scope, out)
|
|
229
|
+
if (node.elseChildren) {
|
|
230
|
+
collectTemplateChecks(node.elseChildren, scope, out)
|
|
231
|
+
}
|
|
160
232
|
} else if (node.type === 'Each') {
|
|
161
233
|
const iterableExpr = typedTemplateExpr(node.iterable, scope)
|
|
162
234
|
const children = []
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
)
|
|
235
|
+
const innerScope = new Set([
|
|
236
|
+
...scope,
|
|
237
|
+
...paramNames([node.itemName]),
|
|
238
|
+
...(node.indexName ? [node.indexName] : []),
|
|
239
|
+
])
|
|
240
|
+
collectTemplateChecks(node.children, innerScope, children)
|
|
168
241
|
out.push({
|
|
169
242
|
kind: 'each',
|
|
170
243
|
itemName: node.itemName,
|
|
244
|
+
indexName: node.indexName ?? null,
|
|
171
245
|
iterableExpr,
|
|
172
246
|
line: node.line,
|
|
173
247
|
children,
|
|
174
248
|
})
|
|
249
|
+
if (node.elseChildren) {
|
|
250
|
+
collectTemplateChecks(node.elseChildren, scope, out)
|
|
251
|
+
}
|
|
175
252
|
} else if (node.type === 'Element') {
|
|
176
253
|
for (const attr of node.attributes) {
|
|
177
254
|
if (!attr.dynamic) continue
|
|
@@ -179,13 +256,15 @@ export function collectTemplateChecks(nodes, scope, out) {
|
|
|
179
256
|
const attrScope = eventMatch
|
|
180
257
|
? new Set([...scope, '$event', 'event'])
|
|
181
258
|
: scope
|
|
259
|
+
const [eventBase, eventType] = eventMatch
|
|
260
|
+
? eventTypesFor(eventMatch[1])
|
|
261
|
+
: [null, null]
|
|
182
262
|
out.push({
|
|
183
263
|
kind: 'check',
|
|
184
264
|
expr: typedTemplateExpr(attr.value, attrScope),
|
|
185
265
|
line: attr.line,
|
|
186
|
-
eventType
|
|
187
|
-
|
|
188
|
-
: null,
|
|
266
|
+
eventType,
|
|
267
|
+
eventBase,
|
|
189
268
|
})
|
|
190
269
|
}
|
|
191
270
|
collectTemplateChecks(node.children, scope, out)
|
package/src/typecheckPage.js
CHANGED
|
@@ -2,6 +2,7 @@ import { parsePageFile } from './parser/parsePageFile.js'
|
|
|
2
2
|
import { parseTemplate } from './parser/parseTemplate.js'
|
|
3
3
|
import { Lexer } from './lexer/Lexer.js'
|
|
4
4
|
import { Parser } from './parser/Parser.js'
|
|
5
|
+
import { normalizeLanguageOptions } from './parser/languageOptions.js'
|
|
5
6
|
import {
|
|
6
7
|
buildVirtualPageTs,
|
|
7
8
|
mapVirtualLineToSource,
|
|
@@ -20,17 +21,40 @@ import { substituteCapabilityRefs } from './ast/substituteCapabilityRefs.js'
|
|
|
20
21
|
// at all and floods the diagnostics list with dozens of
|
|
21
22
|
// cascading-recovery errors, none of which point at anything wrong
|
|
22
23
|
// in the actual .tyx source.
|
|
23
|
-
|
|
24
|
+
//
|
|
25
|
+
// `source` must already have its import lines blanked out by
|
|
26
|
+
// extractImports() (which keeps line numbers intact), or any page with
|
|
27
|
+
// an `import` line fails to parse here.
|
|
28
|
+
//
|
|
29
|
+
// `languageOption` must be the same object passed to
|
|
30
|
+
// renderPageDocument ({ not: 'loose' } etc.): the typechecker has to
|
|
31
|
+
// parse the page the same way the real build does, or it would report
|
|
32
|
+
// on a different program than the one that runs.
|
|
33
|
+
export function typecheckPage(
|
|
34
|
+
source,
|
|
35
|
+
apiHelpersSource = '',
|
|
36
|
+
languageOption = {},
|
|
37
|
+
) {
|
|
38
|
+
// createApp has a variable named `language` that holds
|
|
39
|
+
// 'typescript' | 'javascript'. Passing that here by mistake would
|
|
40
|
+
// silently fall back to defaults, so refuse it loudly.
|
|
41
|
+
if (languageOption !== null && typeof languageOption !== 'object') {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`typecheckPage: the language options must be an object like { not: 'loose' }, got ${typeof languageOption} "${languageOption}"`,
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
const language = normalizeLanguageOptions(languageOption ?? {})
|
|
47
|
+
|
|
24
48
|
const { pageName, script, template, scriptStartLine } = parsePageFile(source)
|
|
25
49
|
|
|
26
50
|
const pageNode = substituteCapabilityRefs(
|
|
27
51
|
script.trim().length > 0
|
|
28
|
-
? new Parser(new Lexer(script).tokenize()).parse()
|
|
52
|
+
? new Parser(new Lexer(script).tokenize(), language).parse()
|
|
29
53
|
: { props: [], state: [], computed: [], actions: [], onMount: null },
|
|
30
54
|
)
|
|
31
55
|
|
|
32
56
|
const templateNodes =
|
|
33
|
-
template.trim().length > 0 ? parseTemplate(template) : null
|
|
57
|
+
template.trim().length > 0 ? parseTemplate(template, language) : null
|
|
34
58
|
|
|
35
59
|
const { source: virtualSource, lineMap } = buildVirtualPageTs(
|
|
36
60
|
pageNode,
|
|
@@ -43,4 +67,4 @@ export function typecheckPage(source, apiHelpersSource = '') {
|
|
|
43
67
|
const scriptLine = mapVirtualLineToSource(lineMap, d.virtualLine)
|
|
44
68
|
return { message: d.message, line: scriptLine + scriptStartLine - 1 }
|
|
45
69
|
})
|
|
46
|
-
}
|
|
70
|
+
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
// A component package (like tylix-icons) ships a manifest at its
|
|
5
|
-
// root mapping exported names to the .tyx file that defines them:
|
|
6
|
-
// { "IconCheck": "icons/IconCheck.tyx", "IconTrash": "icons/IconTrash.tyx" }
|
|
7
|
-
// This keeps resolution to "read one JSON file, then read the .tyx
|
|
8
|
-
// files it points at" -- no bundler, no special package.json export
|
|
9
|
-
// map, just a plain manifest a package author (or a scaffolding tool)
|
|
10
|
-
// can hand-write.
|
|
11
|
-
const MANIFEST_FILE = 'tylix-components.json'
|
|
12
|
-
|
|
13
|
-
export async function resolvePackageComponents(imports, baseDir) {
|
|
14
|
-
const components = {}
|
|
15
|
-
|
|
16
|
-
for (const { names, package: pkgName } of imports) {
|
|
17
|
-
const pkgDir = path.join(baseDir, 'node_modules', pkgName)
|
|
18
|
-
const manifestPath = path.join(pkgDir, MANIFEST_FILE)
|
|
19
|
-
|
|
20
|
-
let manifest
|
|
21
|
-
try {
|
|
22
|
-
manifest = JSON.parse(await fs.readFile(manifestPath, 'utf-8'))
|
|
23
|
-
} catch {
|
|
24
|
-
throw new Error(
|
|
25
|
-
`Cannot resolve components from "${pkgName}": missing or invalid ${MANIFEST_FILE} in node_modules/${pkgName}. Is the package installed?`,
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
for (const name of names) {
|
|
30
|
-
const relPath = manifest[name]
|
|
31
|
-
if (!relPath) {
|
|
32
|
-
throw new Error(
|
|
33
|
-
`"${pkgName}" does not export a component named "${name}" (checked ${MANIFEST_FILE})`,
|
|
34
|
-
)
|
|
35
|
-
}
|
|
36
|
-
components[name] = await fs.readFile(path.join(pkgDir, relPath), 'utf-8')
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
return components
|
|
41
|
-
}
|