Skip to content

Commit d084055

Browse files
gkzfacebook-github-bot
authored andcommitted
[flow][match] Implement ast differ for match expressions and statements
Summary: Implement ast differ for match expressions and statements. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D70825700 fbshipit-source-id: 64b2ef9043dc8748d3ac3a074ecd48e0552cd402
1 parent 8be18ab commit d084055

File tree

7 files changed

+475
-32
lines changed

7 files changed

+475
-32
lines changed

newtests/lsp/code-action/quickfix/__snapshots__/fix-match-invalid-object-shorthand.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
{
1212
"range": {
1313
"start": {
14-
"line": 4,
15-
"character": 12
14+
"line": 5,
15+
"character": 3
1616
},
1717
"end": {
18-
"line": 7,
19-
"character": 1
18+
"line": 5,
19+
"character": 6
2020
}
2121
},
22-
"newText": "match (x) {\n {const foo}: 0,\n _: 0,\n}"
22+
"newText": "const foo"
2323
}
2424
]
2525
}
@@ -44,15 +44,15 @@
4444
{
4545
"range": {
4646
"start": {
47-
"line": 4,
48-
"character": 12
47+
"line": 5,
48+
"character": 3
4949
},
5050
"end": {
51-
"line": 7,
52-
"character": 1
51+
"line": 5,
52+
"character": 6
5353
}
5454
},
55-
"newText": "match (x) {\n {foo: foo}: 0,\n _: 0,\n}"
55+
"newText": "foo: foo"
5656
}
5757
]
5858
}

src/parser_utils/__tests__/flow_ast_differ_test.ml

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,28 @@ class useless_mapper =
287287
}
288288
)
289289
| _ -> member
290+
291+
method! match_pattern pattern =
292+
let open Ast.MatchPattern in
293+
match pattern with
294+
| (loc, NullPattern _) ->
295+
(loc, IdentifierPattern (Flow_ast_utils.ident_of_source (loc, "UpdatedPattern")))
296+
| _ -> super#match_pattern pattern
297+
298+
method! match_object_pattern_property prop =
299+
let open Ast.MatchPattern.ObjectPattern.Property in
300+
match prop with
301+
| (loc, InvalidShorthand (_, { Ast.Identifier.name = "changeProp"; _ })) ->
302+
( loc,
303+
Valid
304+
{
305+
key = Identifier (Flow_ast_utils.ident_of_source (loc, "UpdatedProp"));
306+
pattern = (loc, Ast.MatchPattern.NullPattern None);
307+
shorthand = false;
308+
comments = None;
309+
}
310+
)
311+
| _ -> super#match_object_pattern_property prop
290312
end
291313

292314
(* TODO: add test for RegExp case? *)
@@ -3546,4 +3568,166 @@ import type { there as here } from \"new_import2\";const x: (() => number) = (bl
35463568
~expected:"enum Status {On = 1// a comment\n, Off = 2}"
35473569
~mapper:(new useless_mapper)
35483570
);
3571+
( "match_expression_arg" >:: fun ctxt ->
3572+
let source = "const e = match (null) {};" in
3573+
assert_edits_equal
3574+
ctxt
3575+
~edits:[((17, 21), "\"wasNull\"")]
3576+
~source
3577+
~expected:"const e = match (\"wasNull\") {};"
3578+
~mapper:(new literal_mapper)
3579+
);
3580+
( "match_statement_arg" >:: fun ctxt ->
3581+
let source = "match (null) {}" in
3582+
assert_edits_equal
3583+
ctxt
3584+
~edits:[((7, 11), "\"wasNull\"")]
3585+
~source
3586+
~expected:"match (\"wasNull\") {}"
3587+
~mapper:(new literal_mapper)
3588+
);
3589+
( "match_expression_case_body" >:: fun ctxt ->
3590+
let source = "const e = match (x) {1: null};" in
3591+
assert_edits_equal
3592+
ctxt
3593+
~edits:[((24, 28), "\"wasNull\"")]
3594+
~source
3595+
~expected:"const e = match (x) {1: \"wasNull\"};"
3596+
~mapper:(new literal_mapper)
3597+
);
3598+
( "match_expression_case_guard" >:: fun ctxt ->
3599+
let source = "const e = match (x) {1 if null: 0};" in
3600+
assert_edits_equal
3601+
ctxt
3602+
~edits:[((26, 30), "\"wasNull\"")]
3603+
~source
3604+
~expected:"const e = match (x) {1 if \"wasNull\": 0};"
3605+
~mapper:(new literal_mapper)
3606+
);
3607+
( "match_statement_case_guard" >:: fun ctxt ->
3608+
let source = "match (x) {1 if null: {}}" in
3609+
assert_edits_equal
3610+
ctxt
3611+
~edits:[((16, 20), "\"wasNull\"")]
3612+
~source
3613+
~expected:"match (x) {1 if \"wasNull\": {}}"
3614+
~mapper:(new literal_mapper)
3615+
);
3616+
( "match_null_pattern" >:: fun ctxt ->
3617+
let source = "match (x) {null: {}}" in
3618+
assert_edits_equal
3619+
ctxt
3620+
~edits:[((11, 15), "UpdatedPattern")]
3621+
~source
3622+
~expected:"match (x) {UpdatedPattern: {}}"
3623+
~mapper:(new useless_mapper)
3624+
);
3625+
( "match_identifier_pattern" >:: fun ctxt ->
3626+
let source = "match (x) {rename: {}}" in
3627+
assert_edits_equal
3628+
ctxt
3629+
~edits:[((11, 17), "gotRenamed")]
3630+
~source
3631+
~expected:"match (x) {gotRenamed: {}}"
3632+
~mapper:(new useless_mapper)
3633+
);
3634+
( "match_number_pattern" >:: fun ctxt ->
3635+
let source = "match (x) {4: {}}" in
3636+
assert_edits_equal
3637+
ctxt
3638+
~edits:[((11, 12), "5")]
3639+
~source
3640+
~expected:"match (x) {5: {}}"
3641+
~mapper:(new useless_mapper)
3642+
);
3643+
( "match_string_pattern" >:: fun ctxt ->
3644+
let source = "match (x) {\"RenameSL\": {}}" in
3645+
assert_edits_equal
3646+
ctxt
3647+
~edits:[((11, 21), "\"GotRenamedSL\"")]
3648+
~source
3649+
~expected:"match (x) {\"GotRenamedSL\": {}}"
3650+
~mapper:(new useless_mapper)
3651+
);
3652+
( "match_member_pattern" >:: fun ctxt ->
3653+
let source = "match (x) {O.rename: {}}" in
3654+
assert_edits_equal
3655+
ctxt
3656+
~edits:[((13, 19), "gotRenamed")]
3657+
~source
3658+
~expected:"match (x) {O.gotRenamed: {}}"
3659+
~mapper:(new useless_mapper)
3660+
);
3661+
( "match_unary_pattern" >:: fun ctxt ->
3662+
let source = "match (x) {-4: {}}" in
3663+
assert_edits_equal
3664+
ctxt
3665+
~edits:[((12, 13), "5")]
3666+
~source
3667+
~expected:"match (x) {-5: {}}"
3668+
~mapper:(new useless_mapper)
3669+
);
3670+
( "match_binding_pattern" >:: fun ctxt ->
3671+
let source = "match (x) {const rename: {}}" in
3672+
assert_edits_equal
3673+
ctxt
3674+
~edits:[((17, 23), "gotRenamed")]
3675+
~source
3676+
~expected:"match (x) {const gotRenamed: {}}"
3677+
~mapper:(new useless_mapper)
3678+
);
3679+
( "match_array_pattern" >:: fun ctxt ->
3680+
let source = "match (x) {[1, null, 3]: {}}" in
3681+
assert_edits_equal
3682+
ctxt
3683+
~edits:[((15, 19), "UpdatedPattern")]
3684+
~source
3685+
~expected:"match (x) {[1, UpdatedPattern, 3]: {}}"
3686+
~mapper:(new useless_mapper)
3687+
);
3688+
( "match_object_pattern_property_key" >:: fun ctxt ->
3689+
let source = "match (x) {{a: 1, rename: 2, c: 3}: {}}" in
3690+
assert_edits_equal
3691+
ctxt
3692+
~edits:[((18, 24), "gotRenamed")]
3693+
~source
3694+
~expected:"match (x) {{a: 1, gotRenamed: 2, c: 3}: {}}"
3695+
~mapper:(new useless_mapper)
3696+
);
3697+
( "match_object_pattern_property_value" >:: fun ctxt ->
3698+
let source = "match (x) {{a: 1, b: null, c: 3}: {}}" in
3699+
assert_edits_equal
3700+
ctxt
3701+
~edits:[((21, 25), "UpdatedPattern")]
3702+
~source
3703+
~expected:"match (x) {{a: 1, b: UpdatedPattern, c: 3}: {}}"
3704+
~mapper:(new useless_mapper)
3705+
);
3706+
( "match_object_pattern_property_entire" >:: fun ctxt ->
3707+
let source = "match (x) {{a: 1, changeProp, c: 3}: {}}" in
3708+
assert_edits_equal
3709+
ctxt
3710+
~edits:[((18, 28), "UpdatedProp: null")]
3711+
~source
3712+
~expected:"match (x) {{a: 1, UpdatedProp: null, c: 3}: {}}"
3713+
~mapper:(new useless_mapper)
3714+
);
3715+
( "match_as_pattern" >:: fun ctxt ->
3716+
let source = "match (x) {null as rename: {}}" in
3717+
assert_edits_equal
3718+
ctxt
3719+
~edits:[((11, 15), "UpdatedPattern"); ((19, 25), "gotRenamed")]
3720+
~source
3721+
~expected:"match (x) {UpdatedPattern as gotRenamed: {}}"
3722+
~mapper:(new useless_mapper)
3723+
);
3724+
( "match_or_pattern" >:: fun ctxt ->
3725+
let source = "match (x) {1 | null | 3: {}}" in
3726+
assert_edits_equal
3727+
ctxt
3728+
~edits:[((15, 19), "UpdatedPattern")]
3729+
~source
3730+
~expected:"match (x) {1 | UpdatedPattern | 3: {}}"
3731+
~mapper:(new useless_mapper)
3732+
);
35493733
]

0 commit comments

Comments
 (0)