Skip to content

Commit ad2b5b3

Browse files
cknittclaude
andcommitted
Allow unary minus only on numbers in fixed literals
Negating an array, object or regexp can run a valueOf method. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NP47qMfjMbjUTKZDsLthDo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 415e2ec commit ad2b5b3

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎compiler/ml/classify_function.ml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ let rec is_obj_literal (x : _ Flow_ast.Expression.t) : bool =
2828
| StringLiteral _ | BooleanLiteral _ | NullLiteral _ | NumberLiteral _
2929
| BigIntLiteral _ | RegExpLiteral _ | ModuleRefLiteral _ ->
3030
true
31-
| Unary {operator = Minus; argument} -> is_obj_literal argument
31+
(* Negating anything but a number converts it to a primitive, which can run
32+
a [valueOf] method. *)
33+
| Unary {operator = Minus; argument = _, (NumberLiteral _ | BigIntLiteral _)}
34+
->
35+
true
3236
| Object {properties} -> Ext_list.for_all properties is_literal_kv
3337
| Array {elements} ->
3438
Ext_list.for_all elements (fun x ->

‎tests/ounit_tests/ounit_flow_parser_tests.ml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ let suites =
5858
[
5959
"1";
6060
"-1";
61+
"-1n";
62+
"-1.5";
63+
"{a: -2}";
6164
"1n";
6265
{|"s"|};
6366
"{}";
@@ -80,6 +83,12 @@ let suites =
8083
"{[f()]: 0}";
8184
{|{["a"]: 0}|};
8285
"{a: {[f()]: 0}}";
86+
"-[]";
87+
"{x: -{}}";
88+
"-/a/";
89+
{|-"1"|};
90+
"-true";
91+
"-(-1)";
8392
"1 2";
8493
"1) + alert(2";
8594
"1; alert(2)";

0 commit comments

Comments
 (0)