From 5665f711fd699d8b7d2e07524b19cab194951d11 Mon Sep 17 00:00:00 2001 From: Yuichiro Yamashita Date: Thu, 27 Jan 2022 00:16:20 +0900 Subject: [PATCH] [fix] raise compile error if variable name is same as imported variable name (#7145) --- src/compiler/compile/Component.ts | 36 +++++++++++-------- src/compiler/compile/compiler_errors.ts | 4 +++ src/compiler/compile/nodes/Action.ts | 2 +- src/compiler/compile/nodes/Animation.ts | 2 +- src/compiler/compile/nodes/InlineComponent.ts | 6 ++-- src/compiler/compile/nodes/Transition.ts | 2 +- src/compiler/compile/nodes/shared/Context.ts | 2 +- .../compile/nodes/shared/Expression.ts | 8 ++--- src/compiler/compile/render_dom/Renderer.ts | 2 +- .../illegal-variable-declaration/errors.json | 17 +++++++++ .../illegal-variable-declaration/input.svelte | 9 +++++ 11 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 test/validator/samples/illegal-variable-declaration/errors.json create mode 100644 test/validator/samples/illegal-variable-declaration/input.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 0347a28743..5f7d4b9758 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 exists_var = this.var_lookup.get(variable.name); + if (exists_var.module && exists_var.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}