mirror of https://github.com/sveltejs/svelte
parent
c8ef540985
commit
2edb05172a
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': minor
|
||||
---
|
||||
|
||||
feat: allow `$state` in computed class fields
|
||||
@ -0,0 +1,32 @@
|
||||
/** @import { ClassBody, ClassDeclaration, Expression, VariableDeclaration } from 'estree' */
|
||||
/** @import { ClientTransformState, Context } from '../types' */
|
||||
import * as b from '#compiler/builders';
|
||||
|
||||
/**
|
||||
* @param {ClassDeclaration} node
|
||||
* @param {Context} context
|
||||
*/
|
||||
export function ClassDeclaration(node, context) {
|
||||
/** @type {ClientTransformState & { computed_field_declarations: VariableDeclaration[] }} */
|
||||
const state = {
|
||||
...context.state,
|
||||
computed_field_declarations: []
|
||||
};
|
||||
const super_class = node.superClass
|
||||
? /** @type {Expression} */ (context.visit(node.superClass))
|
||||
: null;
|
||||
const body = /** @type {ClassBody} */ (context.visit(node.body, state));
|
||||
if (state.computed_field_declarations.length > 0) {
|
||||
const init = b.call(
|
||||
b.arrow(
|
||||
[],
|
||||
b.block([
|
||||
...state.computed_field_declarations,
|
||||
b.return(b.class(node.id, body, super_class))
|
||||
])
|
||||
)
|
||||
);
|
||||
return node.id ? b.var(node.id, init) : init;
|
||||
}
|
||||
return b.class_declaration(node.id, body, super_class);
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
/** @import { ClassBody, ClassExpression, Expression, VariableDeclaration } from 'estree' */
|
||||
/** @import { ClientTransformState, Context } from '../types' */
|
||||
import * as b from '#compiler/builders';
|
||||
|
||||
/**
|
||||
* @param {ClassExpression} node
|
||||
* @param {Context} context
|
||||
*/
|
||||
export function ClassExpression(node, context) {
|
||||
/** @type {ClientTransformState & { computed_field_declarations: VariableDeclaration[] }} */
|
||||
const state = {
|
||||
...context.state,
|
||||
computed_field_declarations: []
|
||||
};
|
||||
const super_class = node.superClass
|
||||
? /** @type {Expression} */ (context.visit(node.superClass))
|
||||
: null;
|
||||
const body = /** @type {ClassBody} */ (context.visit(node.body, state));
|
||||
if (state.computed_field_declarations.length > 0) {
|
||||
return b.call(
|
||||
b.arrow(
|
||||
[],
|
||||
b.block([
|
||||
...state.computed_field_declarations,
|
||||
b.return(b.class(node.id, body, super_class))
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
return b.class(node.id, body, super_class);
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
/** @import { ClassBody, ClassDeclaration, Expression, VariableDeclaration } from 'estree' */
|
||||
/** @import { ServerTransformState, Context } from '../types' */
|
||||
import * as b from '#compiler/builders';
|
||||
|
||||
/**
|
||||
* @param {ClassDeclaration} node
|
||||
* @param {Context} context
|
||||
*/
|
||||
export function ClassDeclaration(node, context) {
|
||||
/** @type {ServerTransformState & { computed_field_declarations: VariableDeclaration[] }} */
|
||||
const state = {
|
||||
...context.state,
|
||||
computed_field_declarations: []
|
||||
};
|
||||
const super_class = node.superClass
|
||||
? /** @type {Expression} */ (context.visit(node.superClass))
|
||||
: null;
|
||||
const body = /** @type {ClassBody} */ (context.visit(node.body, state));
|
||||
if (state.computed_field_declarations.length > 0) {
|
||||
const init = b.call(
|
||||
b.arrow(
|
||||
[],
|
||||
b.block([
|
||||
...state.computed_field_declarations,
|
||||
b.return(b.class(node.id, body, super_class))
|
||||
])
|
||||
)
|
||||
);
|
||||
return node.id ? b.var(node.id, init) : init;
|
||||
}
|
||||
return b.class_declaration(node.id, body, super_class);
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
/** @import { ClassBody, ClassExpression, Expression, VariableDeclaration } from 'estree' */
|
||||
/** @import { ServerTransformState, Context } from '../types' */
|
||||
import * as b from '#compiler/builders';
|
||||
|
||||
/**
|
||||
* @param {ClassExpression} node
|
||||
* @param {Context} context
|
||||
*/
|
||||
export function ClassExpression(node, context) {
|
||||
/** @type {ServerTransformState & { computed_field_declarations: VariableDeclaration[] }} */
|
||||
const state = {
|
||||
...context.state,
|
||||
computed_field_declarations: []
|
||||
};
|
||||
const super_class = node.superClass
|
||||
? /** @type {Expression} */ (context.visit(node.superClass))
|
||||
: null;
|
||||
const body = /** @type {ClassBody} */ (context.visit(node.body, state));
|
||||
if (state.computed_field_declarations.length > 0) {
|
||||
return b.call(
|
||||
b.arrow(
|
||||
[],
|
||||
b.block([
|
||||
...state.computed_field_declarations,
|
||||
b.return(b.class(node.id, body, super_class))
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
return b.class(node.id, body, super_class);
|
||||
}
|
||||
Loading…
Reference in new issue