Skip to content

Commit 63215ab

Browse files
committed
change expected layout of import attributes in @module
1 parent 707b0e7 commit 63215ab

File tree

4 files changed

+75
-49
lines changed

4 files changed

+75
-49
lines changed

jscomp/frontend/ast_external_process.ml

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -276,56 +276,70 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
276276
({pexp_loc; pexp_desc = Pexp_record (fields, _); _}, _);
277277
_;
278278
};
279-
] ->
279+
] -> (
280280
let fromName = ref None in
281-
let importAttributesFromRecord =
282-
fields
283-
|> List.filter_map
284-
(fun
285-
((l, exp) :
286-
Longident.t Location.loc * Parsetree.expression)
287-
->
288-
match exp.pexp_desc with
289-
| Pexp_constant (Pconst_string (s, _)) -> (
290-
match l.txt with
291-
| Longident.Lident "from" ->
292-
fromName := Some s;
293-
None
294-
| Longident.Lident "type_" -> Some ("type", s)
295-
| Longident.Lident txt -> Some (txt, s)
281+
let with_ = ref None in
282+
fields
283+
|> List.iter
284+
(fun
285+
((l, exp) :
286+
Longident.t Location.loc * Parsetree.expression)
287+
->
288+
match (l, exp.pexp_desc) with
289+
| ( {txt = Lident "from"; _},
290+
Pexp_constant (Pconst_string (s, _)) ) ->
291+
fromName := Some s
292+
| {txt = Lident "with"; _}, Pexp_record (fields, _) ->
293+
with_ := Some fields
294+
| _ -> ());
295+
match (!fromName, !with_) with
296+
| None, _ ->
297+
Location.raise_errorf ~loc:pexp_loc
298+
"@module annotations with import attributes must have a \
299+
\"from\" field. This \"from\" field should point to the JS \
300+
module to import, just like the string payload to @module \
301+
normally does."
302+
| Some _, None ->
303+
Location.raise_errorf ~loc:pexp_loc
304+
"@module annotations with import attributes must have a \
305+
\"with\" field. This \"with\" field should hold a record of \
306+
the import attributes you want applied to the import."
307+
| Some fromName, Some withFields ->
308+
let importAttributesFromRecord =
309+
withFields
310+
|> List.filter_map
311+
(fun
312+
((l, exp) :
313+
Longident.t Location.loc * Parsetree.expression)
314+
->
315+
match exp.pexp_desc with
316+
| Pexp_constant (Pconst_string (s, _)) -> (
317+
match l.txt with
318+
| Longident.Lident "type_" -> Some ("type", s)
319+
| Longident.Lident txt -> Some (txt, s)
320+
| _ ->
321+
Location.raise_errorf ~loc:exp.pexp_loc
322+
"Field must be a regular key.")
296323
| _ ->
297324
Location.raise_errorf ~loc:exp.pexp_loc
298-
"Field must be a regular key.")
299-
| _ ->
300-
Location.raise_errorf ~loc:exp.pexp_loc
301-
"Only string values are allowed here.")
302-
in
303-
let fromName =
304-
match !fromName with
305-
| None ->
306-
Location.raise_errorf ~loc:pexp_loc
307-
"@module annotations with import attributes must have a \
308-
\"from\" field. This \"from\" field should point to the \
309-
JS module to import, just like the string payload to \
310-
@module normally does."
311-
| Some fromName -> fromName
312-
in
313-
let import_attributes =
314-
Hashtbl.create (List.length importAttributesFromRecord)
315-
in
316-
importAttributesFromRecord
317-
|> List.iter (fun (key, value) ->
318-
Hashtbl.replace import_attributes key value);
319-
{
320-
st with
321-
external_module_name =
322-
Some
323-
{
324-
bundle = fromName;
325-
module_bind_name = Phint_nothing;
326-
import_attributes = Some import_attributes;
327-
};
328-
}
325+
"Only string values are allowed here.")
326+
in
327+
let import_attributes =
328+
Hashtbl.create (List.length importAttributesFromRecord)
329+
in
330+
importAttributesFromRecord
331+
|> List.iter (fun (key, value) ->
332+
Hashtbl.replace import_attributes key value);
333+
{
334+
st with
335+
external_module_name =
336+
Some
337+
{
338+
bundle = fromName;
339+
module_bind_name = Phint_nothing;
340+
import_attributes = Some import_attributes;
341+
};
342+
})
329343
| _ -> (
330344
match Ast_payload.assert_strings loc payload with
331345
| [bundle] ->

jscomp/test/ImportAttributes.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/ImportAttributes.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@@config({flags: ["-bs-package-output", "es6:jscomp/test:.mjs"]})
22

3-
@module({from: "./myJson.json", type_: "json"})
3+
@module({from: "./myJson.json", with: {type_: "json"}})
44
external myJson: Js.Json.t = "default"
55

66
Js.log(myJson)

jscomp/test/build.ninja

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)