pull/15820/head
Rich Harris 4 months ago
parent 0dcd3cd2c1
commit 7341f403af

@ -118,10 +118,7 @@ export function CallExpression(node, context) {
const valid = const valid =
is_variable_declaration(parent, context) || is_variable_declaration(parent, context) ||
is_class_property_definition(parent) || is_class_property_definition(parent) ||
context.state.class?.is_class_property_assignment_at_constructor_root( is_class_property_assignment_at_constructor_root(parent, context);
parent,
context.path.slice(0, -1)
);
if (!valid) { if (!valid) {
e.state_invalid_placement(node, rune); e.state_invalid_placement(node, rune);
@ -292,3 +289,32 @@ function is_variable_declaration(parent, context) {
function is_class_property_definition(parent) { function is_class_property_definition(parent) {
return parent.type === 'PropertyDefinition' && !parent.static && !parent.computed; return parent.type === 'PropertyDefinition' && !parent.static && !parent.computed;
} }
/**
* @param {AST.SvelteNode} node
* @param {Context} context
* @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' | 'PrivateIdentifier' | 'Literal' } } }}
*/
function is_class_property_assignment_at_constructor_root(node, context) {
if (
!(
node.type === 'AssignmentExpression' &&
node.operator === '=' &&
node.left.type === 'MemberExpression' &&
node.left.object.type === 'ThisExpression' &&
((node.left.property.type === 'Identifier' && !node.left.computed) ||
node.left.property.type === 'PrivateIdentifier' ||
node.left.property.type === 'Literal')
)
) {
return false;
}
// AssignmentExpression (here) -> ExpressionStatement (-1) -> BlockStatement (-2) -> FunctionExpression (-3) -> MethodDefinition (-4)
const maybe_constructor = get_parent(context.path, -5);
return (
maybe_constructor &&
maybe_constructor.type === 'MethodDefinition' &&
maybe_constructor.kind === 'constructor'
);
}

@ -1,8 +1,6 @@
/** @import { AssignmentExpression, ClassBody, PropertyDefinition, Expression, Identifier, PrivateIdentifier, Literal, MethodDefinition } from 'estree' */ /** @import { AssignmentExpression, ClassBody, PropertyDefinition, Expression, PrivateIdentifier, MethodDefinition } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */ /** @import { Context } from '../types' */
/** @import { StateCreationRuneName } from '../../../../utils.js' */ /** @import { StateCreationRuneName } from '../../../../utils.js' */
import { get_parent } from '../../../utils/ast.js';
import { get_rune } from '../../scope.js'; import { get_rune } from '../../scope.js';
import * as e from '../../../errors.js'; import * as e from '../../../errors.js';
import { is_state_creation_rune } from '../../../../utils.js'; import { is_state_creation_rune } from '../../../../utils.js';
@ -102,33 +100,4 @@ function get_name(node) {
class ClassAnalysis { class ClassAnalysis {
/** @type {Record<string, PropertyAssignmentDetails>} */ /** @type {Record<string, PropertyAssignmentDetails>} */
fields = {}; fields = {};
/**
* @template {AST.SvelteNode} T
* @param {AST.SvelteNode} node
* @param {T[]} path
* @returns {node is AssignmentExpression & { left: { type: 'MemberExpression' } & { object: { type: 'ThisExpression' }; property: { type: 'Identifier' | 'PrivateIdentifier' | 'Literal' } } }}
*/
is_class_property_assignment_at_constructor_root(node, path) {
if (
!(
node.type === 'AssignmentExpression' &&
node.operator === '=' &&
node.left.type === 'MemberExpression' &&
node.left.object.type === 'ThisExpression' &&
((node.left.property.type === 'Identifier' && !node.left.computed) ||
node.left.property.type === 'PrivateIdentifier' ||
node.left.property.type === 'Literal')
)
) {
return false;
}
// AssignmentExpression (here) -> ExpressionStatement (-1) -> BlockStatement (-2) -> FunctionExpression (-3) -> MethodDefinition (-4)
const maybe_constructor = get_parent(path, -4);
return (
maybe_constructor &&
maybe_constructor.type === 'MethodDefinition' &&
maybe_constructor.kind === 'constructor'
);
}
} }

Loading…
Cancel
Save