diff --git a/src/parser/match_pattern_parser.ml b/src/parser/match_pattern_parser.ml index 430a32e51fb..6ba1f6621bd 100644 --- a/src/parser/match_pattern_parser.ml +++ b/src/parser/match_pattern_parser.ml @@ -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 diff --git a/src/parser/test/flow/match/pattern-array.tree.json b/src/parser/test/flow/match/pattern-array.tree.json index 1d43b633434..29de504f78d 100644 --- a/src/parser/test/flow/match/pattern-array.tree.json +++ b/src/parser/test/flow/match/pattern-array.tree.json @@ -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}}, diff --git a/src/parser/test/flow/match/pattern-object-rest-non-last.tree.json b/src/parser/test/flow/match/pattern-object-rest-non-last.tree.json index 7027c4da221..4a03a6cb5db 100644 --- a/src/parser/test/flow/match/pattern-object-rest-non-last.tree.json +++ b/src/parser/test/flow/match/pattern-object-rest-non-last.tree.json @@ -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}}, diff --git a/src/parser/test/flow/match/pattern-object.tree.json b/src/parser/test/flow/match/pattern-object.tree.json index 997fa7d67ee..df118b9b9a7 100644 --- a/src/parser/test/flow/match/pattern-object.tree.json +++ b/src/parser/test/flow/match/pattern-object.tree.json @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, diff --git a/src/parser/test/flow/match/pattern-or-as.tree.json b/src/parser/test/flow/match/pattern-or-as.tree.json index 9869e470999..c94a4462ed4 100644 --- a/src/parser/test/flow/match/pattern-or-as.tree.json +++ b/src/parser/test/flow/match/pattern-or-as.tree.json @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}}, @@ -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}},