@kudzujs/core 0.17.9 → 0.17.11
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/README.md +1 -1
- package/framework/compiler/source-compiler.mjs +49 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Kudzu compiles ordinary React-shaped TypeScript and TSX into complete static HTM
|
|
|
14
14
|
|
|
15
15
|
> Experimental `0.17.x`: the compiler API and supported TSX surface may change.
|
|
16
16
|
|
|
17
|
-
**Current release: [0.17.
|
|
17
|
+
**Current release: [0.17.11](https://github.com/kudzujs/kudzu/releases/tag/v0.17.11).** Supported Context-returning hooks now allow identifier aliases for state, setters and actions. Repeated consumers retain shared Provider state, and hidden setter names avoid consumer collisions. No browser runtime is added; private-timer aliases remain unsupported. Generator `create-kudzu@0.1.172` targets core `^0.17.11`. Regression and artifact evidence is recorded in `PERFORMANCE.md`; no speed or whole-application migration claim is made.
|
|
18
18
|
|
|
19
19
|
- [Documentation](https://kudzujs.cloud/docs)
|
|
20
20
|
- [Installation guide](https://kudzujs.cloud/docs#install)
|
|
@@ -713,11 +713,14 @@ function createKudzuTransformer({ semantic, handlerUrl, file, sourceFiles, sourc
|
|
|
713
713
|
if (!isLocalConst(node) || !ts.isObjectBindingPattern(node.name) || node.initializer.arguments.length) throw sourceNodeError(node, sourceFile, "Relative custom hooks must initialize one top-level const object destructuring with no arguments")
|
|
714
714
|
const hook = resolveCustomHook(importBindings.get(callName), node.initializer)
|
|
715
715
|
const names = new Set()
|
|
716
|
+
const localNames = new Map()
|
|
716
717
|
for (const element of node.name.elements) {
|
|
717
|
-
if (element.dotDotDotToken || element.
|
|
718
|
-
const name = element.name.text
|
|
718
|
+
if (element.dotDotDotToken || element.initializer || !ts.isIdentifier(element.name) || element.propertyName && (!ts.isIdentifier(element.propertyName) || hook.privateStates.size)) throw sourceNodeError(element, sourceFile, "Relative custom hook results require identifier fields and local names without defaults or rest; private-timer result aliases remain unsupported")
|
|
719
|
+
const name = (element.propertyName ?? element.name).text
|
|
719
720
|
if (!hook.fields.has(name)) throw sourceNodeError(element, sourceFile, `Relative custom hook does not directly return ${JSON.stringify(name)}`)
|
|
721
|
+
if (names.has(name)) throw sourceNodeError(element, sourceFile, `Relative custom hook result ${JSON.stringify(name)} must be destructured only once`)
|
|
720
722
|
names.add(name)
|
|
723
|
+
localNames.set(name, element.name.text)
|
|
721
724
|
}
|
|
722
725
|
const owner = nearestFunction(node)
|
|
723
726
|
if (!owner) throw sourceNodeError(node, sourceFile, "Relative custom hooks cannot be used outside a Kudzu component")
|
|
@@ -729,7 +732,7 @@ function createKudzuTransformer({ semantic, handlerUrl, file, sourceFiles, sourc
|
|
|
729
732
|
if (callback) for (const state of referencedStateNames(callback.body, hook.states, callback)) requiredContextStates.add(state)
|
|
730
733
|
}
|
|
731
734
|
}
|
|
732
|
-
const contextSubstitutions = new Map()
|
|
735
|
+
const contextSubstitutions = new Map([...localNames].filter(([name, local]) => name !== local).map(([name, local]) => [name, factory.createIdentifier(local)]))
|
|
733
736
|
const contextSharedStates = new Map()
|
|
734
737
|
for (const [setter, state] of hook.states) {
|
|
735
738
|
if (hook.context) {
|
|
@@ -737,8 +740,8 @@ function createKudzuTransformer({ semantic, handlerUrl, file, sourceFiles, sourc
|
|
|
737
740
|
if (!names.has(state) && !requiredContextStates.has(state)) continue
|
|
738
741
|
const privateFields = customHookPrivateFields.get(node) ?? []
|
|
739
742
|
const localName = field => {
|
|
740
|
-
if (names.has(field)) return field
|
|
741
|
-
const occupied = name => owner.parameters.some(parameter => bindingNames(parameter.name).includes(name)) || owner.body.statements.some(statement => statement !== node.parent.parent && statementDeclaresName(statement, name))
|
|
743
|
+
if (names.has(field)) return localNames.get(field)
|
|
744
|
+
const occupied = name => [...localNames.values()].includes(name) || setters.has(name) || [...setters.values()].includes(name) || owner.parameters.some(parameter => bindingNames(parameter.name).includes(name)) || owner.body.statements.some(statement => statement !== node.parent.parent && statementDeclaresName(statement, name))
|
|
742
745
|
if (!occupied(field)) return field
|
|
743
746
|
let index = 0
|
|
744
747
|
let local
|
|
@@ -773,16 +776,20 @@ function createKudzuTransformer({ semantic, handlerUrl, file, sourceFiles, sourc
|
|
|
773
776
|
}
|
|
774
777
|
if (names.has(setter) !== names.has(state)) throw sourceNodeError(node.name, sourceFile, `Relative custom hook state ${JSON.stringify(state)} and setter ${JSON.stringify(setter)} must be destructured together`)
|
|
775
778
|
if (names.has(setter)) {
|
|
776
|
-
setters.set(setter, state)
|
|
777
|
-
registerState(owner, state, setter, "custom-hook", node)
|
|
779
|
+
setters.set(localNames.get(setter), localNames.get(state))
|
|
780
|
+
registerState(owner, localNames.get(state), localNames.get(setter), "custom-hook", node)
|
|
778
781
|
}
|
|
779
782
|
}
|
|
780
783
|
settersByFunction.set(owner, setters)
|
|
781
784
|
for (const name of names) {
|
|
782
785
|
if (hook.callbacks.has(name)) {
|
|
783
786
|
const callback = contextSubstitutions.size ? substituteClone(hook.callbacks.get(name), contextSubstitutions, factory, context) : hook.callbacks.get(name)
|
|
787
|
+
if (contextSubstitutions.size) {
|
|
788
|
+
ts.setParentRecursive(callback, false)
|
|
789
|
+
callback.parent = hook.callbacks.get(name).parent
|
|
790
|
+
}
|
|
784
791
|
const callbacks = customHookFunctionsByOwner.get(owner) ?? new Map()
|
|
785
|
-
callbacks.set(name, callback)
|
|
792
|
+
callbacks.set(localNames.get(name), callback)
|
|
786
793
|
customHookFunctionsByOwner.set(owner, callbacks)
|
|
787
794
|
if (hook.context) {
|
|
788
795
|
const reducers = reducersByFunction.get(owner) ?? new Map()
|
|
@@ -791,12 +798,12 @@ function createKudzuTransformer({ semantic, handlerUrl, file, sourceFiles, sourc
|
|
|
791
798
|
const anchor = [...hook.states.values()].find(state => referenced.has(state))
|
|
792
799
|
const sharedState = contextSharedStates.get(anchor)
|
|
793
800
|
if (!sharedState) {
|
|
794
|
-
reducers.set(name, { sourceKind: "Context", directImplementation: callback, states })
|
|
801
|
+
reducers.set(localNames.get(name), { sourceKind: "Context", directImplementation: callback, states })
|
|
795
802
|
reducersByFunction.set(owner, reducers)
|
|
796
803
|
continue
|
|
797
804
|
}
|
|
798
805
|
const action = registerSharedAction(moduleIR, { state: sharedState.slot, name })
|
|
799
|
-
reducers.set(name, { state: contextSubstitutions.get(anchor)?.text ?? anchor, sourceKind: "Context", sharedAction: { ...action, directImplementation: callback, states } })
|
|
806
|
+
reducers.set(localNames.get(name), { state: contextSubstitutions.get(anchor)?.text ?? anchor, sourceKind: "Context", sharedAction: { ...action, directImplementation: callback, states } })
|
|
800
807
|
reducersByFunction.set(owner, reducers)
|
|
801
808
|
}
|
|
802
809
|
}
|
|
@@ -3274,12 +3281,42 @@ function isEventOnlyComponentLocal(root, name) {
|
|
|
3274
3281
|
}
|
|
3275
3282
|
|
|
3276
3283
|
function substituteClone(root, substitutions, factory, context) {
|
|
3284
|
+
const index = isFunctionLike(root) ? createBindingIndex(root) : undefined
|
|
3285
|
+
const locals = new Map()
|
|
3286
|
+
if (index) {
|
|
3287
|
+
// Renaming a capture must not bind it to a callback-local name.
|
|
3288
|
+
const targets = new Set([...substitutions.values()].filter(ts.isIdentifier).map(node => node.text))
|
|
3289
|
+
const bindings = index.bindings()
|
|
3290
|
+
const occupied = new Set([...bindings.map(binding => binding.debugName), ...targets])
|
|
3291
|
+
for (const binding of bindings) {
|
|
3292
|
+
if (!targets.has(binding.debugName) || ["global", "unresolved"].includes(binding.declarationKind)) continue
|
|
3293
|
+
let name = `__kLocal_${binding.debugName}`
|
|
3294
|
+
while (occupied.has(name)) name += "_"
|
|
3295
|
+
occupied.add(name)
|
|
3296
|
+
locals.set(binding.slot, name)
|
|
3297
|
+
}
|
|
3298
|
+
}
|
|
3299
|
+
const localName = node => {
|
|
3300
|
+
if (!index || !locals.size) return undefined
|
|
3301
|
+
const parent = node.parent
|
|
3302
|
+
const declaration = parent?.name === node && (ts.isVariableDeclaration(parent) || ts.isParameter(parent) || ts.isBindingElement(parent) || ts.isFunctionDeclaration(parent) || ts.isFunctionExpression(parent) || ts.isClassDeclaration(parent) || ts.isClassExpression(parent))
|
|
3303
|
+
const binding = declaration ? index.resolveBinding(node) : index.resolveReference(node)
|
|
3304
|
+
return binding && locals.get(binding.slot)
|
|
3305
|
+
}
|
|
3277
3306
|
const visit = (node, shadowed = new Set()) => {
|
|
3278
3307
|
if (ts.isTypeNode(node)) return cloneAst(node, factory, context)
|
|
3279
|
-
if (ts.isShorthandPropertyAssignment(node) &&
|
|
3308
|
+
if (ts.isShorthandPropertyAssignment(node) && localName(node.name)) {
|
|
3309
|
+
const name = factory.createIdentifier(localName(node.name))
|
|
3310
|
+
return factory.createPropertyAssignment(cloneAst(node.name, factory, context), node.objectAssignmentInitializer ? factory.createAssignment(name, visit(node.objectAssignmentInitializer, shadowed)) : name)
|
|
3311
|
+
}
|
|
3312
|
+
if (ts.isBindingElement(node) && !node.propertyName && !node.dotDotDotToken && ts.isIdentifier(node.name) && localName(node.name) && ts.isObjectBindingPattern(node.parent)) {
|
|
3313
|
+
return factory.updateBindingElement(node, node.dotDotDotToken, cloneAst(node.name, factory, context), factory.createIdentifier(localName(node.name)), node.initializer && visit(node.initializer, shadowed))
|
|
3314
|
+
}
|
|
3315
|
+
if (ts.isIdentifier(node) && localName(node)) return factory.createIdentifier(localName(node))
|
|
3316
|
+
if (ts.isShorthandPropertyAssignment(node) && substitutions.has(node.name.text) && !shadowed.has(node.name.text) && (!isFunctionLike(root) || !isShadowedIdentifier(node.name, root))) {
|
|
3280
3317
|
return factory.createPropertyAssignment(cloneAst(node.name, factory, context), cloneAst(substitutions.get(node.name.text), factory, context))
|
|
3281
3318
|
}
|
|
3282
|
-
if (ts.isIdentifier(node) && substitutions.has(node.text) && !shadowed.has(node.text) && isReferenceIdentifier(node) && !isJsxSyntaxIdentifier(node)) {
|
|
3319
|
+
if (ts.isIdentifier(node) && substitutions.has(node.text) && !shadowed.has(node.text) && (!isFunctionLike(root) || !isShadowedIdentifier(node, root)) && isReferenceIdentifier(node) && !isJsxSyntaxIdentifier(node)) {
|
|
3283
3320
|
return cloneAst(substitutions.get(node.text), factory, context)
|
|
3284
3321
|
}
|
|
3285
3322
|
const nextShadowed = isFunctionLike(node)
|