@@ -1385,15 +1385,15 @@ module V4 = struct
1385
1385
type 'a children = ListLiteral of 'a | Exact of 'a
1386
1386
1387
1387
(* if children is a list, convert it to an array while mapping each element. If not, just map over it, as usual *)
1388
- let transformChildrenIfListUpper ~loc ~ mapper theList =
1388
+ let transformChildrenIfListUpper ~mapper theList =
1389
1389
let rec transformChildren_ theList accum =
1390
1390
(* not in the sense of converting a list to an array; convert the AST
1391
1391
reprensentation of a list to the AST reprensentation of an array *)
1392
1392
match theList with
1393
1393
| {pexp_desc = Pexp_construct ({txt = Lident "[]" } , None)} -> (
1394
1394
match accum with
1395
1395
| [singleElement] -> Exact singleElement
1396
- | accum -> ListLiteral (Exp. array ~loc (List. rev accum)))
1396
+ | accum -> ListLiteral (Exp. array (List. rev accum)))
1397
1397
| {
1398
1398
pexp_desc =
1399
1399
Pexp_construct
@@ -1404,13 +1404,13 @@ module V4 = struct
1404
1404
in
1405
1405
transformChildren_ theList []
1406
1406
1407
- let transformChildrenIfList ~loc ~ mapper theList =
1407
+ let transformChildrenIfList ~mapper theList =
1408
1408
let rec transformChildren_ theList accum =
1409
1409
(* not in the sense of converting a list to an array; convert the AST
1410
1410
reprensentation of a list to the AST reprensentation of an array *)
1411
1411
match theList with
1412
1412
| {pexp_desc = Pexp_construct ({txt = Lident "[]" } , None)} ->
1413
- Exp. array ~loc (List. rev accum)
1413
+ Exp. array (List. rev accum)
1414
1414
| {
1415
1415
pexp_desc =
1416
1416
Pexp_construct
@@ -1421,7 +1421,7 @@ module V4 = struct
1421
1421
in
1422
1422
transformChildren_ theList []
1423
1423
1424
- let extractChildren ?(removeLastPositionUnit = false ) ~ loc propsAndChildren =
1424
+ let extractChildren ?(removeLastPositionUnit = false ) propsAndChildren =
1425
1425
let rec allButLast_ lst acc =
1426
1426
match lst with
1427
1427
| [] -> []
@@ -1445,7 +1445,7 @@ module V4 = struct
1445
1445
with
1446
1446
| [] , props ->
1447
1447
(* no children provided? Place a placeholder list *)
1448
- ( Exp. construct ~loc {loc; txt = Lident " []" } None ,
1448
+ ( Exp. construct {loc = Location. none ; txt = Lident " []" } None ,
1449
1449
if removeLastPositionUnit then allButLast props else props )
1450
1450
| [(_, childrenExpr)], props ->
1451
1451
(childrenExpr, if removeLastPositionUnit then allButLast props else props)
@@ -1523,7 +1523,7 @@ module V4 = struct
1523
1523
*)
1524
1524
1525
1525
(* make record from props and spread props if exists *)
1526
- let recordFromProps ?(removeKey = false ) callArguments =
1526
+ let recordFromProps ~ loc ?(removeKey = false ) callArguments =
1527
1527
let rec removeLastPositionUnitAux props acc =
1528
1528
match props with
1529
1529
| [] -> acc
@@ -1561,19 +1561,19 @@ module V4 = struct
1561
1561
| [] ->
1562
1562
{
1563
1563
pexp_desc = Pexp_record (fields, None );
1564
- pexp_loc = Location. none ;
1564
+ pexp_loc = loc ;
1565
1565
pexp_attributes = [] ;
1566
1566
}
1567
1567
| [spreadProps] ->
1568
1568
{
1569
1569
pexp_desc = Pexp_record (fields, Some spreadProps);
1570
- pexp_loc = Location. none ;
1570
+ pexp_loc = loc ;
1571
1571
pexp_attributes = [] ;
1572
1572
}
1573
1573
| spreadProps :: _ ->
1574
1574
{
1575
1575
pexp_desc = Pexp_record (fields, Some spreadProps);
1576
- pexp_loc = Location. none ;
1576
+ pexp_loc = loc ;
1577
1577
pexp_attributes = [] ;
1578
1578
}
1579
1579
@@ -1640,13 +1640,13 @@ module V4 = struct
1640
1640
let makePropsRecordTypeSig propsName loc namedTypeList =
1641
1641
Sig. type_ Nonrecursive (makeTypeDecls propsName loc namedTypeList)
1642
1642
1643
- let transformUppercaseCall3 ~config modulePath mapper loc attrs callArguments
1644
- =
1643
+ let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
1644
+ attrs callArguments =
1645
1645
let children, argsWithLabels =
1646
- extractChildren ~loc ~ remove LastPositionUnit:true callArguments
1646
+ extractChildren ~remove LastPositionUnit:true callArguments
1647
1647
in
1648
1648
let argsForMake = argsWithLabels in
1649
- let childrenExpr = transformChildrenIfListUpper ~loc ~ mapper children in
1649
+ let childrenExpr = transformChildrenIfListUpper ~mapper children in
1650
1650
let recursivelyTransformedArgsForMake =
1651
1651
argsForMake
1652
1652
|> List. map (fun (label , expression ) ->
@@ -1674,7 +1674,8 @@ module V4 = struct
1674
1674
| _ ->
1675
1675
[
1676
1676
( labelled " children" ,
1677
- Exp. ident ~loc {loc; txt = Ldot (Lident " React" , " null" )} );
1677
+ Exp. ident
1678
+ {loc = Location. none; txt = Ldot (Lident " React" , " null" )} );
1678
1679
])
1679
1680
in
1680
1681
@@ -1702,9 +1703,10 @@ module V4 = struct
1702
1703
match config.mode with
1703
1704
(* The new jsx transform *)
1704
1705
| "automatic" ->
1705
- let record = recordFromProps ~remove Key:true args in
1706
+ let record = recordFromProps ~loc: jsxExprLoc ~ remove Key:true args in
1706
1707
let props =
1707
- if isEmptyRecord record then recordWithOnlyKey ~loc else record
1708
+ if isEmptyRecord record then recordWithOnlyKey ~loc: jsxExprLoc
1709
+ else record
1708
1710
in
1709
1711
let keyProp =
1710
1712
args |> List. filter (fun (arg_label , _ ) -> " key" = getLabel arg_label)
@@ -1727,22 +1729,30 @@ module V4 = struct
1727
1729
[] )
1728
1730
in
1729
1731
Exp. apply ~attrs jsxExpr
1730
- ([(nolabel, Exp. ident {txt = ident; loc}); (nolabel, props)] @ key)
1732
+ ([
1733
+ (nolabel, Exp. ident {txt = ident; loc = callExprLoc});
1734
+ (nolabel, props);
1735
+ ]
1736
+ @ key)
1731
1737
| _ -> (
1732
- let record = recordFromProps args in
1738
+ let record = recordFromProps ~loc: jsxExprLoc args in
1733
1739
(* check if record which goes to Foo.make({ ... } as record) empty or not
1734
1740
if empty then change it to {key: @optional None} only for upper case jsx
1735
1741
This would be redundant regarding PR progress https://github.com/rescript-lang/syntax/pull/299
1736
1742
*)
1737
1743
let props =
1738
- if isEmptyRecord record then recordWithOnlyKey ~loc else record
1744
+ if isEmptyRecord record then recordWithOnlyKey ~loc: jsxExprLoc
1745
+ else record
1739
1746
in
1740
1747
match ! childrenArg with
1741
1748
| None ->
1742
1749
Exp. apply ~attrs
1743
1750
(Exp. ident
1744
1751
{loc = Location. none; txt = Ldot (Lident " React" , " createElement" )})
1745
- [(nolabel, Exp. ident {txt = ident; loc}); (nolabel, props)]
1752
+ [
1753
+ (nolabel, Exp. ident {txt = ident; loc = callExprLoc});
1754
+ (nolabel, props);
1755
+ ]
1746
1756
| Some children ->
1747
1757
Exp. apply ~attrs
1748
1758
(Exp. ident
@@ -1751,22 +1761,23 @@ module V4 = struct
1751
1761
txt = Ldot (Lident " React" , " createElementVariadic" );
1752
1762
})
1753
1763
[
1754
- (nolabel, Exp. ident {txt = ident; loc});
1764
+ (nolabel, Exp. ident {txt = ident; loc = callExprLoc });
1755
1765
(nolabel, props);
1756
1766
(nolabel, children);
1757
1767
])
1758
1768
[@@ raises Invalid_argument ]
1759
1769
1760
- let transformLowercaseCall3 ~config mapper loc attrs callArguments id =
1761
- let componentNameExpr = constantString ~loc id in
1770
+ let transformLowercaseCall3 ~config mapper jsxExprLoc callExprLoc attrs
1771
+ callArguments id =
1772
+ let componentNameExpr = constantString ~loc: callExprLoc id in
1762
1773
match config.mode with
1763
1774
(* the new jsx transform *)
1764
1775
| "automatic" ->
1765
1776
let children, nonChildrenProps =
1766
- extractChildren ~remove LastPositionUnit:true ~loc callArguments
1777
+ extractChildren ~remove LastPositionUnit:true callArguments
1767
1778
in
1768
1779
let argsForMake = nonChildrenProps in
1769
- let childrenExpr = transformChildrenIfListUpper ~loc ~ mapper children in
1780
+ let childrenExpr = transformChildrenIfListUpper ~mapper children in
1770
1781
let recursivelyTransformedArgsForMake =
1771
1782
argsForMake
1772
1783
|> List. map (fun (label , expression ) ->
@@ -1795,31 +1806,38 @@ module V4 = struct
1795
1806
| Pexp_record (labelDecls , _ ) when List. length labelDecls = 0 -> true
1796
1807
| _ -> false
1797
1808
in
1798
- let record = recordFromProps ~remove Key:true args in
1809
+ let record = recordFromProps ~loc: jsxExprLoc ~ remove Key:true args in
1799
1810
let props =
1800
- if isEmptyRecord record then recordWithOnlyKey ~loc else record
1811
+ if isEmptyRecord record then recordWithOnlyKey ~loc: jsxExprLoc
1812
+ else record
1801
1813
in
1802
1814
let keyProp =
1803
1815
args |> List. filter (fun (arg_label , _ ) -> " key" = getLabel arg_label)
1804
1816
in
1805
1817
let jsxExpr, key =
1806
1818
match (! childrenArg, keyProp) with
1807
1819
| None , (_ , keyExpr ) :: _ ->
1808
- ( Exp. ident ~loc {loc; txt = Ldot (Lident " ReactDOM" , " jsxKeyed" )},
1820
+ ( Exp. ident
1821
+ {loc = Location. none; txt = Ldot (Lident " ReactDOM" , " jsxKeyed" )},
1809
1822
[(nolabel, keyExpr)] )
1810
1823
| None , [] ->
1811
- (Exp. ident ~loc {loc; txt = Ldot (Lident " ReactDOM" , " jsx" )}, [] )
1824
+ ( Exp. ident
1825
+ {loc = Location. none; txt = Ldot (Lident " ReactDOM" , " jsx" )},
1826
+ [] )
1812
1827
| Some _ , (_ , keyExpr ) :: _ ->
1813
- ( Exp. ident ~loc {loc; txt = Ldot (Lident " ReactDOM" , " jsxsKeyed" )},
1828
+ ( Exp. ident
1829
+ {loc = Location. none; txt = Ldot (Lident " ReactDOM" , " jsxsKeyed" )},
1814
1830
[(nolabel, keyExpr)] )
1815
1831
| Some _ , [] ->
1816
- (Exp. ident ~loc {loc; txt = Ldot (Lident " ReactDOM" , " jsxs" )}, [] )
1832
+ ( Exp. ident
1833
+ {loc = Location. none; txt = Ldot (Lident " ReactDOM" , " jsxs" )},
1834
+ [] )
1817
1835
in
1818
- Exp. apply ~loc ~ attrs jsxExpr
1836
+ Exp. apply ~attrs jsxExpr
1819
1837
([(nolabel, componentNameExpr); (nolabel, props)] @ key)
1820
1838
| _ ->
1821
- let children, nonChildrenProps = extractChildren ~loc callArguments in
1822
- let childrenExpr = transformChildrenIfList ~loc ~ mapper children in
1839
+ let children, nonChildrenProps = extractChildren callArguments in
1840
+ let childrenExpr = transformChildrenIfList ~mapper children in
1823
1841
let createElementCall =
1824
1842
match children with
1825
1843
(* [@JSX] div(~children=[a]), coming from <div> a </div> *)
@@ -1848,9 +1866,12 @@ module V4 = struct
1848
1866
]
1849
1867
| nonEmptyProps ->
1850
1868
let propsCall =
1851
- Exp. apply ~loc
1852
- (Exp. ident ~loc
1853
- {loc; txt = Ldot (Lident " ReactDOMRe" , " domProps" )})
1869
+ Exp. apply
1870
+ (Exp. ident
1871
+ {
1872
+ loc = Location. none;
1873
+ txt = Ldot (Lident " ReactDOMRe" , " domProps" );
1874
+ })
1854
1875
(nonEmptyProps
1855
1876
|> List. map (fun (label , expression ) ->
1856
1877
(label, mapper.expr mapper expression)))
@@ -1864,12 +1885,13 @@ module V4 = struct
1864
1885
(nolabel, childrenExpr);
1865
1886
]
1866
1887
in
1867
- Exp. apply
1868
- ~loc (* throw away the [@JSX] attribute and keep the others, if any *)
1869
- ~attrs
1888
+ Exp. apply ~loc: jsxExprLoc ~attrs
1870
1889
(* ReactDOMRe.createElement *)
1871
- (Exp. ident ~loc
1872
- {loc; txt = Ldot (Lident " ReactDOMRe" , createElementCall)})
1890
+ (Exp. ident
1891
+ {
1892
+ loc = Location. none;
1893
+ txt = Ldot (Lident " ReactDOMRe" , createElementCall);
1894
+ })
1873
1895
args
1874
1896
[@@ raises Invalid_argument ]
1875
1897
@@ -2547,7 +2569,8 @@ module V4 = struct
2547
2569
| _ -> [item]
2548
2570
[@@ raises Invalid_argument ]
2549
2571
2550
- let transformJsxCall ~config mapper callExpression callArguments attrs =
2572
+ let transformJsxCall ~config mapper callExpression callArguments jsxExprLoc
2573
+ attrs =
2551
2574
match callExpression.pexp_desc with
2552
2575
| Pexp_ident caller -> (
2553
2576
match caller with
@@ -2557,13 +2580,14 @@ module V4 = struct
2557
2580
" JSX: `createElement` should be preceeded by a module name." )
2558
2581
(* Foo.createElement(~prop1=foo, ~prop2=bar, ~children=[], ()) *)
2559
2582
| {loc; txt = Ldot (modulePath , ("createElement" | "make" ))} ->
2560
- transformUppercaseCall3 ~config modulePath mapper loc attrs
2583
+ transformUppercaseCall3 ~config modulePath mapper jsxExprLoc loc attrs
2561
2584
callArguments
2562
2585
(* div(~prop1=foo, ~prop2=bar, ~children=[bla], ()) *)
2563
2586
(* turn that into
2564
2587
ReactDOMRe.createElement(~props=ReactDOMRe.props(~props1=foo, ~props2=bar, ()), [|bla|]) *)
2565
2588
| {loc; txt = Lident id } ->
2566
- transformLowercaseCall3 ~config mapper loc attrs callArguments id
2589
+ transformLowercaseCall3 ~config mapper jsxExprLoc loc attrs
2590
+ callArguments id
2567
2591
| {txt = Ldot (_ , anythingNotCreateElementOrMake )} ->
2568
2592
raise
2569
2593
(Invalid_argument
@@ -2586,8 +2610,11 @@ module V4 = struct
2586
2610
let expr ~config mapper expression =
2587
2611
match expression with
2588
2612
(* Does the function application have the @JSX attribute? *)
2589
- | {pexp_desc = Pexp_apply (callExpression, callArguments); pexp_attributes}
2590
- -> (
2613
+ | {
2614
+ pexp_desc = Pexp_apply (callExpression, callArguments);
2615
+ pexp_attributes;
2616
+ pexp_loc;
2617
+ } -> (
2591
2618
let jsxAttribute, nonJSXAttributes =
2592
2619
List. partition
2593
2620
(fun (attribute , _ ) -> attribute.txt = " JSX" )
@@ -2597,7 +2624,7 @@ module V4 = struct
2597
2624
(* no JSX attribute *)
2598
2625
| [] , _ -> default_mapper.expr mapper expression
2599
2626
| _ , nonJSXAttributes ->
2600
- transformJsxCall ~config mapper callExpression callArguments
2627
+ transformJsxCall ~config mapper callExpression callArguments pexp_loc
2601
2628
nonJSXAttributes)
2602
2629
(* is it a list with jsx attribute? Reason <>foo</> desugars to [@JSX][foo]*)
2603
2630
| {
@@ -2624,7 +2651,7 @@ module V4 = struct
2624
2651
| "classic" | _ ->
2625
2652
Exp. ident ~loc {loc; txt = Ldot (Lident " ReasonReact" , " fragment" )}
2626
2653
in
2627
- let childrenExpr = transformChildrenIfList ~loc ~ mapper listItems in
2654
+ let childrenExpr = transformChildrenIfList ~mapper listItems in
2628
2655
let args =
2629
2656
[
2630
2657
(nolabel, fragment);
0 commit comments