Skip to content

Truly independent web browser

LibJS: Stop marking outer functions for an inner arrow's use of this

Andreas Kling5f8298f1a87b
diff --git a/Libraries/LibJS/Rust/src/parser/base.rs b/Libraries/LibJS/Rust/src/parser/base.rs
index 58f18d122c3..2474fe73caf 100644
--- a/Libraries/LibJS/Rust/src/parser/base.rs
+++ b/Libraries/LibJS/Rust/src/parser/base.rs
@@ -2024,8 +2024,10 @@ impl<'a, U: Unit> Parser<'a, U> {
self.cover_error = state.cover_error;
self.retain = state.retain;
self.duplicate_parameter = state.duplicate_parameter;
- self.uses_this |= state.uses_this;
- self.uses_this_from_environment |= state.uses_this_from_environment;
+ // Only an arrow function takes its `this` from the function around it.
+ self.uses_this = state.uses_this || (is_arrow && self.uses_this);
+ self.uses_this_from_environment =
+ state.uses_this_from_environment || (is_arrow && self.uses_this_from_environment);
// An arrow function has no arguments object, so that what it mentions counts for the function around it.
self.might_need_arguments = state.might_need_arguments || (is_arrow && self.might_need_arguments);
self.in_class_of_sloppy_function = state.in_class_of_sloppy_function;
diff --git a/Tests/LibJS/AST/expected/arrow-this-in-nested-function.txt b/Tests/LibJS/AST/expected/arrow-this-in-nested-function.txt
new file mode 100644
index 00000000000..aa96b119e9e
--- /dev/null
+++ b/Tests/LibJS/AST/expected/arrow-this-in-nested-function.txt
@@ -0,0 +1,67 @@
+Program (script) @3:1
+├─ FunctionDeclaration "outer_of_function" @3:1
+│ └─ body
+│ └─ FunctionBody @4:5
+│ ├─ FunctionDeclaration "inner" [uses-this] [uses-this-from-environment] @4:5
+│ │ └─ body
+│ │ └─ FunctionBody @5:9
+│ │ └─ ReturnStatement @5:9
+│ │ └─ FunctionExpression "" [arrow] [uses-this-from-environment] @5:16
+│ │ └─ body
+│ │ └─ FunctionBody @5:16
+│ │ └─ ReturnStatement @5:16
+│ │ └─ ThisExpression @5:22
+│ └─ ReturnStatement @7:5
+│ └─ Identifier "inner" [variable:0] @7:12
+├─ FunctionDeclaration "outer_of_class" @10:1
+│ └─ body
+│ └─ FunctionBody @11:5
+│ ├─ ClassDeclaration @11:5
+│ │ └─ ClassExpression "C" @11:5
+│ │ ├─ constructor
+│ │ │ └─ FunctionExpression "C" [strict] [uses-this] @11:5
+│ │ │ └─ body
+│ │ │ └─ BlockStatement @11:5
+│ │ └─ elements
+│ │ └─ ClassMethod @11:5
+│ │ ├─ StringLiteral "m" @12:9
+│ │ └─ FunctionExpression "" [strict] [uses-this] [uses-this-from-environment] @12:9
+│ │ └─ body
+│ │ └─ FunctionBody @13:13
+│ │ └─ ReturnStatement @13:13
+│ │ └─ FunctionExpression "" [strict] [arrow] [uses-this-from-environment] @13:20
+│ │ └─ body
+│ │ └─ FunctionBody @13:20
+│ │ └─ ReturnStatement @13:20
+│ │ └─ ThisExpression @13:26
+│ └─ ReturnStatement @16:5
+│ └─ Identifier "C" [variable:0] @16:12
+├─ FunctionDeclaration "outer_of_object_method" @19:1
+│ └─ body
+│ └─ FunctionBody @20:5
+│ └─ ReturnStatement @20:5
+│ └─ ObjectExpression @20:12
+│ └─ ObjectProperty (method) @20:12
+│ ├─ StringLiteral "m" @21:9
+│ └─ FunctionExpression "" [uses-this] [uses-this-from-environment] @21:9
+│ └─ body
+│ └─ FunctionBody @22:13
+│ └─ ReturnStatement @22:13
+│ └─ FunctionExpression "" [arrow] [uses-this-from-environment] @22:20
+│ └─ body
+│ └─ FunctionBody @22:20
+│ └─ ReturnStatement @22:20
+│ └─ ThisExpression @22:26
+└─ FunctionDeclaration "outer_of_arrow" [uses-this] [uses-this-from-environment] @27:1
+ └─ body
+ └─ FunctionBody @28:5
+ └─ ReturnStatement @28:5
+ └─ FunctionExpression "" [arrow] [uses-this-from-environment] @28:12
+ └─ body
+ └─ FunctionBody @28:12
+ └─ ReturnStatement @28:12
+ └─ FunctionExpression "" [arrow] [uses-this-from-environment] @28:18
+ └─ body
+ └─ FunctionBody @28:18
+ └─ ReturnStatement @28:18
+ └─ ThisExpression @28:24
diff --git a/Tests/LibJS/AST/expected/eval-defeats-locals.txt b/Tests/LibJS/AST/expected/eval-defeats-locals.txt
index 4b18df82b01..9662b19caa8 100644
--- a/Tests/LibJS/AST/expected/eval-defeats-locals.txt
+++ b/Tests/LibJS/AST/expected/eval-defeats-locals.txt
@@ -32,7 +32,7 @@ Program (script) @3:1
│ │ └─ StringLiteral "" @14:16
│ └─ ReturnStatement @16:5
│ └─ Identifier "x" (let) @16:12
-└─ FunctionDeclaration "outer_safe" [uses-this] @20:1
+└─ FunctionDeclaration "outer_safe" @20:1
└─ body
└─ FunctionBody @21:5
├─ VariableDeclaration (let) @21:5
diff --git a/Tests/LibJS/AST/expected/eval-in-scope-chain.txt b/Tests/LibJS/AST/expected/eval-in-scope-chain.txt
index 8ec6d9ea63a..1de2c41f468 100644
--- a/Tests/LibJS/AST/expected/eval-in-scope-chain.txt
+++ b/Tests/LibJS/AST/expected/eval-in-scope-chain.txt
@@ -1,5 +1,5 @@
Program (script) @1:1
-└─ FunctionDeclaration "outer" [uses-this] @1:1
+└─ FunctionDeclaration "outer" @1:1
└─ body
└─ FunctionBody @2:5
├─ VariableDeclaration (let) @2:5
diff --git a/Tests/LibJS/AST/expected/eval-scope-chain-deep.txt b/Tests/LibJS/AST/expected/eval-scope-chain-deep.txt
index 736eea9d160..9a829e8a6b7 100644
--- a/Tests/LibJS/AST/expected/eval-scope-chain-deep.txt
+++ b/Tests/LibJS/AST/expected/eval-scope-chain-deep.txt
@@ -1,5 +1,5 @@
Program (script) @6:1
-├─ FunctionDeclaration "clean_outer" [uses-this] @6:1
+├─ FunctionDeclaration "clean_outer" @6:1
│ └─ body
│ └─ FunctionBody @7:5
│ ├─ VariableDeclaration (let) @7:5
diff --git a/Tests/LibJS/AST/expected/speculative-arrow-this-leak.txt b/Tests/LibJS/AST/expected/speculative-arrow-this-leak.txt
index fc1b08dbd0a..3345b07420b 100644
--- a/Tests/LibJS/AST/expected/speculative-arrow-this-leak.txt
+++ b/Tests/LibJS/AST/expected/speculative-arrow-this-leak.txt
@@ -1,5 +1,5 @@
Program (script) @6:1
-└─ FunctionDeclaration "outer" [uses-this] @6:1
+└─ FunctionDeclaration "outer" @6:1
└─ body
└─ FunctionBody @7:5
├─ VariableDeclaration (var) @7:5
diff --git a/Tests/LibJS/AST/input/arrow-this-in-nested-function.js b/Tests/LibJS/AST/input/arrow-this-in-nested-function.js
new file mode 100644
index 00000000000..86762dbdafa
--- /dev/null
+++ b/Tests/LibJS/AST/input/arrow-this-in-nested-function.js
@@ -0,0 +1,29 @@
+// An arrow function takes `this` from the nearest function that is not an arrow function.
+// Functions further out must not be marked as reading `this` through the environment.
+function outer_of_function() {
+ function inner() {
+ return () => this;
+ }
+ return inner;
+}
+
+function outer_of_class() {
+ class C {
+ m() {
+ return () => this;
+ }
+ }
+ return C;
+}
+
+function outer_of_object_method() {
+ return {
+ m() {
+ return () => this;
+ }
+ };
+}
+
+function outer_of_arrow() {
+ return () => () => this;
+}