diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts
index 0347a28743..ed71357bb0 100644
--- a/src/compiler/compile/Component.ts
+++ b/src/compiler/compile/Component.ts
@@ -191,27 +191,33 @@ export default class Component {
this.stylesheet.warn_on_unused_selectors(this);
}
- add_var(variable: Var, add_to_lookup = true) {
+ add_var(node: Node, variable: Var, add_to_lookup = true) {
this.vars.push(variable);
if (add_to_lookup) {
+ if (this.var_lookup.has(variable.name)) {
+ const existsVar = this.var_lookup.get(variable.name);
+ if (existsVar.module && existsVar.imported) {
+ this.error(node as any, compiler_errors.illegal_variable_declaration);
+ }
+ }
this.var_lookup.set(variable.name, variable);
}
}
- add_reference(name: string) {
+ add_reference(node: Node, name: string) {
const variable = this.var_lookup.get(name);
if (variable) {
variable.referenced = true;
} else if (is_reserved_keyword(name)) {
- this.add_var({
+ this.add_var(node, {
name,
injected: true,
referenced: true
});
} else if (name[0] === '$') {
- this.add_var({
+ this.add_var(node, {
name,
injected: true,
referenced: true,
@@ -228,7 +234,7 @@ export default class Component {
}
} else {
if (this.compile_options.varsReport === 'full') {
- this.add_var({ name, referenced: true }, false);
+ this.add_var(node, { name, referenced: true }, false);
}
this.used_names.add(name);
@@ -599,12 +605,14 @@ export default class Component {
}
const writable = node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
+ const imported = node.type.startsWith('Import');
- this.add_var({
+ this.add_var(node, {
name,
module: true,
hoistable: true,
- writable
+ writable,
+ imported
});
});
@@ -612,7 +620,7 @@ export default class Component {
if (name[0] === '$') {
return this.error(node as any, compiler_errors.illegal_subscription);
} else {
- this.add_var({
+ this.add_var(node, {
name,
global: true,
hoistable: true
@@ -674,7 +682,7 @@ export default class Component {
const writable = node.type === 'VariableDeclaration' && (node.kind === 'var' || node.kind === 'let');
const imported = node.type.startsWith('Import');
- this.add_var({
+ this.add_var(node, {
name,
initialised: instance_scope.initialised_declarations.has(name),
writable,
@@ -697,7 +705,7 @@ export default class Component {
const node = globals.get(name);
if (this.injected_reactive_declaration_vars.has(name)) {
- this.add_var({
+ this.add_var(node, {
name,
injected: true,
writable: true,
@@ -705,7 +713,7 @@ export default class Component {
initialised: true
});
} else if (is_reserved_keyword(name)) {
- this.add_var({
+ this.add_var(node, {
name,
injected: true
});
@@ -714,14 +722,14 @@ export default class Component {
return this.error(node as any, compiler_errors.illegal_global(name));
}
- this.add_var({
+ this.add_var(node, {
name,
injected: true,
mutated: true,
writable: true
});
- this.add_reference(name.slice(1));
+ this.add_reference(node, name.slice(1));
const variable = this.var_lookup.get(name.slice(1));
if (variable) {
@@ -729,7 +737,7 @@ export default class Component {
variable.referenced_from_script = true;
}
} else {
- this.add_var({
+ this.add_var(node, {
name,
global: true,
hoistable: true
diff --git a/src/compiler/compile/compiler_errors.ts b/src/compiler/compile/compiler_errors.ts
index f6e9d17ea3..c9282189f1 100644
--- a/src/compiler/compile/compiler_errors.ts
+++ b/src/compiler/compile/compiler_errors.ts
@@ -190,6 +190,10 @@ export default {
code: 'illegal-global',
message: `${name} is an illegal variable name`
}),
+ illegal_variable_declaration: {
+ code: 'illegal-variable-declaration',
+ message: 'Cannot declare same variable name which is imported inside
+
+
+
+{FOO}