Skip to content

Commit 691d7ce

Browse files
committed
deconflict variable for action and each block
1 parent e7edd7c commit 691d7ce

File tree

6 files changed

+56
-16
lines changed

6 files changed

+56
-16
lines changed

src/compiler/compile/nodes/shared/Expression.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Node, FunctionExpression, Identifier } from 'estree';
1414
import { INode } from '../interfaces';
1515
import { is_reserved_keyword } from '../../utils/reserved_keywords';
1616
import replace_object from '../../utils/replace_object';
17+
import is_contextual from './is_contextual';
1718
import EachBlock from '../EachBlock';
1819

1920
type Owner = INode;
@@ -409,18 +410,3 @@ function get_function_name(_node, parent) {
409410

410411
return 'func';
411412
}
412-
413-
function is_contextual(component: Component, scope: TemplateScope, name: string) {
414-
if (is_reserved_keyword(name)) return true;
415-
416-
// if it's a name below root scope, it's contextual
417-
if (!scope.is_top_level(name)) return true;
418-
419-
const variable = component.var_lookup.get(name);
420-
421-
// hoistables, module declarations, and imports are non-contextual
422-
if (!variable || variable.hoistable) return false;
423-
424-
// assume contextual
425-
return true;
426-
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Component from '../../Component';
2+
import TemplateScope from './TemplateScope';
3+
import { is_reserved_keyword } from '../../utils/reserved_keywords';
4+
5+
export default function is_contextual(component: Component, scope: TemplateScope, name: string) {
6+
if (is_reserved_keyword(name)) return true;
7+
8+
// if it's a name below root scope, it's contextual
9+
if (!scope.is_top_level(name)) return true;
10+
11+
const variable = component.var_lookup.get(name);
12+
13+
// hoistables, module declarations, and imports are non-contextual
14+
if (!variable || variable.hoistable) return false;
15+
16+
// assume contextual
17+
return true;
18+
}

src/compiler/compile/render_dom/wrappers/shared/add_actions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { b, x } from 'code-red';
22
import Block from '../../Block';
33
import Action from '../../../nodes/Action';
4+
import is_contextual from '../../../nodes/shared/is_contextual';
45

56
export default function add_actions(
67
block: Block,
@@ -28,7 +29,9 @@ export function add_action(block: Block, target: string, action: Action) {
2829

2930
const [obj, ...properties] = action.name.split('.');
3031

31-
const fn = block.renderer.reference(obj);
32+
const fn = is_contextual(action.component, action.expression.template_scope, obj)
33+
? block.renderer.reference(obj)
34+
: obj;
3235

3336
if (properties.length) {
3437
block.event_listeners.push(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let result;
2+
3+
export default {
4+
before_test() {
5+
result = [];
6+
},
7+
props: {
8+
collect: (str) => result.push(str)
9+
},
10+
test({ assert }) {
11+
assert.deepEqual(result, ['each_action', 'import_action']);
12+
}
13+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script>
2+
import action from './util';
3+
export let collect;
4+
5+
function each_action(_, fn) {
6+
fn('each_action');
7+
}
8+
const array = [each_action];
9+
</script>
10+
11+
<div use:action={collect} />
12+
13+
<ul>
14+
{#each array as action}
15+
<div use:action={collect} />
16+
{/each}
17+
</ul>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function (_, fn) {
2+
fn('import_action');
3+
}

0 commit comments

Comments
 (0)