fix/simplify

pull/12745/head
Rich Harris 2 years ago
parent a6cdb7ad77
commit fe7676d2e8

@ -7,7 +7,7 @@ import * as b from '../../../../utils/builders.js';
import { binding_properties } from '../../../bindings.js';
import { build_setter } from '../utils.js';
import { build_attribute_value } from './shared/element.js';
import { build_bind_this, build_validate_binding } from './shared/utils.js';
import { build_bind_this, validate_binding } from './shared/utils.js';
/**
* @param {BindDirective} node
@ -30,12 +30,10 @@ export function BindDirective(node, context) {
)) &&
!is_ignored(node, 'binding_property_non_reactive')
) {
context.state.init.push(
build_validate_binding(
context.state,
node,
/**@type {MemberExpression} */ (context.visit(expression))
)
validate_binding(
context.state,
node,
/**@type {MemberExpression} */ (context.visit(expression))
);
}

@ -6,7 +6,7 @@ import { get_attribute_chunks } from '../../../../../utils/ast.js';
import * as b from '../../../../../utils/builders.js';
import { is_element_node } from '../../../../nodes.js';
import { create_derived, build_setter } from '../../utils.js';
import { build_bind_this, build_validate_binding } from '../shared/utils.js';
import { build_bind_this, validate_binding } from '../shared/utils.js';
import { build_attribute_value } from '../shared/element.js';
import { build_event_handler } from './events.js';
@ -151,7 +151,7 @@ export function build_component(node, component_name, context, anchor = context.
context.state.analysis.runes &&
!is_ignored(node, 'binding_property_non_reactive')
) {
context.state.init.push(build_validate_binding(context.state, attribute, expression));
validate_binding(context.state, attribute, expression);
}
if (attribute.name === 'this') {

@ -204,40 +204,30 @@ export function build_bind_this(expression, value, { state, visit }) {
* @param {BindDirective} binding
* @param {MemberExpression} expression
*/
export function build_validate_binding(state, binding, expression) {
const string = state.analysis.source.slice(binding.start, binding.end);
export function validate_binding(state, binding, expression) {
// If we are referencing a $store.foo then we don't need to add validation
if (
binding.expression.type === 'MemberExpression' &&
binding.expression.object.type === 'Identifier'
) {
var ref_binding = state.scope.get(binding.expression.object.name);
if (ref_binding?.kind === 'store_sub') {
return b.empty;
}
}
const get_object = b.thunk(/** @type {Expression} */ (expression.object));
const get_property = b.thunk(
/** @type {Expression} */ (
expression.computed
? expression.property
: b.literal(/** @type {Identifier} */ (expression.property).name)
)
);
const left = object(binding.expression);
const left_binding = left && state.scope.get(left.name);
if (left_binding?.kind === 'store_sub') return;
const loc = locator(binding.start);
return b.stmt(
b.call(
'$.validate_binding',
b.literal(string),
get_object,
get_property,
loc && b.literal(loc.line),
loc && b.literal(loc.column)
state.init.push(
b.stmt(
b.call(
'$.validate_binding',
b.literal(state.analysis.source.slice(binding.start, binding.end)),
b.thunk(/** @type {Expression} */ (expression.object)),
b.thunk(
/** @type {Expression} */ (
expression.computed
? expression.property
: b.literal(/** @type {Identifier} */ (expression.property).name)
)
),
loc && b.literal(loc.line),
loc && b.literal(loc.column)
)
)
);
}

Loading…
Cancel
Save