Skip to content

Commit 347ce4b

Browse files
authored
Sync the latest syntax (#5944)
1 parent 5be29e7 commit 347ce4b

File tree

6 files changed

+117
-44
lines changed

6 files changed

+117
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- Fix build error where aliasing arguments to `_` in the make function with JSX V4. https://github.com/rescript-lang/syntax/pull/720
3030
- Fix parsing of spread props as an expression in JSX V4 https://github.com/rescript-lang/syntax/pull/721
3131
- Fix dropping attributes from props in make function in JSX V4 https://github.com/rescript-lang/syntax/pull/723
32+
- Fix an issue where error messages related to duplicate props were displayed without a loc and were unclear https://github.com/rescript-lang/syntax/pull/728
3233

3334
# 10.1.0
3435

jscomp/gentype_tests/typescript-react-example/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274934,9 +274934,9 @@ let recordFromProps ~loc ~removeKey callArguments =
274934274934
(* let make = ({id, name, children}: props<'id, 'name, 'children>) *)
274935274935
let makePropsTypeParamsTvar namedTypeList =
274936274936
namedTypeList
274937-
|> List.filter_map (fun (_isOptional, label, _, _interiorType) ->
274937+
|> List.filter_map (fun (_isOptional, label, _, loc, _interiorType) ->
274938274938
if label = "key" then None
274939-
else Some (Typ.var @@ safeTypeFromValue (Labelled label)))
274939+
else Some (Typ.var ~loc @@ safeTypeFromValue (Labelled label)))
274940274940

274941274941
let stripOption coreType =
274942274942
match coreType with
@@ -274960,7 +274960,7 @@ let stripJsNullable coreType =
274960274960
let makePropsTypeParams ?(stripExplicitOption = false)
274961274961
?(stripExplicitJsNullableOfRef = false) namedTypeList =
274962274962
namedTypeList
274963-
|> List.filter_map (fun (isOptional, label, _, interiorType) ->
274963+
|> List.filter_map (fun (isOptional, label, _, loc, interiorType) ->
274964274964
if label = "key" then None
274965274965
(* TODO: Worth thinking how about "ref_" or "_ref" usages *)
274966274966
else if label = "ref" then
@@ -274969,7 +274969,7 @@ let makePropsTypeParams ?(stripExplicitOption = false)
274969274969
For example, if JSX ppx is used for React Native, type would be different.
274970274970
*)
274971274971
match interiorType with
274972-
| {ptyp_desc = Ptyp_var "ref"} -> Some (refType Location.none)
274972+
| {ptyp_desc = Ptyp_var "ref"} -> Some (refType loc)
274973274973
| _ ->
274974274974
(* Strip explicit Js.Nullable.t in case of forwardRef *)
274975274975
if stripExplicitJsNullableOfRef then stripJsNullable interiorType
@@ -274979,9 +274979,25 @@ let makePropsTypeParams ?(stripExplicitOption = false)
274979274979
else if isOptional && stripExplicitOption then stripOption interiorType
274980274980
else Some interiorType)
274981274981

274982-
let makeLabelDecls ~loc namedTypeList =
274982+
let makeLabelDecls namedTypeList =
274983+
let rec checkDuplicatedLabel l =
274984+
let rec mem_label ((_, (la : string), _, _, _) as x) = function
274985+
| [] -> false
274986+
| (_, (lb : string), _, _, _) :: l -> lb = la || mem_label x l
274987+
in
274988+
match l with
274989+
| [] -> ()
274990+
| hd :: tl ->
274991+
if mem_label hd tl then
274992+
let _, label, _, loc, _ = hd in
274993+
React_jsx_common.raiseError ~loc "JSX: found the duplicated prop `%s`"
274994+
label
274995+
else checkDuplicatedLabel tl
274996+
in
274997+
let () = namedTypeList |> List.rev |> checkDuplicatedLabel in
274998+
274983274999
namedTypeList
274984-
|> List.map (fun (isOptional, label, attrs, interiorType) ->
275000+
|> List.map (fun (isOptional, label, attrs, loc, interiorType) ->
274985275001
if label = "key" then
274986275002
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
274987275003
interiorType
@@ -274993,7 +275009,7 @@ let makeLabelDecls ~loc namedTypeList =
274993275009
(Typ.var @@ safeTypeFromValue @@ Labelled label))
274994275010

274995275011
let makeTypeDecls propsName loc namedTypeList =
274996-
let labelDeclList = makeLabelDecls ~loc namedTypeList in
275012+
let labelDeclList = makeLabelDecls namedTypeList in
274997275013
(* 'id, 'className, ... *)
274998275014
let params =
274999275015
makePropsTypeParamsTvar namedTypeList
@@ -275394,17 +275410,22 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
275394275410
in
275395275411
match (type_, name, default) with
275396275412
| Some type_, name, _ when isOptional name ->
275397-
(true, getLabel name, attrs, {type_ with ptyp_attributes = optionalAttrs})
275413+
( true,
275414+
getLabel name,
275415+
attrs,
275416+
loc,
275417+
{type_ with ptyp_attributes = optionalAttrs} )
275398275418
:: types
275399-
| Some type_, name, _ -> (false, getLabel name, attrs, type_) :: types
275419+
| Some type_, name, _ -> (false, getLabel name, attrs, loc, type_) :: types
275400275420
| None, name, _ when isOptional name ->
275401275421
( true,
275402275422
getLabel name,
275403275423
attrs,
275424+
loc,
275404275425
Typ.var ~loc ~attrs:optionalAttrs (safeTypeFromValue name) )
275405275426
:: types
275406275427
| None, name, _ when isLabelled name ->
275407-
(false, getLabel name, attrs, Typ.var ~loc (safeTypeFromValue name))
275428+
(false, getLabel name, attrs, loc, Typ.var ~loc (safeTypeFromValue name))
275408275429
:: types
275409275430
| _ -> types
275410275431

@@ -275413,10 +275434,12 @@ let argWithDefaultValue (name, default, _, _, _, _) =
275413275434
| Some default when isOptional name -> Some (getLabel name, default)
275414275435
| _ -> None
275415275436

275416-
let argToConcreteType types (name, attrs, _loc, type_) =
275437+
let argToConcreteType types (name, attrs, loc, type_) =
275417275438
match name with
275418-
| name when isLabelled name -> (false, getLabel name, attrs, type_) :: types
275419-
| name when isOptional name -> (true, getLabel name, attrs, type_) :: types
275439+
| name when isLabelled name ->
275440+
(false, getLabel name, attrs, loc, type_) :: types
275441+
| name when isOptional name ->
275442+
(true, getLabel name, attrs, loc, type_) :: types
275420275443
| _ -> types
275421275444

275422275445
let check_string_int_attribute_iter =
@@ -275971,7 +275994,8 @@ let transformSignatureItem ~config _mapper item =
275971275994
makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType "props"
275972275995
psig_loc
275973275996
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
275974-
(if !hasForwardRef then [(true, "ref", [], refType Location.none)]
275997+
(if !hasForwardRef then
275998+
[(true, "ref", [], Location.none, refType Location.none)]
275975275999
else [])
275976276000
@ namedTypeList)
275977276001
in

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274934,9 +274934,9 @@ let recordFromProps ~loc ~removeKey callArguments =
274934274934
(* let make = ({id, name, children}: props<'id, 'name, 'children>) *)
274935274935
let makePropsTypeParamsTvar namedTypeList =
274936274936
namedTypeList
274937-
|> List.filter_map (fun (_isOptional, label, _, _interiorType) ->
274937+
|> List.filter_map (fun (_isOptional, label, _, loc, _interiorType) ->
274938274938
if label = "key" then None
274939-
else Some (Typ.var @@ safeTypeFromValue (Labelled label)))
274939+
else Some (Typ.var ~loc @@ safeTypeFromValue (Labelled label)))
274940274940

274941274941
let stripOption coreType =
274942274942
match coreType with
@@ -274960,7 +274960,7 @@ let stripJsNullable coreType =
274960274960
let makePropsTypeParams ?(stripExplicitOption = false)
274961274961
?(stripExplicitJsNullableOfRef = false) namedTypeList =
274962274962
namedTypeList
274963-
|> List.filter_map (fun (isOptional, label, _, interiorType) ->
274963+
|> List.filter_map (fun (isOptional, label, _, loc, interiorType) ->
274964274964
if label = "key" then None
274965274965
(* TODO: Worth thinking how about "ref_" or "_ref" usages *)
274966274966
else if label = "ref" then
@@ -274969,7 +274969,7 @@ let makePropsTypeParams ?(stripExplicitOption = false)
274969274969
For example, if JSX ppx is used for React Native, type would be different.
274970274970
*)
274971274971
match interiorType with
274972-
| {ptyp_desc = Ptyp_var "ref"} -> Some (refType Location.none)
274972+
| {ptyp_desc = Ptyp_var "ref"} -> Some (refType loc)
274973274973
| _ ->
274974274974
(* Strip explicit Js.Nullable.t in case of forwardRef *)
274975274975
if stripExplicitJsNullableOfRef then stripJsNullable interiorType
@@ -274979,9 +274979,25 @@ let makePropsTypeParams ?(stripExplicitOption = false)
274979274979
else if isOptional && stripExplicitOption then stripOption interiorType
274980274980
else Some interiorType)
274981274981

274982-
let makeLabelDecls ~loc namedTypeList =
274982+
let makeLabelDecls namedTypeList =
274983+
let rec checkDuplicatedLabel l =
274984+
let rec mem_label ((_, (la : string), _, _, _) as x) = function
274985+
| [] -> false
274986+
| (_, (lb : string), _, _, _) :: l -> lb = la || mem_label x l
274987+
in
274988+
match l with
274989+
| [] -> ()
274990+
| hd :: tl ->
274991+
if mem_label hd tl then
274992+
let _, label, _, loc, _ = hd in
274993+
React_jsx_common.raiseError ~loc "JSX: found the duplicated prop `%s`"
274994+
label
274995+
else checkDuplicatedLabel tl
274996+
in
274997+
let () = namedTypeList |> List.rev |> checkDuplicatedLabel in
274998+
274983274999
namedTypeList
274984-
|> List.map (fun (isOptional, label, attrs, interiorType) ->
275000+
|> List.map (fun (isOptional, label, attrs, loc, interiorType) ->
274985275001
if label = "key" then
274986275002
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
274987275003
interiorType
@@ -274993,7 +275009,7 @@ let makeLabelDecls ~loc namedTypeList =
274993275009
(Typ.var @@ safeTypeFromValue @@ Labelled label))
274994275010

274995275011
let makeTypeDecls propsName loc namedTypeList =
274996-
let labelDeclList = makeLabelDecls ~loc namedTypeList in
275012+
let labelDeclList = makeLabelDecls namedTypeList in
274997275013
(* 'id, 'className, ... *)
274998275014
let params =
274999275015
makePropsTypeParamsTvar namedTypeList
@@ -275394,17 +275410,22 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
275394275410
in
275395275411
match (type_, name, default) with
275396275412
| Some type_, name, _ when isOptional name ->
275397-
(true, getLabel name, attrs, {type_ with ptyp_attributes = optionalAttrs})
275413+
( true,
275414+
getLabel name,
275415+
attrs,
275416+
loc,
275417+
{type_ with ptyp_attributes = optionalAttrs} )
275398275418
:: types
275399-
| Some type_, name, _ -> (false, getLabel name, attrs, type_) :: types
275419+
| Some type_, name, _ -> (false, getLabel name, attrs, loc, type_) :: types
275400275420
| None, name, _ when isOptional name ->
275401275421
( true,
275402275422
getLabel name,
275403275423
attrs,
275424+
loc,
275404275425
Typ.var ~loc ~attrs:optionalAttrs (safeTypeFromValue name) )
275405275426
:: types
275406275427
| None, name, _ when isLabelled name ->
275407-
(false, getLabel name, attrs, Typ.var ~loc (safeTypeFromValue name))
275428+
(false, getLabel name, attrs, loc, Typ.var ~loc (safeTypeFromValue name))
275408275429
:: types
275409275430
| _ -> types
275410275431

@@ -275413,10 +275434,12 @@ let argWithDefaultValue (name, default, _, _, _, _) =
275413275434
| Some default when isOptional name -> Some (getLabel name, default)
275414275435
| _ -> None
275415275436

275416-
let argToConcreteType types (name, attrs, _loc, type_) =
275437+
let argToConcreteType types (name, attrs, loc, type_) =
275417275438
match name with
275418-
| name when isLabelled name -> (false, getLabel name, attrs, type_) :: types
275419-
| name when isOptional name -> (true, getLabel name, attrs, type_) :: types
275439+
| name when isLabelled name ->
275440+
(false, getLabel name, attrs, loc, type_) :: types
275441+
| name when isOptional name ->
275442+
(true, getLabel name, attrs, loc, type_) :: types
275420275443
| _ -> types
275421275444

275422275445
let check_string_int_attribute_iter =
@@ -275971,7 +275994,8 @@ let transformSignatureItem ~config _mapper item =
275971275994
makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType "props"
275972275995
psig_loc
275973275996
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
275974-
(if !hasForwardRef then [(true, "ref", [], refType Location.none)]
275997+
(if !hasForwardRef then
275998+
[(true, "ref", [], Location.none, refType Location.none)]
275975275999
else [])
275976276000
@ namedTypeList)
275977276001
in

lib/4.06.1/whole_compiler.ml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -285318,9 +285318,9 @@ let recordFromProps ~loc ~removeKey callArguments =
285318285318
(* let make = ({id, name, children}: props<'id, 'name, 'children>) *)
285319285319
let makePropsTypeParamsTvar namedTypeList =
285320285320
namedTypeList
285321-
|> List.filter_map (fun (_isOptional, label, _, _interiorType) ->
285321+
|> List.filter_map (fun (_isOptional, label, _, loc, _interiorType) ->
285322285322
if label = "key" then None
285323-
else Some (Typ.var @@ safeTypeFromValue (Labelled label)))
285323+
else Some (Typ.var ~loc @@ safeTypeFromValue (Labelled label)))
285324285324

285325285325
let stripOption coreType =
285326285326
match coreType with
@@ -285344,7 +285344,7 @@ let stripJsNullable coreType =
285344285344
let makePropsTypeParams ?(stripExplicitOption = false)
285345285345
?(stripExplicitJsNullableOfRef = false) namedTypeList =
285346285346
namedTypeList
285347-
|> List.filter_map (fun (isOptional, label, _, interiorType) ->
285347+
|> List.filter_map (fun (isOptional, label, _, loc, interiorType) ->
285348285348
if label = "key" then None
285349285349
(* TODO: Worth thinking how about "ref_" or "_ref" usages *)
285350285350
else if label = "ref" then
@@ -285353,7 +285353,7 @@ let makePropsTypeParams ?(stripExplicitOption = false)
285353285353
For example, if JSX ppx is used for React Native, type would be different.
285354285354
*)
285355285355
match interiorType with
285356-
| {ptyp_desc = Ptyp_var "ref"} -> Some (refType Location.none)
285356+
| {ptyp_desc = Ptyp_var "ref"} -> Some (refType loc)
285357285357
| _ ->
285358285358
(* Strip explicit Js.Nullable.t in case of forwardRef *)
285359285359
if stripExplicitJsNullableOfRef then stripJsNullable interiorType
@@ -285363,9 +285363,25 @@ let makePropsTypeParams ?(stripExplicitOption = false)
285363285363
else if isOptional && stripExplicitOption then stripOption interiorType
285364285364
else Some interiorType)
285365285365

285366-
let makeLabelDecls ~loc namedTypeList =
285366+
let makeLabelDecls namedTypeList =
285367+
let rec checkDuplicatedLabel l =
285368+
let rec mem_label ((_, (la : string), _, _, _) as x) = function
285369+
| [] -> false
285370+
| (_, (lb : string), _, _, _) :: l -> lb = la || mem_label x l
285371+
in
285372+
match l with
285373+
| [] -> ()
285374+
| hd :: tl ->
285375+
if mem_label hd tl then
285376+
let _, label, _, loc, _ = hd in
285377+
React_jsx_common.raiseError ~loc "JSX: found the duplicated prop `%s`"
285378+
label
285379+
else checkDuplicatedLabel tl
285380+
in
285381+
let () = namedTypeList |> List.rev |> checkDuplicatedLabel in
285382+
285367285383
namedTypeList
285368-
|> List.map (fun (isOptional, label, attrs, interiorType) ->
285384+
|> List.map (fun (isOptional, label, attrs, loc, interiorType) ->
285369285385
if label = "key" then
285370285386
Type.field ~loc ~attrs:(optionalAttrs @ attrs) {txt = label; loc}
285371285387
interiorType
@@ -285377,7 +285393,7 @@ let makeLabelDecls ~loc namedTypeList =
285377285393
(Typ.var @@ safeTypeFromValue @@ Labelled label))
285378285394

285379285395
let makeTypeDecls propsName loc namedTypeList =
285380-
let labelDeclList = makeLabelDecls ~loc namedTypeList in
285396+
let labelDeclList = makeLabelDecls namedTypeList in
285381285397
(* 'id, 'className, ... *)
285382285398
let params =
285383285399
makePropsTypeParamsTvar namedTypeList
@@ -285778,17 +285794,22 @@ let argToType ~newtypes ~(typeConstraints : core_type option) types
285778285794
in
285779285795
match (type_, name, default) with
285780285796
| Some type_, name, _ when isOptional name ->
285781-
(true, getLabel name, attrs, {type_ with ptyp_attributes = optionalAttrs})
285797+
( true,
285798+
getLabel name,
285799+
attrs,
285800+
loc,
285801+
{type_ with ptyp_attributes = optionalAttrs} )
285782285802
:: types
285783-
| Some type_, name, _ -> (false, getLabel name, attrs, type_) :: types
285803+
| Some type_, name, _ -> (false, getLabel name, attrs, loc, type_) :: types
285784285804
| None, name, _ when isOptional name ->
285785285805
( true,
285786285806
getLabel name,
285787285807
attrs,
285808+
loc,
285788285809
Typ.var ~loc ~attrs:optionalAttrs (safeTypeFromValue name) )
285789285810
:: types
285790285811
| None, name, _ when isLabelled name ->
285791-
(false, getLabel name, attrs, Typ.var ~loc (safeTypeFromValue name))
285812+
(false, getLabel name, attrs, loc, Typ.var ~loc (safeTypeFromValue name))
285792285813
:: types
285793285814
| _ -> types
285794285815

@@ -285797,10 +285818,12 @@ let argWithDefaultValue (name, default, _, _, _, _) =
285797285818
| Some default when isOptional name -> Some (getLabel name, default)
285798285819
| _ -> None
285799285820

285800-
let argToConcreteType types (name, attrs, _loc, type_) =
285821+
let argToConcreteType types (name, attrs, loc, type_) =
285801285822
match name with
285802-
| name when isLabelled name -> (false, getLabel name, attrs, type_) :: types
285803-
| name when isOptional name -> (true, getLabel name, attrs, type_) :: types
285823+
| name when isLabelled name ->
285824+
(false, getLabel name, attrs, loc, type_) :: types
285825+
| name when isOptional name ->
285826+
(true, getLabel name, attrs, loc, type_) :: types
285804285827
| _ -> types
285805285828

285806285829
let check_string_int_attribute_iter =
@@ -286355,7 +286378,8 @@ let transformSignatureItem ~config _mapper item =
286355286378
makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType "props"
286356286379
psig_loc
286357286380
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
286358-
(if !hasForwardRef then [(true, "ref", [], refType Location.none)]
286381+
(if !hasForwardRef then
286382+
[(true, "ref", [], Location.none, refType Location.none)]
286359286383
else [])
286360286384
@ namedTypeList)
286361286385
in

0 commit comments

Comments
 (0)