Fix issue where computed_prop_# collides due to const tags

pull/8418/head
Nguyen Tran 4 years ago
parent 235d019ee0
commit 791dcf0e4d

@ -11,7 +11,7 @@ export type Context = DestructuredVariable | ComputedProperty;
interface ComputedProperty { interface ComputedProperty {
type: 'ComputedProperty'; type: 'ComputedProperty';
property_name: string; property_name: Identifier;
key: Expression | PrivateIdentifier; key: Expression | PrivateIdentifier;
} }
@ -30,8 +30,7 @@ export function unpack_destructuring({
default_modifier = (node) => node, default_modifier = (node) => node,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props = { n: 0 }
}: { }: {
contexts: Context[]; contexts: Context[];
node: Node; node: Node;
@ -40,10 +39,6 @@ export function unpack_destructuring({
scope: TemplateScope; scope: TemplateScope;
component: Component; component: Component;
context_rest_properties: Map<string, Node>; context_rest_properties: Map<string, Node>;
// we want to pass this by reference, as a sort of global variable, because
// if we pass this by value, we could get computed_property_# variable collisions
// when we deal with nested object destructuring
number_of_computed_props?: { n: number };
}) { }) {
if (!node) return; if (!node) return;
@ -72,8 +67,7 @@ export function unpack_destructuring({
default_modifier, default_modifier,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props
}); });
context_rest_properties.set((element.argument as Identifier).name, element); context_rest_properties.set((element.argument as Identifier).name, element);
} else if (element && element.type === 'AssignmentPattern') { } else if (element && element.type === 'AssignmentPattern') {
@ -93,8 +87,7 @@ export function unpack_destructuring({
)}` as Node, )}` as Node,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props
}); });
} else { } else {
unpack_destructuring({ unpack_destructuring({
@ -104,8 +97,7 @@ export function unpack_destructuring({
default_modifier, default_modifier,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props
}); });
} }
}); });
@ -124,8 +116,7 @@ export function unpack_destructuring({
default_modifier, default_modifier,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props
}); });
context_rest_properties.set((property.argument as Identifier).name, property); context_rest_properties.set((property.argument as Identifier).name, property);
} else if (property.type === 'Property') { } else if (property.type === 'Property') {
@ -136,8 +127,7 @@ export function unpack_destructuring({
if (property.computed) { if (property.computed) {
// e.g { [computedProperty]: ... } // e.g { [computedProperty]: ... }
const property_name = `computed_property_${number_of_computed_props.n}`; const property_name = x`computed_property` as Identifier;
number_of_computed_props.n += 1;
contexts.push({ contexts.push({
type: 'ComputedProperty', type: 'ComputedProperty',
@ -178,8 +168,7 @@ export function unpack_destructuring({
)}` as Node, )}` as Node,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props
}); });
} else { } else {
// e.g. { property } or { property: newName } // e.g. { property } or { property: newName }
@ -190,8 +179,7 @@ export function unpack_destructuring({
default_modifier, default_modifier,
scope, scope,
component, component,
context_rest_properties, context_rest_properties
number_of_computed_props
}); });
} }
} }

@ -12,6 +12,7 @@ import { Context } from '../../nodes/shared/Context';
import { Identifier, Literal, Node } from 'estree'; import { Identifier, Literal, Node } from 'estree';
import { add_const_tags, add_const_tags_context } from './shared/add_const_tags'; import { add_const_tags, add_const_tags_context } from './shared/add_const_tags';
import Expression from '../../nodes/shared/Expression'; import Expression from '../../nodes/shared/Expression';
import { resolve_computed_prop_conflicts } from '../../utils/resolve_computed_props';
type Status = 'pending' | 'then' | 'catch'; type Status = 'pending' | 'then' | 'catch';
@ -98,6 +99,10 @@ class AwaitBlockBranch extends Wrapper {
} }
render_get_context() { render_get_context() {
if (this.has_consts(this.node)) {
resolve_computed_prop_conflicts(this.block, this.value_contexts, this.node.const_tags);
}
const props = this.is_destructured ? this.value_contexts.map(prop => { const props = this.is_destructured ? this.value_contexts.map(prop => {
if (prop.type === 'ComputedProperty') { if (prop.type === 'ComputedProperty') {
const expression = new Expression(this.renderer.component, this.node, this.has_consts(this.node) ? this.node.scope : null, prop.key); const expression = new Expression(this.renderer.component, this.node, this.has_consts(this.node) ? this.node.scope : null, prop.key);

@ -10,6 +10,7 @@ import { Identifier, Node } from 'estree';
import get_object from '../../utils/get_object'; import get_object from '../../utils/get_object';
import { add_const_tags, add_const_tags_context } from './shared/add_const_tags'; import { add_const_tags, add_const_tags_context } from './shared/add_const_tags';
import Expression from '../../nodes/shared/Expression'; import Expression from '../../nodes/shared/Expression';
import { resolve_computed_prop_conflicts } from '../../utils/resolve_computed_props';
export class ElseBlockWrapper extends Wrapper { export class ElseBlockWrapper extends Wrapper {
node: ElseBlock; node: ElseBlock;
@ -364,6 +365,8 @@ export default class EachBlockWrapper extends Wrapper {
this.else.fragment.render(this.else.block, null, x`#nodes` as Identifier); this.else.fragment.render(this.else.block, null, x`#nodes` as Identifier);
} }
resolve_computed_prop_conflicts(this.block, this.node.contexts, this.node.const_tags);
this.context_props = this.node.contexts.map(prop => { this.context_props = this.node.contexts.map(prop => {
if (prop.type === 'DestructuredVariable') { if (prop.type === 'DestructuredVariable') {
const to_ctx = (name: string) => renderer.context_lookup.has(name) ? x`child_ctx[${renderer.context_lookup.get(name).index}]` : { type: 'Identifier', name } as Node; const to_ctx = (name: string) => renderer.context_lookup.has(name) ? x`child_ctx[${renderer.context_lookup.get(name).index}]` : { type: 'Identifier', name } as Node;

@ -0,0 +1,19 @@
import { Context } from '../nodes/shared/Context';
import ConstTag from '../nodes/ConstTag';
import Block from '../render_dom/Block';
export function resolve_computed_prop_conflicts(block: Block, node_contexts: Context[], node_const_tags: ConstTag[]) {
node_contexts.forEach((context: Context) => {
if (context.type === 'ComputedProperty') {
context.property_name.name = block.get_unique_name('computed_prop').name;
}
});
node_const_tags.forEach((const_tag: ConstTag) => {
const_tag.contexts.forEach((context: Context) => {
if (context.type === 'ComputedProperty') {
context.property_name.name = block.get_unique_name('computed_prop').name;
}
});
});
}
Loading…
Cancel
Save