Skip to content

Commit bbba8c4

Browse files
committed
Constrain innermost/return expression; do not use outer loc
1 parent 9b77ccb commit bbba8c4

File tree

11 files changed

+77
-89
lines changed

11 files changed

+77
-89
lines changed

compiler/syntax/src/jsx_v4.ml

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,26 @@ let ref_type loc =
4949
let jsx_element_type ~loc =
5050
Typ.constr ~loc {loc; txt = Ldot (Lident "Jsx", "element")} []
5151

52-
let jsx_element_constraint ~loc expr =
53-
Exp.constraint_ ~loc expr (jsx_element_type ~loc)
52+
let jsx_element_constraint expr =
53+
Exp.constraint_ expr (jsx_element_type ~loc:expr.pexp_loc)
5454

55-
let rec constrain_jsx_return ~loc expr =
55+
let rec constrain_jsx_return expr =
5656
match expr.pexp_desc with
5757
| Pexp_fun ({rhs} as desc) ->
58-
{
59-
expr with
60-
pexp_desc = Pexp_fun {desc with rhs = constrain_jsx_return ~loc rhs};
61-
}
58+
{expr with pexp_desc = Pexp_fun {desc with rhs = constrain_jsx_return rhs}}
6259
| Pexp_newtype (param, inner) ->
63-
{
64-
expr with
65-
pexp_desc = Pexp_newtype (param, constrain_jsx_return ~loc inner);
66-
}
60+
{expr with pexp_desc = Pexp_newtype (param, constrain_jsx_return inner)}
6761
| Pexp_constraint (inner, _) ->
68-
jsx_element_constraint ~loc (constrain_jsx_return ~loc inner)
62+
let constrained_inner = constrain_jsx_return inner in
63+
jsx_element_constraint constrained_inner
6964
| Pexp_let (rec_flag, bindings, body) ->
7065
{
7166
expr with
72-
pexp_desc = Pexp_let (rec_flag, bindings, constrain_jsx_return ~loc body);
67+
pexp_desc = Pexp_let (rec_flag, bindings, constrain_jsx_return body);
7368
}
7469
| Pexp_sequence (first, second) ->
75-
{
76-
expr with
77-
pexp_desc = Pexp_sequence (first, constrain_jsx_return ~loc second);
78-
}
79-
| _ -> jsx_element_constraint ~loc expr
70+
{expr with pexp_desc = Pexp_sequence (first, constrain_jsx_return second)}
71+
| _ -> jsx_element_constraint expr
8072

8173
(* Helper method to filter out any attribute that isn't [@react.component] *)
8274
let other_attrs_pure (loc, _) =
@@ -743,10 +735,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
743735
vb_match_expr named_arg_list expression
744736
else expression
745737
in
746-
let expression =
747-
Exp.constraint_ ~loc:binding_loc expression
748-
(jsx_element_type ~loc:binding_loc)
749-
in
738+
let expression = constrain_jsx_return expression in
750739
(* (ref) => expr *)
751740
let expression =
752741
List.fold_left
@@ -890,9 +879,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
890879
let applied_expression =
891880
Jsx_common.async_component ~async:is_async applied_expression
892881
in
893-
let applied_expression =
894-
Exp.constraint_ ~loc applied_expression (jsx_element_type ~loc)
895-
in
882+
let applied_expression = constrain_jsx_return applied_expression in
896883
let wrapper_expr =
897884
Exp.fun_ ~arity:None Nolabel None props_pattern
898885
~attrs:binding.pvb_expr.pexp_attributes applied_expression
@@ -929,7 +916,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
929916
{
930917
binding with
931918
pvb_attributes = binding.pvb_attributes |> List.filter other_attrs_pure;
932-
pvb_expr = binding_expr |> constrain_jsx_return ~loc:binding_loc;
919+
pvb_expr = binding_expr |> constrain_jsx_return;
933920
},
934921
new_binding )
935922
else (None, binding, None)

tests/analysis_tests/tests/src/expected/CodeLens.res.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
Code Lens src/CodeLens.res
22
[{
3-
"range": {"start": {"line": 9, "character": 4}, "end": {"line": 9, "character": 8}},
4-
"command": {"title": "Jsx.element", "command": ""}
5-
}, {
63
"range": {"start": {"line": 4, "character": 4}, "end": {"line": 4, "character": 6}},
74
"command": {"title": "(~opt1: int=?, ~a: int, ~b: int, unit, ~opt2: int=?, unit, ~c: int) => int", "command": ""}
85
}, {

tests/analysis_tests/tests/src/expected/Hover.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ Hover src/Hover.res 33:4
2222
{"contents": {"kind": "markdown", "value": "```rescript\nunit => int\n```\n---\nDoc comment for functionWithTypeAnnotation"}}
2323

2424
Hover src/Hover.res 37:13
25-
{"contents": {"kind": "markdown", "value": "```rescript\nJsx.element\n```\n\n---\n\n```\n \n```\n```rescript\ntype Jsx.element\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Jsx.res%22%2C25%2C0%5D)\n"}}
25+
{"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}}
2626

2727
Hover src/Hover.res 42:15
28-
{"contents": {"kind": "markdown", "value": "```rescript\nJsx.element\n```\n\n---\n\n```\n \n```\n```rescript\ntype Jsx.element\n```\nGo to: [Type definition](command:rescript-vscode.go_to_location?%5B%22Jsx.res%22%2C25%2C0%5D)\n"}}
28+
{"contents": {"kind": "markdown", "value": "```rescript\nstring\n```"}}
2929

3030
Hover src/Hover.res 46:10
3131
{"contents": {"kind": "markdown", "value": "```rescript\nint\n```"}}

tests/analysis_tests/tests/src/expected/InlayHint.res.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ Inlay Hint src/InlayHint.res 1:34
3535
"kind": 1,
3636
"paddingLeft": true,
3737
"paddingRight": false
38-
}, {
39-
"position": {"line": 14, "character": 8},
40-
"label": ": Jsx.element",
41-
"kind": 1,
42-
"paddingLeft": true,
43-
"paddingRight": false
4438
}, {
4539
"position": {"line": 10, "character": 10},
4640
"label": ": (~xx: int) => int",

tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module C0 = {
44
@res.jsxComponentProps
55
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
66

7-
let make = ({priority: _, text: ?__text, _}: props<_, _>): Jsx.element => {
7+
let make = ({priority: _, text: ?__text, _}: props<_, _>) => {
88
let text = switch __text {
99
| Some(text) => text
1010
| None => "Test"
1111
}
1212

13-
React.string(text)
13+
(React.string(text): Jsx.element)
1414
}
1515
let make = {
1616
let \"AliasProps$C0" = (props: props<_>) => make(props)
@@ -23,13 +23,13 @@ module C1 = {
2323
@res.jsxComponentProps
2424
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
2525

26-
let make = ({priority: p, text: ?__text, _}: props<_, _>): Jsx.element => {
26+
let make = ({priority: p, text: ?__text, _}: props<_, _>) => {
2727
let text = switch __text {
2828
| Some(text) => text
2929
| None => "Test"
3030
}
3131

32-
React.string(p ++ text)
32+
(React.string(p ++ text): Jsx.element)
3333
}
3434
let make = {
3535
let \"AliasProps$C1" = (props: props<_>) => make(props)
@@ -42,13 +42,13 @@ module C2 = {
4242
@res.jsxComponentProps
4343
type props<'foo> = {foo?: 'foo}
4444

45-
let make = ({foo: ?__bar, _}: props<_>): Jsx.element => {
45+
let make = ({foo: ?__bar, _}: props<_>) => {
4646
let bar = switch __bar {
4747
| Some(foo) => foo
4848
| None => ""
4949
}
5050

51-
React.string(bar)
51+
(React.string(bar): Jsx.element)
5252
}
5353
let make = {
5454
let \"AliasProps$C2" = (props: props<_>) => make(props)
@@ -61,7 +61,7 @@ module C3 = {
6161
@res.jsxComponentProps
6262
type props<'foo, 'a, 'b> = {foo?: 'foo, a?: 'a, b: 'b}
6363

64-
let make = ({foo: ?__bar, a: ?__a, b, _}: props<_, _, _>): Jsx.element => {
64+
let make = ({foo: ?__bar, a: ?__a, b, _}: props<_, _, _>) => {
6565
let bar = switch __bar {
6666
| Some(foo) => foo
6767
| None => ""
@@ -71,9 +71,11 @@ module C3 = {
7171
| None => bar
7272
}
7373

74-
{
75-
React.string(bar ++ a ++ b)
76-
}
74+
(
75+
{
76+
React.string(bar ++ a ++ b)
77+
}: Jsx.element
78+
)
7779
}
7880
let make = {
7981
let \"AliasProps$C3" = (props: props<_>) => make(props)
@@ -86,13 +88,13 @@ module C4 = {
8688
@res.jsxComponentProps
8789
type props<'a, 'x> = {a: 'a, x?: 'x}
8890

89-
let make = ({a: b, x: ?__x, _}: props<_, _>): Jsx.element => {
91+
let make = ({a: b, x: ?__x, _}: props<_, _>) => {
9092
let x = switch __x {
9193
| Some(x) => x
9294
| None => true
9395
}
9496

95-
ReactDOM.jsx("div", {children: ?ReactDOM.someElement(b)})
97+
(ReactDOM.jsx("div", {children: ?ReactDOM.someElement(b)}): Jsx.element)
9698
}
9799
let make = {
98100
let \"AliasProps$C4" = (props: props<_>) => make(props)
@@ -105,13 +107,13 @@ module C5 = {
105107
@res.jsxComponentProps
106108
type props<'a, 'z> = {a: 'a, z?: 'z}
107109

108-
let make = ({a: (x, y), z: ?__z, _}: props<_, _>): Jsx.element => {
110+
let make = ({a: (x, y), z: ?__z, _}: props<_, _>) => {
109111
let z = switch __z {
110112
| Some(z) => z
111113
| None => 3
112114
}
113115

114-
x + y + z
116+
(x + y + z: Jsx.element)
115117
}
116118
let make = {
117119
let \"AliasProps$C5" = (props: props<_>) => make(props)

tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module C0 = {
44
@res.jsxComponentProps
55
type props<'a> = {a: 'a}
66

7-
let make = async ({a, _}: props<_>): Jsx.element => {
7+
let make = async ({a, _}: props<_>) => {
88
let a = await f(a)
9-
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
9+
(ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})}): Jsx.element)
1010
}
1111
let make = {
1212
let \"AsyncAwait$C0" = (props: props<_>) => Jsx.promise(make(props))

tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module C0 = {
22
@res.jsxComponentProps
33
type props<'a, 'b> = {a?: 'a, b?: 'b}
4-
let make = ({a: ?__a, b: ?__b, _}: props<_, _>): Jsx.element => {
4+
let make = ({a: ?__a, b: ?__b, _}: props<_, _>) => {
55
let a = switch __a {
66
| Some(a) => a
77
| None => 2
@@ -11,7 +11,7 @@ module C0 = {
1111
| None => a * 2
1212
}
1313

14-
React.int(a + b)
14+
(React.int(a + b): Jsx.element)
1515
}
1616
let make = {
1717
let \"DefaultValueProp$C0" = (props: props<_>) => make(props)
@@ -23,13 +23,13 @@ module C1 = {
2323
@res.jsxComponentProps
2424
type props<'a, 'b> = {a?: 'a, b: 'b}
2525

26-
let make = ({a: ?__a, b, _}: props<_, _>): Jsx.element => {
26+
let make = ({a: ?__a, b, _}: props<_, _>) => {
2727
let a = switch __a {
2828
| Some(a) => a
2929
| None => 2
3030
}
3131

32-
React.int(a + b)
32+
(React.int(a + b): Jsx.element)
3333
}
3434
let make = {
3535
let \"DefaultValueProp$C1" = (props: props<_>) => make(props)
@@ -43,13 +43,13 @@ module C2 = {
4343
@res.jsxComponentProps
4444
type props<'a> = {a?: 'a}
4545

46-
let make = ({a: ?__a, _}: props<_>): Jsx.element => {
46+
let make = ({a: ?__a, _}: props<_>) => {
4747
let a = switch __a {
4848
| Some(a) => a
4949
| None => a
5050
}
5151

52-
React.string(a)
52+
(React.string(a): Jsx.element)
5353
}
5454
let make = {
5555
let \"DefaultValueProp$C2" = (props: props<_>) => make(props)
@@ -62,15 +62,17 @@ module C3 = {
6262
@res.jsxComponentProps
6363
type props<'disabled> = {disabled?: 'disabled}
6464

65-
let make = ({disabled: ?__everythingDisabled, _}: props<bool>): Jsx.element => {
65+
let make = ({disabled: ?__everythingDisabled, _}: props<bool>) => {
6666
let everythingDisabled = switch __everythingDisabled {
6767
| Some(disabled) => disabled
6868
| None => false
6969
}
7070

71-
{
72-
React.string(everythingDisabled ? "true" : "false")
73-
}
71+
(
72+
{
73+
React.string(everythingDisabled ? "true" : "false")
74+
}: Jsx.element
75+
)
7476
}
7577
let make = {
7678
let \"DefaultValueProp$C3" = (props: props<_>) => make(props)

tests/syntax_tests/data/ppx/react/expected/forwardRef.res.txt

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ module V4A = {
3838
@res.jsxComponentProps
3939
type props = {}
4040

41-
let make = (_: props): Jsx.element => {
41+
let make = (_: props) => {
4242
let input = React.useRef(Js.Nullable.null)
4343

44-
ReactDOM.jsx(
45-
"div",
46-
{
47-
children: ?ReactDOM.someElement(
48-
React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}),
49-
),
50-
},
44+
(
45+
ReactDOM.jsx(
46+
"div",
47+
{
48+
children: ?ReactDOM.someElement(
49+
React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}),
50+
),
51+
},
52+
): Jsx.element
5153
)
5254
}
5355
let make = {
@@ -95,16 +97,18 @@ module V4AUncurried = {
9597
@res.jsxComponentProps
9698
type props = {}
9799

98-
let make = (_: props): Jsx.element => {
100+
let make = (_: props) => {
99101
let input = React.useRef(Js.Nullable.null)
100102

101-
ReactDOM.jsx(
102-
"div",
103-
{
104-
children: ?ReactDOM.someElement(
105-
React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}),
106-
),
107-
},
103+
(
104+
ReactDOM.jsx(
105+
"div",
106+
{
107+
children: ?ReactDOM.someElement(
108+
React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}),
109+
),
110+
},
111+
): Jsx.element
108112
)
109113
}
110114
let make = {

tests/syntax_tests/data/ppx/react/expected/interfaceWithRef.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
let make = (
33
{x, _}: props<string, ReactDOM.Ref.currentDomRef>,
44
ref: Js.Nullable.t<ReactDOM.Ref.currentDomRef>,
5-
): Jsx.element => {
5+
) => {
66
let _ = ref->Js.Nullable.toOption->Belt.Option.map(ReactDOM.Ref.domRef)
7-
React.string(x)
7+
(React.string(x): Jsx.element)
88
}
99
let make = React.forwardRef({
1010
let \"InterfaceWithRef" = (props: props<_>, ref) => make(props, ref)

tests/syntax_tests/data/ppx/react/expected/topLevel.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module V4A = {
44
@res.jsxComponentProps
55
type props<'a, 'b> = {a: 'a, b: 'b}
66

7-
let make = ({a, b, _}: props<_, _>): Jsx.element => {
7+
let make = ({a, b, _}: props<_, _>) => {
88
Js.log("This function should be named 'TopLevel.react'")
9-
ReactDOM.jsx("div", {})
9+
(ReactDOM.jsx("div", {}): Jsx.element)
1010
}
1111
let make = {
1212
let \"TopLevel$V4A" = (props: props<_>) => make(props)

0 commit comments

Comments
 (0)