From 170b7729652a25b9c7c2a647fb2cfbcb256b9a88 Mon Sep 17 00:00:00 2001 From: Cheng Lou Date: Sat, 27 Feb 2021 22:23:15 -0800 Subject: [PATCH] Clean up the conversion of @bs.* and %bs.* Fixes #292 Linked tasks: - Check editor plugins' deprecation highlighting (ST, VSCode) - Update docs interop cheat sheet's decorators --- .depend | 2 +- src/res_ast_conversion.ml | 41 ++-------- src/res_printer.ml | 75 +++++++++++-------- src/res_printer.mli | 2 + .../reason/__snapshots__/render.spec.js.snap | 19 +++++ tests/conversion/reason/attributes.re | 18 +++++ .../__snapshots__/render.spec.js.snap | 2 +- tests/printer/structure/include.js | 2 +- 8 files changed, 95 insertions(+), 66 deletions(-) create mode 100644 tests/conversion/reason/attributes.re diff --git a/.depend b/.depend index fdabd771..737f864a 100644 --- a/.depend +++ b/.depend @@ -1,6 +1,6 @@ src/reactjs_jsx_ppx_v3.cmx : src/reactjs_jsx_ppx_v3.cmi src/reactjs_jsx_ppx_v3.cmi : -src/res_ast_conversion.cmx : src/res_ast_conversion.cmi +src/res_ast_conversion.cmx : src/res_printer.cmx src/res_ast_conversion.cmi src/res_ast_conversion.cmi : src/res_ast_debugger.cmx : src/res_driver.cmx src/res_doc.cmx \ src/res_ast_debugger.cmi diff --git a/src/res_ast_conversion.ml b/src/res_ast_conversion.ml index f583cd66..3441ad1e 100644 --- a/src/res_ast_conversion.ml +++ b/src/res_ast_conversion.ml @@ -324,43 +324,18 @@ let normalize = 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) - + ( + {id with txt = Res_printer.convertBsExtension id.txt}, + 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) + ( + {id with txt = Res_printer.convertBsExternalAttribute id.txt}, + default_mapper.payload mapper payload + ) ); attributes = (fun mapper attrs -> attrs diff --git a/src/res_printer.ml b/src/res_printer.ml index b64c4106..8e80f448 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -16,6 +16,49 @@ type callbackStyle = *) | ArgumentsFitOnOneLine +(* Since compiler version 8.3, the bs. prefix is no longer needed *) +(* Synced from + https://github.com/rescript-lang/rescript-compiler/blob/29174de1a5fde3b16cf05d10f5ac109cfac5c4ca/jscomp/frontend/ast_external_process.ml#L291-L367 *) +let convertBsExternalAttribute = function + | "bs.as" -> "as" + | "bs.deriving" -> "deriving" + | "bs.get" -> "get" + | "bs.get_index" -> "get_index" + | "bs.ignore" -> "ignore" + | "bs.inline" -> "inline" + | "bs.int" -> "int" + | "bs.meth" -> "meth" + | "bs.module" -> "module" + | "bs.new" -> "new" + | "bs.obj" -> "obj" + | "bs.optional" -> "optional" + | "bs.return" -> "return" + | "bs.send" -> "send" + | "bs.scope" -> "scope" + | "bs.set" -> "set" + | "bs.set_index" -> "set_index" + | "bs.splice" | "bs.variadic" -> "variadic" + | "bs.string" -> "string" + | "bs.this" -> "this" + | "bs.uncurry" -> "uncurry" + | "bs.unwrap" -> "unwrap" + | "bs.val" -> "val" + (* bs.send.pipe shouldn't be transformed *) + | txt -> txt + +(* These haven't been needed for a long time now *) +(* Synced from + https://github.com/rescript-lang/rescript-compiler/blob/29174de1a5fde3b16cf05d10f5ac109cfac5c4ca/jscomp/frontend/ast_exp_extension.ml *) +let convertBsExtension = function + | "bs.debugger" -> "debugger" + | "bs.external" -> "raw" + (* We should never see this one since we use the sugared object form, but still *) + | "bs.obj" -> "obj" + | "bs.raw" -> "raw" + | "bs.re" -> "re" + (* TODO: what about bs.time and bs.node? *) + | txt -> txt + let addParens doc = Doc.group ( Doc.concat [ @@ -1985,11 +2028,7 @@ 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 txt = convertBsExtension stringLoc.Location.txt in let extName = let doc = Doc.concat [ Doc.text "%"; @@ -4808,34 +4847,10 @@ 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 contents; + Doc.text (convertBsExternalAttribute id.txt); printPayload payload cmtTbl ] ) diff --git a/src/res_printer.mli b/src/res_printer.mli index 19220ee8..bfd0cd4d 100644 --- a/src/res_printer.mli +++ b/src/res_printer.mli @@ -1,3 +1,5 @@ +val convertBsExternalAttribute : string -> string +val convertBsExtension : string -> string val printTypeParams : (Parsetree.core_type * Asttypes.variance) list -> Res_comments_table.t -> Res_doc.t diff --git a/tests/conversion/reason/__snapshots__/render.spec.js.snap b/tests/conversion/reason/__snapshots__/render.spec.js.snap index 7f6e76d7..42b0874a 100644 --- a/tests/conversion/reason/__snapshots__/render.spec.js.snap +++ b/tests/conversion/reason/__snapshots__/render.spec.js.snap @@ -1,5 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`attributes.re 1`] = ` +"module Color: { + type t = private string + + @inline(\\"red\\") let red: t + @inline(\\"black\\") let black: t +} = { + type t = string + + @inline let red = \\"red\\" + @inline let black = \\"black\\" +} + +@send external map: (array<'a>, 'a => 'b) => array<'b> = \\"map\\" +@send external filter: (array<'a>, 'a => 'b) => array<'b> = \\"filter\\" +list{1, 2, 3}->map(a => a + 1)->filter(a => modulo(a, 2) == 0)->Js.log +" +`; + exports[`bracedJsx.re 1`] = ` "open Belt diff --git a/tests/conversion/reason/attributes.re b/tests/conversion/reason/attributes.re new file mode 100644 index 00000000..e5e4dfd2 --- /dev/null +++ b/tests/conversion/reason/attributes.re @@ -0,0 +1,18 @@ +module Color: { + type t = pri string; + + [@bs.inline "red"] let red: t; + [@bs.inline "black"] let black: t; +} = { + type t = string; + + [@bs.inline] let red = "red"; + [@bs.inline] let black = "black"; +}; + +[@bs.send] external map: (array('a), 'a => 'b) => array('b) = "map"; +[@bs.send] external filter: (array('a), 'a => 'b) => array('b) = "filter"; +[1, 2, 3] + ->map(a => a + 1) + ->filter(a => modulo(a, 2) == 0) + ->Js.log; \ No newline at end of file diff --git a/tests/printer/structure/__snapshots__/render.spec.js.snap b/tests/printer/structure/__snapshots__/render.spec.js.snap index 4600b248..fb0357e3 100644 --- a/tests/printer/structure/__snapshots__/render.spec.js.snap +++ b/tests/printer/structure/__snapshots__/render.spec.js.snap @@ -117,7 +117,7 @@ include ( { @val @module(\\"react\\") external createElementInternalHack: 'a = \\"createElement\\" - @bs.send + @send external apply: ( 'theFunction, 'theContext, diff --git a/tests/printer/structure/include.js b/tests/printer/structure/include.js index 8c63996e..309d7d71 100644 --- a/tests/printer/structure/include.js +++ b/tests/printer/structure/include.js @@ -8,7 +8,7 @@ include ( { @val @module("react") external createElementInternalHack: 'a = "createElement" - @bs.send + @send external apply: ( 'theFunction, 'theContext,