Skip to content

Commit effb2a1

Browse files
authored
Sync latest syntax (#5909)
1 parent 50aa83e commit effb2a1

File tree

6 files changed

+237
-208
lines changed

6 files changed

+237
-208
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 10.1.1
1414

15+
#### :boom: Breaking Change
16+
17+
- Parse the attributes of labelled argument to the pattern attributes of argument instead of function. https://github.com/rescript-lang/syntax/pull/722
18+
1519
#### :rocket: New Feature
1620

1721
- Add support for empty inlined record literal `{}` for inlined records where all fields are optional https://github.com/rescript-lang/rescript-compiler/pull/5900
@@ -22,6 +26,7 @@
2226
- Revert to ubuntu-18.04 in CI to support Linux versions with older glibc https://github.com/rescript-lang/rescript-compiler/issues/5868
2327
- Fix build error where aliasing arguments to `_` in the make function with JSX V4. https://github.com/rescript-lang/syntax/pull/720
2428
- Fix parsing of spread props as an expression in JSX V4 https://github.com/rescript-lang/syntax/pull/721
29+
- Fix dropping attributes from props in make function in JSX V4 https://github.com/rescript-lang/syntax/pull/723
2530

2631
# 10.1.0
2732

jscomp/napkin/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#### :boom: Breaking Change
1414

1515
- Emit an error when a `@string` or `@int` attribute is used in a V4 component https://github.com/rescript-lang/rescript-compiler/issues/5724
16+
- Parse the attributes of labelled argument to the pattern attributes of argument instead of function. https://github.com/rescript-lang/syntax/pull/722
1617

1718
#### :rocket: New Feature
1819

@@ -53,6 +54,7 @@
5354
- Treat await as almost-unary operator weaker than pipe so `await foo->bar` means `await (foo->bar)` https://github.com/rescript-lang/syntax/pull/711
5455
- Fix build error where aliasing arguments to `_` in the make function with JSX V4. https://github.com/rescript-lang/syntax/pull/720
5556
- Fix parsing of spread props as an expression in JSX V4 https://github.com/rescript-lang/syntax/pull/721
57+
- Fix dropping attributes from props in make function in JSX V4 https://github.com/rescript-lang/syntax/pull/723
5658

5759
#### :eyeglasses: Spec Compliance
5860

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49712,7 +49712,7 @@ let funExpr expr =
4971249712
collectNewTypes (stringLoc :: acc) returnExpr
4971349713
| returnExpr -> (List.rev acc, returnExpr)
4971449714
in
49715-
let rec collect n attrsBefore acc expr =
49715+
let rec collect attrsBefore acc expr =
4971649716
match expr with
4971749717
| {
4971849718
pexp_desc =
@@ -49723,48 +49723,28 @@ let funExpr expr =
4972349723
{pexp_desc = Pexp_apply _} );
4972449724
} ->
4972549725
(attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
49726-
| {
49727-
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49728-
pexp_attributes = [];
49729-
} ->
49730-
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49731-
collect (n + 1) attrsBefore (parameter :: acc) returnExpr
4973249726
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
4973349727
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
4973449728
let param = NewTypes {attrs; locs = stringLocs} in
49735-
collect (n + 1) attrsBefore (param :: acc) returnExpr
49736-
| {pexp_desc = Pexp_fun _; pexp_attributes}
49737-
when pexp_attributes
49738-
|> List.exists (fun ({Location.txt}, _) ->
49739-
txt = "bs" || txt = "res.async")
49740-
&& n > 0 ->
49741-
(* stop here, the uncurried or async attribute always indicates the beginning of an arrow function
49742-
* e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
49743-
(attrsBefore, List.rev acc, expr)
49729+
collect attrsBefore (param :: acc) returnExpr
4974449730
| {
49745-
pexp_desc =
49746-
Pexp_fun
49747-
(((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
49748-
pexp_attributes = attrs;
49731+
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49732+
pexp_attributes = [];
4974949733
} ->
49750-
(* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
49751-
In the case of `@res.async`, pass the attribute to the outside *)
49752-
let attrs_async, attrs_other =
49753-
attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
49754-
in
49755-
let parameter =
49756-
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
49757-
in
49758-
collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
49734+
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49735+
collect attrsBefore (parameter :: acc) returnExpr
49736+
(* If a fun has an attribute, then it stops here and makes currying.
49737+
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
49738+
| {pexp_desc = Pexp_fun _} -> (attrsBefore, List.rev acc, expr)
4975949739
| expr -> (attrsBefore, List.rev acc, expr)
4976049740
in
4976149741
match expr with
4976249742
| {
49763-
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
49743+
pexp_desc = Pexp_fun (_, _defaultExpr, _pattern, _returnExpr);
4976449744
pexp_attributes = attrs;
4976549745
} as expr ->
49766-
collect 0 attrs [] {expr with pexp_attributes = []}
49767-
| expr -> collect 0 [] [] expr
49746+
collect attrs [] {expr with pexp_attributes = []}
49747+
| expr -> collect [] [] expr
4976849748

4976949749
let processBracesAttr expr =
4977049750
match expr.pexp_attributes with
@@ -57955,13 +57935,23 @@ and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
5795557935
attrs = [];
5795657936
lbl = Asttypes.Nolabel;
5795757937
defaultExpr = None;
57958-
pat = {Parsetree.ppat_desc = Ppat_var stringLoc};
57938+
pat =
57939+
{
57940+
Parsetree.ppat_desc = Ppat_var stringLoc;
57941+
Parsetree.ppat_attributes = attrs;
57942+
};
5795957943
};
5796057944
]
5796157945
when not uncurried ->
5796257946
let txtDoc =
5796357947
let var = printIdentLike stringLoc.txt in
57964-
let var = if hasConstraint then addParens var else var in
57948+
let var =
57949+
match attrs with
57950+
| [] -> if hasConstraint then addParens var else var
57951+
| attrs ->
57952+
let attrs = printAttributes ~customLayout attrs cmtTbl in
57953+
addParens (Doc.concat [attrs; var])
57954+
in
5796557955
if async then addAsync var else var
5796657956
in
5796757957
printComments txtDoc cmtTbl stringLoc.loc
@@ -58052,22 +58042,25 @@ and printExpFunParameter ~customLayout parameter cmtTbl =
5805258042
match (lbl, pattern) with
5805358043
| Asttypes.Nolabel, pattern -> printPattern ~customLayout pattern cmtTbl
5805458044
| ( (Asttypes.Labelled lbl | Optional lbl),
58055-
{
58056-
ppat_desc = Ppat_var stringLoc;
58057-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58058-
} )
58045+
{ppat_desc = Ppat_var stringLoc; ppat_attributes} )
5805958046
when lbl = stringLoc.txt ->
5806058047
(* ~d *)
58061-
Doc.concat [Doc.text "~"; printIdentLike lbl]
58048+
Doc.concat
58049+
[
58050+
printAttributes ~customLayout ppat_attributes cmtTbl;
58051+
Doc.text "~";
58052+
printIdentLike lbl;
58053+
]
5806258054
| ( (Asttypes.Labelled lbl | Optional lbl),
5806358055
{
5806458056
ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ);
58065-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58057+
ppat_attributes;
5806658058
} )
5806758059
when lbl = txt ->
5806858060
(* ~d: e *)
5806958061
Doc.concat
5807058062
[
58063+
printAttributes ~customLayout ppat_attributes cmtTbl;
5807158064
Doc.text "~";
5807258065
printIdentLike lbl;
5807358066
Doc.text ": ";
@@ -274988,14 +274981,15 @@ let makePropsTypeParams ?(stripExplicitOption = false)
274988274981

274989274982
let makeLabelDecls ~loc namedTypeList =
274990274983
namedTypeList
274991-
|> List.map (fun (isOptional, label, _, interiorType) ->
274984+
|> List.map (fun (isOptional, label, attrs, interiorType) ->
274992274985
if label = "key" then
274993-
Type.field ~loc ~attrs:optionalAttrs {txt = label; loc} interiorType
274986+
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
274987+
interiorType
274994274988
else if isOptional then
274995-
Type.field ~loc ~attrs:optionalAttrs {txt = label; loc}
274989+
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
274996274990
(Typ.var @@ safeTypeFromValue @@ Labelled label)
274997274991
else
274998-
Type.field ~loc {txt = label; loc}
274992+
Type.field ~loc ~attrs {txt = label; loc}
274999274993
(Typ.var @@ safeTypeFromValue @@ Labelled label))
275000274994

275001274995
let makeTypeDecls propsName loc namedTypeList =
@@ -275380,7 +275374,9 @@ let newtypeToVar newtype type_ =
275380275374
mapper.typ mapper type_
275381275375

275382275376
let argToType ~newtypes ~(typeConstraints : core_type option) types
275383-
(name, default, _noLabelName, _alias, loc, type_) =
275377+
((name, default, {ppat_attributes = attrs}, _alias, loc, type_) :
275378+
arg_label * expression option * pattern * label * 'loc * core_type option)
275379+
=
275384275380
let rec getType name coreType =
275385275381
match coreType with
275386275382
| {ptyp_desc = Ptyp_arrow (arg, c1, c2)} ->
@@ -275398,28 +275394,29 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
275398275394
in
275399275395
match (type_, name, default) with
275400275396
| Some type_, name, _ when isOptional name ->
275401-
(true, getLabel name, [], {type_ with ptyp_attributes = optionalAttrs})
275397+
(true, getLabel name, attrs, {type_ with ptyp_attributes = optionalAttrs})
275402275398
:: types
275403-
| Some type_, name, _ -> (false, getLabel name, [], type_) :: types
275399+
| Some type_, name, _ -> (false, getLabel name, attrs, type_) :: types
275404275400
| None, name, _ when isOptional name ->
275405275401
( true,
275406275402
getLabel name,
275407-
[],
275403+
attrs,
275408275404
Typ.var ~loc ~attrs:optionalAttrs (safeTypeFromValue name) )
275409275405
:: types
275410275406
| None, name, _ when isLabelled name ->
275411-
(false, getLabel name, [], Typ.var ~loc (safeTypeFromValue name)) :: types
275407+
(false, getLabel name, attrs, Typ.var ~loc (safeTypeFromValue name))
275408+
:: types
275412275409
| _ -> types
275413275410

275414275411
let argWithDefaultValue (name, default, _, _, _, _) =
275415275412
match default with
275416275413
| Some default when isOptional name -> Some (getLabel name, default)
275417275414
| _ -> None
275418275415

275419-
let argToConcreteType types (name, _loc, type_) =
275416+
let argToConcreteType types (name, attrs, _loc, type_) =
275420275417
match name with
275421-
| name when isLabelled name -> (false, getLabel name, [], type_) :: types
275422-
| name when isOptional name -> (true, getLabel name, [], type_) :: types
275418+
| name when isLabelled name -> (false, getLabel name, attrs, type_) :: types
275419+
| name when isOptional name -> (true, getLabel name, attrs, type_) :: types
275423275420
| _ -> types
275424275421

275425275422
let check_string_int_attribute_iter =
@@ -275456,15 +275453,19 @@ let transformStructureItem ~config mapper item =
275456275453
|> Option.map React_jsx_common.typVarsOfCoreType
275457275454
|> Option.value ~default:[]
275458275455
in
275459-
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType) =
275456+
let rec getPropTypes types
275457+
({ptyp_loc; ptyp_desc; ptyp_attributes} as fullType) =
275460275458
match ptyp_desc with
275461275459
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
275462275460
when isLabelled name || isOptional name ->
275463-
getPropTypes ((name, ptyp_loc, type_) :: types) rest
275461+
getPropTypes
275462+
((name, ptyp_attributes, ptyp_loc, type_) :: types)
275463+
rest
275464275464
| Ptyp_arrow (Nolabel, _type, rest) -> getPropTypes types rest
275465275465
| Ptyp_arrow (name, type_, returnValue)
275466275466
when isLabelled name || isOptional name ->
275467-
(returnValue, (name, returnValue.ptyp_loc, type_) :: types)
275467+
( returnValue,
275468+
(name, ptyp_attributes, returnValue.ptyp_loc, type_) :: types )
275468275469
| _ -> (fullType, types)
275469275470
in
275470275471
let innerType, propTypes = getPropTypes [] pval_type in
@@ -275936,19 +275937,22 @@ let transformSignatureItem ~config _mapper item =
275936275937
in
275937275938
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType) =
275938275939
match ptyp_desc with
275939-
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
275940+
| Ptyp_arrow
275941+
( name,
275942+
({ptyp_attributes = attrs} as type_),
275943+
({ptyp_desc = Ptyp_arrow _} as rest) )
275940275944
when isOptional name || isLabelled name ->
275941-
getPropTypes ((name, ptyp_loc, type_) :: types) rest
275945+
getPropTypes ((name, attrs, ptyp_loc, type_) :: types) rest
275942275946
| Ptyp_arrow
275943275947
(Nolabel, {ptyp_desc = Ptyp_constr ({txt = Lident "unit"}, _)}, rest)
275944275948
->
275945275949
getPropTypes types rest
275946275950
| Ptyp_arrow (Nolabel, _type, rest) ->
275947275951
hasForwardRef := true;
275948275952
getPropTypes types rest
275949-
| Ptyp_arrow (name, type_, returnValue)
275953+
| Ptyp_arrow (name, ({ptyp_attributes = attrs} as type_), returnValue)
275950275954
when isOptional name || isLabelled name ->
275951-
(returnValue, (name, returnValue.ptyp_loc, type_) :: types)
275955+
(returnValue, (name, attrs, returnValue.ptyp_loc, type_) :: types)
275952275956
| _ -> (fullType, types)
275953275957
in
275954275958
let innerType, propTypes = getPropTypes [] pval_type in

0 commit comments

Comments
 (0)