From 03e83b2323be6897a51e2e085854b584eb1e1c83 Mon Sep 17 00:00:00 2001 From: imteekay Date: Sat, 3 Jun 2023 17:39:19 -0300 Subject: [PATCH] Allow redeclare variables defined with `var` --- baselines/reference/redeclare.errors.baseline | 7 +---- baselines/reference/redeclare.tree.baseline | 4 +++ src/bind.ts | 29 ++++++++++++++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/baselines/reference/redeclare.errors.baseline b/baselines/reference/redeclare.errors.baseline index de1418f..0637a08 100644 --- a/baselines/reference/redeclare.errors.baseline +++ b/baselines/reference/redeclare.errors.baseline @@ -1,6 +1 @@ -[ - { - "pos": 14, - "message": "Cannot redeclare x; first declared at 3" - } -] \ No newline at end of file +[] \ No newline at end of file diff --git a/baselines/reference/redeclare.tree.baseline b/baselines/reference/redeclare.tree.baseline index 6d5f7f3..6b11892 100644 --- a/baselines/reference/redeclare.tree.baseline +++ b/baselines/reference/redeclare.tree.baseline @@ -4,6 +4,10 @@ { "kind": "Var", "pos": 3 + }, + { + "kind": "Var", + "pos": 14 } ] }, diff --git a/src/bind.ts b/src/bind.ts index dccdb0d..8231d14 100644 --- a/src/bind.ts +++ b/src/bind.ts @@ -1,4 +1,4 @@ -import { Module, Node, Statement, Table } from './types'; +import { Declaration, Module, Node, Statement, Table } from './types'; import { error } from './error'; export function bind(m: Module) { @@ -16,9 +16,9 @@ export function bind(m: Module) { if (symbol) { const other = symbol.declarations.find( (d) => - d.kind === statement.kind || - (d.kind === Node.Var && statement.kind === Node.Let) || - (d.kind === Node.Let && statement.kind === Node.Var), + hasEqualKindButNotVar(d, statement) || + willRedeclareVarWithLet(d, statement) || + willRedeclareLetWithVar(d, statement), ); if (other) { error( @@ -41,6 +41,27 @@ export function bind(m: Module) { } } } + + function hasEqualKindButNotVar( + declaration: Declaration, + statement: Statement, + ) { + return declaration.kind === statement.kind && statement.kind !== Node.Var; + } + + function willRedeclareVarWithLet( + declaration: Declaration, + statement: Statement, + ) { + return declaration.kind === Node.Var && statement.kind === Node.Let; + } + + function willRedeclareLetWithVar( + declaration: Declaration, + statement: Statement, + ) { + return declaration.kind === Node.Let && statement.kind === Node.Var; + } } export function resolve(