Skip to content

Commit

Permalink
[flow][match] Factor out code for turning MemberPattern into MemberEx…
Browse files Browse the repository at this point in the history
…pression

Summary:
Factor out code for turning MemberPattern into MemberExpression.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D67261336

fbshipit-source-id: d482e338bec2a4030627e04142eddd6513497100
  • Loading branch information
gkz authored and facebook-github-bot committed Dec 18, 2024
1 parent 02e73a5 commit 4d4ccbd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
22 changes: 1 addition & 21 deletions src/analysis/env_builder/name_def.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,7 @@ end = struct
visit_expression
(loc, Ast.Expression.Unary { Ast.Expression.Unary.operator; argument; comments })
| MemberPattern mem ->
let rec member (loc, { MemberPattern.base; property; comments }) =
let _object =
match base with
| MemberPattern.BaseIdentifier ((loc, _) as id) -> (loc, Ast.Expression.Identifier id)
| MemberPattern.BaseMember mem -> member mem
in
let property =
match property with
| MemberPattern.PropertyIdentifier id -> Ast.Expression.Member.PropertyIdentifier id
| MemberPattern.PropertyString (loc, lit) ->
Ast.Expression.Member.PropertyExpression (loc, Ast.Expression.StringLiteral lit)
| MemberPattern.PropertyNumber (loc, lit) ->
Ast.Expression.Member.PropertyExpression (loc, Ast.Expression.NumberLiteral lit)
in
let exp =
(loc, Ast.Expression.Member { Ast.Expression.Member._object; property; comments })
in
visit_expression exp;
exp
in
ignore @@ member mem
ignore @@ Flow_ast_utils.expression_of_match_member_pattern ~visit_expression mem
| ArrayPattern { ArrayPattern.elements; rest; comments = _ } ->
visit_intermediate loc acc;
let used_elements =
Expand Down
22 changes: 22 additions & 0 deletions src/parser/flow_ast_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,25 @@ let hook_call { E.Call.callee; _ } =
let match_root_name = "<match_root>"

let match_root_ident loc = (loc, { Identifier.name = match_root_name; comments = None })

let expression_of_match_member_pattern ~visit_expression pattern =
let open MatchPattern in
let rec f (loc, { MemberPattern.base; property; comments }) =
let _object =
match base with
| MemberPattern.BaseIdentifier ((loc, _) as id) -> (loc, Expression.Identifier id)
| MemberPattern.BaseMember mem -> f mem
in
let property =
match property with
| MemberPattern.PropertyIdentifier id -> Expression.Member.PropertyIdentifier id
| MemberPattern.PropertyString (loc, lit) ->
Expression.Member.PropertyExpression (loc, Expression.StringLiteral lit)
| MemberPattern.PropertyNumber (loc, lit) ->
Expression.Member.PropertyExpression (loc, Expression.NumberLiteral lit)
in
let exp = (loc, Expression.Member { Expression.Member._object; property; comments }) in
visit_expression exp;
exp
in
f pattern
5 changes: 5 additions & 0 deletions src/parser/flow_ast_utils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,8 @@ val hook_name : string -> bool
val match_root_name : string

val match_root_ident : 'loc -> ('loc, 'loc) Flow_ast.Identifier.t

val expression_of_match_member_pattern :
visit_expression:(('loc, 'loc) Flow_ast.Expression.t -> unit) ->
('loc, 'loc) Flow_ast.MatchPattern.MemberPattern.t ->
('loc, 'loc) Flow_ast.Expression.t

0 comments on commit 4d4ccbd

Please sign in to comment.