Skip to content

Commit 6a951ee

Browse files
authored
Merge pull request #6026 from rescript-lang/gt
GenType: check annotations also in module types.
2 parents 0a10ac2 + 351bdfc commit 6a951ee

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- Fix formatting of `switch` expressions that contain braced `cases` inside https://github.com/rescript-lang/syntax/pull/735
3939
- Fix formatting of props spread for multiline JSX expression https://github.com/rescript-lang/syntax/pull/736
4040
- Support `@gentype.import` as an alias to `@genType.import` in the compiler https://github.com/rescript-lang/rescript-compiler/pull/6021
41+
- In GenType, check annotations also in module types to decide whether to produce the `.gen.tsx` file https://github.com/rescript-lang/rescript-compiler/pull/5903
4142

4243
#### :rocket: New Feature
4344

jscomp/gentype/Annotation.ml

+37-15
Original file line numberDiff line numberDiff line change
@@ -155,26 +155,41 @@ let rec moduleTypeCheckAnnotation ~checkAnnotation
155155
->
156156
false
157157

158+
and moduleTypeDeclarationCheckAnnotation ~checkAnnotation
159+
({ mtd_type; mtd_attributes; mtd_loc = loc } :
160+
Typedtree.module_type_declaration) =
161+
mtd_attributes |> checkAnnotation ~loc
162+
||
163+
match mtd_type with
164+
| None -> false
165+
| Some module_type ->
166+
module_type |> moduleTypeCheckAnnotation ~checkAnnotation
167+
158168
and moduleDeclarationCheckAnnotation ~checkAnnotation
159169
({ md_attributes; md_type; md_loc = loc } : Typedtree.module_declaration) =
160170
md_attributes |> checkAnnotation ~loc
161171
|| md_type |> moduleTypeCheckAnnotation ~checkAnnotation
162172

163173
and signatureItemCheckAnnotation ~checkAnnotation
164174
(signatureItem : Typedtree.signature_item) =
165-
match signatureItem with
166-
| { Typedtree.sig_desc = Typedtree.Tsig_type (_, typeDeclarations) } ->
175+
match signatureItem.sig_desc with
176+
| Tsig_type (_, typeDeclarations) ->
167177
typeDeclarations
168178
|> List.exists
169179
(fun ({ typ_attributes; typ_loc = loc } : Typedtree.type_declaration)
170180
-> typ_attributes |> checkAnnotation ~loc)
171-
| { sig_desc = Tsig_value { val_attributes; val_loc = loc } } ->
181+
| Tsig_value { val_attributes; val_loc = loc } ->
172182
val_attributes |> checkAnnotation ~loc
173-
| { sig_desc = Tsig_module moduleDeclaration } ->
183+
| Tsig_module moduleDeclaration ->
174184
moduleDeclaration |> moduleDeclarationCheckAnnotation ~checkAnnotation
175-
| { sig_desc = Tsig_attribute attribute; sig_loc = loc } ->
176-
[ attribute ] |> checkAnnotation ~loc
177-
| _ -> false
185+
| Tsig_attribute attribute ->
186+
[ attribute ] |> checkAnnotation ~loc:signatureItem.sig_loc
187+
| Tsig_modtype moduleTypeDeclaration ->
188+
moduleTypeDeclaration
189+
|> moduleTypeDeclarationCheckAnnotation ~checkAnnotation
190+
| Tsig_typext _ | Tsig_exception _ | Tsig_recmodule _ | Tsig_open _
191+
| Tsig_include _ | Tsig_class _ | Tsig_class_type _ ->
192+
false
178193

179194
and signatureCheckAnnotation ~checkAnnotation (signature : Typedtree.signature)
180195
=
@@ -183,28 +198,35 @@ and signatureCheckAnnotation ~checkAnnotation (signature : Typedtree.signature)
183198

184199
let rec structureItemCheckAnnotation ~checkAnnotation
185200
(structureItem : Typedtree.structure_item) =
186-
match structureItem with
187-
| { Typedtree.str_desc = Typedtree.Tstr_type (_, typeDeclarations) } ->
201+
match structureItem.str_desc with
202+
| Tstr_type (_, typeDeclarations) ->
188203
typeDeclarations
189204
|> List.exists
190205
(fun ({ typ_attributes; typ_loc = loc } : Typedtree.type_declaration)
191206
-> typ_attributes |> checkAnnotation ~loc)
192-
| { str_desc = Tstr_value (_loc, valueBindings) } ->
207+
| Tstr_value (_loc, valueBindings) ->
193208
valueBindings
194209
|> List.exists
195210
(fun ({ vb_attributes; vb_loc = loc } : Typedtree.value_binding) ->
196211
vb_attributes |> checkAnnotation ~loc)
197-
| { str_desc = Tstr_primitive { val_attributes; val_loc = loc } } ->
212+
| Tstr_primitive { val_attributes; val_loc = loc } ->
198213
val_attributes |> checkAnnotation ~loc
199-
| { str_desc = Tstr_module moduleBinding } ->
214+
| Tstr_module moduleBinding ->
200215
moduleBinding |> moduleBindingCheckAnnotation ~checkAnnotation
201-
| { str_desc = Tstr_recmodule moduleBindings } ->
216+
| Tstr_recmodule moduleBindings ->
202217
moduleBindings
203218
|> List.exists (moduleBindingCheckAnnotation ~checkAnnotation)
204-
| { str_desc = Tstr_include { incl_attributes; incl_mod; incl_loc = loc } } ->
219+
| Tstr_include { incl_attributes; incl_mod; incl_loc = loc } ->
205220
incl_attributes |> checkAnnotation ~loc
206221
|| incl_mod |> moduleExprCheckAnnotation ~checkAnnotation
207-
| _ -> false
222+
| Tstr_modtype moduleTypeDeclaration ->
223+
moduleTypeDeclaration
224+
|> moduleTypeDeclarationCheckAnnotation ~checkAnnotation
225+
| Tstr_attribute attribute ->
226+
[ attribute ] |> checkAnnotation ~loc:structureItem.str_loc
227+
| Tstr_eval _ | Tstr_typext _ | Tstr_exception _ | Tstr_open _ | Tstr_class _
228+
| Tstr_class_type _ ->
229+
false
208230

209231
and moduleExprCheckAnnotation ~checkAnnotation
210232
(moduleExpr : Typedtree.module_expr) =

lib/4.06.1/whole_compiler.ml

+37-15
Original file line numberDiff line numberDiff line change
@@ -239714,26 +239714,41 @@ let rec moduleTypeCheckAnnotation ~checkAnnotation
239714239714
->
239715239715
false
239716239716

239717+
and moduleTypeDeclarationCheckAnnotation ~checkAnnotation
239718+
({ mtd_type; mtd_attributes; mtd_loc = loc } :
239719+
Typedtree.module_type_declaration) =
239720+
mtd_attributes |> checkAnnotation ~loc
239721+
||
239722+
match mtd_type with
239723+
| None -> false
239724+
| Some module_type ->
239725+
module_type |> moduleTypeCheckAnnotation ~checkAnnotation
239726+
239717239727
and moduleDeclarationCheckAnnotation ~checkAnnotation
239718239728
({ md_attributes; md_type; md_loc = loc } : Typedtree.module_declaration) =
239719239729
md_attributes |> checkAnnotation ~loc
239720239730
|| md_type |> moduleTypeCheckAnnotation ~checkAnnotation
239721239731

239722239732
and signatureItemCheckAnnotation ~checkAnnotation
239723239733
(signatureItem : Typedtree.signature_item) =
239724-
match signatureItem with
239725-
| { Typedtree.sig_desc = Typedtree.Tsig_type (_, typeDeclarations) } ->
239734+
match signatureItem.sig_desc with
239735+
| Tsig_type (_, typeDeclarations) ->
239726239736
typeDeclarations
239727239737
|> List.exists
239728239738
(fun ({ typ_attributes; typ_loc = loc } : Typedtree.type_declaration)
239729239739
-> typ_attributes |> checkAnnotation ~loc)
239730-
| { sig_desc = Tsig_value { val_attributes; val_loc = loc } } ->
239740+
| Tsig_value { val_attributes; val_loc = loc } ->
239731239741
val_attributes |> checkAnnotation ~loc
239732-
| { sig_desc = Tsig_module moduleDeclaration } ->
239742+
| Tsig_module moduleDeclaration ->
239733239743
moduleDeclaration |> moduleDeclarationCheckAnnotation ~checkAnnotation
239734-
| { sig_desc = Tsig_attribute attribute; sig_loc = loc } ->
239735-
[ attribute ] |> checkAnnotation ~loc
239736-
| _ -> false
239744+
| Tsig_attribute attribute ->
239745+
[ attribute ] |> checkAnnotation ~loc:signatureItem.sig_loc
239746+
| Tsig_modtype moduleTypeDeclaration ->
239747+
moduleTypeDeclaration
239748+
|> moduleTypeDeclarationCheckAnnotation ~checkAnnotation
239749+
| Tsig_typext _ | Tsig_exception _ | Tsig_recmodule _ | Tsig_open _
239750+
| Tsig_include _ | Tsig_class _ | Tsig_class_type _ ->
239751+
false
239737239752

239738239753
and signatureCheckAnnotation ~checkAnnotation (signature : Typedtree.signature)
239739239754
=
@@ -239742,28 +239757,35 @@ and signatureCheckAnnotation ~checkAnnotation (signature : Typedtree.signature)
239742239757

239743239758
let rec structureItemCheckAnnotation ~checkAnnotation
239744239759
(structureItem : Typedtree.structure_item) =
239745-
match structureItem with
239746-
| { Typedtree.str_desc = Typedtree.Tstr_type (_, typeDeclarations) } ->
239760+
match structureItem.str_desc with
239761+
| Tstr_type (_, typeDeclarations) ->
239747239762
typeDeclarations
239748239763
|> List.exists
239749239764
(fun ({ typ_attributes; typ_loc = loc } : Typedtree.type_declaration)
239750239765
-> typ_attributes |> checkAnnotation ~loc)
239751-
| { str_desc = Tstr_value (_loc, valueBindings) } ->
239766+
| Tstr_value (_loc, valueBindings) ->
239752239767
valueBindings
239753239768
|> List.exists
239754239769
(fun ({ vb_attributes; vb_loc = loc } : Typedtree.value_binding) ->
239755239770
vb_attributes |> checkAnnotation ~loc)
239756-
| { str_desc = Tstr_primitive { val_attributes; val_loc = loc } } ->
239771+
| Tstr_primitive { val_attributes; val_loc = loc } ->
239757239772
val_attributes |> checkAnnotation ~loc
239758-
| { str_desc = Tstr_module moduleBinding } ->
239773+
| Tstr_module moduleBinding ->
239759239774
moduleBinding |> moduleBindingCheckAnnotation ~checkAnnotation
239760-
| { str_desc = Tstr_recmodule moduleBindings } ->
239775+
| Tstr_recmodule moduleBindings ->
239761239776
moduleBindings
239762239777
|> List.exists (moduleBindingCheckAnnotation ~checkAnnotation)
239763-
| { str_desc = Tstr_include { incl_attributes; incl_mod; incl_loc = loc } } ->
239778+
| Tstr_include { incl_attributes; incl_mod; incl_loc = loc } ->
239764239779
incl_attributes |> checkAnnotation ~loc
239765239780
|| incl_mod |> moduleExprCheckAnnotation ~checkAnnotation
239766-
| _ -> false
239781+
| Tstr_modtype moduleTypeDeclaration ->
239782+
moduleTypeDeclaration
239783+
|> moduleTypeDeclarationCheckAnnotation ~checkAnnotation
239784+
| Tstr_attribute attribute ->
239785+
[ attribute ] |> checkAnnotation ~loc:structureItem.str_loc
239786+
| Tstr_eval _ | Tstr_typext _ | Tstr_exception _ | Tstr_open _ | Tstr_class _
239787+
| Tstr_class_type _ ->
239788+
false
239767239789

239768239790
and moduleExprCheckAnnotation ~checkAnnotation
239769239791
(moduleExpr : Typedtree.module_expr) =

0 commit comments

Comments
 (0)