Skip to content

Commit 3535cfd

Browse files
cristianoccknitt
authored andcommitted
PPX v4: mark props type in externals as @live. (#6796)
* PPX v4: mark props type in externals as `@live`. Fixes rescript-lang/rescript-vscode#994 Dead code elimination in the editor tooling complains about props never being read in the `props` type record defined by the V4 ppx. This is because externals don't provide the implementation of the component, and it's in the implementation that props could be read. This OR marks the `props` type definition as `@live` for external components, so the dead code analysis does not fire. * Update CHANGELOG.md
1 parent e079313 commit 3535cfd

10 files changed

+52
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :bug: Bug Fix
1616

1717
- Fix location of let bindings with attributes. https://github.com/rescript-lang/rescript-compiler/pull/6791
18+
- PPX v4: mark props type in externals as `@live` to avoid dead code warnings for prop fields in the editor tooling. https://github.com/rescript-lang/rescript-compiler/pull/6796
1819

1920
# 11.1.1
2021

jscomp/syntax/src/jsx_v4.ml

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ let makeLabelDecls namedTypeList =
323323
Type.field ~loc ~attrs {txt = label; loc}
324324
(Typ.var @@ safeTypeFromValue @@ Labelled label))
325325

326-
let makeTypeDecls propsName loc namedTypeList =
326+
let makeTypeDecls ~attrs propsName loc namedTypeList =
327327
let labelDeclList = makeLabelDecls namedTypeList in
328328
(* 'id, 'className, ... *)
329329
let params =
330330
makePropsTypeParamsTvar namedTypeList
331331
|> List.map (fun coreType -> (coreType, Invariant))
332332
in
333333
[
334-
Type.mk ~loc ~params {txt = propsName; loc}
334+
Type.mk ~attrs ~loc ~params {txt = propsName; loc}
335335
~kind:(Ptype_record labelDeclList);
336336
]
337337

@@ -342,21 +342,25 @@ let makeTypeDeclsWithCoreType propsName loc coreType typVars =
342342
~manifest:coreType;
343343
]
344344

345+
let live_attr = ({txt = "live"; loc = Location.none}, PStr [])
346+
345347
(* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *)
346-
let makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
347-
namedTypeList =
348+
let makePropsRecordType ~coreTypeOfAttr ~external_ ~typVarsOfCoreType propsName
349+
loc namedTypeList =
350+
let attrs = if external_ then [live_attr] else [] in
348351
Str.type_ Nonrecursive
349352
(match coreTypeOfAttr with
350-
| None -> makeTypeDecls propsName loc namedTypeList
353+
| None -> makeTypeDecls ~attrs propsName loc namedTypeList
351354
| Some coreType ->
352355
makeTypeDeclsWithCoreType propsName loc coreType typVarsOfCoreType)
353356

354357
(* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *)
355-
let makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
356-
namedTypeList =
358+
let makePropsRecordTypeSig ~coreTypeOfAttr ~external_ ~typVarsOfCoreType
359+
propsName loc namedTypeList =
360+
let attrs = if external_ then [live_attr] else [] in
357361
Sig.type_ Nonrecursive
358362
(match coreTypeOfAttr with
359-
| None -> makeTypeDecls propsName loc namedTypeList
363+
| None -> makeTypeDecls ~attrs propsName loc namedTypeList
360364
| Some coreType ->
361365
makeTypeDeclsWithCoreType propsName loc coreType typVarsOfCoreType)
362366

@@ -934,8 +938,8 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
934938
let namedTypeList = List.fold_left argToType [] namedArgList in
935939
(* type props = { ... } *)
936940
let propsRecordType =
937-
makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType "props" pstr_loc
938-
namedTypeList
941+
makePropsRecordType ~coreTypeOfAttr ~external_:false ~typVarsOfCoreType
942+
"props" pstr_loc namedTypeList
939943
in
940944
let innerExpression =
941945
Exp.apply
@@ -1188,8 +1192,8 @@ let transformStructureItem ~config item =
11881192
in
11891193
(* type props<'x, 'y> = { x: 'x, y?: 'y, ... } *)
11901194
let propsRecordType =
1191-
makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType "props" pstr_loc
1192-
namedTypeList
1195+
makePropsRecordType ~coreTypeOfAttr ~external_:true ~typVarsOfCoreType
1196+
"props" pstr_loc namedTypeList
11931197
in
11941198
(* can't be an arrow because it will defensively uncurry *)
11951199
let newExternalType =
@@ -1294,9 +1298,10 @@ let transformSignatureItem ~config item =
12941298
| [] -> []
12951299
| _ -> [Typ.any ()]))
12961300
in
1301+
let external_ = psig_desc.pval_prim <> [] in
12971302
let propsRecordType =
1298-
makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType "props"
1299-
psig_loc namedTypeList
1303+
makePropsRecordTypeSig ~coreTypeOfAttr ~external_ ~typVarsOfCoreType
1304+
"props" psig_loc namedTypeList
13001305
in
13011306
(* can't be an arrow because it will defensively uncurry *)
13021307
let newExternalType =

jscomp/syntax/tests/ppx/react/expected/externalWithCustomName.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let t = React.createElement(Foo.component, Foo.componentProps(~a=1, ~b={"1"}, ()
1313
@@jsxConfig({version: 4, mode: "classic"})
1414

1515
module Foo = {
16+
@live
1617
type props<'a, 'b> = {a: 'a, b: 'b}
1718

1819
@module("Foo")
@@ -24,6 +25,7 @@ let t = React.createElement(Foo.component, {a: 1, b: "1"})
2425
@@jsxConfig({version: 4, mode: "automatic"})
2526

2627
module Foo = {
28+
@live
2729
type props<'a, 'b> = {a: 'a, b: 'b}
2830

2931
@module("Foo")

jscomp/syntax/tests/ppx/react/expected/externalWithRef.res.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module V3 = {
1818
@@jsxConfig({version: 4, mode: "classic"})
1919

2020
module V4C = {
21+
@live
2122
type props<'x, 'ref> = {
2223
x: 'x,
2324
ref?: 'ref,
@@ -28,9 +29,21 @@ module V4C = {
2829
"component"
2930
}
3031

32+
module type V4CType = {
33+
@live
34+
type props<'x, 'y> = {
35+
x: 'x,
36+
y: 'y,
37+
}
38+
39+
@module("someModule")
40+
external make: React.componentLike<props<string, string>, React.element> = "component"
41+
}
42+
3143
@@jsxConfig({version: 4, mode: "automatic"})
3244

3345
module V4C = {
46+
@live
3447
type props<'x, 'ref> = {
3548
x: 'x,
3649
ref?: 'ref,

jscomp/syntax/tests/ppx/react/expected/externalWithTypeVariables.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module V3 = {
1616
@@jsxConfig({version: 4, mode: "classic"})
1717

1818
module V4C = {
19+
@live
1920
type props<'x, 'children> = {
2021
x: 'x,
2122
children: 'children,
@@ -28,6 +29,7 @@ module V4C = {
2829
@@jsxConfig({version: 4, mode: "automatic"})
2930

3031
module V4C = {
32+
@live
3133
type props<'x, 'children> = {
3234
x: 'x,
3335
children: 'children,

jscomp/syntax/tests/ppx/react/expected/firstClassModules.res.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ module External = {
8888
type key
8989
type t
9090
}
91+
@live
9192
type props<'model, 'selected, 'onChange, 'items> = {
9293
model: 'model,
9394
selected: 'selected,

jscomp/syntax/tests/ppx/react/expected/mangleKeyword.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module C4C0 = {
4747
}
4848
}
4949
module C4C1 = {
50+
@live
5051
type props<'T_open, 'T_type> = {@as("open") _open: 'T_open, @as("type") _type: 'T_type}
5152

5253
external make: @as("open") React.componentLike<props<string, string>, React.element> = "default"
@@ -68,6 +69,7 @@ module C4A0 = {
6869
}
6970
}
7071
module C4A1 = {
72+
@live
7173
type props<'T_open, 'T_type> = {@as("open") _open: 'T_open, @as("type") _type: 'T_type}
7274

7375
external make: @as("open") React.componentLike<props<string, string>, React.element> = "default"

jscomp/syntax/tests/ppx/react/expected/noPropsWithKey.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module V4CA = {
1212
}
1313

1414
module V4CB = {
15+
@live
1516
type props = {}
1617

1718
@module("c")
@@ -51,6 +52,7 @@ module V4CA = {
5152
}
5253

5354
module V4CB = {
55+
@live
5456
type props = {}
5557

5658
@module("c")

jscomp/syntax/tests/ppx/react/expected/v4.res.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ module type TUncurried = {
3535
}
3636

3737
module E = {
38+
@live
3839
type props<'x> = {x: 'x}
3940

4041
external make: React.componentLike<props<string>, React.element> = "default"
4142
}
4243

4344
module EUncurried = {
45+
@live
4446
type props<'x> = {x: 'x}
4547

4648
external make: React.componentLike<props<string>, React.element> = "default"

jscomp/syntax/tests/ppx/react/externalWithRef.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ module V4C = {
1818
) => React.element = "component"
1919
}
2020

21+
module type V4CType = {
22+
@module("someModule") @react.component
23+
external make: (
24+
~x: string,
25+
~y: string,
26+
) => React.element = "component"
27+
}
28+
2129
@@jsxConfig({version: 4, mode: "automatic"})
2230

2331
module V4C = {

0 commit comments

Comments
 (0)