@tylix/compiler 0.5.2 → 0.7.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 +219 -16
- package/src/ast/substituteCapabilityRefs.js +185 -61
- package/src/codegen/emitStatementLines.js +98 -54
- package/src/codegen/generateExpression.js +339 -36
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +169 -20
- package/src/codegen/generateTemplate.js +183 -101
- package/src/codegen/generateTemplateExpression.js +102 -59
- package/src/codegen/jsGlobals.js +211 -0
- package/src/compileComponent.js +39 -8
- 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 +430 -235
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +2176 -325
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +23 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +350 -62
- package/src/renderPageDocument.js +338 -56
- package/src/runtime/capability.js +39 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +362 -64
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +23 -12
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +195 -53
- package/src/typecheckPage.js +28 -4
- 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/imports/resolvePackageComponents.js +0 -41
- 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
package/src/runtime/useApi.js
CHANGED
|
@@ -4,16 +4,56 @@ export async function useApi(url, options = {}) {
|
|
|
4
4
|
body,
|
|
5
5
|
headers = {},
|
|
6
6
|
credentials = 'include',
|
|
7
|
+
// Abort after this many milliseconds. 0 means no timeout.
|
|
8
|
+
timeout = 0,
|
|
9
|
+
signal,
|
|
7
10
|
} = options
|
|
8
11
|
|
|
9
12
|
const finalHeaders = { ...headers }
|
|
13
|
+
const hasHeader = (name) =>
|
|
14
|
+
Object.keys(finalHeaders).some((k) => k.toLowerCase() === name)
|
|
15
|
+
|
|
16
|
+
// Bodies fetch understands natively are sent untouched. Any other
|
|
17
|
+
// object (plain objects, arrays, reactive proxies) is sent as JSON.
|
|
18
|
+
const isNativeBody =
|
|
19
|
+
body instanceof FormData ||
|
|
20
|
+
body instanceof Blob ||
|
|
21
|
+
body instanceof URLSearchParams ||
|
|
22
|
+
body instanceof ArrayBuffer ||
|
|
23
|
+
ArrayBuffer.isView(body) ||
|
|
24
|
+
(typeof ReadableStream !== 'undefined' && body instanceof ReadableStream)
|
|
25
|
+
|
|
10
26
|
let finalBody = body
|
|
11
|
-
if (body && typeof body === 'object' && !
|
|
12
|
-
|
|
13
|
-
finalHeaders['Content-Type']
|
|
27
|
+
if (body && typeof body === 'object' && !isNativeBody) {
|
|
28
|
+
if (!hasHeader('content-type')) {
|
|
29
|
+
finalHeaders['Content-Type'] = 'application/json'
|
|
30
|
+
}
|
|
14
31
|
finalBody = JSON.stringify(body)
|
|
15
32
|
}
|
|
16
33
|
|
|
34
|
+
// fetch() throws a TypeError for a GET/HEAD with a body, which used
|
|
35
|
+
// to surface as a confusing "could not reach the server".
|
|
36
|
+
const upper = String(method).toUpperCase()
|
|
37
|
+
if ((upper === 'GET' || upper === 'HEAD') && finalBody !== undefined) {
|
|
38
|
+
console.warn(
|
|
39
|
+
`[tylix] ${upper} ${url}: a ${upper} request cannot have a body, so it was ignored.`,
|
|
40
|
+
)
|
|
41
|
+
finalBody = undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let controller = null
|
|
45
|
+
let timer = null
|
|
46
|
+
let fetchSignal = signal
|
|
47
|
+
if (timeout > 0) {
|
|
48
|
+
controller = new AbortController()
|
|
49
|
+
timer = setTimeout(() => controller.abort(), timeout)
|
|
50
|
+
if (signal) {
|
|
51
|
+
if (signal.aborted) controller.abort()
|
|
52
|
+
else signal.addEventListener('abort', () => controller.abort(), { once: true })
|
|
53
|
+
}
|
|
54
|
+
fetchSignal = controller.signal
|
|
55
|
+
}
|
|
56
|
+
|
|
17
57
|
// __tylixRequestStart/End are injected by the runtime. Guarding them
|
|
18
58
|
// lets this exact file work whether or not they exist.
|
|
19
59
|
const track = typeof __tylixRequestStart === 'function'
|
|
@@ -25,13 +65,17 @@ export async function useApi(url, options = {}) {
|
|
|
25
65
|
headers: finalHeaders,
|
|
26
66
|
body: finalBody,
|
|
27
67
|
credentials,
|
|
68
|
+
signal: fetchSignal,
|
|
28
69
|
})
|
|
70
|
+
|
|
29
71
|
let data = null
|
|
30
72
|
try {
|
|
31
|
-
|
|
73
|
+
const text = await response.text()
|
|
74
|
+
if (text) data = JSON.parse(text)
|
|
32
75
|
} catch {
|
|
33
|
-
/* empty body */
|
|
76
|
+
/* empty or non-JSON body */
|
|
34
77
|
}
|
|
78
|
+
|
|
35
79
|
if (!response.ok) {
|
|
36
80
|
// Only present when the server runs with APP_DEBUG=true.
|
|
37
81
|
const debug =
|
|
@@ -41,18 +85,26 @@ export async function useApi(url, options = {}) {
|
|
|
41
85
|
ok: false,
|
|
42
86
|
status: response.status,
|
|
43
87
|
data,
|
|
44
|
-
error:
|
|
88
|
+
error:
|
|
89
|
+
(data && (data.error || data.message)) ||
|
|
90
|
+
`Request failed (HTTP ${response.status})`,
|
|
45
91
|
debug,
|
|
46
92
|
}
|
|
47
93
|
}
|
|
48
94
|
return { ok: true, status: response.status, data, error: null, debug: null }
|
|
49
95
|
} catch (err) {
|
|
96
|
+
const timedOut =
|
|
97
|
+
controller && controller.signal.aborted && !(signal && signal.aborted)
|
|
98
|
+
const message = timedOut
|
|
99
|
+
? `Request timed out after ${timeout}ms`
|
|
100
|
+
: err.message
|
|
50
101
|
console.warn(
|
|
51
|
-
`[tylix] ${method} ${url} could not reach the server: ${
|
|
102
|
+
`[tylix] ${method} ${url} could not reach the server: ${message}. ` +
|
|
52
103
|
'Is "tylix dev" running, and is the URL correct?',
|
|
53
104
|
)
|
|
54
|
-
return { ok: false, status: 0, data: null, error:
|
|
105
|
+
return { ok: false, status: 0, data: null, error: message, debug: null }
|
|
55
106
|
} finally {
|
|
107
|
+
if (timer) clearTimeout(timer)
|
|
56
108
|
if (track) __tylixRequestEnd()
|
|
57
109
|
}
|
|
58
110
|
|
|
@@ -61,7 +113,13 @@ export async function useApi(url, options = {}) {
|
|
|
61
113
|
// file still works if the compiler injects only this one function.
|
|
62
114
|
// ---------------------------------------------------------------
|
|
63
115
|
function showDebug(debug, method, url) {
|
|
64
|
-
|
|
116
|
+
// Neither step may throw: this runs inside the request's try block,
|
|
117
|
+
// so an error here would be reported as a network failure.
|
|
118
|
+
try {
|
|
119
|
+
logToConsole(debug, method, url)
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.warn('[tylix] debug logging failed:', err)
|
|
122
|
+
}
|
|
65
123
|
try {
|
|
66
124
|
showOverlay(debug, method, url)
|
|
67
125
|
} catch (err) {
|
|
@@ -99,10 +157,6 @@ export async function useApi(url, options = {}) {
|
|
|
99
157
|
index: 0,
|
|
100
158
|
open: false,
|
|
101
159
|
})
|
|
102
|
-
store.errors.unshift({ debug, method, url })
|
|
103
|
-
store.errors = store.errors.slice(0, 20)
|
|
104
|
-
store.index = 0
|
|
105
|
-
render()
|
|
106
160
|
|
|
107
161
|
function h(tag, props, ...children) {
|
|
108
162
|
const node = document.createElement(tag)
|
|
@@ -401,5 +455,12 @@ export async function useApi(url, options = {}) {
|
|
|
401
455
|
.frame span { color: var(--ac); font-size: 12px; word-break: break-all; }
|
|
402
456
|
.actions { display: flex; gap: 8px; margin-top: 22px; }
|
|
403
457
|
`
|
|
458
|
+
|
|
459
|
+
// Last on purpose: render() reads CSS and KEYWORDS, which are
|
|
460
|
+
// consts and cannot be touched before their declarations above.
|
|
461
|
+
store.errors.unshift({ debug, method, url })
|
|
462
|
+
store.errors = store.errors.slice(0, 20)
|
|
463
|
+
store.index = 0
|
|
464
|
+
render()
|
|
404
465
|
}
|
|
405
466
|
}
|
|
@@ -1,11 +1,27 @@
|
|
|
1
1
|
export function useChannel(name) {
|
|
2
2
|
const port = window.__TYLIX_CHANNELS_PORT__ ?? 6001
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
)
|
|
6
|
-
|
|
3
|
+
// An https page may not open a plain ws:// socket (SecurityError).
|
|
4
|
+
const scheme = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
|
7
5
|
const listeners = []
|
|
8
6
|
|
|
7
|
+
let socket
|
|
8
|
+
try {
|
|
9
|
+
socket = new WebSocket(
|
|
10
|
+
`${scheme}://${window.location.hostname}:${port}/${name}`,
|
|
11
|
+
)
|
|
12
|
+
} catch (err) {
|
|
13
|
+
// Return an inert channel so one failed socket does not take the
|
|
14
|
+
// whole page down; the page can still render.
|
|
15
|
+
console.error(`useChannel("${name}") could not connect:`, err)
|
|
16
|
+
return {
|
|
17
|
+
send() {},
|
|
18
|
+
onMessage() {
|
|
19
|
+
return () => {}
|
|
20
|
+
},
|
|
21
|
+
close() {},
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
socket.addEventListener('message', (event) => {
|
|
10
26
|
let data
|
|
11
27
|
try {
|
|
@@ -13,7 +29,7 @@ export function useChannel(name) {
|
|
|
13
29
|
} catch {
|
|
14
30
|
return
|
|
15
31
|
}
|
|
16
|
-
for (const fn of listeners) fn(data)
|
|
32
|
+
for (const fn of [...listeners]) fn(data)
|
|
17
33
|
})
|
|
18
34
|
|
|
19
35
|
return {
|
|
@@ -28,11 +44,16 @@ export function useChannel(name) {
|
|
|
28
44
|
)
|
|
29
45
|
}
|
|
30
46
|
},
|
|
47
|
+
// Returns an unsubscribe function.
|
|
31
48
|
onMessage(fn) {
|
|
32
49
|
listeners.push(fn)
|
|
50
|
+
return () => {
|
|
51
|
+
const i = listeners.indexOf(fn)
|
|
52
|
+
if (i !== -1) listeners.splice(i, 1)
|
|
53
|
+
}
|
|
33
54
|
},
|
|
34
55
|
close() {
|
|
35
56
|
socket.close()
|
|
36
57
|
},
|
|
37
58
|
}
|
|
38
|
-
}
|
|
59
|
+
}
|
|
@@ -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)
|
|
@@ -51,6 +51,10 @@ export function buildVirtualPageTs(pageNode, className, templateNodes = null) {
|
|
|
51
51
|
},
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
// Module-level helpers and type declarations go before the class.
|
|
55
|
+
const helpers = pageNode.helpers ?? []
|
|
56
|
+
for (const h of helpers) emitStatementLines(h, sink, '')
|
|
57
|
+
|
|
54
58
|
if (pageNode.props.length > 0) {
|
|
55
59
|
sink.pushLine(`interface ${className}Props {`, null)
|
|
56
60
|
for (const p of pageNode.props) {
|
|
@@ -91,7 +95,7 @@ export function buildVirtualPageTs(pageNode, className, templateNodes = null) {
|
|
|
91
95
|
: ''
|
|
92
96
|
sink.pushLine(``, null)
|
|
93
97
|
sink.pushLine(
|
|
94
|
-
` ${a.isAsync ? 'async ' : ''}${a.name}(${
|
|
98
|
+
` ${a.isAsync ? 'async ' : ''}${a.name}${a.typeParams ?? ''}(${generateParams(a.params, true)})${returnType ? `: ${returnType}` : ''} {`,
|
|
95
99
|
a.line,
|
|
96
100
|
)
|
|
97
101
|
for (const stmt of a.body) emitStatementLines(stmt, sink, ' ')
|
|
@@ -112,7 +116,8 @@ export function buildVirtualPageTs(pageNode, className, templateNodes = null) {
|
|
|
112
116
|
collectTemplateChecks(templateNodes, new Set(), checks)
|
|
113
117
|
if (checks.length > 0) {
|
|
114
118
|
sink.pushLine(``, null)
|
|
115
|
-
|
|
119
|
+
// async so `await` inside event handlers typechecks
|
|
120
|
+
sink.pushLine(` async __render() {`, null)
|
|
116
121
|
emitChecks(checks, sink, ' ')
|
|
117
122
|
sink.pushLine(` }`, null)
|
|
118
123
|
}
|
|
@@ -120,6 +125,12 @@ export function buildVirtualPageTs(pageNode, className, templateNodes = null) {
|
|
|
120
125
|
|
|
121
126
|
sink.pushLine(`}`, null)
|
|
122
127
|
|
|
128
|
+
// Module-level helpers (e.g. `const name = ...`) would clash with DOM
|
|
129
|
+
// globals in a script file; `export {}` makes it a module.
|
|
130
|
+
if (helpers.some((h) => h.type !== 'TypeDeclaration')) {
|
|
131
|
+
sink.pushLine('export {};', null)
|
|
132
|
+
}
|
|
133
|
+
|
|
123
134
|
return { source: lines.join('\n') + '\n', lineMap }
|
|
124
135
|
}
|
|
125
136
|
|
|
@@ -130,4 +141,4 @@ export function mapVirtualLineToSource(lineMap, virtualLine) {
|
|
|
130
141
|
else break
|
|
131
142
|
}
|
|
132
143
|
return best
|
|
133
|
-
}
|
|
144
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
// Present on failed requests only when the server runs with APP_DEBUG=true.
|
|
3
2
|
interface TylixDebugInfo {
|
|
4
3
|
status: number
|
|
@@ -40,15 +39,25 @@ interface TylixApiResult {
|
|
|
40
39
|
debug?: TylixDebugInfo | null
|
|
41
40
|
}
|
|
42
41
|
|
|
42
|
+
interface TylixChannel {
|
|
43
|
+
send(data: unknown): void
|
|
44
|
+
// Returns an unsubscribe function.
|
|
45
|
+
onMessage(fn: (data: any) => void): () => void
|
|
46
|
+
close(): void
|
|
47
|
+
}
|
|
48
|
+
|
|
43
49
|
declare function useApi(
|
|
44
50
|
path: string,
|
|
45
51
|
options?: {
|
|
46
52
|
method?: string
|
|
47
53
|
body?: unknown
|
|
48
54
|
headers?: Record<string, string>
|
|
55
|
+
credentials?: RequestCredentials
|
|
56
|
+
// Abort after this many milliseconds. 0 or omitted means no timeout.
|
|
57
|
+
timeout?: number
|
|
58
|
+
signal?: AbortSignal
|
|
49
59
|
},
|
|
50
60
|
): Promise<TylixApiResult>
|
|
51
|
-
|
|
52
61
|
// getApi/postApi/putApi/deleteApi share useApi's response shape --
|
|
53
62
|
// convenience wrappers over the same underlying call.
|
|
54
63
|
declare function getApi(
|
|
@@ -73,6 +82,16 @@ declare function deleteApi(
|
|
|
73
82
|
options?: { headers?: Record<string, string> },
|
|
74
83
|
): Promise<TylixApiResult>
|
|
75
84
|
|
|
76
|
-
|
|
85
|
+
interface ApiOptions {
|
|
86
|
+
headers?: Record<string, string>
|
|
87
|
+
credentials?: RequestCredentials
|
|
88
|
+
timeout?: number
|
|
89
|
+
signal?: AbortSignal
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
declare function useChannel(name: string): TylixChannel
|
|
93
|
+
|
|
94
|
+
declare function notFound(): void
|
|
77
95
|
|
|
78
|
-
|
|
96
|
+
// Capability names used in page scripts compile to this call.
|
|
97
|
+
declare function resolveCapability(name: string): any
|