Skip to content

Commit

Permalink
[flow][match] Update parsing location of match object pattern properties
Browse files Browse the repository at this point in the history
Summary:
Previously, the location for object pattern properties was set to the location of the first token. Instead it now contains the entire property. If we require an additional location for type-checking purposes, I will add another one. Thanks to the previous diff, this has no use-facing changes.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D68523163

fbshipit-source-id: a6327705ec65f02d6b6a560292f381737f4605d9
  • Loading branch information
gkz authored and facebook-github-bot committed Jan 23, 2025
1 parent 59bd3ef commit a4767a5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 60 deletions.
45 changes: 23 additions & 22 deletions src/parser/match_pattern_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,29 @@ module Match_pattern (Parse : PARSER) : Parser_common.MATCH_PATTERN = struct
let id = identifier_name env in
Identifier id
in
let property env =
let start_loc = Peek.loc env in
let leading = Peek.comments env in
let shorthand_prop (loc, binding) =
let { BindingPattern.id = (_, id); _ } = binding in
let key = ObjectPattern.Property.Identifier (loc, id) in
let pattern = (loc, BindingPattern binding) in
let trailing = Eat.trailing_comments env in
let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in
(start_loc, { ObjectPattern.Property.key; pattern; shorthand = true; comments })
in
match Peek.token env with
| T_CONST -> shorthand_prop (binding_pattern env ~kind:Ast.Variable.Const)
| T_LET -> shorthand_prop (binding_pattern env ~kind:Ast.Variable.Let)
| T_VAR -> shorthand_prop (binding_pattern env ~kind:Ast.Variable.Var)
| _ ->
let key = property_key env in
Expect.token env T_COLON;
let pattern = match_pattern env in
let trailing = Eat.trailing_comments env in
let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in
(start_loc, { ObjectPattern.Property.key; pattern; shorthand = false; comments })
let property =
with_loc (fun env ->
let leading = Peek.comments env in
let shorthand_prop (loc, binding) =
let { BindingPattern.id = (_, id); _ } = binding in
let key = ObjectPattern.Property.Identifier (loc, id) in
let pattern = (loc, BindingPattern binding) in
let trailing = Eat.trailing_comments env in
let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in
{ ObjectPattern.Property.key; pattern; shorthand = true; comments }
in
match Peek.token env with
| T_CONST -> shorthand_prop (binding_pattern env ~kind:Ast.Variable.Const)
| T_LET -> shorthand_prop (binding_pattern env ~kind:Ast.Variable.Let)
| T_VAR -> shorthand_prop (binding_pattern env ~kind:Ast.Variable.Var)
| _ ->
let key = property_key env in
Expect.token env T_COLON;
let pattern = match_pattern env in
let trailing = Eat.trailing_comments env in
let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in
{ ObjectPattern.Property.key; pattern; shorthand = false; comments }
)
in
let rec properties env acc =
match Peek.token env with
Expand Down
4 changes: 2 additions & 2 deletions src/parser/test/flow/match/pattern-array.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":8,"column":4},"end":{"line":8,"column":10}},
"range":[148,154],
"loc":{"source":null,"start":{"line":8,"column":4},"end":{"line":8,"column":24}},
"range":[148,168],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":8,"column":4},"end":{"line":8,"column":10}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":2,"column":3},"end":{"line":2,"column":8}},
"range":[25,30],
"loc":{"source":null,"start":{"line":2,"column":3},"end":{"line":2,"column":10}},
"range":[25,32],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":3},"end":{"line":2,"column":10}},
Expand Down
48 changes: 24 additions & 24 deletions src/parser/test/flow/match/pattern-object.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":2,"column":3},"end":{"line":2,"column":6}},
"range":[25,28],
"loc":{"source":null,"start":{"line":2,"column":3},"end":{"line":2,"column":9}},
"range":[25,31],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":3},"end":{"line":2,"column":6}},
Expand All @@ -70,8 +70,8 @@
},
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":2,"column":11},"end":{"line":2,"column":14}},
"range":[33,36],
"loc":{"source":null,"start":{"line":2,"column":11},"end":{"line":2,"column":17}},
"range":[33,39],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":2,"column":11},"end":{"line":2,"column":14}},
Expand Down Expand Up @@ -117,8 +117,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":3,"column":3},"end":{"line":3,"column":8}},
"range":[48,53],
"loc":{"source":null,"start":{"line":3,"column":3},"end":{"line":3,"column":11}},
"range":[48,56],
"key":{
"type":"Literal",
"loc":{"source":null,"start":{"line":3,"column":3},"end":{"line":3,"column":8}},
Expand Down Expand Up @@ -163,8 +163,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":4,"column":3},"end":{"line":4,"column":6}},
"range":[65,68],
"loc":{"source":null,"start":{"line":4,"column":3},"end":{"line":4,"column":12}},
"range":[65,74],
"key":{
"type":"Literal",
"loc":{"source":null,"start":{"line":4,"column":3},"end":{"line":4,"column":6}},
Expand Down Expand Up @@ -209,8 +209,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":5,"column":3},"end":{"line":5,"column":6}},
"range":[83,86],
"loc":{"source":null,"start":{"line":5,"column":3},"end":{"line":5,"column":15}},
"range":[83,95],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":5,"column":3},"end":{"line":5,"column":6}},
Expand Down Expand Up @@ -259,8 +259,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":6,"column":3},"end":{"line":6,"column":8}},
"range":[104,109],
"loc":{"source":null,"start":{"line":6,"column":3},"end":{"line":6,"column":10}},
"range":[104,111],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":6,"column":3},"end":{"line":6,"column":10}},
Expand All @@ -287,8 +287,8 @@
},
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":6,"column":12},"end":{"line":6,"column":15}},
"range":[113,116],
"loc":{"source":null,"start":{"line":6,"column":12},"end":{"line":6,"column":17}},
"range":[113,118],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":6,"column":12},"end":{"line":6,"column":17}},
Expand All @@ -315,8 +315,8 @@
},
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":6,"column":19},"end":{"line":6,"column":22}},
"range":[120,123],
"loc":{"source":null,"start":{"line":6,"column":19},"end":{"line":6,"column":24}},
"range":[120,125],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":6,"column":19},"end":{"line":6,"column":24}},
Expand Down Expand Up @@ -365,8 +365,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":7,"column":3},"end":{"line":7,"column":8}},
"range":[134,139],
"loc":{"source":null,"start":{"line":7,"column":3},"end":{"line":7,"column":10}},
"range":[134,141],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":7,"column":3},"end":{"line":7,"column":10}},
Expand Down Expand Up @@ -433,8 +433,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":8,"column":3},"end":{"line":8,"column":8}},
"range":[162,167],
"loc":{"source":null,"start":{"line":8,"column":3},"end":{"line":8,"column":10}},
"range":[162,169],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":8,"column":3},"end":{"line":8,"column":10}},
Expand Down Expand Up @@ -501,8 +501,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":9,"column":3},"end":{"line":9,"column":8}},
"range":[188,193],
"loc":{"source":null,"start":{"line":9,"column":3},"end":{"line":9,"column":10}},
"range":[188,195],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":3},"end":{"line":9,"column":10}},
Expand Down Expand Up @@ -569,8 +569,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":10,"column":3},"end":{"line":10,"column":8}},
"range":[214,219],
"loc":{"source":null,"start":{"line":10,"column":3},"end":{"line":10,"column":10}},
"range":[214,221],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":10,"column":3},"end":{"line":10,"column":10}},
Expand Down
20 changes: 10 additions & 10 deletions src/parser/test/flow/match/pattern-or-as.tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":3,"column":3},"end":{"line":3,"column":6}},
"range":[49,52],
"loc":{"source":null,"start":{"line":3,"column":3},"end":{"line":3,"column":13}},
"range":[49,59],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":3,"column":3},"end":{"line":3,"column":6}},
Expand Down Expand Up @@ -166,8 +166,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":4,"column":3},"end":{"line":4,"column":6}},
"range":[68,71],
"loc":{"source":null,"start":{"line":4,"column":3},"end":{"line":4,"column":16}},
"range":[68,81],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":4,"column":3},"end":{"line":4,"column":6}},
Expand Down Expand Up @@ -235,8 +235,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":5,"column":3},"end":{"line":5,"column":6}},
"range":[90,93],
"loc":{"source":null,"start":{"line":5,"column":3},"end":{"line":5,"column":22}},
"range":[90,109],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":5,"column":3},"end":{"line":5,"column":6}},
Expand Down Expand Up @@ -327,8 +327,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":6,"column":3},"end":{"line":6,"column":6}},
"range":[118,121],
"loc":{"source":null,"start":{"line":6,"column":3},"end":{"line":6,"column":24}},
"range":[118,139],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":6,"column":3},"end":{"line":6,"column":6}},
Expand Down Expand Up @@ -419,8 +419,8 @@
"properties":[
{
"type":"MatchObjectPatternProperty",
"loc":{"source":null,"start":{"line":7,"column":3},"end":{"line":7,"column":6}},
"range":[148,151],
"loc":{"source":null,"start":{"line":7,"column":3},"end":{"line":7,"column":22}},
"range":[148,167],
"key":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":7,"column":3},"end":{"line":7,"column":6}},
Expand Down

0 comments on commit a4767a5

Please sign in to comment.