@kayjs/kay 2.0.0-beta.3 → 2.0.0-beta.4
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/llms.txt +42 -0
- package/package.json +24 -13
- package/src/build/bundle.ts +11 -51
- package/src/build/client-plugin.ts +108 -0
- package/src/build/compile.ts +4 -2
- package/src/build/graph.ts +16 -3
- package/src/build/index.ts +23 -15
- package/src/build/islands.ts +28 -30
- package/src/build/manifest.ts +6 -3
- package/src/build/report.ts +82 -13
- package/src/build/resolve.ts +6 -7
- package/src/build/routes.ts +12 -21
- package/src/build/server-entry.ts +1 -1
- package/src/cli/check.ts +49 -43
- package/src/cli/commands.ts +224 -0
- package/src/cli/index.ts +46 -302
- package/src/cli/new.ts +15 -509
- package/src/cli/upgrade-plugins.ts +168 -0
- package/src/cli/upgrade.ts +220 -0
- package/src/compiler/analyze.ts +319 -565
- package/src/compiler/ast.ts +39 -0
- package/src/compiler/data-keys.ts +233 -0
- package/src/compiler/dom.ts +18 -15
- package/src/compiler/interactivity.ts +174 -0
- package/src/compiler/mistakes.ts +19 -1
- package/src/compiler/server-only.ts +4 -1
- package/src/compiler/shared.ts +1 -1
- package/src/compiler/ssr.ts +28 -28
- package/src/context.ts +1 -1
- package/src/dev/index.ts +15 -23
- package/src/dom/extras.ts +43 -12
- package/src/dom/index.ts +25 -1
- package/src/index.ts +0 -13
- package/src/jsx-runtime.ts +1 -1
- package/src/plugins/context.ts +8 -6
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +25 -2
- package/src/plugins/public.ts +6 -0
- package/src/plugins/services.ts +15 -3
- package/src/plugins/types.ts +7 -11
- package/src/server/action-registry.ts +10 -4
- package/src/server/action.ts +8 -2
- package/src/server/document.ts +13 -1
- package/src/server/errors.ts +7 -5
- package/src/server/index.ts +9 -10
- package/src/server/lifecycle.ts +22 -0
- package/src/server/plugin-host.ts +6 -8
- package/src/server/public.ts +15 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/stream.ts +4 -3
- package/src/ssr/context.ts +25 -0
- package/src/ssr/index.ts +10 -7
- package/src/test/browser.ts +180 -0
- package/src/test/index.ts +4 -1
- package/templates/_shared/AGENTS.md +5 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/tsconfig.json +19 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/kay.config.ts +11 -0
- package/templates/app/package.json +20 -0
- package/templates/app/src/auth.ts +14 -0
- package/templates/app/src/routes/_layout.tsx +32 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +13 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +34 -0
- package/templates/app/src/styles/app.css +7 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/kay.config.ts +23 -0
- package/templates/blog/package.json +21 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +20 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +29 -0
- package/templates/blog/src/styles/app.css +10 -0
- package/templates/fullstack/kay.config.ts +6 -0
- package/templates/fullstack/package.json +18 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +26 -0
- package/templates/fullstack/src/routes/_layout.tsx +14 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +29 -0
- package/templates/minimal/kay.config.ts +6 -0
- package/templates/minimal/package.json +18 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +14 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +22 -0
package/src/compiler/analyze.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// OXC-based static analysis for TSX/TS files: automatic island detection, actions, prerender, Head/Suspense.
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
|
-
ArrowFunctionExpression,
|
|
5
4
|
CallExpression,
|
|
6
5
|
ExportNamedDeclaration,
|
|
7
6
|
Function as OxcFunction,
|
|
@@ -11,10 +10,13 @@ import type {
|
|
|
11
10
|
VariableDeclaration,
|
|
12
11
|
} from 'oxc-parser'
|
|
13
12
|
import { parseSync, visitorKeys } from 'oxc-parser'
|
|
14
|
-
import {
|
|
13
|
+
import { walk } from './ast'
|
|
14
|
+
import type { BindingFacts, Interactivity } from './interactivity'
|
|
15
|
+
import { analyzeDefaultDataKeys } from './data-keys'
|
|
16
|
+
import { resolveInteractivity } from './interactivity'
|
|
17
|
+
import { assertScopedKayImport, checkBareSignals, checkHydrateMode, checkMistakes, islandRemoved, mistake, serverOnlyTagError } from './mistakes'
|
|
15
18
|
import { EVENT_ATTR, formatCodeFrame, formatDiagnostic, formatLoc, isComponentTag, isJsxNode, jsxTagName, unwrapTsExpr } from './shared'
|
|
16
19
|
|
|
17
|
-
export type AstNode = Node
|
|
18
20
|
|
|
19
21
|
const REACTIVE_IMPORTS = new Set([
|
|
20
22
|
'signal',
|
|
@@ -24,16 +26,16 @@ const REACTIVE_IMPORTS = new Set([
|
|
|
24
26
|
'onCleanup',
|
|
25
27
|
'selector',
|
|
26
28
|
'store',
|
|
27
|
-
'island',
|
|
28
29
|
'useAction',
|
|
29
30
|
])
|
|
30
31
|
|
|
31
32
|
// Primitives whose result is reactive state another module can import and read.
|
|
32
33
|
const STATE_PRIMITIVES = new Set(['signal', 'computed', 'selector', 'store'])
|
|
33
|
-
const KAY_SOURCES = new Set(['
|
|
34
|
-
const KAY_SSR_SOURCES = new Set(['
|
|
35
|
-
const KAY_SERVER_SOURCES = new Set(['
|
|
34
|
+
const KAY_SOURCES = new Set(['@kayjs/kay'])
|
|
35
|
+
const KAY_SSR_SOURCES = new Set(['@kayjs/kay', '@kayjs/kay/internal/ssr'])
|
|
36
|
+
const KAY_SERVER_SOURCES = new Set(['@kayjs/kay/server'])
|
|
36
37
|
const RENDER_CONTEXT_IMPORTS = new Set(['Head', 'Suspense', 'createContext', 'provideContext', 'useContext', 'useId'])
|
|
38
|
+
const SERVER_ONLY_TAGS = new Set(['Head', 'Suspense'])
|
|
37
39
|
|
|
38
40
|
export interface ImportInfo {
|
|
39
41
|
source: string
|
|
@@ -49,12 +51,9 @@ export interface ReexportInfo {
|
|
|
49
51
|
start: number
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
export interface ModuleAnalysis {
|
|
54
|
+
export interface ModuleAnalysis extends Interactivity {
|
|
53
55
|
filename: string
|
|
54
56
|
program: Program
|
|
55
|
-
isInteractive: boolean
|
|
56
|
-
interactiveReason: string | undefined
|
|
57
|
-
interactiveOffset?: number | undefined
|
|
58
57
|
hasAsyncDefault: boolean
|
|
59
58
|
hasLoadExport: boolean
|
|
60
59
|
hasJsx: boolean
|
|
@@ -77,6 +76,10 @@ export interface ModuleAnalysis {
|
|
|
77
76
|
imports: ImportInfo[]
|
|
78
77
|
reexports: ReexportInfo[]
|
|
79
78
|
exportedNames: string[]
|
|
79
|
+
/** Facts per top-level binding, which `kay build` and `kay check` re-resolve once imported state is known. */
|
|
80
|
+
bindings: BindingFacts[]
|
|
81
|
+
/** Each export by name, with the top-level binding it exports. */
|
|
82
|
+
exportLocals: Map<string, string>
|
|
80
83
|
/** Code that compiles but usually means something else, worded for build and `kay check` to print. */
|
|
81
84
|
warnings: string[]
|
|
82
85
|
}
|
|
@@ -124,38 +127,6 @@ export function computeJsxNodes(program: Program): WeakSet<Node> {
|
|
|
124
127
|
return jsxNodes
|
|
125
128
|
}
|
|
126
129
|
|
|
127
|
-
export function walk(node: unknown, visit: (node: Node) => boolean | void): void {
|
|
128
|
-
if (node === null || typeof node !== 'object')
|
|
129
|
-
return
|
|
130
|
-
if (Array.isArray(node)) {
|
|
131
|
-
for (let i = 0; i < node.length; i++)
|
|
132
|
-
walk(node[i], visit)
|
|
133
|
-
return
|
|
134
|
-
}
|
|
135
|
-
const ast = node as Node
|
|
136
|
-
const type = ast.type
|
|
137
|
-
if (typeof type !== 'string')
|
|
138
|
-
return
|
|
139
|
-
if (visit(ast) === false)
|
|
140
|
-
return
|
|
141
|
-
const keys = visitorKeys[type]
|
|
142
|
-
if (keys !== undefined) {
|
|
143
|
-
const rec = ast as unknown as Record<string, unknown>
|
|
144
|
-
for (let i = 0; i < keys.length; i++) {
|
|
145
|
-
const val = rec[keys[i]!]
|
|
146
|
-
if (val !== null && typeof val === 'object')
|
|
147
|
-
walk(val, visit)
|
|
148
|
-
}
|
|
149
|
-
return
|
|
150
|
-
}
|
|
151
|
-
for (const [key, val] of Object.entries(ast)) {
|
|
152
|
-
if (key === 'start' || key === 'end' || key === 'type')
|
|
153
|
-
continue
|
|
154
|
-
if (val !== null && typeof val === 'object')
|
|
155
|
-
walk(val, visit)
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
130
|
function exportedName(node: { type: string, name?: string, value?: string }): string | undefined {
|
|
160
131
|
return node.type === 'Identifier' ? node.name : node.value
|
|
161
132
|
}
|
|
@@ -196,240 +167,6 @@ export function loadDeclarations(program: Program): Node[] {
|
|
|
196
167
|
return found
|
|
197
168
|
}
|
|
198
169
|
|
|
199
|
-
function staticPropKey(prop: Node): string | undefined {
|
|
200
|
-
if (prop.type !== 'Property')
|
|
201
|
-
return undefined
|
|
202
|
-
if (!prop.computed && prop.key.type === 'Identifier')
|
|
203
|
-
return prop.key.name
|
|
204
|
-
if (prop.key.type === 'Literal' && typeof prop.key.value === 'string')
|
|
205
|
-
return prop.key.value
|
|
206
|
-
return undefined
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function extractObjectPatternKeys(pat: Node): string[] | undefined {
|
|
210
|
-
if (pat.type !== 'ObjectPattern')
|
|
211
|
-
return undefined
|
|
212
|
-
const keys: string[] = []
|
|
213
|
-
for (const prop of pat.properties) {
|
|
214
|
-
if (prop.type === 'RestElement')
|
|
215
|
-
return undefined
|
|
216
|
-
const k = staticPropKey(prop)
|
|
217
|
-
if (k === undefined)
|
|
218
|
-
return undefined
|
|
219
|
-
keys.push(k)
|
|
220
|
-
}
|
|
221
|
-
return keys
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
function memberKeyName(node: Node): string | undefined {
|
|
225
|
-
if (node.type !== 'MemberExpression')
|
|
226
|
-
return undefined
|
|
227
|
-
if (!node.computed && node.property.type === 'Identifier')
|
|
228
|
-
return node.property.name
|
|
229
|
-
if (node.computed && node.property.type === 'Literal' && typeof node.property.value === 'string')
|
|
230
|
-
return node.property.value
|
|
231
|
-
return undefined
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function unwrapParamPattern(param: Node): Node {
|
|
235
|
-
return param.type === 'AssignmentPattern' ? param.left : param
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function resolveDefaultFunction(
|
|
239
|
-
program: Program,
|
|
240
|
-
defaultLocal: string | undefined,
|
|
241
|
-
localInits: ReadonlyMap<string, Node>,
|
|
242
|
-
islandLocalName: string | undefined,
|
|
243
|
-
kayNamespaces: ReadonlySet<string>,
|
|
244
|
-
): OxcFunction | ArrowFunctionExpression | undefined {
|
|
245
|
-
const localFns = new Map<string, OxcFunction>()
|
|
246
|
-
for (const stmt of program.body) {
|
|
247
|
-
if (stmt.type === 'FunctionDeclaration' && stmt.id !== null)
|
|
248
|
-
localFns.set(stmt.id.name, stmt)
|
|
249
|
-
else if (stmt.type === 'ExportNamedDeclaration' && stmt.declaration?.type === 'FunctionDeclaration' && stmt.declaration.id !== null)
|
|
250
|
-
localFns.set(stmt.declaration.id.name, stmt.declaration)
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const unwrapTarget = (node: Node | undefined, depth = 0): OxcFunction | ArrowFunctionExpression | undefined => {
|
|
254
|
-
if (node === undefined || depth > 4)
|
|
255
|
-
return undefined
|
|
256
|
-
const inner = unwrapTsExpr(node)
|
|
257
|
-
if (inner.type === 'FunctionDeclaration' || inner.type === 'FunctionExpression' || inner.type === 'ArrowFunctionExpression')
|
|
258
|
-
return inner
|
|
259
|
-
if (isIslandCall(inner, islandLocalName, kayNamespaces))
|
|
260
|
-
return unwrapTarget(inner.arguments[0], depth + 1)
|
|
261
|
-
if (inner.type === 'Identifier')
|
|
262
|
-
return localFns.get(inner.name) ?? unwrapTarget(localInits.get(inner.name), depth + 1)
|
|
263
|
-
return undefined
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
for (const stmt of program.body) {
|
|
267
|
-
if (stmt.type === 'ExportDefaultDeclaration')
|
|
268
|
-
return unwrapTarget(stmt.declaration)
|
|
269
|
-
}
|
|
270
|
-
if (defaultLocal !== undefined)
|
|
271
|
-
return localFns.get(defaultLocal) ?? unwrapTarget(localInits.get(defaultLocal))
|
|
272
|
-
return undefined
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
/** Statically bounds which top-level keys of `props.data` a page island reads; returns `undefined` if `data` escapes. */
|
|
276
|
-
function analyzeDefaultDataKeys(
|
|
277
|
-
program: Program,
|
|
278
|
-
defaultLocal: string | undefined,
|
|
279
|
-
localInits: ReadonlyMap<string, Node>,
|
|
280
|
-
islandLocalName: string | undefined,
|
|
281
|
-
kayNamespaces: ReadonlySet<string>,
|
|
282
|
-
): string[] | undefined {
|
|
283
|
-
const fn = resolveDefaultFunction(program, defaultLocal, localInits, islandLocalName, kayNamespaces)
|
|
284
|
-
if (fn === undefined || fn.body === null)
|
|
285
|
-
return undefined
|
|
286
|
-
if (fn.params.length === 0)
|
|
287
|
-
return []
|
|
288
|
-
|
|
289
|
-
const firstParam = unwrapParamPattern(fn.params[0]!)
|
|
290
|
-
const keys = new Set<string>()
|
|
291
|
-
let escaped = false
|
|
292
|
-
let dataVar: string | undefined
|
|
293
|
-
let propsVar: string | undefined
|
|
294
|
-
|
|
295
|
-
if (firstParam.type === 'ObjectPattern') {
|
|
296
|
-
let dataProp: Node | undefined
|
|
297
|
-
for (const prop of firstParam.properties) {
|
|
298
|
-
if (prop.type === 'RestElement')
|
|
299
|
-
return undefined
|
|
300
|
-
if (staticPropKey(prop) === 'data')
|
|
301
|
-
dataProp = prop
|
|
302
|
-
}
|
|
303
|
-
if (dataProp === undefined || dataProp.type !== 'Property')
|
|
304
|
-
return []
|
|
305
|
-
const binding = unwrapParamPattern(dataProp.value)
|
|
306
|
-
if (binding.type === 'ObjectPattern') {
|
|
307
|
-
const nested = extractObjectPatternKeys(binding)
|
|
308
|
-
if (nested === undefined)
|
|
309
|
-
return undefined
|
|
310
|
-
return [...new Set(nested)].sort()
|
|
311
|
-
}
|
|
312
|
-
if (binding.type !== 'Identifier')
|
|
313
|
-
return undefined
|
|
314
|
-
dataVar = binding.name
|
|
315
|
-
}
|
|
316
|
-
else if (firstParam.type === 'Identifier') {
|
|
317
|
-
propsVar = firstParam.name
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
return undefined
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// Walk the function body tracking parent chains so every reference to `dataVar` or `propsVar` is verified.
|
|
324
|
-
const ancestors: Node[] = []
|
|
325
|
-
const walkWithParents = (node: unknown): void => {
|
|
326
|
-
if (escaped || node === null || typeof node !== 'object')
|
|
327
|
-
return
|
|
328
|
-
if (Array.isArray(node)) {
|
|
329
|
-
for (let i = 0; i < node.length; i++)
|
|
330
|
-
walkWithParents(node[i])
|
|
331
|
-
return
|
|
332
|
-
}
|
|
333
|
-
const ast = node as Node
|
|
334
|
-
const type = ast.type
|
|
335
|
-
if (typeof type !== 'string')
|
|
336
|
-
return
|
|
337
|
-
|
|
338
|
-
// Respect inner function parameters that shadow `dataVar` or `propsVar`.
|
|
339
|
-
if (ast !== fn && (type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression')) {
|
|
340
|
-
const shadowed = new Set<string>()
|
|
341
|
-
for (const p of (ast as OxcFunction).params) {
|
|
342
|
-
walk(p, (pn) => {
|
|
343
|
-
if (pn.type === 'Identifier')
|
|
344
|
-
shadowed.add(pn.name)
|
|
345
|
-
})
|
|
346
|
-
}
|
|
347
|
-
if ((dataVar !== undefined && shadowed.has(dataVar)) || (propsVar !== undefined && shadowed.has(propsVar)))
|
|
348
|
-
return
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
if (type === 'Identifier') {
|
|
352
|
-
const parent = ancestors[ancestors.length - 1]
|
|
353
|
-
const grand = ancestors[ancestors.length - 2]
|
|
354
|
-
const isNonRef = parent !== undefined && (
|
|
355
|
-
(parent.type === 'MemberExpression' && !parent.computed && parent.property === ast)
|
|
356
|
-
|| (parent.type === 'Property' && !parent.computed && parent.key === ast && !parent.shorthand)
|
|
357
|
-
|| (parent.type === 'VariableDeclarator' && parent.id === ast)
|
|
358
|
-
|| (parent.type === 'LabeledStatement' && parent.label === ast)
|
|
359
|
-
|| (parent.type === 'FunctionDeclaration' && parent.id === ast)
|
|
360
|
-
|| (parent.type === 'FunctionExpression' && parent.id === ast)
|
|
361
|
-
)
|
|
362
|
-
|
|
363
|
-
if (!isNonRef && dataVar !== undefined && ast.name === dataVar) {
|
|
364
|
-
if (parent?.type === 'MemberExpression' && parent.object === ast) {
|
|
365
|
-
const k = memberKeyName(parent)
|
|
366
|
-
if (k === undefined)
|
|
367
|
-
escaped = true
|
|
368
|
-
else
|
|
369
|
-
keys.add(k)
|
|
370
|
-
}
|
|
371
|
-
else if (parent?.type === 'VariableDeclarator' && parent.init === ast) {
|
|
372
|
-
const nested = extractObjectPatternKeys(parent.id)
|
|
373
|
-
if (nested === undefined)
|
|
374
|
-
escaped = true
|
|
375
|
-
else
|
|
376
|
-
for (const k of nested) keys.add(k)
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
escaped = true
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
else if (!isNonRef && propsVar !== undefined && ast.name === propsVar) {
|
|
383
|
-
if (parent?.type === 'MemberExpression' && parent.object === ast) {
|
|
384
|
-
const propName = memberKeyName(parent)
|
|
385
|
-
if (propName === undefined) {
|
|
386
|
-
escaped = true
|
|
387
|
-
}
|
|
388
|
-
else if (propName === 'data') {
|
|
389
|
-
const holder = grand?.type === 'ChainExpression' ? ancestors[ancestors.length - 3] : grand
|
|
390
|
-
const target = grand?.type === 'ChainExpression' ? grand : parent
|
|
391
|
-
if (holder?.type === 'MemberExpression' && holder.object === target) {
|
|
392
|
-
const k = memberKeyName(holder)
|
|
393
|
-
if (k === undefined)
|
|
394
|
-
escaped = true
|
|
395
|
-
else
|
|
396
|
-
keys.add(k)
|
|
397
|
-
}
|
|
398
|
-
else if (holder?.type === 'VariableDeclarator' && holder.init === target) {
|
|
399
|
-
const nested = extractObjectPatternKeys(holder.id)
|
|
400
|
-
if (nested === undefined)
|
|
401
|
-
escaped = true
|
|
402
|
-
else
|
|
403
|
-
for (const k of nested) keys.add(k)
|
|
404
|
-
}
|
|
405
|
-
else {
|
|
406
|
-
escaped = true
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
else {
|
|
411
|
-
escaped = true
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
ancestors.push(ast)
|
|
417
|
-
const childKeys = visitorKeys[type]
|
|
418
|
-
if (childKeys !== undefined) {
|
|
419
|
-
const rec = ast as unknown as Record<string, unknown>
|
|
420
|
-
for (let i = 0; i < childKeys.length; i++) {
|
|
421
|
-
const val = rec[childKeys[i]!]
|
|
422
|
-
if (val !== null && typeof val === 'object')
|
|
423
|
-
walkWithParents(val)
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
ancestors.pop()
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
walkWithParents(fn.body)
|
|
430
|
-
return escaped ? undefined : [...keys].sort()
|
|
431
|
-
}
|
|
432
|
-
|
|
433
170
|
export function parseTsx(filename: string, code: string): Program {
|
|
434
171
|
const lang = /\.[cm]?ts$/i.test(filename) ? 'ts' : 'tsx'
|
|
435
172
|
const result = parseSync(filename, code, { lang, sourceType: 'module' })
|
|
@@ -498,325 +235,338 @@ function isActionCall(node: Node, actionLocalName: string | undefined, serverNam
|
|
|
498
235
|
return false
|
|
499
236
|
}
|
|
500
237
|
|
|
501
|
-
function isIslandCall(node: Node, islandLocalName: string | undefined, kayNamespaces: ReadonlySet<string>): node is CallExpression {
|
|
502
|
-
if (node.type !== 'CallExpression')
|
|
503
|
-
return false
|
|
504
|
-
if (islandLocalName !== undefined && node.callee.type === 'Identifier' && node.callee.name === islandLocalName)
|
|
505
|
-
return true
|
|
506
|
-
if (
|
|
507
|
-
node.callee.type === 'MemberExpression'
|
|
508
|
-
&& !node.callee.computed
|
|
509
|
-
&& node.callee.object.type === 'Identifier'
|
|
510
|
-
&& kayNamespaces.has(node.callee.object.name)
|
|
511
|
-
&& node.callee.property.type === 'Identifier'
|
|
512
|
-
&& node.callee.property.name === 'island'
|
|
513
|
-
) {
|
|
514
|
-
return true
|
|
515
|
-
}
|
|
516
|
-
return false
|
|
517
|
-
}
|
|
518
238
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
const hydrateModes = new Set<string>()
|
|
533
|
-
const kayNamespaces = new Set<string>()
|
|
534
|
-
const serverNamespaces = new Set<string>()
|
|
535
|
-
const stateCallees = new Set<string>()
|
|
536
|
-
const warnings: string[] = []
|
|
537
|
-
const signalCallees = new Set<string>()
|
|
538
|
-
const asyncLocals = new Set<string>()
|
|
539
|
-
const localInits = new Map<string, Node>()
|
|
540
|
-
const localExports = new Map<string, string>()
|
|
541
|
-
let defaultLocal: string | undefined
|
|
542
|
-
const exportedActions: string[] = []
|
|
543
|
-
const imports: ImportInfo[] = []
|
|
544
|
-
const reexports: ReexportInfo[] = []
|
|
545
|
-
const exportedNames: string[] = []
|
|
546
|
-
let actionLocalName: string | undefined
|
|
547
|
-
let islandLocalName: string | undefined
|
|
548
|
-
// The action() calls kay gives a URL to: the initialisers of top-level `export const`s.
|
|
549
|
-
const registeredActions = new Set<Node>()
|
|
550
|
-
|
|
551
|
-
const markInteractive = (reason: string, offset?: number) => {
|
|
552
|
-
if (!isInteractive) {
|
|
553
|
-
isInteractive = true
|
|
554
|
-
interactiveReason = reason
|
|
555
|
-
interactiveOffset = offset
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
const collectInits = (decl: VariableDeclaration): void => {
|
|
560
|
-
for (const d of decl.declarations) {
|
|
561
|
-
if (d.id.type === 'Identifier' && d.init !== null)
|
|
562
|
-
localInits.set(d.id.name, d.init)
|
|
563
|
-
}
|
|
564
|
-
}
|
|
239
|
+
/** What a module's imports declare, read before its body so import order never matters. */
|
|
240
|
+
interface ImportScan {
|
|
241
|
+
imports: ImportInfo[]
|
|
242
|
+
/** Kay's reactive primitives by local name, with the reason a use of one gives. */
|
|
243
|
+
reactiveLocals: Map<string, string>
|
|
244
|
+
stateCallees: Set<string>
|
|
245
|
+
signalCallees: Set<string>
|
|
246
|
+
serverOnlyLocals: Map<string, string>
|
|
247
|
+
kayNamespaces: Set<string>
|
|
248
|
+
serverNamespaces: Set<string>
|
|
249
|
+
actionLocalName: string | undefined
|
|
250
|
+
usesRenderContext: boolean
|
|
251
|
+
}
|
|
565
252
|
|
|
566
|
-
|
|
253
|
+
function scanImports(program: Program, filename: string): ImportScan {
|
|
254
|
+
const scan: ImportScan = { imports: [], reactiveLocals: new Map(), stateCallees: new Set(), signalCallees: new Set(), serverOnlyLocals: new Map(), kayNamespaces: new Set(), serverNamespaces: new Set(), actionLocalName: undefined, usesRenderContext: false }
|
|
567
255
|
for (const stmt of program.body) {
|
|
256
|
+
if (stmt.type === 'ExportAllDeclaration' || (stmt.type === 'ExportNamedDeclaration' && stmt.source !== null))
|
|
257
|
+
assertScopedKayImport(stmt.source!.value, filename)
|
|
568
258
|
if (stmt.type !== 'ImportDeclaration')
|
|
569
259
|
continue
|
|
260
|
+
assertScopedKayImport(stmt.source.value, filename)
|
|
570
261
|
// Entire `import type { ... } from '...'` declarations have zero runtime effect.
|
|
571
262
|
if (stmt.importKind === 'type')
|
|
572
263
|
continue
|
|
573
264
|
const source = stmt.source.value
|
|
574
265
|
const specifiers: { local: string, imported: string }[] = []
|
|
575
|
-
|
|
576
266
|
for (const spec of stmt.specifiers) {
|
|
577
267
|
if (spec.type === 'ImportSpecifier') {
|
|
578
|
-
|
|
579
|
-
continue
|
|
580
|
-
const imported = exportedName(spec.imported)
|
|
268
|
+
const imported = spec.importKind === 'type' ? undefined : exportedName(spec.imported)
|
|
581
269
|
if (imported === undefined)
|
|
582
270
|
continue
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
if (KAY_SOURCES.has(source) && REACTIVE_IMPORTS.has(imported))
|
|
586
|
-
markInteractive(`imports ${imported} from '${source}'`, spec.start)
|
|
587
|
-
if (KAY_SOURCES.has(source) && imported === 'island')
|
|
588
|
-
islandLocalName = local
|
|
589
|
-
if (KAY_SOURCES.has(source) && STATE_PRIMITIVES.has(imported))
|
|
590
|
-
stateCallees.add(local)
|
|
591
|
-
if (KAY_SOURCES.has(source) && (imported === 'signal' || imported === 'computed'))
|
|
592
|
-
signalCallees.add(local)
|
|
593
|
-
if (KAY_SSR_SOURCES.has(source) && RENDER_CONTEXT_IMPORTS.has(imported))
|
|
594
|
-
usesRenderContext = true
|
|
595
|
-
if (KAY_SERVER_SOURCES.has(source) && imported === 'action')
|
|
596
|
-
actionLocalName = local
|
|
271
|
+
specifiers.push({ local: spec.local.name, imported })
|
|
272
|
+
scanKayImport(scan, source, imported, spec.local.name, filename)
|
|
597
273
|
}
|
|
598
274
|
else if (spec.type === 'ImportDefaultSpecifier') {
|
|
599
275
|
specifiers.push({ local: spec.local.name, imported: 'default' })
|
|
600
276
|
}
|
|
601
|
-
else
|
|
277
|
+
else {
|
|
602
278
|
specifiers.push({ local: spec.local.name, imported: '*' })
|
|
603
279
|
if (KAY_SOURCES.has(source))
|
|
604
|
-
kayNamespaces.add(spec.local.name)
|
|
280
|
+
scan.kayNamespaces.add(spec.local.name)
|
|
605
281
|
if (KAY_SERVER_SOURCES.has(source))
|
|
606
|
-
serverNamespaces.add(spec.local.name)
|
|
282
|
+
scan.serverNamespaces.add(spec.local.name)
|
|
607
283
|
}
|
|
608
284
|
}
|
|
285
|
+
scan.imports.push({ source, specifiers, start: stmt.start, end: stmt.end })
|
|
286
|
+
}
|
|
287
|
+
return scan
|
|
288
|
+
}
|
|
609
289
|
|
|
610
|
-
|
|
290
|
+
function scanKayImport(scan: ImportScan, source: string, imported: string, local: string, filename: string): void {
|
|
291
|
+
if (KAY_SOURCES.has(source)) {
|
|
292
|
+
if (REACTIVE_IMPORTS.has(imported))
|
|
293
|
+
scan.reactiveLocals.set(local, `uses ${imported}`)
|
|
294
|
+
if (imported === 'island')
|
|
295
|
+
throw islandRemoved(filename)
|
|
296
|
+
if (STATE_PRIMITIVES.has(imported))
|
|
297
|
+
scan.stateCallees.add(local)
|
|
298
|
+
if (imported === 'signal' || imported === 'computed')
|
|
299
|
+
scan.signalCallees.add(local)
|
|
300
|
+
if (SERVER_ONLY_TAGS.has(imported))
|
|
301
|
+
scan.serverOnlyLocals.set(local, imported)
|
|
611
302
|
}
|
|
303
|
+
if (KAY_SSR_SOURCES.has(source) && RENDER_CONTEXT_IMPORTS.has(imported))
|
|
304
|
+
scan.usesRenderContext = true
|
|
305
|
+
if (KAY_SERVER_SOURCES.has(source) && imported === 'action')
|
|
306
|
+
scan.actionLocalName = local
|
|
307
|
+
}
|
|
612
308
|
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
309
|
+
/** A module's top-level declarations and exports. */
|
|
310
|
+
interface DeclarationScan {
|
|
311
|
+
localInits: Map<string, Node>
|
|
312
|
+
/** Each export by name, with the top-level binding it exports (`default` for an anonymous default export). */
|
|
313
|
+
exportLocals: Map<string, string>
|
|
314
|
+
asyncLocals: Set<string>
|
|
315
|
+
defaultLocal: string | undefined
|
|
316
|
+
hasAsyncDefault: boolean
|
|
317
|
+
hasLoadExport: boolean
|
|
318
|
+
hasPaths: boolean
|
|
319
|
+
hasPrerender: boolean
|
|
320
|
+
defaultHydrate: string | undefined
|
|
321
|
+
exportedActions: string[]
|
|
322
|
+
/** The action() calls kay gives a URL to: the initialisers of top-level `export const`s. */
|
|
323
|
+
registeredActions: Set<Node>
|
|
324
|
+
reexports: ReexportInfo[]
|
|
325
|
+
exportedNames: string[]
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function scanDeclarations(program: Program, filename: string, imports: ImportScan): DeclarationScan {
|
|
329
|
+
const scan: DeclarationScan = { localInits: new Map(), exportLocals: new Map(), asyncLocals: new Set(), defaultLocal: undefined, hasAsyncDefault: false, hasLoadExport: false, hasPaths: false, hasPrerender: false, defaultHydrate: undefined, exportedActions: [], registeredActions: new Set(), reexports: [], exportedNames: [] }
|
|
330
|
+
const collectInits = (decl: VariableDeclaration): void => {
|
|
331
|
+
for (const d of decl.declarations) {
|
|
332
|
+
if (d.id.type === 'Identifier' && d.init !== null)
|
|
333
|
+
scan.localInits.set(d.id.name, d.init)
|
|
630
334
|
}
|
|
631
335
|
}
|
|
632
|
-
|
|
633
|
-
|
|
336
|
+
const addExport = (exported: string, local: string | undefined): void => {
|
|
337
|
+
scan.exportedNames.push(exported)
|
|
338
|
+
if (local !== undefined)
|
|
339
|
+
scan.exportLocals.set(exported, local)
|
|
340
|
+
if (exported === 'load')
|
|
341
|
+
scan.hasLoadExport = true
|
|
342
|
+
if (exported === 'paths')
|
|
343
|
+
scan.hasPaths = true
|
|
344
|
+
}
|
|
634
345
|
for (const stmt of program.body) {
|
|
635
346
|
if (stmt.type === 'VariableDeclaration') {
|
|
636
347
|
collectInits(stmt)
|
|
637
348
|
}
|
|
638
349
|
else if (stmt.type === 'FunctionDeclaration') {
|
|
639
350
|
if (stmt.async === true && stmt.id !== null)
|
|
640
|
-
asyncLocals.add(stmt.id.name)
|
|
351
|
+
scan.asyncLocals.add(stmt.id.name)
|
|
641
352
|
}
|
|
642
353
|
else if (stmt.type === 'ExportAllDeclaration') {
|
|
643
354
|
if (stmt.exportKind !== 'type')
|
|
644
|
-
reexports.push({ exported: '*', local: '*', source: stmt.source.value, start: stmt.start })
|
|
355
|
+
scan.reexports.push({ exported: '*', local: '*', source: stmt.source.value, start: stmt.start })
|
|
645
356
|
}
|
|
646
357
|
else if (stmt.type === 'ExportDefaultDeclaration') {
|
|
647
|
-
exportedNames.push('default')
|
|
648
358
|
const decl = unwrapTsExpr(stmt.declaration)
|
|
359
|
+
const named = (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') ? decl.id?.name : undefined
|
|
360
|
+
addExport('default', decl.type === 'Identifier' ? decl.name : named ?? 'default')
|
|
649
361
|
if (isAsyncFunction(decl))
|
|
650
|
-
hasAsyncDefault = true
|
|
362
|
+
scan.hasAsyncDefault = true
|
|
651
363
|
else if (decl.type === 'Identifier')
|
|
652
|
-
defaultLocal = decl.name
|
|
364
|
+
scan.defaultLocal = decl.name
|
|
653
365
|
else
|
|
654
|
-
localInits.set('default', decl)
|
|
655
|
-
|
|
656
|
-
if (isIslandCall(decl, islandLocalName, kayNamespaces)) {
|
|
657
|
-
readIslandHydrate(decl)
|
|
658
|
-
const target = decl.arguments[0]
|
|
659
|
-
if (target !== undefined && isAsyncFunction(target))
|
|
660
|
-
hasAsyncDefault = true
|
|
661
|
-
}
|
|
366
|
+
scan.localInits.set('default', decl)
|
|
662
367
|
}
|
|
663
|
-
else if (stmt.type === 'ExportNamedDeclaration') {
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
exportedNames.push(exported)
|
|
675
|
-
reexports.push({ exported, local, source: src, start: stmt.start })
|
|
676
|
-
if (exported === 'load')
|
|
677
|
-
hasLoadExport = true
|
|
678
|
-
if (exported === 'paths')
|
|
679
|
-
hasPaths = true
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
else {
|
|
684
|
-
for (const spec of stmt.specifiers) {
|
|
685
|
-
if (spec.exportKind === 'type')
|
|
686
|
-
continue
|
|
687
|
-
const exported = exportedName(spec.exported)
|
|
688
|
-
const local = exportedName(spec.local)
|
|
689
|
-
if (exported !== undefined && local !== undefined) {
|
|
690
|
-
exportedNames.push(exported)
|
|
691
|
-
localExports.set(exported, local)
|
|
692
|
-
if (exported === 'default')
|
|
693
|
-
defaultLocal = local
|
|
694
|
-
if (exported === 'load')
|
|
695
|
-
hasLoadExport = true
|
|
696
|
-
if (exported === 'paths')
|
|
697
|
-
hasPaths = true
|
|
698
|
-
}
|
|
699
|
-
}
|
|
368
|
+
else if (stmt.type === 'ExportNamedDeclaration' && stmt.exportKind !== 'type') {
|
|
369
|
+
for (const spec of stmt.specifiers) {
|
|
370
|
+
const exported = spec.exportKind === 'type' ? undefined : exportedName(spec.exported)
|
|
371
|
+
const local = exportedName(spec.local)
|
|
372
|
+
if (exported === undefined || local === undefined)
|
|
373
|
+
continue
|
|
374
|
+
addExport(exported, stmt.source === null ? local : undefined)
|
|
375
|
+
if (stmt.source !== null)
|
|
376
|
+
scan.reexports.push({ exported, local, source: stmt.source.value, start: stmt.start })
|
|
377
|
+
else if (exported === 'default')
|
|
378
|
+
scan.defaultLocal = local
|
|
700
379
|
}
|
|
701
|
-
|
|
702
|
-
|
|
380
|
+
if (stmt.declaration !== null)
|
|
381
|
+
scanExportedDeclaration(scan, stmt.declaration, filename, imports, collectInits, addExport)
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return scan
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function scanExportedDeclaration(scan: DeclarationScan, decl: NonNullable<ExportNamedDeclaration['declaration']>, filename: string, imports: ImportScan, collectInits: (decl: VariableDeclaration) => void, addExport: (exported: string, local: string) => void): void {
|
|
388
|
+
if (decl.type === 'FunctionDeclaration' && decl.id !== null) {
|
|
389
|
+
if (decl.async === true)
|
|
390
|
+
scan.asyncLocals.add(decl.id.name)
|
|
391
|
+
addExport(decl.id.name, decl.id.name)
|
|
392
|
+
}
|
|
393
|
+
else if (decl.type === 'ClassDeclaration' && decl.id !== null) {
|
|
394
|
+
addExport(decl.id.name, decl.id.name)
|
|
395
|
+
}
|
|
396
|
+
else if (decl.type === 'VariableDeclaration') {
|
|
397
|
+
collectInits(decl)
|
|
398
|
+
for (const d of decl.declarations) {
|
|
399
|
+
if (d.id.type !== 'Identifier')
|
|
703
400
|
continue
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
if (name === 'paths')
|
|
712
|
-
hasPaths = true
|
|
713
|
-
}
|
|
714
|
-
else if (decl.type === 'VariableDeclaration') {
|
|
715
|
-
collectInits(decl)
|
|
716
|
-
for (const d of decl.declarations) {
|
|
717
|
-
if (d.id.type !== 'Identifier')
|
|
718
|
-
continue
|
|
719
|
-
const name = d.id.name
|
|
720
|
-
exportedNames.push(name)
|
|
721
|
-
if (name === 'load')
|
|
722
|
-
hasLoadExport = true
|
|
723
|
-
if (name === 'paths')
|
|
724
|
-
hasPaths = true
|
|
725
|
-
if (name === 'prerender' && d.init?.type === 'Literal' && d.init.value === true)
|
|
726
|
-
hasPrerender = true
|
|
727
|
-
if (name === 'hydrate' && d.init?.type === 'Literal' && typeof d.init.value === 'string') {
|
|
728
|
-
checkHydrateMode(d.init.value, `hydrate = "${d.init.value}"`, filename)
|
|
729
|
-
defaultHydrate = d.init.value
|
|
730
|
-
}
|
|
731
|
-
if (d.init !== null && isIslandCall(d.init, islandLocalName, kayNamespaces) && d.init.arguments.length > 1)
|
|
732
|
-
throw mistake(`island() options on export ${name}`, filename, 'only the default export\'s island() options apply; export the island as default, or set $hydrate where it renders')
|
|
733
|
-
if (d.init !== null && isActionCall(d.init, actionLocalName, serverNamespaces)) {
|
|
734
|
-
exportedActions.push(name)
|
|
735
|
-
registeredActions.add(d.init)
|
|
736
|
-
}
|
|
737
|
-
}
|
|
401
|
+
const name = d.id.name
|
|
402
|
+
addExport(name, name)
|
|
403
|
+
if (name === 'prerender' && d.init?.type === 'Literal' && d.init.value === true)
|
|
404
|
+
scan.hasPrerender = true
|
|
405
|
+
if (name === 'hydrate' && d.init?.type === 'Literal' && typeof d.init.value === 'string') {
|
|
406
|
+
checkHydrateMode(d.init.value, `hydrate = "${d.init.value}"`, filename)
|
|
407
|
+
scan.defaultHydrate = d.init.value
|
|
738
408
|
}
|
|
739
|
-
|
|
740
|
-
|
|
409
|
+
if (d.init !== null && isActionCall(d.init, imports.actionLocalName, imports.serverNamespaces)) {
|
|
410
|
+
scan.exportedActions.push(name)
|
|
411
|
+
scan.registeredActions.add(d.init)
|
|
741
412
|
}
|
|
742
413
|
}
|
|
743
414
|
}
|
|
415
|
+
}
|
|
744
416
|
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
417
|
+
// A module's body as top-level bindings, each with the node holding its code; module-level statements go under ''.
|
|
418
|
+
function bindingUnits(program: Program): { name: string, node: Node }[] {
|
|
419
|
+
const units: { name: string, node: Node }[] = []
|
|
420
|
+
const add = (node: Node): void => {
|
|
421
|
+
if ((node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') && node.id !== null) {
|
|
422
|
+
units.push({ name: node.id.name, node })
|
|
423
|
+
}
|
|
424
|
+
else if (node.type === 'VariableDeclaration') {
|
|
425
|
+
for (const d of node.declarations)
|
|
426
|
+
units.push({ name: d.id.type === 'Identifier' ? d.id.name : '', node: d })
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
units.push({ name: '', node })
|
|
753
430
|
}
|
|
754
431
|
}
|
|
432
|
+
for (const stmt of program.body) {
|
|
433
|
+
if (stmt.type === 'ExportNamedDeclaration') {
|
|
434
|
+
if (stmt.declaration !== null)
|
|
435
|
+
add(stmt.declaration)
|
|
436
|
+
}
|
|
437
|
+
else if (stmt.type === 'ExportDefaultDeclaration') {
|
|
438
|
+
const decl = stmt.declaration
|
|
439
|
+
if ((decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') && decl.id !== null)
|
|
440
|
+
add(decl)
|
|
441
|
+
else if (decl.type !== 'Identifier')
|
|
442
|
+
units.push({ name: 'default', node: decl })
|
|
443
|
+
}
|
|
444
|
+
else if (stmt.type !== 'ImportDeclaration') {
|
|
445
|
+
add(stmt)
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return units
|
|
449
|
+
}
|
|
755
450
|
|
|
756
|
-
|
|
757
|
-
|
|
451
|
+
/** What the walk over a module's body finds. */
|
|
452
|
+
interface BodyScan {
|
|
453
|
+
hasJsx: boolean
|
|
454
|
+
usesRenderContext: boolean
|
|
455
|
+
deferredIslandTags: Set<string>
|
|
456
|
+
hydrateModes: Set<string>
|
|
457
|
+
jsxTags: Set<string>
|
|
458
|
+
bindings: BindingFacts[]
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// Walks each top-level binding for JSX, mistakes, action() placement and what makes it run in the browser.
|
|
462
|
+
function scanBody(program: Program, filename: string, code: string, imports: ImportScan, decls: DeclarationScan, warnings: string[]): BodyScan {
|
|
463
|
+
const body: BodyScan = { hasJsx: false, usesRenderContext: imports.usesRenderContext, deferredIslandTags: new Set(), hydrateModes: new Set(), jsxTags: new Set(), bindings: [] }
|
|
464
|
+
const units = bindingUnits(program)
|
|
465
|
+
const tracked = new Set(units.map(u => u.name))
|
|
466
|
+
for (const imp of imports.imports) {
|
|
467
|
+
for (const spec of imp.specifiers)
|
|
468
|
+
tracked.add(spec.local)
|
|
469
|
+
}
|
|
470
|
+
// Identifiers naming a property or a member, which are no reference to a binding.
|
|
471
|
+
const keys = new Set<number>()
|
|
472
|
+
const checkActions = imports.actionLocalName !== undefined || imports.serverNamespaces.size > 0
|
|
473
|
+
let current: BindingFacts | undefined
|
|
474
|
+
const ref = (binding: BindingFacts, name: string, offset: number): void => {
|
|
475
|
+
if (tracked.has(name) && !binding.refs.has(name))
|
|
476
|
+
binding.refs.set(name, offset)
|
|
477
|
+
}
|
|
478
|
+
const visit = (node: Node): void => {
|
|
479
|
+
const binding = current!
|
|
758
480
|
if (isJsxNode(node))
|
|
759
|
-
hasJsx = true
|
|
481
|
+
body.hasJsx = true
|
|
760
482
|
if (node.type === 'JSXElement')
|
|
761
483
|
checkMistakes(node, filename, code, warnings)
|
|
762
|
-
if (
|
|
763
|
-
throw mistake('action() not declared as export const', filename, `write export const ${actionVariable(program, node) ?? 'name'} = ${actionLocalName ?? 'action'}(...) at the top level, only those get a URL`)
|
|
484
|
+
if (checkActions && isActionCall(node, imports.actionLocalName, imports.serverNamespaces) && !decls.registeredActions.has(node))
|
|
485
|
+
throw mistake('action() not declared as export const', filename, `write export const ${actionVariable(program, node) ?? 'name'} = ${imports.actionLocalName ?? 'action'}(...) at the top level, only those get a URL`)
|
|
764
486
|
if (node.type === 'JSXOpeningElement') {
|
|
765
|
-
|
|
766
|
-
const
|
|
767
|
-
if (
|
|
768
|
-
|
|
769
|
-
if (tag.includes('.')) {
|
|
770
|
-
const [ns, member] = tag.split('.')
|
|
771
|
-
if (ns !== undefined && kayNamespaces.has(ns) && member !== undefined && RENDER_CONTEXT_IMPORTS.has(member))
|
|
772
|
-
usesRenderContext = true
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
for (const attr of node.attributes) {
|
|
776
|
-
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier')
|
|
777
|
-
continue
|
|
778
|
-
const attrName = attr.name.name
|
|
779
|
-
if (isComponent && attrName === '$hydrate') {
|
|
780
|
-
const mode = hydrateMode(attr.value)
|
|
781
|
-
checkHydrateMode(mode, `$hydrate="${mode}"`, filename)
|
|
782
|
-
hydrateModes.add(mode)
|
|
783
|
-
if (mode !== 'load')
|
|
784
|
-
deferredIslandTags.add(tag)
|
|
785
|
-
}
|
|
786
|
-
// A handler or ref on a component tag is a prop for that component; only a DOM element binds it in the browser.
|
|
787
|
-
if (!isComponent && tag !== '' && (EVENT_ATTR.test(attrName) || attrName === 'ref'))
|
|
788
|
-
markInteractive(`uses ${attrName}`, attr.start)
|
|
789
|
-
}
|
|
487
|
+
scanOpeningElement(node, binding, body, imports, filename)
|
|
488
|
+
const root = jsxTagName(node.name).split('.')[0]!
|
|
489
|
+
if (isComponentTag(root))
|
|
490
|
+
ref(binding, root, node.start)
|
|
790
491
|
}
|
|
791
|
-
if (node.type === 'MemberExpression'
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
if (STATE_PRIMITIVES.has(member))
|
|
796
|
-
stateCallees.add(`${node.object.name}.${member}`)
|
|
797
|
-
if (member === 'signal' || member === 'computed')
|
|
798
|
-
signalCallees.add(`${node.object.name}.${member}`)
|
|
799
|
-
if (RENDER_CONTEXT_IMPORTS.has(member))
|
|
800
|
-
usesRenderContext = true
|
|
492
|
+
else if (node.type === 'MemberExpression') {
|
|
493
|
+
if (!node.computed)
|
|
494
|
+
keys.add(node.property.start)
|
|
495
|
+
scanKayMember(node, binding, body, imports, filename)
|
|
801
496
|
}
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
497
|
+
else if (node.type === 'Property' && !node.computed && !node.shorthand) {
|
|
498
|
+
keys.add(node.key.start)
|
|
499
|
+
}
|
|
500
|
+
else if (node.type === 'Identifier' && !keys.has(node.start)) {
|
|
501
|
+
ref(binding, node.name, node.start)
|
|
502
|
+
const reason = imports.reactiveLocals.get(node.name)
|
|
503
|
+
if (reason !== undefined)
|
|
504
|
+
binding.fact ??= { reason, offset: node.start }
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
for (const unit of units) {
|
|
508
|
+
current = { name: unit.name, start: unit.node.start, end: unit.node.end, fact: undefined, refs: new Map(), tags: new Set(), serverOnlyTag: undefined }
|
|
509
|
+
body.bindings.push(current)
|
|
510
|
+
walk(unit.node, visit)
|
|
511
|
+
}
|
|
512
|
+
return body
|
|
513
|
+
}
|
|
805
514
|
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
515
|
+
function scanOpeningElement(node: Extract<Node, { type: 'JSXOpeningElement' }>, binding: BindingFacts, body: BodyScan, imports: ImportScan, filename: string): void {
|
|
516
|
+
const tag = jsxTagName(node.name)
|
|
517
|
+
const isComponent = isComponentTag(tag)
|
|
518
|
+
if (isComponent) {
|
|
519
|
+
body.jsxTags.add(tag)
|
|
520
|
+
binding.tags.add(tag)
|
|
521
|
+
binding.serverOnlyTag ??= imports.serverOnlyLocals.get(tag)
|
|
522
|
+
const [ns, member] = tag.split('.')
|
|
523
|
+
if (member !== undefined && imports.kayNamespaces.has(ns!) && RENDER_CONTEXT_IMPORTS.has(member)) {
|
|
524
|
+
body.usesRenderContext = true
|
|
525
|
+
if (SERVER_ONLY_TAGS.has(member))
|
|
526
|
+
binding.serverOnlyTag ??= member
|
|
527
|
+
}
|
|
809
528
|
}
|
|
810
|
-
|
|
811
|
-
|
|
529
|
+
for (const attr of node.attributes) {
|
|
530
|
+
if (attr.type !== 'JSXAttribute' || attr.name.type !== 'JSXIdentifier')
|
|
531
|
+
continue
|
|
532
|
+
const attrName = attr.name.name
|
|
533
|
+
if (isComponent && attrName === '$hydrate') {
|
|
534
|
+
const mode = hydrateMode(attr.value)
|
|
535
|
+
checkHydrateMode(mode, `$hydrate="${mode}"`, filename)
|
|
536
|
+
body.hydrateModes.add(mode)
|
|
537
|
+
if (mode !== 'load')
|
|
538
|
+
body.deferredIslandTags.add(tag)
|
|
539
|
+
}
|
|
540
|
+
// A handler or ref on a component tag is a prop for that component; only a DOM element binds it in the browser.
|
|
541
|
+
if (!isComponent && tag !== '' && (EVENT_ATTR.test(attrName) || attrName === 'ref'))
|
|
542
|
+
binding.fact ??= { reason: `uses ${attrName}`, offset: attr.start }
|
|
543
|
+
}
|
|
544
|
+
}
|
|
812
545
|
|
|
546
|
+
// `kay.signal(...)`, `kay.Head` and the like, through a namespace import of kay.
|
|
547
|
+
function scanKayMember(node: Extract<Node, { type: 'MemberExpression' }>, binding: BindingFacts, body: BodyScan, imports: ImportScan, filename: string): void {
|
|
548
|
+
if (node.computed || node.object.type !== 'Identifier' || !imports.kayNamespaces.has(node.object.name) || node.property.type !== 'Identifier')
|
|
549
|
+
return
|
|
550
|
+
const member = node.property.name
|
|
551
|
+
const callee = `${node.object.name}.${member}`
|
|
552
|
+
if (member === 'island')
|
|
553
|
+
throw islandRemoved(filename)
|
|
554
|
+
if (REACTIVE_IMPORTS.has(member))
|
|
555
|
+
binding.fact ??= { reason: `calls ${callee}`, offset: node.start }
|
|
556
|
+
if (STATE_PRIMITIVES.has(member))
|
|
557
|
+
imports.stateCallees.add(callee)
|
|
558
|
+
if (member === 'signal' || member === 'computed')
|
|
559
|
+
imports.signalCallees.add(callee)
|
|
560
|
+
if (RENDER_CONTEXT_IMPORTS.has(member))
|
|
561
|
+
body.usesRenderContext = true
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function stateExportsOf(decls: DeclarationScan, stateCallees: ReadonlySet<string>): string[] {
|
|
813
565
|
const isStateCall = (init: Node | undefined): boolean => {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
const unwrapped = unwrapTsExpr(init)
|
|
817
|
-
if (unwrapped.type !== 'CallExpression')
|
|
566
|
+
const call = init === undefined ? undefined : unwrapTsExpr(init)
|
|
567
|
+
if (call?.type !== 'CallExpression')
|
|
818
568
|
return false
|
|
819
|
-
const callee =
|
|
569
|
+
const callee = call.callee
|
|
820
570
|
const name = callee.type === 'Identifier'
|
|
821
571
|
? callee.name
|
|
822
572
|
: callee.type === 'MemberExpression' && !callee.computed && callee.object.type === 'Identifier' && callee.property.type === 'Identifier'
|
|
@@ -824,48 +574,52 @@ export function analyzeModule(filename: string, code: string, program: Program =
|
|
|
824
574
|
: ''
|
|
825
575
|
return stateCallees.has(name)
|
|
826
576
|
}
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
if (isStateCall(localInits.get(name)))
|
|
830
|
-
stateExportSet.add(name)
|
|
831
|
-
}
|
|
832
|
-
for (const [exported, local] of localExports) {
|
|
833
|
-
if (isStateCall(localInits.get(local)))
|
|
834
|
-
stateExportSet.add(exported)
|
|
835
|
-
}
|
|
836
|
-
const stateExports = [...stateExportSet]
|
|
577
|
+
return [...decls.exportLocals].filter(([, local]) => isStateCall(decls.localInits.get(local))).map(([exported]) => exported)
|
|
578
|
+
}
|
|
837
579
|
|
|
838
|
-
|
|
839
|
-
|
|
580
|
+
export function analyzeModule(filename: string, code: string, program: Program = parseTsx(filename, code)): ModuleAnalysis {
|
|
581
|
+
const imports = scanImports(program, filename)
|
|
582
|
+
const decls = scanDeclarations(program, filename, imports)
|
|
583
|
+
const warnings: string[] = []
|
|
584
|
+
const body = scanBody(program, filename, code, imports, decls, warnings)
|
|
585
|
+
checkBareSignals(program, imports.signalCallees, filename)
|
|
840
586
|
|
|
841
|
-
const
|
|
842
|
-
|
|
843
|
-
|
|
587
|
+
for (const [name, init] of decls.localInits) {
|
|
588
|
+
if (isAsyncFunction(init))
|
|
589
|
+
decls.asyncLocals.add(name)
|
|
590
|
+
}
|
|
591
|
+
const hasAsyncDefault = decls.hasAsyncDefault || (decls.defaultLocal !== undefined && decls.asyncLocals.has(decls.defaultLocal))
|
|
592
|
+
const interactivity = resolveInteractivity(body.bindings, decls.exportLocals)
|
|
593
|
+
const page = interactivity.exports.get('default')
|
|
594
|
+
if (hasAsyncDefault && page?.interactive === true)
|
|
595
|
+
throw asyncInteractiveError(filename, page.reason)
|
|
596
|
+
if (interactivity.serverOnly !== undefined)
|
|
597
|
+
throw serverOnlyTagError(filename, interactivity.serverOnly)
|
|
844
598
|
|
|
845
599
|
return {
|
|
846
600
|
filename,
|
|
847
601
|
program,
|
|
848
602
|
warnings,
|
|
849
|
-
|
|
850
|
-
interactiveReason,
|
|
851
|
-
interactiveOffset,
|
|
603
|
+
...interactivity,
|
|
852
604
|
hasAsyncDefault,
|
|
853
|
-
hasLoadExport: hasLoadExport || loadDeclarations(program).length > 0,
|
|
854
|
-
hasJsx,
|
|
855
|
-
jsxNodes: hasJsx ? computeJsxNodes(program) : new WeakSet<Node>(),
|
|
856
|
-
hasPrerender,
|
|
857
|
-
hasPaths,
|
|
858
|
-
usesRenderContext,
|
|
859
|
-
defaultHydrate,
|
|
860
|
-
deferredIslandTags,
|
|
861
|
-
hydrateModes,
|
|
862
|
-
jsxTags,
|
|
863
|
-
stateExports,
|
|
864
|
-
defaultDataKeys,
|
|
865
|
-
exportedActions,
|
|
866
|
-
imports,
|
|
867
|
-
reexports,
|
|
868
|
-
exportedNames,
|
|
605
|
+
hasLoadExport: decls.hasLoadExport || loadDeclarations(program).length > 0,
|
|
606
|
+
hasJsx: body.hasJsx,
|
|
607
|
+
jsxNodes: body.hasJsx ? computeJsxNodes(program) : new WeakSet<Node>(),
|
|
608
|
+
hasPrerender: decls.hasPrerender,
|
|
609
|
+
hasPaths: decls.hasPaths,
|
|
610
|
+
usesRenderContext: body.usesRenderContext,
|
|
611
|
+
defaultHydrate: decls.defaultHydrate,
|
|
612
|
+
deferredIslandTags: body.deferredIslandTags,
|
|
613
|
+
hydrateModes: body.hydrateModes,
|
|
614
|
+
jsxTags: body.jsxTags,
|
|
615
|
+
stateExports: stateExportsOf(decls, imports.stateCallees),
|
|
616
|
+
defaultDataKeys: page?.interactive === true ? analyzeDefaultDataKeys(program, decls.defaultLocal, decls.localInits) : undefined,
|
|
617
|
+
exportedActions: decls.exportedActions,
|
|
618
|
+
imports: imports.imports,
|
|
619
|
+
reexports: decls.reexports,
|
|
620
|
+
exportedNames: decls.exportedNames,
|
|
621
|
+
bindings: body.bindings,
|
|
622
|
+
exportLocals: decls.exportLocals,
|
|
869
623
|
}
|
|
870
624
|
}
|
|
871
625
|
|