@barefootjs/jsx 0.39.8 → 0.40.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.
Files changed (118) hide show
  1. package/dist/adapters/attr-plan.d.ts +1 -20
  2. package/dist/adapters/attr-plan.d.ts.map +1 -1
  3. package/dist/adapters/callback-method.d.ts +19 -7
  4. package/dist/adapters/callback-method.d.ts.map +1 -1
  5. package/dist/adapters/dsl-template-adapter.d.ts +79 -1
  6. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  7. package/dist/adapters/loop-plan.d.ts +173 -9
  8. package/dist/adapters/loop-plan.d.ts.map +1 -1
  9. package/dist/adapters/parsed-expr-emitter.d.ts +20 -3
  10. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  11. package/dist/analyzer.d.ts.map +1 -1
  12. package/dist/assignment-target.d.ts +26 -0
  13. package/dist/assignment-target.d.ts.map +1 -0
  14. package/dist/augment-inherited-props.d.ts.map +1 -1
  15. package/dist/css-layer-prefixer.d.ts.map +1 -1
  16. package/dist/debug.d.ts.map +1 -1
  17. package/dist/errors.d.ts +1 -0
  18. package/dist/errors.d.ts.map +1 -1
  19. package/dist/index.d.ts +6 -6
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2566 -2007
  22. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
  24. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +24 -1
  25. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
  27. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -1
  29. package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
  31. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  33. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/phases/props-extraction.d.ts +5 -3
  35. package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
  37. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
  39. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +1 -0
  40. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  41. package/dist/ir-to-client-js/utils.d.ts +1 -1
  42. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  43. package/dist/jsx-to-ir.d.ts.map +1 -1
  44. package/dist/module-exports.d.ts.map +1 -1
  45. package/dist/prop-rewrite.d.ts +17 -8
  46. package/dist/prop-rewrite.d.ts.map +1 -1
  47. package/dist/props-binding.d.ts +57 -0
  48. package/dist/props-binding.d.ts.map +1 -1
  49. package/dist/scope/binding-scope.d.ts +32 -2
  50. package/dist/scope/binding-scope.d.ts.map +1 -1
  51. package/dist/scope/lexical-scope-walk.d.ts +26 -0
  52. package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
  53. package/dist/template-parts.d.ts +1 -1
  54. package/dist/template-parts.d.ts.map +1 -1
  55. package/dist/types.d.ts +30 -2
  56. package/dist/types.d.ts.map +1 -1
  57. package/package.json +2 -2
  58. package/src/__tests__/arithmetic-lowering.test.ts +5 -1
  59. package/src/__tests__/attr-plan.test.ts +1 -14
  60. package/src/__tests__/binding-scope.test.ts +29 -0
  61. package/src/__tests__/client-template-escape-soundness.test.ts +17 -4
  62. package/src/__tests__/client-template-static-delimiters.test.ts +57 -0
  63. package/src/__tests__/create-query.test.ts +1 -1
  64. package/src/__tests__/dsl-template-adapter.test.ts +70 -0
  65. package/src/__tests__/emit-to-string-call.test.ts +58 -0
  66. package/src/__tests__/extract-function-params.test.ts +37 -0
  67. package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +18 -0
  68. package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
  69. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
  70. package/src/__tests__/loop-plan.test.ts +196 -1
  71. package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
  72. package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
  73. package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
  74. package/src/__tests__/props-alias-const.test.ts +60 -1
  75. package/src/__tests__/props-named-callback-param.test.ts +256 -0
  76. package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
  77. package/src/__tests__/rewrite-props-object.test.ts +90 -0
  78. package/src/__tests__/template-parts-literal-boundary.test.ts +22 -0
  79. package/src/__tests__/written-binding-substitution.test.ts +430 -0
  80. package/src/adapters/attr-plan.ts +2 -54
  81. package/src/adapters/callback-method.ts +25 -10
  82. package/src/adapters/dsl-template-adapter.ts +155 -2
  83. package/src/adapters/loop-plan.ts +267 -14
  84. package/src/adapters/parsed-expr-emitter.ts +37 -4
  85. package/src/analyzer.ts +283 -2
  86. package/src/assignment-target.ts +79 -0
  87. package/src/augment-inherited-props.ts +5 -9
  88. package/src/css-layer-prefixer.ts +4 -5
  89. package/src/debug.ts +1 -0
  90. package/src/errors.ts +11 -0
  91. package/src/index.ts +23 -4
  92. package/src/ir-to-client-js/compute-inlinability.ts +7 -4
  93. package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
  94. package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
  95. package/src/ir-to-client-js/control-flow/shared.ts +41 -3
  96. package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
  97. package/src/ir-to-client-js/csr-substitute.ts +70 -22
  98. package/src/ir-to-client-js/emit-reactive.ts +2 -2
  99. package/src/ir-to-client-js/emit-registration.ts +6 -1
  100. package/src/ir-to-client-js/html-template.ts +70 -52
  101. package/src/ir-to-client-js/phases/props-extraction.ts +26 -5
  102. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
  103. package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
  104. package/src/ir-to-client-js/rewrite-destructured-props.ts +2 -2
  105. package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
  106. package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
  107. package/src/ir-to-client-js/utils.ts +41 -97
  108. package/src/jsx-to-ir.ts +65 -32
  109. package/src/loop-destructure.ts +4 -1
  110. package/src/module-exports.ts +13 -21
  111. package/src/prop-rewrite.ts +31 -72
  112. package/src/props-binding.ts +73 -1
  113. package/src/rich-type-refusal.ts +3 -1
  114. package/src/scope/binding-scope.ts +39 -3
  115. package/src/scope/lexical-scope-walk.ts +167 -0
  116. package/src/template-parts.ts +4 -2
  117. package/src/template-position-walk.ts +3 -1
  118. package/src/types.ts +27 -2
@@ -0,0 +1,167 @@
1
+ /**
2
+ * Lexical binding frames for TS-AST walks over emitted or source JS text.
3
+ *
4
+ * Scope model: a name bound INSIDE the walked node — a nested arrow /
5
+ * function parameter, a block-scoped declaration, a `for` head's `let` /
6
+ * `const`, a catch variable — refers to that binding, not to an outer
7
+ * name of the same spelling, for the whole binding's scope. Names bound
8
+ * OUTSIDE the walked text (a loop callback that encloses a template
9
+ * fragment) are the caller's job — that is what `BindingScope`
10
+ * (`./binding-scope.ts`) answers; this module answers the complementary
11
+ * question for binders that live inside the text itself.
12
+ *
13
+ * Shared by the prop-reference rewrites in `prop-rewrite.ts`, the
14
+ * props-object rename in `ir-to-client-js/rewrite-props-object.ts` and the
15
+ * loop-param accessor rewrite in `ir-to-client-js/utils.ts`
16
+ * (`rewriteIdentifierReferences`, #3595), so all of them agree on which
17
+ * identifiers an inner binding shadows.
18
+ */
19
+
20
+ import ts from 'typescript'
21
+
22
+ /** Collect every name introduced by a binding name (identifier or pattern). */
23
+ function collectBindingNames(name: ts.BindingName, out: Set<string>): void {
24
+ if (ts.isIdentifier(name)) {
25
+ out.add(name.text)
26
+ return
27
+ }
28
+ for (const el of name.elements) {
29
+ if (ts.isBindingElement(el)) collectBindingNames(el.name, out)
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Names bound by `n` for its subtree, or null when `n` introduces no
35
+ * scope. The frames follow JS's two scoping rules:
36
+ *
37
+ * - Function scope: a function-like binds its parameters and a function
38
+ * expression's own name; this frame covers the parameter list, so a
39
+ * default initializer sees the parameters but none of the body's
40
+ * declarations. A method's or accessor's computed name (`[k](k) { … }`)
41
+ * sits outside this frame: it is evaluated in the enclosing scope. The
42
+ * function's body block binds, on top of its own
43
+ * `let` / `const`, every `var` declared anywhere in the body — inside
44
+ * nested blocks and loop heads too (`if (c) { var x }` and `for (var x …)`
45
+ * are visible to the whole body), but not inside a nested function, which
46
+ * owns its own `var`s. A concise arrow body declares nothing. A class
47
+ * `static { … }` block is a var scope of its own in the same way. A named
48
+ * class expression binds its own name inside the class, as a named
49
+ * function expression does.
50
+ * - Block scope: a block binds its statement-level `let` / `const`, class
51
+ * and function declarations (the emitted client JS is a module, so it is
52
+ * strict code: a function declared in a block is scoped to that block);
53
+ * a `for` head binds its `let` / `const`; a catch clause binds its
54
+ * variable.
55
+ *
56
+ * A `var` with no enclosing function in the walked text binds at its top
57
+ * level, where no frame is pushed — the same as any other top-level
58
+ * declaration.
59
+ */
60
+ function scopeFrameOf(n: ts.Node, parent: ts.Node | undefined): Set<string> | null {
61
+ const frame = new Set<string>()
62
+ if (ts.isFunctionLike(n)) collectFunctionBindings(n, frame)
63
+ else if (ts.isBlock(n)) {
64
+ if (parent && isFunctionBody(n, parent)) collectVarBindings(n, frame)
65
+ collectBlockBindings(n, frame)
66
+ }
67
+ else if (ts.isForStatement(n) || ts.isForOfStatement(n) || ts.isForInStatement(n)) collectForHeadBindings(n, frame)
68
+ else if (ts.isCatchClause(n) && n.variableDeclaration) collectBindingNames(n.variableDeclaration.name, frame)
69
+ else if (ts.isClassExpression(n) && n.name) frame.add(n.name.text)
70
+ return frame.size > 0 ? frame : null
71
+ }
72
+
73
+ function collectFunctionBindings(n: ts.SignatureDeclaration, frame: Set<string>): void {
74
+ for (const p of n.parameters) collectBindingNames(p.name, frame)
75
+ if ((ts.isFunctionExpression(n) || ts.isFunctionDeclaration(n)) && n.name) frame.add(n.name.text)
76
+ }
77
+
78
+ /**
79
+ * A var-scope boundary: a function-like (function declaration / expression,
80
+ * arrow, method, accessor, constructor) or a class `static { … }` block. Each
81
+ * owns the `var`s declared in it. A class field initializer is an
82
+ * expression, so a `var` can only sit inside one of these boundaries there.
83
+ */
84
+ function isVarScopeBoundary(n: ts.Node): n is ts.SignatureDeclaration | ts.ClassStaticBlockDeclaration {
85
+ return ts.isFunctionLike(n) || ts.isClassStaticBlockDeclaration(n)
86
+ }
87
+
88
+ /** True when `block` is the body of the var-scope boundary `parent`. */
89
+ function isFunctionBody(block: ts.Block, parent: ts.Node): boolean {
90
+ return isVarScopeBoundary(parent) && (parent as { body?: ts.Node }).body === block
91
+ }
92
+
93
+ /** True for a `let` / `const` (and `using`) declaration list; false for `var`. */
94
+ function isBlockScopedList(list: ts.VariableDeclarationList): boolean {
95
+ return (list.flags & ts.NodeFlags.BlockScoped) !== 0
96
+ }
97
+
98
+ /**
99
+ * Every `var`-declared name under `n`, through blocks and loop heads but not
100
+ * into a nested var-scope boundary (each binds its own `var`s).
101
+ */
102
+ function collectVarBindings(n: ts.Node, frame: Set<string>): void {
103
+ if (ts.isVariableDeclarationList(n) && !isBlockScopedList(n)) {
104
+ for (const d of n.declarations) collectBindingNames(d.name, frame)
105
+ }
106
+ ts.forEachChild(n, child => {
107
+ if (!isVarScopeBoundary(child)) collectVarBindings(child, frame)
108
+ })
109
+ }
110
+
111
+ function collectBlockBindings(n: ts.Block, frame: Set<string>): void {
112
+ for (const st of n.statements) {
113
+ if (ts.isVariableStatement(st)) {
114
+ if (!isBlockScopedList(st.declarationList)) continue
115
+ for (const d of st.declarationList.declarations) collectBindingNames(d.name, frame)
116
+ } else if ((ts.isFunctionDeclaration(st) || ts.isClassDeclaration(st)) && st.name) {
117
+ frame.add(st.name.text)
118
+ }
119
+ }
120
+ }
121
+
122
+ /**
123
+ * A `for` head's `let` / `const` binds for the loop (`for (let i = 0; …;
124
+ * i++)`, `for (const x of xs)`): its condition, incrementor and body read
125
+ * the loop variable, not an outer prop of the same name.
126
+ */
127
+ function collectForHeadBindings(n: ts.ForStatement | ts.ForOfStatement | ts.ForInStatement, frame: Set<string>): void {
128
+ if (!n.initializer || !ts.isVariableDeclarationList(n.initializer)) return
129
+ // A `for (var …)` binds in the enclosing function (`collectVarBindings`).
130
+ if (!isBlockScopedList(n.initializer)) return
131
+ for (const d of n.initializer.declarations) collectBindingNames(d.name, frame)
132
+ }
133
+
134
+ /** The computed property name of a function-like (`[k](k) { … }`), if any. */
135
+ function computedNameOf(n: ts.Node): ts.ComputedPropertyName | undefined {
136
+ if (!ts.isFunctionLike(n)) return undefined
137
+ const name = (n as { name?: ts.Node }).name
138
+ return name && ts.isComputedPropertyName(name) ? name : undefined
139
+ }
140
+
141
+ /**
142
+ * Depth-first walk that maintains the binding stack described above and
143
+ * reports every identifier along with whether a binding within `root`
144
+ * currently shadows it.
145
+ */
146
+ export function walkWithScope(
147
+ root: ts.Node,
148
+ visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean) => void,
149
+ ): void {
150
+ const scopeStack: Set<string>[] = []
151
+ const isShadowed = (name: string) => scopeStack.some(frame => frame.has(name))
152
+ function rec(n: ts.Node, parent?: ts.Node) {
153
+ const frame = scopeFrameOf(n, parent)
154
+ // A method's / accessor's computed name (`{ [k](k) { … } }`) is evaluated
155
+ // in the enclosing scope, before the parameters exist: walk it outside
156
+ // the function's frame.
157
+ const outerName = frame ? computedNameOf(n) : undefined
158
+ if (outerName) rec(outerName, n)
159
+ if (frame) scopeStack.push(frame)
160
+ if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text))
161
+ ts.forEachChild(n, child => {
162
+ if (child !== outerName) rec(child, n)
163
+ })
164
+ if (frame) scopeStack.pop()
165
+ }
166
+ rec(root)
167
+ }
@@ -14,7 +14,7 @@ import type { IRTemplatePart } from './types.ts'
14
14
 
15
15
  export interface TemplatePartsToJsOptions {
16
16
  /**
17
- * Prefer each part's prop-rewritten projection (`templateValue` /
17
+ * Prefer each part's prop-rewritten projection (`templateExpr` /
18
18
  * `templateCondition` / `templateKey`, i.e. destructured props rewritten
19
19
  * to `_p.X`) when present. Used by the client-JS / module-registration
20
20
  * template emitters, which run outside the component's destructured scope.
@@ -68,7 +68,9 @@ export function templatePartsToJsExpr(
68
68
  let result = '`'
69
69
  for (const part of parts) {
70
70
  if (part.type === 'string') {
71
- result += (opts?.useTemplate && part.templateValue) ? part.templateValue : part.value
71
+ result += part.value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${').replace(/\r/g, '\\r')
72
+ } else if (part.type === 'expression') {
73
+ result += `\${${opts?.useTemplate ? part.templateExpr ?? part.expr : part.expr}}`
72
74
  } else if (part.type === 'ternary') {
73
75
  const cond = (opts?.useTemplate && part.templateCondition) ? part.templateCondition : part.condition
74
76
  result += `\${${cond} ? '${part.whenTrue}' : '${part.whenFalse}'}`
@@ -105,7 +105,9 @@ function walkTemplateParts(
105
105
  visit({ expr: parseExpression(trimmed), loc, role: 'template-part', attrName })
106
106
  }
107
107
  for (const part of parts) {
108
- if (part.type === 'ternary') {
108
+ if (part.type === 'expression') {
109
+ visit({ expr: part.parsed, loc, role: 'template-part', attrName })
110
+ } else if (part.type === 'ternary') {
109
111
  visitText(part.condition)
110
112
  visitText(part.whenTrue)
111
113
  visitText(part.whenFalse)
package/src/types.ts CHANGED
@@ -128,6 +128,18 @@ export interface ParamInfo {
128
128
  defaultParts?: PropDefaultPart[]
129
129
  /** When true, the parameter is a rest spread (`...args`) — emit must prepend `...`. */
130
130
  isRest?: boolean
131
+ /**
132
+ * Set when the component writes the destructured binding anywhere in its
133
+ * body (`++start`, `start = clamp(start)`, `[a, b] = [b, a]`,
134
+ * `for (start of …)`), event handlers and other callbacks included. The
135
+ * binding is then a local the component owns, as in JS, not the prop: the
136
+ * client init body declares it once (`let start = _p.start ?? 0`) and reads
137
+ * and writes that local instead of the live prop read `_p.start ?? 0`,
138
+ * which no write can take (`++(_p.start ?? 0)` is a SyntaxError). Set by
139
+ * `markWrittenBindings` (analyzer.ts); read through `keepsDeclaredValue`
140
+ * (props-binding.ts).
141
+ */
142
+ reassigned?: boolean
131
143
  /**
132
144
  * Source property name for an aliased destructured prop (`{ createdAt: c }`
133
145
  * → name: 'c', sourceName: 'createdAt'). Set ONLY when the binding renames —
@@ -1326,7 +1338,8 @@ export interface IRIfStatement {
1326
1338
  // =============================================================================
1327
1339
 
1328
1340
  export type IRTemplatePart =
1329
- | { type: 'string'; value: string; templateValue?: string }
1341
+ | { type: 'string'; value: string }
1342
+ | { type: 'expression'; expr: JsSourceText; templateExpr?: JsSourceText; parsed: ParsedExpr }
1330
1343
  | { type: 'ternary'; condition: string; templateCondition?: string; whenTrue: string; whenFalse: string }
1331
1344
  /**
1332
1345
  * `${MAP[KEY]}` indexed lookup against a `Record<T, string>` literal
@@ -2270,7 +2283,10 @@ export interface ConstantInfo {
2270
2283
  * (the declaration's initializer text) no longer reflects what the
2271
2284
  * binding actually holds by the time the rest of the component runs
2272
2285
  * (#2910). Set by `markMutatedConstants` (analyzer.ts) from
2273
- * `InitStatementInfo.mutatedIdentifiers`/`assignedIdentifiers`.
2286
+ * `InitStatementInfo.mutatedIdentifiers`/`assignedIdentifiers`, and for
2287
+ * a binding a markup expression writes during render (`{++nextId}`, a
2288
+ * `.map()` callback that increments a counter) — not an event handler,
2289
+ * which runs after render.
2274
2290
  *
2275
2291
  * Every consumer that would otherwise re-inline `value` verbatim in
2276
2292
  * place of a bare reference to `name` — `expandDynamicPropValue`,
@@ -2282,6 +2298,15 @@ export interface ConstantInfo {
2282
2298
  * was supposed to do.
2283
2299
  */
2284
2300
  mutatedAfterDeclaration?: boolean
2301
+ /**
2302
+ * Set when the component writes the binding anywhere in its body, event
2303
+ * handlers and other callbacks included — the same fact
2304
+ * `ParamInfo.reassigned` records for a destructured prop, from the same
2305
+ * pass (`markWrittenBindings`, analyzer.ts). A written binding fails
2306
+ * `keepsDeclaredValue` (props-binding.ts), the one rule for whether a
2307
+ * binding may be read as the prop it was destructured from.
2308
+ */
2309
+ reassigned?: boolean
2285
2310
  /** The kind of system construct, if the initializer is createContext() or new WeakMap(). */
2286
2311
  systemConstructKind?: 'createContext' | 'weakMap'
2287
2312
  /** Value with destructured prop refs rewritten to _p.propName, for template inlining. */