Skip to content

Commit

Permalink
Allow trailing comma after rest param in component declaration
Browse files Browse the repository at this point in the history
Summary:
There is no reason why we don't want to allow that, other than keeping consistent with the behavior of parsing function rest params. We can't change the JS spec for function rest params, but we can for component syntax to make it more permissive.

Prettier will still remove the trailing comma if we still care about the consistency of checked-in code, but it will at least make the experience of manually convert function component to component syntax slightly easier.

Changelog: [parser] Trailing comma is now allowed after rest parameter in component syntax.

Reviewed By: alexmckenley

Differential Revision: D63488906

fbshipit-source-id: d3afbdd5163de46ef11caf0824c45261af8c7482
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Sep 27, 2024
1 parent df839b7 commit f5ca193
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ component Comp2(...rest: TRest) {}
component Comp3(param, ...rest) {}

component Comp4(param: TParam, ...rest: TRest) {}

component Comp5(param: TParam, ...rest: TRest,) {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":7,"column":49}},
"range":[0,150],
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":9,"column":50}},
"range":[0,202],
"body":[
{
"type":"ComponentDeclaration",
Expand Down Expand Up @@ -244,6 +244,100 @@
],
"rendersType":null,
"typeParameters":null
},
{
"type":"ComponentDeclaration",
"loc":{"source":null,"start":{"line":9,"column":0},"end":{"line":9,"column":50}},
"range":[152,202],
"body":{
"type":"BlockStatement",
"loc":{"source":null,"start":{"line":9,"column":48},"end":{"line":9,"column":50}},
"range":[200,202],
"body":[]
},
"id":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":10},"end":{"line":9,"column":15}},
"range":[162,167],
"name":"Comp5",
"typeAnnotation":null,
"optional":false
},
"params":[
{
"type":"ComponentParameter",
"loc":{"source":null,"start":{"line":9,"column":16},"end":{"line":9,"column":29}},
"range":[168,181],
"name":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":16},"end":{"line":9,"column":21}},
"range":[168,173],
"name":"param",
"typeAnnotation":null,
"optional":false
},
"local":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":16},"end":{"line":9,"column":29}},
"range":[168,181],
"name":"param",
"typeAnnotation":{
"type":"TypeAnnotation",
"loc":{"source":null,"start":{"line":9,"column":21},"end":{"line":9,"column":29}},
"range":[173,181],
"typeAnnotation":{
"type":"GenericTypeAnnotation",
"loc":{"source":null,"start":{"line":9,"column":23},"end":{"line":9,"column":29}},
"range":[175,181],
"id":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":23},"end":{"line":9,"column":29}},
"range":[175,181],
"name":"TParam",
"typeAnnotation":null,
"optional":false
},
"typeParameters":null
}
},
"optional":false
},
"shorthand":true
},
{
"type":"RestElement",
"loc":{"source":null,"start":{"line":9,"column":31},"end":{"line":9,"column":45}},
"range":[183,197],
"argument":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":34},"end":{"line":9,"column":45}},
"range":[186,197],
"name":"rest",
"typeAnnotation":{
"type":"TypeAnnotation",
"loc":{"source":null,"start":{"line":9,"column":38},"end":{"line":9,"column":45}},
"range":[190,197],
"typeAnnotation":{
"type":"GenericTypeAnnotation",
"loc":{"source":null,"start":{"line":9,"column":40},"end":{"line":9,"column":45}},
"range":[192,197],
"id":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":9,"column":40},"end":{"line":9,"column":45}},
"range":[192,197],
"name":"TRest",
"typeAnnotation":null,
"optional":false
},
"typeParameters":null
}
},
"optional":false
}
}
],
"rendersType":null,
"typeParameters":null
}
],
"comments":[]
Expand Down
1 change: 1 addition & 0 deletions lib/Parser/JSParserImpl-flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ bool JSParserImpl::parseComponentParametersFlow(
if (!optRestElem)
return false;
paramList.push_back(*optRestElem.getValue());
checkAndEat(TokenKind::comma, JSLexer::GrammarContext::Type);
break;
}

Expand Down

0 comments on commit f5ca193

Please sign in to comment.