@kayjs/kay 2.0.0-alpha.8 → 2.0.0-beta.1
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/attrs.ts +35 -0
- package/src/build/bundle.ts +442 -0
- package/src/build/compile.ts +20 -11
- package/src/build/files.ts +136 -0
- package/src/build/graph.ts +158 -0
- package/src/build/index.ts +231 -1296
- package/src/build/islands.ts +177 -0
- package/src/build/manifest.ts +224 -0
- package/src/build/plugin-host.ts +184 -0
- package/src/build/prerender.ts +93 -0
- package/src/build/report.ts +157 -0
- package/src/build/resolve.ts +58 -0
- package/src/build/routes.ts +135 -0
- package/src/build/static.ts +73 -0
- package/src/cli/check.ts +228 -114
- package/src/cli/index.ts +298 -79
- package/src/cli/new.ts +67 -35
- package/src/compiler/analyze.ts +666 -91
- package/src/compiler/dom.ts +822 -629
- package/src/compiler/mistakes.ts +354 -0
- package/src/compiler/patch.ts +1 -1
- package/src/compiler/server-only.ts +164 -21
- package/src/compiler/shared.ts +442 -0
- package/src/compiler/ssr.ts +390 -324
- package/src/context.ts +120 -0
- package/src/dev/index.ts +291 -76
- package/src/dom/action.ts +23 -13
- package/src/dom/extras.ts +157 -84
- package/src/dom/index.ts +154 -33
- package/src/dom/island.ts +117 -39
- package/src/dom/loop.ts +34 -21
- package/src/index.ts +2 -1
- package/src/jsx-runtime.ts +11 -0
- package/src/plugins/context.ts +57 -0
- package/src/plugins/index.ts +120 -17
- package/src/reactivity/index.ts +250 -157
- package/src/reactivity/selector.ts +11 -9
- package/src/reactivity/store.ts +37 -12
- package/src/server/action.ts +217 -74
- package/src/server/compress.ts +155 -56
- package/src/server/cookies.ts +59 -22
- package/src/server/env.ts +18 -13
- package/src/server/http.ts +201 -33
- package/src/server/index.ts +964 -737
- package/src/server/loader.ts +120 -0
- package/src/server/router.ts +27 -8
- package/src/server/security.ts +87 -39
- package/src/server/standard-schema.ts +42 -11
- package/src/server/static-dir.ts +144 -0
- package/src/ssr/context.ts +96 -10
- package/src/ssr/index.ts +225 -56
- package/src/style.ts +33 -0
- package/src/test/index.ts +5 -0
- package/src/unrenderable.ts +35 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
// Shared compiler helpers & JSX child/hydration IR classification used by both `ssr.ts` and `dom.ts`.
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ArrowFunctionExpression,
|
|
5
|
+
BlockStatement,
|
|
6
|
+
CallExpression,
|
|
7
|
+
ConditionalExpression,
|
|
8
|
+
Expression,
|
|
9
|
+
Function as OxcFunction,
|
|
10
|
+
JSXChild,
|
|
11
|
+
JSXElement,
|
|
12
|
+
JSXElementName,
|
|
13
|
+
JSXExpressionContainer,
|
|
14
|
+
JSXFragment,
|
|
15
|
+
JSXNamespacedName,
|
|
16
|
+
LogicalExpression,
|
|
17
|
+
Node,
|
|
18
|
+
ParamPattern,
|
|
19
|
+
Program,
|
|
20
|
+
} from 'oxc-parser'
|
|
21
|
+
import { walk } from './analyze'
|
|
22
|
+
|
|
23
|
+
export { DOM_PROPS, isBlockedAttr, isStringBoolAttr, isUnsafeUrlAttr } from '../attrs'
|
|
24
|
+
|
|
25
|
+
export const VOID_ELEMENTS = new Set([
|
|
26
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
27
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
28
|
+
])
|
|
29
|
+
|
|
30
|
+
export const RAW_TEXT_ELEMENTS = new Set(['style', 'script'])
|
|
31
|
+
export const EVENT_ATTR = /^on([A-Z]\w*)$/
|
|
32
|
+
|
|
33
|
+
const CLOSE_STYLE_RE = /<\/(style)/gi
|
|
34
|
+
const SCRIPT_UNSAFE_RE = /<(\/script|script|!--)/gi
|
|
35
|
+
const LINE_BREAK_RE = /\r\n|\n|\r/
|
|
36
|
+
|
|
37
|
+
export function escapeRawText(str: string, tag: 'style' | 'script'): string {
|
|
38
|
+
if (tag === 'script') {
|
|
39
|
+
return str.replace(SCRIPT_UNSAFE_RE, (_, body: string) =>
|
|
40
|
+
body.charCodeAt(0) === 47 ? `<\\${body}` : `\\u003c${body}`)
|
|
41
|
+
}
|
|
42
|
+
return str.replace(CLOSE_STYLE_RE, '<\\/$1')
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function escapeStaticText(str: string): string {
|
|
46
|
+
return str.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function escapeStaticAttr(str: string): string {
|
|
50
|
+
return escapeStaticText(str).replaceAll('"', '"')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const HTML_ENTITIES: Record<string, string> = {
|
|
54
|
+
// Core XML/HTML & whitespace
|
|
55
|
+
amp: '&', lt: '<', gt: '>', quot: '"', apos: '\'', nbsp: '\u00A0',
|
|
56
|
+
ensp: '\u2002', emsp: '\u2003', thinsp: '\u2009', zwnj: '\u200C', zwj: '\u200D', lrm: '\u200E', rlm: '\u200F', shy: '\u00AD',
|
|
57
|
+
// Punctuation & symbols
|
|
58
|
+
ndash: '\u2013', mdash: '\u2014', lsquo: '\u2018', rsquo: '\u2019', sbquo: '\u201A',
|
|
59
|
+
ldquo: '\u201C', rdquo: '\u201D', bdquo: '\u201E', hellip: '\u2026', bull: '\u2022',
|
|
60
|
+
middot: '\u00B7', laquo: '\u00AB', raquo: '\u00BB', lsaquo: '\u2039', rsaquo: '\u203A',
|
|
61
|
+
prime: '\u2032', Prime: '\u2033', sect: '\u00A7', para: '\u00B6', dagger: '\u2020', Dagger: '\u2021', permil: '\u2030',
|
|
62
|
+
iexcl: '\u00A1', iquest: '\u00BF', brvbar: '\u00A6', macr: '\u00AF', acute: '\u00B4', cedil: '\u00B8', uml: '\u00A8',
|
|
63
|
+
ordf: '\u00AA', ordm: '\u00BA', not: '\u00AC', loz: '\u25CA',
|
|
64
|
+
// Suits, stars & checkmarks
|
|
65
|
+
spades: '\u2660', clubs: '\u2663', hearts: '\u2665', diams: '\u2666',
|
|
66
|
+
star: '\u2606', check: '\u2713', checkmark: '\u2713', cross: '\u2717',
|
|
67
|
+
// Currency & legal
|
|
68
|
+
copy: '\u00A9', reg: '\u00AE', trade: '\u2122', euro: '\u20AC', pound: '\u00A3', yen: '\u00A5', cent: '\u00A2', curren: '\u00A4',
|
|
69
|
+
// Math & arrows
|
|
70
|
+
deg: '\u00B0', plusmn: '\u00B1', times: '\u00D7', divide: '\u00F7',
|
|
71
|
+
frac12: '\u00BD', frac14: '\u00BC', frac34: '\u00BE', sup1: '\u00B9', sup2: '\u00B2', sup3: '\u00B3',
|
|
72
|
+
micro: '\u00B5', minus: '\u2212', ne: '\u2260', le: '\u2264', ge: '\u2265', approx: '\u2248', infin: '\u221E',
|
|
73
|
+
sum: '\u2211', prod: '\u220F', radic: '\u221A', int: '\u222B', part: '\u2202', empty: '\u2205', nabla: '\u2207',
|
|
74
|
+
isin: '\u2208', notin: '\u2209', ni: '\u220B', cap: '\u2229', cup: '\u222A', sub: '\u2282', sup: '\u2283',
|
|
75
|
+
nsub: '\u2284', sube: '\u2286', supe: '\u2287', oplus: '\u2295', otimes: '\u2297', perp: '\u22A5', sdot: '\u22C5',
|
|
76
|
+
equiv: '\u2261', cong: '\u2245', asymp: '\u2248', prop: '\u221D', and: '\u2227', or: '\u2228', forall: '\u2200', exist: '\u2203',
|
|
77
|
+
larr: '\u2190', uarr: '\u2191', rarr: '\u2192', darr: '\u2193', harr: '\u2194', crarr: '\u21B5',
|
|
78
|
+
lArr: '\u21D0', uArr: '\u21D1', rArr: '\u21D2', dArr: '\u21D3', hArr: '\u21D4',
|
|
79
|
+
// Greek
|
|
80
|
+
Alpha: '\u0391', Beta: '\u0392', Gamma: '\u0393', Delta: '\u0394', Epsilon: '\u0395', Zeta: '\u0396', Eta: '\u0397', Theta: '\u0398',
|
|
81
|
+
Iota: '\u0399', Kappa: '\u039A', Lambda: '\u039B', Mu: '\u039C', Nu: '\u039D', Xi: '\u039E', Omicron: '\u039F', Pi: '\u03A0',
|
|
82
|
+
Rho: '\u03A1', Sigma: '\u03A3', Tau: '\u03A4', Upsilon: '\u03A5', Phi: '\u03A6', Chi: '\u03A7', Psi: '\u03A8', Omega: '\u03A9',
|
|
83
|
+
alpha: '\u03B1', beta: '\u03B2', gamma: '\u03B3', delta: '\u03B4', epsilon: '\u03B5', zeta: '\u03B6', eta: '\u03B7', theta: '\u03B8',
|
|
84
|
+
iota: '\u03B9', kappa: '\u03BA', lambda: '\u03BB', mu: '\u03BC', nu: '\u03BD', xi: '\u03BE', omicron: '\u03BF', pi: '\u03C0',
|
|
85
|
+
rho: '\u03C1', sigmaf: '\u03C2', sigma: '\u03C3', tau: '\u03C4', upsilon: '\u03C5', phi: '\u03C6', chi: '\u03C7', psi: '\u03C8', omega: '\u03C9',
|
|
86
|
+
// Latin-1 & European accents
|
|
87
|
+
Agrave: '\u00C0', Aacute: '\u00C1', Acirc: '\u00C2', Atilde: '\u00C3', Auml: '\u00C4', Aring: '\u00C5', AElig: '\u00C6', Ccedil: '\u00C7',
|
|
88
|
+
Egrave: '\u00C8', Eacute: '\u00C9', Ecirc: '\u00CA', Euml: '\u00CB', Igrave: '\u00CC', Iacute: '\u00CD', Icirc: '\u00CE', Iuml: '\u00CF',
|
|
89
|
+
ETH: '\u00D0', Ntilde: '\u00D1', Ograve: '\u00D2', Oacute: '\u00D3', Ocirc: '\u00D4', Otilde: '\u00D5', Ouml: '\u00D6', Oslash: '\u00D8',
|
|
90
|
+
Ugrave: '\u00D9', Uacute: '\u00DA', Ucirc: '\u00DB', Uuml: '\u00DC', Yacute: '\u00DD', THORN: '\u00DE', szlig: '\u00DF',
|
|
91
|
+
agrave: '\u00E0', aacute: '\u00E1', acirc: '\u00E2', atilde: '\u00E3', auml: '\u00E4', aring: '\u00E5', aelig: '\u00E6', ccedil: '\u00E7',
|
|
92
|
+
egrave: '\u00E8', eacute: '\u00E9', ecirc: '\u00EA', euml: '\u00EB', igrave: '\u00EC', iacute: '\u00ED', icirc: '\u00EE', iuml: '\u00EF',
|
|
93
|
+
eth: '\u00F0', ntilde: '\u00F1', ograve: '\u00F2', oacute: '\u00F3', ocirc: '\u00F4', otilde: '\u00F5', ouml: '\u00F6', oslash: '\u00F8',
|
|
94
|
+
ugrave: '\u00F9', uacute: '\u00FA', ucirc: '\u00FB', uuml: '\u00FC', yacute: '\u00FD', thorn: '\u00FE', yuml: '\u00FF',
|
|
95
|
+
OElig: '\u0152', oelig: '\u0153', Scaron: '\u0160', scaron: '\u0161', Yuml: '\u0178',
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const ENTITY_RE = /&(?:#(\d+)|#x([0-9a-fA-F]+)|([A-Za-z][A-Za-z0-9]*));/g
|
|
99
|
+
|
|
100
|
+
export function decodeHtmlEntities(str: string): string {
|
|
101
|
+
if (!str.includes('&'))
|
|
102
|
+
return str
|
|
103
|
+
return str.replace(ENTITY_RE, (match, dec: string | undefined, hex: string | undefined, named: string | undefined) => {
|
|
104
|
+
if (dec !== undefined) {
|
|
105
|
+
const cp = Number.parseInt(dec, 10)
|
|
106
|
+
return cp === 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF) ? '\uFFFD' : String.fromCodePoint(cp)
|
|
107
|
+
}
|
|
108
|
+
if (hex !== undefined) {
|
|
109
|
+
const cp = Number.parseInt(hex, 16)
|
|
110
|
+
return cp === 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF) ? '\uFFFD' : String.fromCodePoint(cp)
|
|
111
|
+
}
|
|
112
|
+
if (named !== undefined && Object.hasOwn(HTML_ENTITIES, named))
|
|
113
|
+
return HTML_ENTITIES[named]!
|
|
114
|
+
return match
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function formatLoc(filename: string, code: string, offset?: number): string {
|
|
119
|
+
if (offset === undefined || offset < 0)
|
|
120
|
+
return filename
|
|
121
|
+
let line = 1
|
|
122
|
+
let col = 1
|
|
123
|
+
const end = Math.min(offset, code.length)
|
|
124
|
+
for (let i = 0; i < end; i++) {
|
|
125
|
+
if (code.charCodeAt(i) === 10) {
|
|
126
|
+
line++
|
|
127
|
+
col = 1
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
col++
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return `${filename}:${line}:${col}`
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function cleanJsxText(raw: string): string {
|
|
137
|
+
if (!LINE_BREAK_RE.test(raw))
|
|
138
|
+
return raw
|
|
139
|
+
const lines = raw.split(LINE_BREAK_RE)
|
|
140
|
+
let out = ''
|
|
141
|
+
for (let i = 0; i < lines.length; i++) {
|
|
142
|
+
let line = lines[i]!.replace(/\t/g, ' ')
|
|
143
|
+
if (i > 0)
|
|
144
|
+
line = line.replace(/^ +/, '')
|
|
145
|
+
if (i < lines.length - 1)
|
|
146
|
+
line = line.replace(/ +$/, '')
|
|
147
|
+
if (line !== '') {
|
|
148
|
+
if (out !== '')
|
|
149
|
+
out += ' '
|
|
150
|
+
out += line
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return out
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function jsxTagName(node: JSXElementName | JSXNamespacedName | { type: 'JSXIdentifier', name: string }): string {
|
|
157
|
+
if (node.type === 'JSXIdentifier')
|
|
158
|
+
return node.name
|
|
159
|
+
if (node.type === 'JSXMemberExpression')
|
|
160
|
+
return `${jsxTagName(node.object)}.${node.property.name}`
|
|
161
|
+
if (node.type === 'JSXNamespacedName')
|
|
162
|
+
return `${node.namespace.name}:${node.name.name}`
|
|
163
|
+
return ''
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function isComponentTag(tag: string): boolean {
|
|
167
|
+
const first = tag.charCodeAt(0)
|
|
168
|
+
return (first >= 65 && first <= 90) || tag.includes('.')
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function unwrapParens(node: Node): Node {
|
|
172
|
+
let cur = node
|
|
173
|
+
while (cur.type === 'ParenthesizedExpression')
|
|
174
|
+
cur = cur.expression
|
|
175
|
+
return cur
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function unwrapTsExpr(node: Node): Node {
|
|
179
|
+
let cur = node
|
|
180
|
+
while (
|
|
181
|
+
cur.type === 'ParenthesizedExpression'
|
|
182
|
+
|| cur.type === 'TSAsExpression'
|
|
183
|
+
|| cur.type === 'TSSatisfiesExpression'
|
|
184
|
+
|| cur.type === 'TSNonNullExpression'
|
|
185
|
+
|| cur.type === 'TSTypeAssertion'
|
|
186
|
+
) {
|
|
187
|
+
cur = cur.expression
|
|
188
|
+
}
|
|
189
|
+
return cur
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function staticLiteral(node: Node): { ok: true, value: string | number | boolean | null } | { ok: false } {
|
|
193
|
+
const n = unwrapParens(node)
|
|
194
|
+
if (n.type === 'Literal') {
|
|
195
|
+
const v = n.value
|
|
196
|
+
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null)
|
|
197
|
+
return { ok: true, value: v }
|
|
198
|
+
}
|
|
199
|
+
if (n.type === 'UnaryExpression' && n.operator === '-' && n.argument.type === 'Literal' && typeof n.argument.value === 'number')
|
|
200
|
+
return { ok: true, value: -n.argument.value }
|
|
201
|
+
return { ok: false }
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function isJsxNode(node: Node | null | undefined): node is JSXElement | JSXFragment {
|
|
205
|
+
return node?.type === 'JSXElement' || node?.type === 'JSXFragment'
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Whether JSX appears anywhere inside `node`, itself included. */
|
|
209
|
+
export function containsJsx(node: Node | null | undefined): boolean {
|
|
210
|
+
let found = false
|
|
211
|
+
walk(node, (child) => {
|
|
212
|
+
if (found)
|
|
213
|
+
return false
|
|
214
|
+
if (isJsxNode(child))
|
|
215
|
+
found = true
|
|
216
|
+
return undefined
|
|
217
|
+
})
|
|
218
|
+
return found
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Whether a BlockStatement directly returns JSX (ignoring nested functions). */
|
|
222
|
+
export function blockReturnsJsx(block: BlockStatement): boolean {
|
|
223
|
+
let found = false
|
|
224
|
+
walk(block, (child) => {
|
|
225
|
+
if (found)
|
|
226
|
+
return false
|
|
227
|
+
if (child !== block && (
|
|
228
|
+
child.type === 'ArrowFunctionExpression'
|
|
229
|
+
|| child.type === 'FunctionExpression'
|
|
230
|
+
|| child.type === 'FunctionDeclaration'
|
|
231
|
+
)) {
|
|
232
|
+
return false
|
|
233
|
+
}
|
|
234
|
+
if (child.type === 'ReturnStatement' && child.argument !== null && containsJsx(child.argument)) {
|
|
235
|
+
found = true
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
return undefined
|
|
239
|
+
})
|
|
240
|
+
return found
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function filterMeaningfulChildren(children: readonly JSXChild[]): JSXChild[] {
|
|
244
|
+
return children.filter((c) => {
|
|
245
|
+
if (c.type === 'JSXText')
|
|
246
|
+
return cleanJsxText(c.value) !== ''
|
|
247
|
+
if (c.type === 'JSXExpressionContainer' && c.expression.type === 'JSXEmptyExpression')
|
|
248
|
+
return false
|
|
249
|
+
// Empty string literal `{""}` produces zero DOM nodes on SSR and DOM alike.
|
|
250
|
+
if (c.type === 'JSXExpressionContainer' && c.expression.type === 'Literal' && c.expression.value === '')
|
|
251
|
+
return false
|
|
252
|
+
return true
|
|
253
|
+
})
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface MapCallMatch {
|
|
257
|
+
call: CallExpression
|
|
258
|
+
target: Expression
|
|
259
|
+
cb: ArrowFunctionExpression | OxcFunction
|
|
260
|
+
body: JSXElement | JSXFragment | BlockStatement
|
|
261
|
+
isJsxBody: boolean
|
|
262
|
+
params: readonly ParamPattern[]
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Recognizes `arr.map((item, idx?) => <jsx> | { ... return <jsx> })` identically in SSR and DOM compilers. */
|
|
266
|
+
export function matchJsxMapCall(expr: Node): MapCallMatch | undefined {
|
|
267
|
+
if (
|
|
268
|
+
expr.type !== 'CallExpression'
|
|
269
|
+
|| expr.callee.type !== 'MemberExpression'
|
|
270
|
+
|| expr.callee.computed
|
|
271
|
+
|| expr.callee.property.type !== 'Identifier'
|
|
272
|
+
|| expr.callee.property.name !== 'map'
|
|
273
|
+
|| expr.arguments.length !== 1
|
|
274
|
+
) {
|
|
275
|
+
return undefined
|
|
276
|
+
}
|
|
277
|
+
const arg = expr.arguments[0]
|
|
278
|
+
if (arg === undefined || (arg.type !== 'ArrowFunctionExpression' && arg.type !== 'FunctionExpression'))
|
|
279
|
+
return undefined
|
|
280
|
+
if (arg.async || arg.body === null)
|
|
281
|
+
return undefined
|
|
282
|
+
// 3rd `(item, idx, arr)` param or rest param cannot be mapped cleanly to `(item, idx)` loop signals.
|
|
283
|
+
if (arg.params.length > 2 || arg.params.some(p => p.type === 'RestElement'))
|
|
284
|
+
return undefined
|
|
285
|
+
const body = unwrapParens(arg.body)
|
|
286
|
+
if (isJsxNode(body))
|
|
287
|
+
return { call: expr, target: expr.callee.object, cb: arg, body, isJsxBody: true, params: arg.params }
|
|
288
|
+
if (body.type === 'BlockStatement' && blockReturnsJsx(body))
|
|
289
|
+
return { call: expr, target: expr.callee.object, cb: arg, body, isJsxBody: false, params: arg.params }
|
|
290
|
+
return undefined
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function isStaticBranch(node: Node): boolean {
|
|
294
|
+
const n = unwrapParens(node)
|
|
295
|
+
if (isJsxNode(n))
|
|
296
|
+
return true
|
|
297
|
+
if (n.type === 'Identifier' && n.name === 'undefined')
|
|
298
|
+
return true
|
|
299
|
+
return staticLiteral(n).ok
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export type ChildSlotKind =
|
|
303
|
+
| { kind: 'static-text', text: string, fromLiteral: boolean }
|
|
304
|
+
| { kind: 'element', node: JSXElement }
|
|
305
|
+
| { kind: 'component', node: JSXElement }
|
|
306
|
+
| { kind: 'fragment', node: JSXFragment }
|
|
307
|
+
| { kind: 'loop', match: MapCallMatch }
|
|
308
|
+
| { kind: 'and-show', expr: LogicalExpression, right: JSXElement | JSXFragment }
|
|
309
|
+
| { kind: 'ternary-show', expr: ConditionalExpression, cons: Node, alt: Node }
|
|
310
|
+
| { kind: 'expr', container: JSXExpressionContainer, expr: Expression }
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Single shared child classifier for SSR & DOM codegen.
|
|
314
|
+
* Both emitters derive exact hydration marker placement (`<!--k[-->...<!--k]-->` vs `<!---->`) from this classification.
|
|
315
|
+
*/
|
|
316
|
+
export function classifyJsxChild(child: JSXChild): ChildSlotKind | undefined {
|
|
317
|
+
if (child.type === 'JSXText') {
|
|
318
|
+
const text = cleanJsxText(child.value)
|
|
319
|
+
return text === '' ? undefined : { kind: 'static-text', text, fromLiteral: false }
|
|
320
|
+
}
|
|
321
|
+
if (child.type === 'JSXFragment')
|
|
322
|
+
return { kind: 'fragment', node: child }
|
|
323
|
+
if (child.type === 'JSXElement') {
|
|
324
|
+
const tag = jsxTagName(child.openingElement.name)
|
|
325
|
+
return isComponentTag(tag)
|
|
326
|
+
? { kind: 'component', node: child }
|
|
327
|
+
: { kind: 'element', node: child }
|
|
328
|
+
}
|
|
329
|
+
if (child.type === 'JSXExpressionContainer') {
|
|
330
|
+
if (child.expression.type === 'JSXEmptyExpression')
|
|
331
|
+
return undefined
|
|
332
|
+
const expr = child.expression
|
|
333
|
+
if (expr.type === 'Literal' && typeof expr.value === 'string') {
|
|
334
|
+
return expr.value === ''
|
|
335
|
+
? undefined
|
|
336
|
+
: { kind: 'static-text', text: expr.value, fromLiteral: true }
|
|
337
|
+
}
|
|
338
|
+
const mapMatch = matchJsxMapCall(expr)
|
|
339
|
+
if (mapMatch !== undefined)
|
|
340
|
+
return { kind: 'loop', match: mapMatch }
|
|
341
|
+
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
342
|
+
const right = unwrapParens(expr.right)
|
|
343
|
+
if (isJsxNode(right))
|
|
344
|
+
return { kind: 'and-show', expr, right }
|
|
345
|
+
}
|
|
346
|
+
if (expr.type === 'ConditionalExpression') {
|
|
347
|
+
const cons = unwrapParens(expr.consequent)
|
|
348
|
+
const alt = unwrapParens(expr.alternate)
|
|
349
|
+
// Use `show()` only when at least one branch is JSX and the other branch is JSX or a static literal/nullish;
|
|
350
|
+
// mixed dynamic text (`cond ? <span/> : count.value`) goes through `expr` (`insert`) so both branches stay reactive.
|
|
351
|
+
if ((isJsxNode(cons) && isStaticBranch(alt)) || (isJsxNode(alt) && isStaticBranch(cons)))
|
|
352
|
+
return { kind: 'ternary-show', expr, cons, alt }
|
|
353
|
+
}
|
|
354
|
+
return { kind: 'expr', container: child, expr }
|
|
355
|
+
}
|
|
356
|
+
return undefined
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Collects every identifier name bound by a destructuring/parameter pattern. */
|
|
360
|
+
export function collectPatternNames(pattern: Node | null | undefined, out: Set<string>): void {
|
|
361
|
+
if (pattern === null || pattern === undefined)
|
|
362
|
+
return
|
|
363
|
+
switch (pattern.type) {
|
|
364
|
+
case 'Identifier':
|
|
365
|
+
out.add(pattern.name)
|
|
366
|
+
return
|
|
367
|
+
case 'AssignmentPattern':
|
|
368
|
+
collectPatternNames(pattern.left, out)
|
|
369
|
+
return
|
|
370
|
+
case 'RestElement':
|
|
371
|
+
collectPatternNames(pattern.argument, out)
|
|
372
|
+
return
|
|
373
|
+
case 'TSParameterProperty':
|
|
374
|
+
collectPatternNames(pattern.parameter, out)
|
|
375
|
+
return
|
|
376
|
+
case 'ArrayPattern':
|
|
377
|
+
for (const el of pattern.elements) {
|
|
378
|
+
if (el !== null)
|
|
379
|
+
collectPatternNames(el, out)
|
|
380
|
+
}
|
|
381
|
+
return
|
|
382
|
+
case 'ObjectPattern':
|
|
383
|
+
for (const prop of pattern.properties) {
|
|
384
|
+
if (prop.type === 'RestElement')
|
|
385
|
+
collectPatternNames(prop.argument, out)
|
|
386
|
+
else
|
|
387
|
+
collectPatternNames(prop.value, out)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const PRIMITIVE_GLOBALS = new Set([
|
|
393
|
+
'String', 'Number', 'Boolean', 'parseInt', 'parseFloat',
|
|
394
|
+
'encodeURI', 'encodeURIComponent', 'decodeURI', 'decodeURIComponent',
|
|
395
|
+
])
|
|
396
|
+
|
|
397
|
+
const PRIMITIVE_METHODS = new Set([
|
|
398
|
+
'toString', 'toFixed', 'toPrecision', 'toExponential', 'toLocaleString',
|
|
399
|
+
'toLocaleDateString', 'toLocaleTimeString', 'toISOString', 'toUpperCase',
|
|
400
|
+
'toLowerCase', 'toLocaleUpperCase', 'toLocaleLowerCase', 'trim', 'trimStart',
|
|
401
|
+
'trimEnd', 'join', 'padStart', 'padEnd', 'repeat', 'replace', 'replaceAll',
|
|
402
|
+
'substring', 'substr', 'charAt', 'charCodeAt', 'codePointAt', 'indexOf',
|
|
403
|
+
'lastIndexOf', 'includes', 'startsWith', 'endsWith', 'getTime', 'getFullYear',
|
|
404
|
+
'getMonth', 'getDate', 'getHours', 'getMinutes', 'getSeconds',
|
|
405
|
+
])
|
|
406
|
+
|
|
407
|
+
function isKnownPrimitiveCall(node: CallExpression): boolean {
|
|
408
|
+
const callee = node.callee
|
|
409
|
+
if (callee.type === 'Identifier' && PRIMITIVE_GLOBALS.has(callee.name))
|
|
410
|
+
return true
|
|
411
|
+
if (callee.type === 'MemberExpression' && !callee.computed && callee.property.type === 'Identifier') {
|
|
412
|
+
const prop = callee.property.name
|
|
413
|
+
if (callee.object.type === 'Identifier') {
|
|
414
|
+
const obj = callee.object.name
|
|
415
|
+
if (obj === 'Math' || (obj === 'JSON' && prop === 'stringify') || (obj === 'Date' && prop === 'now'))
|
|
416
|
+
return true
|
|
417
|
+
}
|
|
418
|
+
if (PRIMITIVE_METHODS.has(prop))
|
|
419
|
+
return true
|
|
420
|
+
}
|
|
421
|
+
return false
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function mayReturnNode(node: Node): boolean {
|
|
425
|
+
const cur = unwrapTsExpr(node)
|
|
426
|
+
if (cur.type === 'CallExpression')
|
|
427
|
+
return !isKnownPrimitiveCall(cur)
|
|
428
|
+
if (cur.type === 'LogicalExpression')
|
|
429
|
+
return mayReturnNode(cur.left) || mayReturnNode(cur.right)
|
|
430
|
+
if (cur.type === 'ConditionalExpression')
|
|
431
|
+
return mayReturnNode(cur.consequent) || mayReturnNode(cur.alternate)
|
|
432
|
+
return false
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Shared SSR/DOM check for whether a dynamic expression slot may produce DOM nodes/fragments (requiring balanced `<!--k[-->...<!--k]-->` markers). */
|
|
436
|
+
export function isNodeCapableExpr(expr: Expression): boolean {
|
|
437
|
+
const isChildrenExpr = (expr.type === 'Identifier' && expr.name === 'children')
|
|
438
|
+
|| (expr.type === 'MemberExpression' && !expr.computed && expr.property.type === 'Identifier' && expr.property.name === 'children')
|
|
439
|
+
return isChildrenExpr || containsJsx(expr) || mayReturnNode(expr)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export type { Program }
|