1
- type import = { name : string ; importPath : ImportPath .t }
1
+ type import = {name : string ; importPath : ImportPath .t }
2
2
3
3
type attributePayload =
4
4
| BoolPayload of bool
@@ -39,64 +39,61 @@ let tagIsInternLocal s = s = "internal.local"
39
39
let rec getAttributePayload checkText (attributes : Typedtree.attributes ) =
40
40
let rec fromExpr (expr : Parsetree.expression ) =
41
41
match expr with
42
- | { pexp_desc = Pexp_constant (Pconst_string (s , _ )) } ->
43
- Some (StringPayload s)
44
- | { pexp_desc = Pexp_constant (Pconst_integer (n , _ )) } ->
45
- Some (IntPayload n)
46
- | { pexp_desc = Pexp_constant (Pconst_float (s , _ )) } ->
47
- Some (FloatPayload s)
42
+ | {pexp_desc = Pexp_constant (Pconst_string (s , _ ))} ->
43
+ Some (StringPayload s)
44
+ | {pexp_desc = Pexp_constant (Pconst_integer (n , _ ))} -> Some (IntPayload n)
45
+ | {pexp_desc = Pexp_constant (Pconst_float (s , _ ))} -> Some (FloatPayload s)
48
46
| {
49
- pexp_desc = Pexp_construct ({ txt = Lident ((" true" | " false" ) as s) }, _);
47
+ pexp_desc = Pexp_construct ({txt = Lident ((" true" | " false" ) as s)}, _);
50
48
_;
51
49
} ->
52
- Some (BoolPayload (s = " true" ))
53
- | { pexp_desc = Pexp_tuple exprs } ->
54
- let payloads =
55
- exprs |> List. rev
56
- |> List. fold_left
57
- (fun payloads expr ->
58
- match expr |> fromExpr with
59
- | Some payload -> payload :: payloads
60
- | None -> payloads)
61
- []
62
- in
63
- Some (TuplePayload payloads)
64
- | { pexp_desc = Pexp_ident { txt } } -> Some (IdentPayload txt)
50
+ Some (BoolPayload (s = " true" ))
51
+ | {pexp_desc = Pexp_tuple exprs } ->
52
+ let payloads =
53
+ exprs |> List. rev
54
+ |> List. fold_left
55
+ (fun payloads expr ->
56
+ match expr |> fromExpr with
57
+ | Some payload -> payload :: payloads
58
+ | None -> payloads)
59
+ []
60
+ in
61
+ Some (TuplePayload payloads)
62
+ | {pexp_desc = Pexp_ident {txt} } -> Some (IdentPayload txt)
65
63
| _ -> None
66
64
in
67
65
match attributes with
68
66
| [] -> None
69
- | ({ Asttypes. txt } , payload ) :: _tl when checkText txt -> (
70
- match payload with
71
- | PStr [] -> Some UnrecognizedPayload
72
- | PStr ({ pstr_desc = Pstr_eval (expr , _ ) } :: _ ) -> expr |> fromExpr
73
- | PStr ({ pstr_desc = Pstr_extension _ } :: _ ) -> Some UnrecognizedPayload
74
- | PStr ({ pstr_desc = Pstr_value _ } :: _ ) -> Some UnrecognizedPayload
75
- | PStr ({ pstr_desc = Pstr_primitive _ } :: _ ) -> Some UnrecognizedPayload
76
- | PStr ({ pstr_desc = Pstr_type _ } :: _ ) -> Some UnrecognizedPayload
77
- | PStr ({ pstr_desc = Pstr_typext _ } :: _ ) -> Some UnrecognizedPayload
78
- | PStr ({ pstr_desc = Pstr_exception _ } :: _ ) -> Some UnrecognizedPayload
79
- | PStr ({ pstr_desc = Pstr_module _ } :: _ ) -> Some UnrecognizedPayload
80
- | PStr ({ pstr_desc = Pstr_recmodule _ } :: _ ) -> Some UnrecognizedPayload
81
- | PStr ({ pstr_desc = Pstr_modtype _ } :: _ ) -> Some UnrecognizedPayload
82
- | PStr ({ pstr_desc = Pstr_open _ } :: _ ) -> Some UnrecognizedPayload
83
- | PStr ({ pstr_desc = Pstr_class _ } :: _ ) -> Some UnrecognizedPayload
84
- | PStr ({ pstr_desc = Pstr_class_type _ } :: _ ) ->
85
- Some UnrecognizedPayload
86
- | PStr ({ pstr_desc = Pstr_include _ } :: _ ) -> Some UnrecognizedPayload
87
- | PStr ({ pstr_desc = Pstr_attribute _ } :: _ ) -> Some UnrecognizedPayload
88
- | PPat _ -> Some UnrecognizedPayload
89
- | PSig _ -> Some UnrecognizedPayload
90
- | PTyp _ -> Some UnrecognizedPayload )
67
+ | ({Asttypes. txt} , payload ) :: _tl when checkText txt -> (
68
+ match payload with
69
+ | PStr [] -> Some UnrecognizedPayload
70
+ | PStr ({pstr_desc = Pstr_eval (expr , _ )} :: _ ) -> expr |> fromExpr
71
+ | PStr ({pstr_desc = Pstr_extension _ } :: _ ) -> Some UnrecognizedPayload
72
+ | PStr ({pstr_desc = Pstr_value _ } :: _ ) -> Some UnrecognizedPayload
73
+ | PStr ({pstr_desc = Pstr_primitive _ } :: _ ) -> Some UnrecognizedPayload
74
+ | PStr ({pstr_desc = Pstr_type _ } :: _ ) -> Some UnrecognizedPayload
75
+ | PStr ({pstr_desc = Pstr_typext _ } :: _ ) -> Some UnrecognizedPayload
76
+ | PStr ({pstr_desc = Pstr_exception _ } :: _ ) -> Some UnrecognizedPayload
77
+ | PStr ({pstr_desc = Pstr_module _ } :: _ ) -> Some UnrecognizedPayload
78
+ | PStr ({pstr_desc = Pstr_recmodule _ } :: _ ) -> Some UnrecognizedPayload
79
+ | PStr ({pstr_desc = Pstr_modtype _ } :: _ ) -> Some UnrecognizedPayload
80
+ | PStr ({pstr_desc = Pstr_open _ } :: _ ) -> Some UnrecognizedPayload
81
+ | PStr ({pstr_desc = Pstr_class _ } :: _ ) -> Some UnrecognizedPayload
82
+ | PStr ({pstr_desc = Pstr_class_type _ } :: _ ) -> Some UnrecognizedPayload
83
+ | PStr ({pstr_desc = Pstr_include _ } :: _ ) -> Some UnrecognizedPayload
84
+ | PStr ({pstr_desc = Pstr_attribute _ } :: _ ) -> Some UnrecognizedPayload
85
+ | PPat _ -> Some UnrecognizedPayload
86
+ | PSig _ -> Some UnrecognizedPayload
87
+ | PTyp _ -> Some UnrecognizedPayload )
91
88
| _hd :: tl -> getAttributePayload checkText tl
92
89
93
90
let getGenTypeAsRenaming attributes =
94
91
match attributes |> getAttributePayload tagIsGenTypeAs with
95
92
| Some (StringPayload s ) -> Some s
96
93
| None -> (
97
- match attributes |> getAttributePayload tagIsGenType with
98
- | Some (StringPayload s ) -> Some s
99
- | _ -> None )
94
+ match attributes |> getAttributePayload tagIsGenType with
95
+ | Some (StringPayload s ) -> Some s
96
+ | _ -> None )
100
97
| _ -> None
101
98
102
99
let getBsAsRenaming attributes =
@@ -107,20 +104,19 @@ let getBsAsRenaming attributes =
107
104
let getBsAsInt attributes =
108
105
match attributes |> getAttributePayload tagIsBsAs with
109
106
| Some (IntPayload s ) -> (
110
- try Some (int_of_string s) with Failure _ -> None )
107
+ try Some (int_of_string s) with Failure _ -> None )
111
108
| _ -> None
112
109
113
110
let getAttributeImportRenaming attributes =
114
111
let attributeImport = attributes |> getAttributePayload tagIsGenTypeImport in
115
112
let genTypeAsRenaming = attributes |> getGenTypeAsRenaming in
116
113
match (attributeImport, genTypeAsRenaming) with
117
114
| Some (StringPayload importString ), _ ->
118
- (Some importString, genTypeAsRenaming)
115
+ (Some importString, genTypeAsRenaming)
119
116
| ( Some
120
- (TuplePayload
121
- [ StringPayload importString; StringPayload renameString ]),
117
+ (TuplePayload [StringPayload importString; StringPayload renameString]),
122
118
_ ) ->
123
- (Some importString, Some renameString)
119
+ (Some importString, Some renameString)
124
120
| _ -> (None , genTypeAsRenaming)
125
121
126
122
let getDocString attributes =
@@ -139,41 +135,41 @@ let fromAttributes ~loc (attributes : Typedtree.attributes) =
139
135
(match attributes |> getAttributePayload tagIsGenType with
140
136
| Some UnrecognizedPayload -> ()
141
137
| Some _ ->
142
- Log_.Color. setup () ;
143
- Log_. info ~loc ~name: " Warning genType" (fun ppf () ->
144
- Format. fprintf ppf " Annotation payload is ignored" )
138
+ Log_.Color. setup () ;
139
+ Log_. info ~loc ~name: " Warning genType" (fun ppf () ->
140
+ Format. fprintf ppf " Annotation payload is ignored" )
145
141
| _ -> () );
146
142
GenType )
147
143
else NoGenType
148
144
149
145
let rec moduleTypeCheckAnnotation ~checkAnnotation
150
- ({ mty_desc } : Typedtree.module_type ) =
146
+ ({mty_desc} : Typedtree.module_type ) =
151
147
match mty_desc with
152
148
| Tmty_signature signature ->
153
- signature |> signatureCheckAnnotation ~check Annotation
149
+ signature |> signatureCheckAnnotation ~check Annotation
154
150
| Tmty_ident _ | Tmty_functor _ | Tmty_with _ | Tmty_typeof _ | Tmty_alias _
155
151
->
156
- false
152
+ false
157
153
158
154
and moduleDeclarationCheckAnnotation ~checkAnnotation
159
- ({ md_attributes; md_type; md_loc = loc } : Typedtree.module_declaration ) =
155
+ ({md_attributes; md_type; md_loc = loc } : Typedtree.module_declaration ) =
160
156
md_attributes |> checkAnnotation ~loc
161
157
|| md_type |> moduleTypeCheckAnnotation ~check Annotation
162
158
163
159
and signatureItemCheckAnnotation ~checkAnnotation
164
160
(signatureItem : Typedtree.signature_item ) =
165
161
match signatureItem with
166
- | { Typedtree. sig_desc = Typedtree. Tsig_type (_ , typeDeclarations ) } ->
167
- typeDeclarations
168
- |> List. exists
169
- (fun ({ typ_attributes; typ_loc = loc } : Typedtree.type_declaration )
170
- -> typ_attributes |> checkAnnotation ~loc )
171
- | { sig_desc = Tsig_value { val_attributes; val_loc = loc } } ->
172
- val_attributes |> checkAnnotation ~loc
173
- | { sig_desc = Tsig_module moduleDeclaration } ->
174
- moduleDeclaration |> moduleDeclarationCheckAnnotation ~check Annotation
175
- | { sig_desc = Tsig_attribute attribute ; sig_loc = loc } ->
176
- [ attribute ] |> checkAnnotation ~loc
162
+ | {Typedtree. sig_desc = Typedtree. Tsig_type (_ , typeDeclarations )} ->
163
+ typeDeclarations
164
+ |> List. exists
165
+ (fun ({typ_attributes; typ_loc = loc } : Typedtree.type_declaration ) ->
166
+ typ_attributes |> checkAnnotation ~loc )
167
+ | {sig_desc = Tsig_value {val_attributes; val_loc = loc } } ->
168
+ val_attributes |> checkAnnotation ~loc
169
+ | {sig_desc = Tsig_module moduleDeclaration } ->
170
+ moduleDeclaration |> moduleDeclarationCheckAnnotation ~check Annotation
171
+ | {sig_desc = Tsig_attribute attribute ; sig_loc = loc } ->
172
+ [ attribute] |> checkAnnotation ~loc
177
173
| _ -> false
178
174
179
175
and signatureCheckAnnotation ~checkAnnotation (signature : Typedtree.signature )
@@ -183,46 +179,46 @@ and signatureCheckAnnotation ~checkAnnotation (signature : Typedtree.signature)
183
179
184
180
let rec structureItemCheckAnnotation ~checkAnnotation
185
181
(structureItem : Typedtree.structure_item ) =
186
- match structureItem with
187
- | { Typedtree. str_desc = Typedtree. Tstr_type (_ , typeDeclarations ) } ->
188
- typeDeclarations
189
- |> List. exists
190
- (fun ({ typ_attributes; typ_loc = loc } : Typedtree.type_declaration )
191
- -> typ_attributes |> checkAnnotation ~loc )
192
- | { str_desc = Tstr_value (_loc , valueBindings ) } ->
193
- valueBindings
194
- |> List. exists
195
- (fun ({ vb_attributes; vb_loc = loc } : Typedtree.value_binding ) ->
196
- vb_attributes |> checkAnnotation ~loc )
197
- | { str_desc = Tstr_primitive { val_attributes; val_loc = loc } } ->
198
- val_attributes |> checkAnnotation ~loc
199
- | { str_desc = Tstr_module moduleBinding } ->
200
- moduleBinding |> moduleBindingCheckAnnotation ~check Annotation
201
- | { str_desc = Tstr_recmodule moduleBindings } ->
202
- moduleBindings
203
- |> List. exists (moduleBindingCheckAnnotation ~check Annotation)
204
- | { str_desc = Tstr_include { incl_attributes; incl_mod; incl_loc = loc } } ->
205
- incl_attributes |> checkAnnotation ~loc
206
- || incl_mod |> moduleExprCheckAnnotation ~check Annotation
182
+ match structureItem.str_desc with
183
+ | Tstr_type (_ , typeDeclarations ) ->
184
+ typeDeclarations
185
+ |> List. exists
186
+ (fun ({typ_attributes; typ_loc = loc } : Typedtree.type_declaration ) ->
187
+ typ_attributes |> checkAnnotation ~loc )
188
+ | Tstr_value (_loc , valueBindings ) ->
189
+ valueBindings
190
+ |> List. exists
191
+ (fun ({vb_attributes; vb_loc = loc } : Typedtree.value_binding ) ->
192
+ vb_attributes |> checkAnnotation ~loc )
193
+ | Tstr_primitive {val_attributes; val_loc = loc } ->
194
+ val_attributes |> checkAnnotation ~loc
195
+ | Tstr_module moduleBinding ->
196
+ moduleBinding |> moduleBindingCheckAnnotation ~check Annotation
197
+ | Tstr_recmodule moduleBindings ->
198
+ moduleBindings
199
+ |> List. exists (moduleBindingCheckAnnotation ~check Annotation)
200
+ | Tstr_include {incl_attributes; incl_mod; incl_loc = loc } ->
201
+ incl_attributes |> checkAnnotation ~loc
202
+ || incl_mod |> moduleExprCheckAnnotation ~check Annotation
207
203
| _ -> false
208
204
209
205
and moduleExprCheckAnnotation ~checkAnnotation
210
206
(moduleExpr : Typedtree.module_expr ) =
211
207
match moduleExpr.mod_desc with
212
208
| Tmod_structure structure ->
213
- structure |> structureCheckAnnotation ~check Annotation
209
+ structure |> structureCheckAnnotation ~check Annotation
214
210
| Tmod_constraint
215
211
(moduleExpr, _moduleType, moduleTypeConstraint, _moduleCoercion) -> (
216
- moduleExpr |> moduleExprCheckAnnotation ~check Annotation
217
- ||
218
- match moduleTypeConstraint with
219
- | Tmodtype_explicit moduleType ->
220
- moduleType |> moduleTypeCheckAnnotation ~check Annotation
221
- | Tmodtype_implicit -> false )
212
+ moduleExpr |> moduleExprCheckAnnotation ~check Annotation
213
+ ||
214
+ match moduleTypeConstraint with
215
+ | Tmodtype_explicit moduleType ->
216
+ moduleType |> moduleTypeCheckAnnotation ~check Annotation
217
+ | Tmodtype_implicit -> false )
222
218
| Tmod_ident _ | Tmod_functor _ | Tmod_apply _ | Tmod_unpack _ -> false
223
219
224
220
and moduleBindingCheckAnnotation ~checkAnnotation
225
- ({ mb_expr; mb_attributes; mb_loc = loc } : Typedtree.module_binding ) =
221
+ ({mb_expr; mb_attributes; mb_loc = loc } : Typedtree.module_binding ) =
226
222
mb_attributes |> checkAnnotation ~loc
227
223
|| mb_expr |> moduleExprCheckAnnotation ~check Annotation
228
224
@@ -232,7 +228,10 @@ and structureCheckAnnotation ~checkAnnotation (structure : Typedtree.structure)
232
228
|> List. exists (structureItemCheckAnnotation ~check Annotation)
233
229
234
230
let sanitizeVariableName name =
235
- name |> String. map (function '-' -> '_' | c -> c)
231
+ name
232
+ |> String. map (function
233
+ | '-' -> '_'
234
+ | c -> c)
236
235
237
236
let importFromString importString : import =
238
237
let name =
@@ -241,4 +240,4 @@ let importFromString importString : import =
241
240
|> sanitizeVariableName
242
241
in
243
242
let importPath = ImportPath. fromStringUnsafe importString in
244
- { name; importPath }
243
+ {name; importPath}
0 commit comments