Skip to content

Commit

Permalink
Allow redeclare variables defined with var
Browse files Browse the repository at this point in the history
  • Loading branch information
imteekay committed Jun 3, 2023
1 parent 7657d8d commit 03e83b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
7 changes: 1 addition & 6 deletions baselines/reference/redeclare.errors.baseline
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
[
{
"pos": 14,
"message": "Cannot redeclare x; first declared at 3"
}
]
[]
4 changes: 4 additions & 0 deletions baselines/reference/redeclare.tree.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
{
"kind": "Var",
"pos": 3
},
{
"kind": "Var",
"pos": 14
}
]
},
Expand Down
29 changes: 25 additions & 4 deletions src/bind.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit 03e83b2

Please sign in to comment.