fix for contextual bindings

pull/1864/head
Rich Harris 7 years ago
parent 0fe16c8ecd
commit ac1be5def2

@ -6,7 +6,7 @@ import Block from '../../Block';
import Node from '../../../nodes/shared/Node'; import Node from '../../../nodes/shared/Node';
import Renderer from '../../Renderer'; import Renderer from '../../Renderer';
import flattenReference from '../../../../utils/flattenReference'; import flattenReference from '../../../../utils/flattenReference';
import getTailSnippet from '../../../../utils/getTailSnippet'; import { get_tail } from '../../../../utils/get_tail_snippet';
// TODO this should live in a specific binding // TODO this should live in a specific binding
const readOnlyMediaAttributes = new Set([ const readOnlyMediaAttributes = new Set([
@ -99,7 +99,7 @@ export default class BindingWrapper {
// view to model // view to model
const valueFromDom = getValueFromDom(renderer, this.parent, this); const valueFromDom = getValueFromDom(renderer, this.parent, this);
const handler = getEventHandler(this, block, name, contextless_snippet, valueFromDom); const handler = getEventHandler(this, renderer, block, name, contextless_snippet, valueFromDom);
// model to view // model to view
let updateDom = getDomUpdater(parent, this, snippet); let updateDom = getDomUpdater(parent, this, snippet);
@ -161,7 +161,6 @@ export default class BindingWrapper {
needsLock: !isReadOnly && needsLock, needsLock: !isReadOnly && needsLock,
updateCondition: updateConditions.length ? updateConditions.join(' && ') : undefined, updateCondition: updateConditions.length ? updateConditions.join(' && ') : undefined,
isReadOnlyMediaAttribute: this.isReadOnlyMediaAttribute(), isReadOnlyMediaAttribute: this.isReadOnlyMediaAttribute(),
// dependencies: this.node.expression.dependencies,
dependencies, dependencies,
contextual_dependencies: this.node.expression.contextual_dependencies contextual_dependencies: this.node.expression.contextual_dependencies
}; };
@ -215,15 +214,18 @@ function getBindingGroup(renderer: Renderer, value: Node) {
function getEventHandler( function getEventHandler(
binding: BindingWrapper, binding: BindingWrapper,
renderer: Renderer,
block: Block, block: Block,
name: string, name: string,
snippet: string, snippet: string,
value: string value: string
) { ) {
if (binding.node.isContextual) { if (binding.node.isContextual) {
const tail = binding.node.expression.node.type === 'MemberExpression' let tail = '';
? getTailSnippet(binding.node.expression.node) if (binding.node.expression.node.type === 'MemberExpression') {
: ''; const { start, end } = get_tail(binding.node.expression.node);
tail = renderer.component.source.slice(start, end);
}
const { object, property, snippet } = block.bindings.get(name)(); const { object, property, snippet } = block.bindings.get(name)();

@ -9,10 +9,8 @@ import stringifyProps from '../../../../utils/stringifyProps';
import addToSet from '../../../../utils/addToSet'; import addToSet from '../../../../utils/addToSet';
import deindent from '../../../../utils/deindent'; import deindent from '../../../../utils/deindent';
import Attribute from '../../../nodes/Attribute'; import Attribute from '../../../nodes/Attribute';
import CodeBuilder from '../../../../utils/CodeBuilder';
import getObject from '../../../../utils/getObject'; import getObject from '../../../../utils/getObject';
import Binding from '../../../nodes/Binding'; import Binding from '../../../nodes/Binding';
import getTailSnippet from '../../../../utils/getTailSnippet';
export default class InlineComponentWrapper extends Wrapper { export default class InlineComponentWrapper extends Wrapper {
var: string; var: string;

@ -1,6 +1,6 @@
import { escape, escapeTemplate, stringify } from '../../../utils/stringify'; import { escape, escapeTemplate, stringify } from '../../../utils/stringify';
import getObject from '../../../utils/getObject'; import getObject from '../../../utils/getObject';
import getTailSnippet from '../../../utils/getTailSnippet'; import { get_tail_snippet } from '../../../utils/get_tail_snippet';
import { quoteNameIfNecessary, quotePropIfNecessary } from '../../../utils/quoteIfNecessary'; import { quoteNameIfNecessary, quotePropIfNecessary } from '../../../utils/quoteIfNecessary';
import deindent from '../../../utils/deindent'; import deindent from '../../../utils/deindent';
@ -18,7 +18,7 @@ export default function(node, renderer, options) {
const bindingProps = node.bindings.map(binding => { const bindingProps = node.bindings.map(binding => {
const { name } = getObject(binding.value.node); const { name } = getObject(binding.value.node);
const tail = binding.value.node.type === 'MemberExpression' const tail = binding.value.node.type === 'MemberExpression'
? getTailSnippet(binding.value.node) ? get_tail_snippet(binding.value.node)
: ''; : '';
return `${quoteNameIfNecessary(binding.name)}: ctx${quotePropIfNecessary(name)}${tail}`; return `${quoteNameIfNecessary(binding.name)}: ctx${quotePropIfNecessary(name)}${tail}`;

@ -1,9 +1,12 @@
import { Node } from '../interfaces'; import { Node } from '../interfaces';
export default function getTailSnippet(node: Node) { export function get_tail_snippet(node: Node) {
const end = node.end; const { start, end } = get_tail(node);
while (node.type === 'MemberExpression') node = node.object;
const start = node.end;
return `[✂${start}-${end}✂]`; return `[✂${start}-${end}✂]`;
} }
export function get_tail(node: Node) {
const end = node.end;
while (node.type === 'MemberExpression') node = node.object;
return { start: node.end, end };
}
Loading…
Cancel
Save