From 0bf519933d24669fea6402523e2a1b881ab8be97 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Thu, 30 Nov 2023 06:10:08 +0100 Subject: [PATCH] Allow to redeclare parameters multiple times in nested scopes (#5276) --- src/ast/scopes/BlockScope.ts | 5 ++++- .../redeclare-var-parameter-nested/_config.js | 3 +++ .../redeclare-var-parameter-nested/main.js | 21 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/function/samples/ast-validations/redeclare-var-parameter-nested/_config.js create mode 100644 test/function/samples/ast-validations/redeclare-var-parameter-nested/main.js diff --git a/src/ast/scopes/BlockScope.ts b/src/ast/scopes/BlockScope.ts index c4146818f..ae5701482 100644 --- a/src/ast/scopes/BlockScope.ts +++ b/src/ast/scopes/BlockScope.ts @@ -22,7 +22,10 @@ export default class BlockScope extends ChildScope { const existingVariable = this.hoistedVariables?.get(name) || (this.variables.get(name) as LocalVariable | undefined); if (existingVariable) { - if (existingVariable.kind === VariableKind.var) { + if ( + existingVariable.kind === VariableKind.var || + (kind === VariableKind.var && existingVariable.kind === VariableKind.parameter) + ) { existingVariable.addDeclaration(identifier, init); return existingVariable; } diff --git a/test/function/samples/ast-validations/redeclare-var-parameter-nested/_config.js b/test/function/samples/ast-validations/redeclare-var-parameter-nested/_config.js new file mode 100644 index 000000000..36a2b6a82 --- /dev/null +++ b/test/function/samples/ast-validations/redeclare-var-parameter-nested/_config.js @@ -0,0 +1,3 @@ +module.exports = defineTest({ + description: 'allows to redeclare parameters with multiple nested vars' +}); diff --git a/test/function/samples/ast-validations/redeclare-var-parameter-nested/main.js b/test/function/samples/ast-validations/redeclare-var-parameter-nested/main.js new file mode 100644 index 000000000..4973c455d --- /dev/null +++ b/test/function/samples/ast-validations/redeclare-var-parameter-nested/main.js @@ -0,0 +1,21 @@ +export function f(val) { + { + var val = 1; + var val = 2; + } + assert.equal(val, 2); +} +f(0); + +export function g(val) { + { + { + var val = 1; + } + { + var val = 2; + } + } + assert.equal(val, 2); +} +g(0);