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

Print attributes without the @bs prefix #230

Merged
merged 4 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased

* Print attributes/extension without bs prefix where possible in [#230](https://github.com/rescript-lang/syntax/pull/230)
* Cleanup gentype attribute printing [fe05e1051aa94b16f6993ddc5ba9651f89e86907](https://github.com/rescript-lang/syntax/commit/fe05e1051aa94b16f6993ddc5ba9651f89e86907)
* Implement light weight syntax for poly-variants [f84c5760b3f743f65e934195c87fc06bf88bff75](https://github.com/rescript-lang/syntax/commit/f84c5760b3f743f65e934195c87fc06bf88bff75)
* Fix bug in fast pipe conversion from Reason. [3d5f2daba5418b821c577ba03e2de1afb0dd66de](https://github.com/rescript-lang/syntax/commit/3d5f2daba5418b821c577ba03e2de1afb0dd66de)
Expand Down
41 changes: 41 additions & 0 deletions src/res_ast_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,47 @@ let stringLiteralMapper stringData =
let normalize =
let open Ast_mapper in
{ default_mapper with
extension = (fun mapper ext ->
match ext with
| (id, payload) ->
let contents = match id.txt with
| "bs.raw" -> "raw"
| "bs.obj" -> "obj"
| txt -> txt
in
({id with txt = contents}, default_mapper.payload mapper payload)

);
attribute = (fun mapper attr ->
match attr with
| (id, payload) ->
(* Reminder, keep this in sync with src/res_printer.ml *)
let contents = match id.txt with
| "bs.val" -> "val"
| "bs.module" -> "module"
| "bs.scope" -> "scope"
| "bs.splice" | "bs.variadic" -> "variadic"
| "bs.set" -> "set"
| "bs.set_index" -> "set_index"
| "bs.get" -> "get"
| "bs.get_index" -> "get_index"
| "bs.new" -> "new"
| "bs.obj" -> "obj"
| "bs.return" -> "return"
| "bs.uncurry" -> "uncurry"
| "bs.this" -> "this"
| "bs.meth" -> "meth"
| "bs.deriving" -> "deriving"
| "bs.string" -> "string"
| "bs.int" -> "int"
| "bs.ignore" -> "ignore"
| "bs.unwrap" -> "unwrap"
| "bs.as" -> "as"
| "bs.optional" -> "optional"
| txt -> txt
in
({id with txt = contents}, default_mapper.payload mapper payload)
);
attributes = (fun mapper attrs ->
attrs
|> List.filter (fun attr ->
Expand Down
2 changes: 1 addition & 1 deletion src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ let rec walkStructure s t comments =
attach t.trailing expr2.pexp_loc trailing
)
| Pexp_extension (
{txt = "bs.obj"},
{txt = "bs.obj" | "obj"},
PStr [{
pstr_desc = Pstr_eval({pexp_desc = Pexp_record (rows, _)}, [])
}]
Expand Down
2 changes: 1 addition & 1 deletion src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2919,7 +2919,7 @@ and parseRecordExprWithStringKeys ~startPos firstRow p =
Ast_helper.Exp.record ~loc rows None
) in
Ast_helper.Exp.extension ~loc
(Location.mkloc "bs.obj" loc, Parsetree.PStr [recordStrExpr])
(Location.mkloc "obj" loc, Parsetree.PStr [recordStrExpr])

and parseRecordExpr ~startPos ?(spread=None) rows p =
let exprs =
Expand Down
6 changes: 3 additions & 3 deletions src/res_js_ffi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let importDescr ~attrs ~scope ~importSpec ~loc = {
}

let toParsetree importDescr =
let bsVal = (Location.mknoloc "bs.val", Parsetree.PStr []) in
let bsVal = (Location.mknoloc "val", Parsetree.PStr []) in
let attrs = match importDescr.jid_scope with
| Global -> [bsVal]
(* @genType.import("./MyMath"),
Expand All @@ -63,7 +63,7 @@ let toParsetree importDescr =
Ast_helper.Str.eval expr
in
let bsScope = (
Location.mknoloc "bs.scope",
Location.mknoloc "scope",
Parsetree. PStr [structureItem]
) in
[bsVal; bsScope]
Expand Down Expand Up @@ -113,4 +113,4 @@ let toParsetree importDescr =
let jsFfiAttr = (Location.mknoloc "ns.jsFfi", Parsetree.PStr []) in
Ast_helper.Mod.structure ~loc:importDescr.jid_loc valueDescrs
|> Ast_helper.Incl.mk ~attrs:[jsFfiAttr] ~loc:importDescr.jid_loc
|> Ast_helper.Str.include_ ~loc:importDescr.jid_loc
|> Ast_helper.Str.include_ ~loc:importDescr.jid_loc
4 changes: 2 additions & 2 deletions src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ let isHuggableExpression expr =
| Pexp_tuple _
| Pexp_constant (Pconst_string (_, Some _))
| Pexp_construct ({txt = Longident.Lident ("::" | "[]")}, _)
| Pexp_extension ({txt = "bs.obj"}, _)
| Pexp_extension ({txt = "bs.obj" | "obj"}, _)
| Pexp_record _ -> true
| _ when isBlockExpr expr -> true
| _ when isBracedExpr expr -> true
Expand All @@ -205,7 +205,7 @@ let isHuggableRhs expr =
| Pexp_array _
| Pexp_tuple _
| Pexp_construct ({txt = Longident.Lident ("::" | "[]")}, _)
| Pexp_extension ({txt = "bs.obj"}, _)
| Pexp_extension ({txt = "bs.obj" | "obj"}, _)
| Pexp_record _ -> true
| _ when isBracedExpr expr -> true
| _ -> false
Expand Down
35 changes: 32 additions & 3 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1973,11 +1973,16 @@ and printPackageConstraint i cmtTbl (longidentLoc, typ) =
]

and printExtension ~atModuleLvl (stringLoc, payload) cmtTbl =
let txt = match stringLoc.Location.txt with
| "bs.raw" -> "raw"
| "bs.obj" -> "obj"
| txt -> txt
in
let extName =
let doc = Doc.concat [
Doc.text "%";
if atModuleLvl then Doc.text "%" else Doc.nil;
Doc.text stringLoc.Location.txt;
Doc.text txt
] in
printComments doc cmtTbl stringLoc.Location.loc
in
Expand Down Expand Up @@ -2709,7 +2714,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
| Pexp_extension extension ->
begin match extension with
| (
{txt = "bs.obj"},
{txt = "bs.obj" | "obj"},
PStr [{
pstr_loc = loc;
pstr_desc = Pstr_eval({pexp_desc = Pexp_record (rows, _)}, [])
Expand Down Expand Up @@ -4730,10 +4735,34 @@ and printPayload (payload : Parsetree.payload) cmtTbl =
]

and printAttribute ((id, payload) : Parsetree.attribute) cmtTbl =
let contents = match id.txt with
| "bs.val" -> "val"
| "bs.module" -> "module"
| "bs.scope" -> "scope"
| "bs.splice" | "bs.variadic" -> "variadic"
| "bs.set" -> "set"
| "bs.set_index" -> "set_index"
| "bs.get" -> "get"
| "bs.get_index" -> "get_index"
| "bs.new" -> "new"
| "bs.obj" -> "obj"
| "bs.return" -> "return"
| "bs.uncurry" -> "uncurry"
| "bs.this" -> "this"
| "bs.meth" -> "meth"
| "bs.deriving" -> "deriving"
| "bs.string" -> "string"
| "bs.int" -> "int"
| "bs.ignore" -> "ignore"
| "bs.unwrap" -> "unwrap"
| "bs.as" -> "as"
| "bs.optional" -> "optional"
| txt -> txt
in
Doc.group (
Doc.concat [
Doc.text "@";
Doc.text id.txt;
Doc.text contents;
printPayload payload cmtTbl
]
)
Expand Down
14 changes: 11 additions & 3 deletions tests/conversion/reason/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1007,12 +1007,15 @@ let x = 1

exports[`extension.re 1`] = `
"// here
%%bs.raw(\` eval(
%%raw(\` eval(
__gc,
1,
0
)
\`)

let x = %raw(\\"10\\")
let y = %raw(\\"20\\")
"
`;

Expand Down Expand Up @@ -1086,6 +1089,9 @@ exports[`jsObject.re 1`] = `
"let component = props[\\"Component\\"]

let element = props[\\"element\\"]

let y = {\\"age\\": 30}
let y = {\\"age\\": 30, \\"name\\": \\"steve\\"}
"
`;

Expand Down Expand Up @@ -1246,7 +1252,7 @@ module Form = %form(
name: string,
email: string,
message: string,
@bs.as(\\"form-name\\")
@as(\\"form-name\\")
formName: string,
}
type output = input
Expand Down Expand Up @@ -1324,7 +1330,9 @@ let x = 1
`;

exports[`string.re 1`] = `
"%%bs.raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")
"%%raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")

%%raw(\\"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})\\")

let x = \`This is a long string with a slash and line break \\\\\\\\
carriage return\`
Expand Down
3 changes: 3 additions & 0 deletions tests/conversion/reason/extension.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ __gc,
0
)
|}]

let x = [%bs.raw "10"]
let y = [%raw "20"]
3 changes: 3 additions & 0 deletions tests/conversion/reason/jsObject.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
let component = props##"Component"

let element = props##element

let y = {"age": 30}
let y = {"age": 30, "name": "steve"}
3 changes: 3 additions & 0 deletions tests/conversion/reason/string.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
%bs.raw
"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})";

%raw
"define(x.y, 'userAgent', {value: 'USER_AGENT_STRING'})";

let x = {js|This is a long string with a slash and line break \
carriage return|js};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ let x = ((let a = 1 in let b = 2 in a + b)[@ns.braces ])
`;

exports[`bsObject.js 1`] = `
"let x = [%bs.obj { age = 30 }]
let y = [%bs.obj { age = 30 }]
let y = [%bs.obj { age = 30; name = \\"steve\\" }]
let y = [%bs.obj { age = 30; name = \\"steve\\" }]
"let x = [%obj { age = 30 }]
let y = [%obj { age = 30 }]
let y = [%obj { age = 30; name = \\"steve\\" }]
let y = [%obj { age = 30; name = \\"steve\\" }]
let x = ((\\"age\\")[@ns.braces ])
let x = ((\\"age\\".(0))[@ns.braces ])
let x = ((\\"age\\" |. Js.log)[@ns.braces ])
Expand Down
14 changes: 6 additions & 8 deletions tests/parsing/grammar/structure/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,25 @@ exports[`jsFfiSugar.js 1`] = `
"include
struct
external setTimeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"
[@@bs.val ]
[@@val ]
end[@@ns.jsFfi ]
include
struct
external timeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"
[@@bs.val ]
external timeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"[@@val ]
end[@@ns.jsFfi ]
include
struct
external setTimeout : (unit -> unit) -> unit -> float = \\"setTimeout\\"
[@@bs.val ]
external clearTimeout : float -> unit = \\"clearTimeout\\"[@@bs.val ]
[@@val ]
external clearTimeout : float -> unit = \\"clearTimeout\\"[@@val ]
end[@@ns.jsFfi ]
include
struct
external random : unit -> float = \\"random\\"[@@bs.val ][@@bs.scope \\"Math\\"]
external random : unit -> float = \\"random\\"[@@val ][@@scope \\"Math\\"]
end[@@ns.jsFfi ]
include
struct
external href : string = \\"href\\"[@@bs.val ][@@bs.scope
(\\"window\\", \\"location\\")]
external href : string = \\"href\\"[@@val ][@@scope (\\"window\\", \\"location\\")]
end[@@ns.jsFfi ]
include
struct
Expand Down
12 changes: 6 additions & 6 deletions tests/ppx/react/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`commentAtTop.res 1`] = `
"@bs.obj
"@obj
external makeProps: (~msg: 'msg, ~key: string=?, unit) => {\\"msg\\": 'msg} = \\"\\" // test React JSX file

let make =
Expand All @@ -17,14 +17,14 @@ let make = {

exports[`externalWithCustomName.res 1`] = `
"module Foo = {
@bs.obj
@obj
external componentProps: (
~a: int,
~b: string,
~key: string=?,
unit,
) => {\\"a\\": int, \\"b\\": string} = \\"\\"
@bs.module(\\"Foo\\")
@module(\\"Foo\\")
external component: React.componentLike<
{\\"a\\": int, \\"b\\": string},
React.element,
Expand All @@ -40,7 +40,7 @@ let t = React.createElement(

exports[`innerModule.res 1`] = `
"module Bar = {
@bs.obj
@obj
external makeProps: (
~a: 'a,
~b: 'b,
Expand All @@ -57,7 +57,7 @@ exports[`innerModule.res 1`] = `
make(~b=\\\\\\"Props\\"[\\"b\\"], ~a=\\\\\\"Props\\"[\\"a\\"], ())
\\\\\\"InnerModule$Bar\\"
}
@bs.obj
@obj
external componentProps: (
~a: 'a,
~b: 'b,
Expand All @@ -80,7 +80,7 @@ exports[`innerModule.res 1`] = `
`;

exports[`topLevel.res 1`] = `
"@bs.obj
"@obj
external makeProps: (
~a: 'a,
~b: 'b,
Expand Down
2 changes: 1 addition & 1 deletion tests/ppx/react/externalWithCustomName.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Foo = {
@react.component @bs.module("Foo")
@react.component @module("Foo")
external component: (~a: int, ~b: string, _) => React.element = "component"
}

Expand Down
Loading