summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authordavidot <davidot@serenityos.org>2021-11-26 23:37:14 +0100
committerLinus Groh <mail@linusgroh.de>2021-11-30 17:05:32 +0000
commit156dfe3d629f36a03bdcde3a9a253934e45dd4de (patch)
tree22425793ccb57f136d299b776a7740ef15f13588 /Userland
parent51e23cd04303e6cd18987d90dec4cc032341b371 (diff)
downloadserenity-156dfe3d629f36a03bdcde3a9a253934e45dd4de.zip
LibJS: Disallow member expression in binding pattern as parameters
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Parser.cpp4
-rw-r--r--Userland/Libraries/LibJS/Tests/syntax/destructuring-assignment.js14
2 files changed, 17 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Parser.cpp b/Userland/Libraries/LibJS/Parser.cpp
index c8f5690798..5379c891d5 100644
--- a/Userland/Libraries/LibJS/Parser.cpp
+++ b/Userland/Libraries/LibJS/Parser.cpp
@@ -2383,7 +2383,7 @@ Vector<FunctionNode::Parameter> Parser::parse_formal_parameters(int& function_le
Vector<FunctionNode::Parameter> parameters;
auto consume_identifier_or_binding_pattern = [&]() -> Variant<FlyString, NonnullRefPtr<BindingPattern>> {
- if (auto pattern = parse_binding_pattern(AllowDuplicates::No, AllowMemberExpressions::Yes))
+ if (auto pattern = parse_binding_pattern(AllowDuplicates::No, AllowMemberExpressions::No))
return pattern.release_nonnull();
auto token = consume_identifier();
@@ -2635,6 +2635,8 @@ RefPtr<BindingPattern> Parser::parse_binding_pattern(Parser::AllowDuplicates all
return {};
}
consume();
+ } else if (is_object && !match(TokenType::CurlyClose)) {
+ consume(TokenType::Comma);
}
}
diff --git a/Userland/Libraries/LibJS/Tests/syntax/destructuring-assignment.js b/Userland/Libraries/LibJS/Tests/syntax/destructuring-assignment.js
index 7a709a31bb..198561c4a8 100644
--- a/Userland/Libraries/LibJS/Tests/syntax/destructuring-assignment.js
+++ b/Userland/Libraries/LibJS/Tests/syntax/destructuring-assignment.js
@@ -46,6 +46,20 @@ describe("parsing", () => {
expect(`const [ a, [ ...{length} ] ] = [];`).toEval();
expect(`let [ a, [ ...{length} ] ] = [];`).toEval();
});
+
+ test("function parameters cannot use member expresssions", () => {
+ expect("function f([a.b]) {}").not.toEval();
+ expect("function f([b[0]]) {}").not.toEval();
+
+ expect("function f({c:a.b}) {}").not.toEval();
+ expect("function f({a:b[0]}) {}").not.toEval();
+
+ expect("([a.b]) => 1").not.toEval();
+ expect("([b[0]]) => 2").not.toEval();
+
+ expect("({c:a.b}) => 3").not.toEval();
+ expect("({a:b[0]}) => 4").not.toEval();
+ });
});
describe("evaluating", () => {