Merge pull request #2003 from sveltejs/gh-2002

Make sure a hoistable name is not also shadowed when rendering and expr - #2002
pull/2007/head
Rich Harris 6 years ago committed by GitHub
commit 82f4ae6643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -240,7 +240,7 @@ export default class Expression {
dependencies.add(name);
component.template_references.add(name);
}
} else if (!is_synthetic && !component.hoistable_names.has(name) && !component.imported_declarations.has(name)) {
} else if (!is_synthetic && isContextual(component, template_scope, name)) {
code.prependRight(node.start, key === 'key' && parent.shorthand
? `${name}: ctx.`
: 'ctx.');
@ -436,4 +436,17 @@ function get_function_name(node, parent) {
}
return 'func';
}
function isContextual(component: Component, scope: TemplateScope, name: string) {
// if it's a name below root scope, it's contextual
if (!scope.isTopLevel(name)) return true;
// hoistables, module declarations, and imports are non-contextual
if (component.hoistable_names.has(name)) return false;
if (component.module_scope && component.module_scope.declarations.has(name)) return false;
if (component.imported_declarations.has(name)) return false;
// assume contextual
return true;
}

@ -40,4 +40,8 @@ export default class TemplateScope {
if (this.parent) return this.parent.containsMutable(names);
else return false;
}
isTopLevel(name: string) {
return !this.parent || !this.names.has(name) && this.parent.isTopLevel(name);
}
}

@ -0,0 +1,3 @@
export default {
html: '(alpaca)(baboon)(capybara)'
};

@ -0,0 +1,8 @@
{#each animals as animal}
({animal})
{/each}
<script>
let animal = 'lemur';
let animals = ['alpaca', 'baboon', 'capybara'];
</script>

@ -0,0 +1,3 @@
export default {
html: `<p>(42)(99)</p>`
};

@ -0,0 +1,9 @@
<script context="module">
const foo = 42;
</script>
<script>
export let bar = 99;
</script>
<p>({foo})({bar})</p>
Loading…
Cancel
Save