Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 3081360

Browse files
Print attributes without the @bs prefix (#230)
* Print attributes without the @bs prefix where possible. * Tweak %bs.obj vs %obj * Implement printing and conversion of bs.raw -> raw * Add changelog entry
1 parent 3ec83d1 commit 3081360

39 files changed

+231
-146
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
* Print attributes/extension without bs prefix where possible in [#230](https://github.com/rescript-lang/syntax/pull/230)
34
* Cleanup gentype attribute printing [fe05e1051aa94b16f6993ddc5ba9651f89e86907](https://github.com/rescript-lang/syntax/commit/fe05e1051aa94b16f6993ddc5ba9651f89e86907)
45
* Implement light weight syntax for poly-variants [f84c5760b3f743f65e934195c87fc06bf88bff75](https://github.com/rescript-lang/syntax/commit/f84c5760b3f743f65e934195c87fc06bf88bff75)
56
* Fix bug in fast pipe conversion from Reason. [3d5f2daba5418b821c577ba03e2de1afb0dd66de](https://github.com/rescript-lang/syntax/commit/3d5f2daba5418b821c577ba03e2de1afb0dd66de)

src/res_ast_conversion.ml

+41
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,47 @@ let stringLiteralMapper stringData =
321321
let normalize =
322322
let open Ast_mapper in
323323
{ default_mapper with
324+
extension = (fun mapper ext ->
325+
match ext with
326+
| (id, payload) ->
327+
let contents = match id.txt with
328+
| "bs.raw" -> "raw"
329+
| "bs.obj" -> "obj"
330+
| txt -> txt
331+
in
332+
({id with txt = contents}, default_mapper.payload mapper payload)
333+
334+
);
335+
attribute = (fun mapper attr ->
336+
match attr with
337+
| (id, payload) ->
338+
(* Reminder, keep this in sync with src/res_printer.ml *)
339+
let contents = match id.txt with
340+
| "bs.val" -> "val"
341+
| "bs.module" -> "module"
342+
| "bs.scope" -> "scope"
343+
| "bs.splice" | "bs.variadic" -> "variadic"
344+
| "bs.set" -> "set"
345+
| "bs.set_index" -> "set_index"
346+
| "bs.get" -> "get"
347+
| "bs.get_index" -> "get_index"
348+
| "bs.new" -> "new"
349+
| "bs.obj" -> "obj"
350+
| "bs.return" -> "return"
351+
| "bs.uncurry" -> "uncurry"
352+
| "bs.this" -> "this"
353+
| "bs.meth" -> "meth"
354+
| "bs.deriving" -> "deriving"
355+
| "bs.string" -> "string"
356+
| "bs.int" -> "int"
357+
| "bs.ignore" -> "ignore"
358+
| "bs.unwrap" -> "unwrap"
359+
| "bs.as" -> "as"
360+
| "bs.optional" -> "optional"
361+
| txt -> txt
362+
in
363+
({id with txt = contents}, default_mapper.payload mapper payload)
364+
);
324365
attributes = (fun mapper attrs ->
325366
attrs
326367
|> List.filter (fun attr ->

src/res_comments_table.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ let rec walkStructure s t comments =
879879
attach t.trailing expr2.pexp_loc trailing
880880
)
881881
| Pexp_extension (
882-
{txt = "bs.obj"},
882+
{txt = "bs.obj" | "obj"},
883883
PStr [{
884884
pstr_desc = Pstr_eval({pexp_desc = Pexp_record (rows, _)}, [])
885885
}]

src/res_core.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,7 @@ and parseRecordExprWithStringKeys ~startPos firstRow p =
29192919
Ast_helper.Exp.record ~loc rows None
29202920
) in
29212921
Ast_helper.Exp.extension ~loc
2922-
(Location.mkloc "bs.obj" loc, Parsetree.PStr [recordStrExpr])
2922+
(Location.mkloc "obj" loc, Parsetree.PStr [recordStrExpr])
29232923

29242924
and parseRecordExpr ~startPos ?(spread=None) rows p =
29252925
let exprs =

src/res_js_ffi.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let importDescr ~attrs ~scope ~importSpec ~loc = {
3939
}
4040

4141
let toParsetree importDescr =
42-
let bsVal = (Location.mknoloc "bs.val", Parsetree.PStr []) in
42+
let bsVal = (Location.mknoloc "val", Parsetree.PStr []) in
4343
let attrs = match importDescr.jid_scope with
4444
| Global -> [bsVal]
4545
(* @genType.import("./MyMath"),
@@ -63,7 +63,7 @@ let toParsetree importDescr =
6363
Ast_helper.Str.eval expr
6464
in
6565
let bsScope = (
66-
Location.mknoloc "bs.scope",
66+
Location.mknoloc "scope",
6767
Parsetree. PStr [structureItem]
6868
) in
6969
[bsVal; bsScope]
@@ -113,4 +113,4 @@ let toParsetree importDescr =
113113
let jsFfiAttr = (Location.mknoloc "ns.jsFfi", Parsetree.PStr []) in
114114
Ast_helper.Mod.structure ~loc:importDescr.jid_loc valueDescrs
115115
|> Ast_helper.Incl.mk ~attrs:[jsFfiAttr] ~loc:importDescr.jid_loc
116-
|> Ast_helper.Str.include_ ~loc:importDescr.jid_loc
116+
|> Ast_helper.Str.include_ ~loc:importDescr.jid_loc

src/res_parsetree_viewer.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ let isHuggableExpression expr =
193193
| Pexp_tuple _
194194
| Pexp_constant (Pconst_string (_, Some _))
195195
| Pexp_construct ({txt = Longident.Lident ("::" | "[]")}, _)
196-
| Pexp_extension ({txt = "bs.obj"}, _)
196+
| Pexp_extension ({txt = "bs.obj" | "obj"}, _)
197197
| Pexp_record _ -> true
198198
| _ when isBlockExpr expr -> true
199199
| _ when isBracedExpr expr -> true
@@ -205,7 +205,7 @@ let isHuggableRhs expr =
205205
| Pexp_array _
206206
| Pexp_tuple _
207207
| Pexp_construct ({txt = Longident.Lident ("::" | "[]")}, _)
208-
| Pexp_extension ({txt = "bs.obj"}, _)
208+
| Pexp_extension ({txt = "bs.obj" | "obj"}, _)
209209
| Pexp_record _ -> true
210210
| _ when isBracedExpr expr -> true
211211
| _ -> false

src/res_printer.ml

+32-3
Original file line numberDiff line numberDiff line change
@@ -1973,11 +1973,16 @@ and printPackageConstraint i cmtTbl (longidentLoc, typ) =
19731973
]
19741974

19751975
and printExtension ~atModuleLvl (stringLoc, payload) cmtTbl =
1976+
let txt = match stringLoc.Location.txt with
1977+
| "bs.raw" -> "raw"
1978+
| "bs.obj" -> "obj"
1979+
| txt -> txt
1980+
in
19761981
let extName =
19771982
let doc = Doc.concat [
19781983
Doc.text "%";
19791984
if atModuleLvl then Doc.text "%" else Doc.nil;
1980-
Doc.text stringLoc.Location.txt;
1985+
Doc.text txt
19811986
] in
19821987
printComments doc cmtTbl stringLoc.Location.loc
19831988
in
@@ -2709,7 +2714,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
27092714
| Pexp_extension extension ->
27102715
begin match extension with
27112716
| (
2712-
{txt = "bs.obj"},
2717+
{txt = "bs.obj" | "obj"},
27132718
PStr [{
27142719
pstr_loc = loc;
27152720
pstr_desc = Pstr_eval({pexp_desc = Pexp_record (rows, _)}, [])
@@ -4730,10 +4735,34 @@ and printPayload (payload : Parsetree.payload) cmtTbl =
47304735
]
47314736

47324737
and printAttribute ((id, payload) : Parsetree.attribute) cmtTbl =
4738+
let contents = match id.txt with
4739+
| "bs.val" -> "val"
4740+
| "bs.module" -> "module"
4741+
| "bs.scope" -> "scope"
4742+
| "bs.splice" | "bs.variadic" -> "variadic"
4743+
| "bs.set" -> "set"
4744+
| "bs.set_index" -> "set_index"
4745+
| "bs.get" -> "get"
4746+
| "bs.get_index" -> "get_index"
4747+
| "bs.new" -> "new"
4748+
| "bs.obj" -> "obj"
4749+
| "bs.return" -> "return"
4750+
| "bs.uncurry" -> "uncurry"
4751+
| "bs.this" -> "this"
4752+
| "bs.meth" -> "meth"
4753+
| "bs.deriving" -> "deriving"
4754+
| "bs.string" -> "string"
4755+
| "bs.int" -> "int"
4756+
| "bs.ignore" -> "ignore"
4757+
| "bs.unwrap" -> "unwrap"
4758+
| "bs.as" -> "as"
4759+
| "bs.optional" -> "optional"
4760+
| txt -> txt
4761+
in
47334762
Doc.group (
47344763
Doc.concat [
47354764
Doc.text "@";
4736-
Doc.text id.txt;
4765+
Doc.text contents;
47374766
printPayload payload cmtTbl
47384767
]
47394768
)

tests/conversion/reason/__snapshots__/render.spec.js.snap

+11-3
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,15 @@ let x = 1
10071007
10081008
exports[`extension.re 1`] = `
10091009
"// here
1010-
%%bs.raw(\` eval(
1010+
%%raw(\` eval(
10111011
__gc,
10121012
1,
10131013
0
10141014
)
10151015
\`)
1016+
1017+
let x = %raw(\\"10\\")
1018+
let y = %raw(\\"20\\")
10161019
"
10171020
`;
10181021
@@ -1086,6 +1089,9 @@ exports[`jsObject.re 1`] = `
10861089
"let component = props[\\"Component\\"]
10871090
10881091
let element = props[\\"element\\"]
1092+
1093+
let y = {\\"age\\": 30}
1094+
let y = {\\"age\\": 30, \\"name\\": \\"steve\\"}
10891095
"
10901096
`;
10911097
@@ -1246,7 +1252,7 @@ module Form = %form(
12461252
name: string,
12471253
email: string,
12481254
message: string,
1249-
@bs.as(\\"form-name\\")
1255+
@as(\\"form-name\\")
12501256
formName: string,
12511257
}
12521258
type output = input
@@ -1324,7 +1330,9 @@ let x = 1
13241330
`;
13251331
13261332
exports[`string.re 1`] = `
1327-
"%%bs.raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")
1333+
"%%raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")
1334+
1335+
%%raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")
13281336
13291337
let x = \`This is a long string with a slash and line break \\\\\\\\
13301338
carriage return\`

tests/conversion/reason/extension.re

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ __gc,
55
0
66
)
77
|}]
8+
9+
let x = [%bs.raw "10"]
10+
let y = [%raw "20"]

tests/conversion/reason/jsObject.re

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
let component = props##"Component"
22

33
let element = props##element
4+
5+
let y = {"age": 30}
6+
let y = {"age": 30, "name": "steve"}

tests/conversion/reason/string.re

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
%bs.raw
22
"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})";
33

4+
%raw
5+
"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})";
6+
47
let x = {js|This is a long string with a slash and line break \
58
carriage return|js};
69

tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,10 @@ let x = ((let a = 1 in let b = 2 in a + b)[@ns.braces ])
281281
`;
282282

283283
exports[`bsObject.js 1`] = `
284-
"let x = [%bs.obj { age = 30 }]
285-
let y = [%bs.obj { age = 30 }]
286-
let y = [%bs.obj { age = 30; name = \\"steve\\" }]
287-
let y = [%bs.obj { age = 30; name = \\"steve\\" }]
284+
"let x = [%obj { age = 30 }]
285+
let y = [%obj { age = 30 }]
286+
let y = [%obj { age = 30; name = \\"steve\\" }]
287+
let y = [%obj { age = 30; name = \\"steve\\" }]
288288
let x = ((\\"age\\")[@ns.braces ])
289289
let x = ((\\"age\\".(0))[@ns.braces ])
290290
let x = ((\\"age\\" |. Js.log)[@ns.braces ])

tests/parsing/grammar/structure/__snapshots__/parse.spec.js.snap

+6-8
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,25 @@ exports[`jsFfiSugar.js 1`] = `
6262
"include
6363
struct
6464
external setTimeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"
65-
[@@bs.val ]
65+
[@@val ]
6666
end[@@ns.jsFfi ]
6767
include
6868
struct
69-
external timeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"
70-
[@@bs.val ]
69+
external timeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"[@@val ]
7170
end[@@ns.jsFfi ]
7271
include
7372
struct
7473
external setTimeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"
75-
[@@bs.val ]
76-
external clearTimeout : float -> unit = \\"clearTimeout\\"[@@bs.val ]
74+
[@@val ]
75+
external clearTimeout : float -> unit = \\"clearTimeout\\"[@@val ]
7776
end[@@ns.jsFfi ]
7877
include
7978
struct
80-
external random : unit -> float = \\"random\\"[@@bs.val ][@@bs.scope \\"Math\\"]
79+
external random : unit -> float = \\"random\\"[@@val ][@@scope \\"Math\\"]
8180
end[@@ns.jsFfi ]
8281
include
8382
struct
84-
external href : string = \\"href\\"[@@bs.val ][@@bs.scope
85-
(\\"window\\", \\"location\\")]
83+
external href : string = \\"href\\"[@@val ][@@scope (\\"window\\", \\"location\\")]
8684
end[@@ns.jsFfi ]
8785
include
8886
struct

tests/ppx/react/__snapshots__/render.spec.js.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`commentAtTop.res 1`] = `
4-
"@bs.obj
4+
"@obj
55
external makeProps: (~msg: 'msg, ~key: string=?, unit) => {\\"msg\\": 'msg} = \\"\\" // test React JSX file
66
77
let make =
@@ -17,14 +17,14 @@ let make = {
1717
1818
exports[`externalWithCustomName.res 1`] = `
1919
"module Foo = {
20-
@bs.obj
20+
@obj
2121
external componentProps: (
2222
~a: int,
2323
~b: string,
2424
~key: string=?,
2525
unit,
2626
) => {\\"a\\": int, \\"b\\": string} = \\"\\"
27-
@bs.module(\\"Foo\\")
27+
@module(\\"Foo\\")
2828
external component: React.componentLike<
2929
{\\"a\\": int, \\"b\\": string},
3030
React.element,
@@ -40,7 +40,7 @@ let t = React.createElement(
4040
4141
exports[`innerModule.res 1`] = `
4242
"module Bar = {
43-
@bs.obj
43+
@obj
4444
external makeProps: (
4545
~a: 'a,
4646
~b: 'b,
@@ -57,7 +57,7 @@ exports[`innerModule.res 1`] = `
5757
make(~b=\\\\\\"Props\\"[\\"b\\"], ~a=\\\\\\"Props\\"[\\"a\\"], ())
5858
\\\\\\"InnerModule$Bar\\"
5959
}
60-
@bs.obj
60+
@obj
6161
external componentProps: (
6262
~a: 'a,
6363
~b: 'b,
@@ -80,7 +80,7 @@ exports[`innerModule.res 1`] = `
8080
`;
8181
8282
exports[`topLevel.res 1`] = `
83-
"@bs.obj
83+
"@obj
8484
external makeProps: (
8585
~a: 'a,
8686
~b: 'b,

tests/ppx/react/externalWithCustomName.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Foo = {
2-
@react.component @bs.module("Foo")
2+
@react.component @module("Foo")
33
external component: (~a: int, ~b: string, _) => React.element = "component"
44
}
55

0 commit comments

Comments
 (0)