Skip to content

Truly independent web browser

LibJS: Reject redeclarations of imported bindings

Andreas Kling05c8faaa5c46
diff --git a/Libraries/LibJS/Rust/src/parser/declarations.rs b/Libraries/LibJS/Rust/src/parser/declarations.rs
index 94da1680569..a0036edb5fd 100644
--- a/Libraries/LibJS/Rust/src/parser/declarations.rs
+++ b/Libraries/LibJS/Rust/src/parser/declarations.rs
@@ -1914,7 +1914,8 @@ impl Parser<'_> {
self.consume_or_insert_semicolon();
for entry in &entries {
- self.scope_collector.add_import_binding(&entry.local_name);
+ self.scope_collector
+ .add_import_binding(&entry.local_name, start.line, start.column);
}
self.statement(
diff --git a/Libraries/LibJS/Rust/src/scope_collector.rs b/Libraries/LibJS/Rust/src/scope_collector.rs
index a527ddb41aa..6dcb9ad6664 100644
--- a/Libraries/LibJS/Rust/src/scope_collector.rs
+++ b/Libraries/LibJS/Rust/src/scope_collector.rs
@@ -535,7 +535,9 @@ impl ScopeCollector {
for name in bound_names {
let flags = self.records[index].variable(name).flags;
- if flags.intersects(VarFlags::VAR | VarFlags::FORBIDDEN_LEXICAL | VarFlags::FUNCTION | VarFlags::LEXICAL) {
+ if flags.intersects(
+ VarFlags::VAR | VarFlags::FORBIDDEN_LEXICAL | VarFlags::FUNCTION | VarFlags::LEXICAL | VarFlags::IMPORT,
+ ) {
self.already_declared_error(name, declaration_line, declaration_column);
}
self.records[index].variable(name).flags |= VarFlags::LEXICAL;
@@ -569,7 +571,9 @@ impl ScopeCollector {
let mut scope_index = index;
loop {
let existing_flags = self.records[scope_index].variable(name).flags;
- if existing_flags.intersects(VarFlags::LEXICAL | VarFlags::FUNCTION | VarFlags::FORBIDDEN_VAR) {
+ if existing_flags
+ .intersects(VarFlags::LEXICAL | VarFlags::FUNCTION | VarFlags::FORBIDDEN_VAR | VarFlags::IMPORT)
+ {
self.already_declared_error(name, declaration_line, declaration_column);
}
let var = self.records[scope_index].variable(name);
@@ -623,7 +627,7 @@ impl ScopeCollector {
.get(name)
.map_or(VarFlags::EMPTY, |v| v.flags);
- if existing_flags.intersects(VarFlags::VAR | VarFlags::LEXICAL) {
+ if existing_flags.intersects(VarFlags::VAR | VarFlags::LEXICAL | VarFlags::IMPORT) {
self.already_declared_error(name, declaration_line, declaration_column);
}
@@ -675,8 +679,15 @@ impl ScopeCollector {
// https://tc39.es/ecma262/#sec-imports
// Imported bindings live in the module environment, next to the module's own declarations.
- pub fn add_import_binding(&mut self, name: &[u16]) {
+ // https://tc39.es/ecma262/#sec-module-semantics-static-semantics-early-errors
+ // They are lexically declared names of the module, so they must not collide with any other
+ // import, var, function, or lexical binding of the module.
+ pub fn add_import_binding(&mut self, name: &[u16], declaration_line: u32, declaration_column: u32) {
let index = self.current.expect("no current scope");
+ let flags = self.records[index].variable(name).flags;
+ if flags.intersects(VarFlags::VAR | VarFlags::FUNCTION | VarFlags::LEXICAL | VarFlags::IMPORT) {
+ self.already_declared_error(name, declaration_line, declaration_column);
+ }
self.records[index].variable(name).flags |= VarFlags::IMPORT;
}
diff --git a/Tests/LibJS/Runtime/modules/basic-modules.js b/Tests/LibJS/Runtime/modules/basic-modules.js
index 742d52a5c36..f9b9bb99f20 100644
--- a/Tests/LibJS/Runtime/modules/basic-modules.js
+++ b/Tests/LibJS/Runtime/modules/basic-modules.js
@@ -200,6 +200,14 @@ describe("in- and exports", () => {
expectedModuleToThrowSyntaxError("./duplicate-import-attribute-key.mjs", "Duplicate import attribute key");
});
+ test("imported bindings cannot be redeclared", () => {
+ expectedModuleToThrowSyntaxError("./import-redeclared-by-let.mjs", "Identifier 'passed' already declared");
+ expectedModuleToThrowSyntaxError("./import-redeclared-by-var.mjs", "Identifier 'passed' already declared");
+ expectedModuleToThrowSyntaxError("./import-redeclared-by-function.mjs", "Identifier 'passed' already declared");
+ expectedModuleToThrowSyntaxError("./import-redeclaring-class.mjs", "Identifier 'passed' already declared");
+ expectedModuleToThrowSyntaxError("./duplicate-import-binding.mjs", "Identifier 'passed' already declared");
+ });
+
test("namespace has expected ordering", () => {
expectModulePassed("./namespace-order.mjs");
});
diff --git a/Tests/LibJS/Runtime/modules/duplicate-import-binding.mjs b/Tests/LibJS/Runtime/modules/duplicate-import-binding.mjs
new file mode 100644
index 00000000000..5e84f5ae81d
--- /dev/null
+++ b/Tests/LibJS/Runtime/modules/duplicate-import-binding.mjs
@@ -0,0 +1,2 @@
+import { passed } from "./single-const-export.mjs";
+import * as passed from "./single-const-export.mjs";
diff --git a/Tests/LibJS/Runtime/modules/import-redeclared-by-function.mjs b/Tests/LibJS/Runtime/modules/import-redeclared-by-function.mjs
new file mode 100644
index 00000000000..1f0b9f89716
--- /dev/null
+++ b/Tests/LibJS/Runtime/modules/import-redeclared-by-function.mjs
@@ -0,0 +1,2 @@
+import { passed } from "./single-const-export.mjs";
+function passed() {}
diff --git a/Tests/LibJS/Runtime/modules/import-redeclared-by-let.mjs b/Tests/LibJS/Runtime/modules/import-redeclared-by-let.mjs
new file mode 100644
index 00000000000..2aa75132102
--- /dev/null
+++ b/Tests/LibJS/Runtime/modules/import-redeclared-by-let.mjs
@@ -0,0 +1,2 @@
+import { passed } from "./single-const-export.mjs";
+let passed;
diff --git a/Tests/LibJS/Runtime/modules/import-redeclared-by-var.mjs b/Tests/LibJS/Runtime/modules/import-redeclared-by-var.mjs
new file mode 100644
index 00000000000..dc11bbd321f
--- /dev/null
+++ b/Tests/LibJS/Runtime/modules/import-redeclared-by-var.mjs
@@ -0,0 +1,4 @@
+import { passed } from "./single-const-export.mjs";
+{
+ var passed;
+}
diff --git a/Tests/LibJS/Runtime/modules/import-redeclaring-class.mjs b/Tests/LibJS/Runtime/modules/import-redeclaring-class.mjs
new file mode 100644
index 00000000000..85b714978b1
--- /dev/null
+++ b/Tests/LibJS/Runtime/modules/import-redeclaring-class.mjs
@@ -0,0 +1,2 @@
+class passed {}
+import { passed } from "./single-const-export.mjs";