deconflict variable for action and each block

pull/5839/head
Tan Li Hau 6 years ago
parent 2eda5b0bf3
commit 200d452d05

@ -14,6 +14,7 @@ import { Node, FunctionExpression, Identifier } from 'estree';
import { INode } from '../interfaces';
import { is_reserved_keyword } from '../../utils/reserved_keywords';
import replace_object from '../../utils/replace_object';
import is_contextual from './is_contextual';
import EachBlock from '../EachBlock';
type Owner = INode;
@ -409,18 +410,3 @@ function get_function_name(_node, parent) {
return 'func';
}
function is_contextual(component: Component, scope: TemplateScope, name: string) {
if (is_reserved_keyword(name)) return true;
// if it's a name below root scope, it's contextual
if (!scope.is_top_level(name)) return true;
const variable = component.var_lookup.get(name);
// hoistables, module declarations, and imports are non-contextual
if (!variable || variable.hoistable) return false;
// assume contextual
return true;
}

@ -0,0 +1,18 @@
import Component from '../../Component';
import TemplateScope from './TemplateScope';
import { is_reserved_keyword } from '../../utils/reserved_keywords';
export default function is_contextual(component: Component, scope: TemplateScope, name: string) {
if (is_reserved_keyword(name)) return true;
// if it's a name below root scope, it's contextual
if (!scope.is_top_level(name)) return true;
const variable = component.var_lookup.get(name);
// hoistables, module declarations, and imports are non-contextual
if (!variable || variable.hoistable) return false;
// assume contextual
return true;
}

@ -1,6 +1,7 @@
import { b, x } from 'code-red';
import Block from '../../Block';
import Action from '../../../nodes/Action';
import is_contextual from '../../../nodes/shared/is_contextual';
export default function add_actions(
block: Block,
@ -28,7 +29,9 @@ export function add_action(block: Block, target: string, action: Action) {
const [obj, ...properties] = action.name.split('.');
const fn = block.renderer.reference(obj);
const fn = is_contextual(action.component, action.expression.template_scope, obj)
? block.renderer.reference(obj)
: obj;
if (properties.length) {
const member_expression = properties.reduce((lhs, rhs) => x`${lhs}.${rhs}`, fn);

@ -0,0 +1,13 @@
let result;
export default {
before_test() {
result = [];
},
props: {
collect: (str) => result.push(str)
},
test({ assert }) {
assert.deepEqual(result, ['each_action', 'import_action']);
}
};

@ -0,0 +1,17 @@
<script>
import action from './util';
export let collect;
function each_action(_, fn) {
fn('each_action');
}
const array = [each_action];
</script>
<div use:action={collect} />
<ul>
{#each array as action}
<div use:action={collect} />
{/each}
</ul>

@ -0,0 +1,3 @@
export default function (_, fn) {
fn('import_action');
}
Loading…
Cancel
Save