From 84a5b69ae6457289c098a3355d7192d968e13b90 Mon Sep 17 00:00:00 2001 From: Roman Schejbal Date: Wed, 9 Oct 2019 22:45:14 -0500 Subject: [PATCH] Make external... = "" optional in reason syntax --- .../expected_output/reservedRecordPunned.re | 2 +- .../reservedRecordPunned.re.4.08.0 | 2 +- .../reservedRecordPunned.re.4.09.0 | 2 +- .../expected_output/attributes.re | 27 +++++++++---------- .../expected_output/attributes.rei | 2 +- .../expected_output/mlSyntax.re | 4 +-- .../expected_output/mlSyntax.re.4.02.3 | 4 +-- .../expected_output/reasonComments.re | 8 +++--- .../typeCheckedTests/input/attributes.re | 2 ++ .../typeCheckedTests/input/attributes.rei | 2 +- src/reason-parser/reason_parser.mly | 9 +++++++ src/reason-parser/reason_pprint_ast.ml | 10 ++++--- 12 files changed, 43 insertions(+), 31 deletions(-) diff --git a/formatTest/errorTests/expected_output/reservedRecordPunned.re b/formatTest/errorTests/expected_output/reservedRecordPunned.re index 9abf20b01..1c9d93ee7 100644 --- a/formatTest/errorTests/expected_output/reservedRecordPunned.re +++ b/formatTest/errorTests/expected_output/reservedRecordPunned.re @@ -1,2 +1,2 @@ File "reservedRecordPunned.re", line 1, characters 22-26: -Error: 3411: type is a reserved keyword, it cannot be used as an identifier. Try `type_` or `_type` instead +Error: 3417: type is a reserved keyword, it cannot be used as an identifier. Try `type_` or `_type` instead diff --git a/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.08.0 b/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.08.0 index cbd6e0039..38c941da5 100644 --- a/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.08.0 +++ b/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.08.0 @@ -1,5 +1,5 @@ File "reservedRecordPunned.re", line 1, characters 22-26: 1 | let foo = { ...other, type }; ^^^^ -Error: 3411: type is a reserved keyword, it cannot be used as an identifier. Try `type_` or `_type` instead +Error: 3417: type is a reserved keyword, it cannot be used as an identifier. Try `type_` or `_type` instead diff --git a/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.09.0 b/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.09.0 index cbd6e0039..38c941da5 100644 --- a/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.09.0 +++ b/formatTest/errorTests/expected_output/reservedRecordPunned.re.4.09.0 @@ -1,5 +1,5 @@ File "reservedRecordPunned.re", line 1, characters 22-26: 1 | let foo = { ...other, type }; ^^^^ -Error: 3411: type is a reserved keyword, it cannot be used as an identifier. Try `type_` or `_type` instead +Error: 3417: type is a reserved keyword, it cannot be used as an identifier. Try `type_` or `_type` instead diff --git a/formatTest/typeCheckedTests/expected_output/attributes.re b/formatTest/typeCheckedTests/expected_output/attributes.re index 9f1a1d60d..98cbc6b01 100644 --- a/formatTest/typeCheckedTests/expected_output/attributes.re +++ b/formatTest/typeCheckedTests/expected_output/attributes.re @@ -427,7 +427,7 @@ external add_nat: (int, int) => int = [@ocaml.deprecated "Use bar instead. It's a much cooler function. This string needs to be a little long" ] -external foo: bool => bool = ""; +external foo: bool => bool; /* Attributes on an entire polymorphic variant leaf */ [@bs.module "fs"] @@ -439,8 +439,7 @@ external readFileSync: | [@bs.as "ascii"] `my_name ] ) => - string = - ""; + string; [@bs.module "fs"] external readFileSync2: @@ -451,8 +450,7 @@ external readFileSync2: | [@bs.as "ascii"] `my_name ] ) => - string = - ""; + string; /* Ensure that attributes on extensions are printed */ [@test @@ -460,17 +458,18 @@ external readFileSync2: [%%extension] ]; -external debounce: (int, [@bs.meth] unit) => unit = - ""; +external debounce: + (int, [@bs.meth] unit) => unit; + +external debounce: + (int, [@bs.meth] unit) => unit; external debounce: - int => [@bs.meth] (unit => unit) = - ""; + int => [@bs.meth] (unit => unit); external debounce: (int, [@bs.meth] (unit => unit)) => - [@bs.meth] (unit => unit) = - ""; + [@bs.meth] (unit => unit); external debounce: ( @@ -478,8 +477,7 @@ external debounce: [@bs.meth] (unit => unit), [@bs.meth] (unit => unit) ) => - [@bs.meth] (unit => unit) = - ""; + [@bs.meth] (unit => unit); external debounce: ( @@ -489,8 +487,7 @@ external debounce: unit => [@bs.meth] (unit => unit) ) ) => - [@bs.meth] (unit => unit) = - ""; + [@bs.meth] (unit => unit); let x = "hi"; diff --git a/formatTest/typeCheckedTests/expected_output/attributes.rei b/formatTest/typeCheckedTests/expected_output/attributes.rei index 4e3b0ec3c..490a46387 100644 --- a/formatTest/typeCheckedTests/expected_output/attributes.rei +++ b/formatTest/typeCheckedTests/expected_output/attributes.rei @@ -29,7 +29,7 @@ external createClassInternalHack: "createClass"; [@bs.send.pipe: array('a)] -external map: (. ('a => 'b)) => array('b) = ""; +external map: (. ('a => 'b)) => array('b); [@bs.val] [@bs.module "react"] external createClassInternalHack: diff --git a/formatTest/typeCheckedTests/expected_output/mlSyntax.re b/formatTest/typeCheckedTests/expected_output/mlSyntax.re index 2b63004d2..084c07817 100644 --- a/formatTest/typeCheckedTests/expected_output/mlSyntax.re +++ b/formatTest/typeCheckedTests/expected_output/mlSyntax.re @@ -108,8 +108,8 @@ let pub__ = "method"; let pri_ = "private"; let pri__ = "private"; -external private: unit => unit = ""; -external pri_: unit => unit = ""; +external private: unit => unit; +external pri_: unit => unit; type pub_ = int; type pub__ = int; diff --git a/formatTest/typeCheckedTests/expected_output/mlSyntax.re.4.02.3 b/formatTest/typeCheckedTests/expected_output/mlSyntax.re.4.02.3 index 43157de30..770071080 100644 --- a/formatTest/typeCheckedTests/expected_output/mlSyntax.re.4.02.3 +++ b/formatTest/typeCheckedTests/expected_output/mlSyntax.re.4.02.3 @@ -108,8 +108,8 @@ let pub__ = "method"; let pri_ = "private"; let pri__ = "private"; -external private: unit => unit = ""; -external pri_: unit => unit = ""; +external private: unit => unit; +external pri_: unit => unit; type pub_ = int; type pub__ = int; diff --git a/formatTest/typeCheckedTests/expected_output/reasonComments.re b/formatTest/typeCheckedTests/expected_output/reasonComments.re index 9aa1de9d9..63b87d939 100644 --- a/formatTest/typeCheckedTests/expected_output/reasonComments.re +++ b/formatTest/typeCheckedTests/expected_output/reasonComments.re @@ -731,20 +731,20 @@ let r = { /** doc comment */ [@bs.send] -external url: t => string = ""; +external url: t => string; /** * Short multiline doc comment */ [@bs.send] -external url: t => string = ""; +external url: t => string; /** Longer doc comment before an attribute on an external. */ [@bs.send] -external url: t => string = ""; +external url: t => string; /* normal comment */ -[@bs.send] external url: t => string = ""; +[@bs.send] external url: t => string; /** doc type */ type q = { diff --git a/formatTest/typeCheckedTests/input/attributes.re b/formatTest/typeCheckedTests/input/attributes.re index 84314d7c0..6e74e2ae3 100644 --- a/formatTest/typeCheckedTests/input/attributes.re +++ b/formatTest/typeCheckedTests/input/attributes.re @@ -360,6 +360,8 @@ external readFileSync2 : ( /* Ensure that attributes on extensions are printed */ [@test [@attr] [%%extension]]; +external debounce : (int, [@bs.meth] unit) => unit; + external debounce : (int, [@bs.meth] unit) => unit = ""; external debounce : int => ([@bs.meth] (unit => unit)) = ""; diff --git a/formatTest/typeCheckedTests/input/attributes.rei b/formatTest/typeCheckedTests/input/attributes.rei index 3ba372674..a8f5c8132 100644 --- a/formatTest/typeCheckedTests/input/attributes.rei +++ b/formatTest/typeCheckedTests/input/attributes.rei @@ -27,7 +27,7 @@ type reactElement; [@bs.val] [@bs.module "React"] external createClassInternalHack : (t('classSpec)) => reactClass = "createClass"; -[@bs.send.pipe : array('a)] external map : [@bs] (('a) => 'b) => array('b) = ""; +[@bs.send.pipe : array('a)] external map: [@bs] (('a) => 'b) => array('b) = ""; [@bs.val] [@bs.module "react"] external createClassInternalHack : (t('classSpec)) => reactClass = diff --git a/src/reason-parser/reason_parser.mly b/src/reason-parser/reason_parser.mly index 05e5a139e..148c6ad39 100644 --- a/src/reason-parser/reason_parser.mly +++ b/src/reason-parser/reason_parser.mly @@ -1705,6 +1705,10 @@ structure_item: EXTERNAL as_loc(val_ident) COLON core_type EQUAL primitive_declaration { let loc = mklocation $symbolstartpos $endpos in mkstr (Pstr_primitive (Val.mk $3 $5 ~prim:$7 ~attrs:$1 ~loc)) } + | item_attributes + EXTERNAL as_loc(val_ident) COLON core_type SEMI + { let loc = mklocation $symbolstartpos $endpos in + mkstr (Pstr_primitive (Val.mk $3 $5 ~prim:[""] ~attrs:$1 ~loc)) } | type_declarations { let (nonrec_flag, tyl) = $1 in mkstr(Pstr_type (nonrec_flag, tyl)) } | str_type_extension @@ -1912,6 +1916,11 @@ signature_item: { let loc = mklocation $symbolstartpos $endpos in Psig_value (Val.mk $3 $5 ~prim:$7 ~attrs:$1 ~loc) } + | item_attributes + EXTERNAL as_loc(val_ident) COLON core_type SEMI + { let loc = mklocation $symbolstartpos $endpos in + Psig_value (Val.mk $3 $5 ~prim:[""] ~attrs:$1 ~loc) + } | type_declarations { let (nonrec_flag, tyl) = $1 in Psig_type (nonrec_flag, tyl) } | sig_type_extension diff --git a/src/reason-parser/reason_pprint_ast.ml b/src/reason-parser/reason_pprint_ast.ml index cae3a549a..fc656b391 100644 --- a/src/reason-parser/reason_pprint_ast.ml +++ b/src/reason-parser/reason_pprint_ast.ml @@ -6487,9 +6487,13 @@ let printer = object(self:'self) [(makeList ~postSpace:true [atom "external"; protectIdentifier vd.pval_name.txt]); (atom ":")]) (self#core_type vd.pval_type) in - let frstHalf = makeList ~postSpace:true [lblBefore; atom "="] in - let sndHalf = makeSpacedBreakableInlineList (List.map self#constant_string vd.pval_prim) in - let primDecl = label ~space:true frstHalf sndHalf in + let primDecl = match vd.pval_prim with + | [""] -> lblBefore + | _ -> + let frstHalf = makeList ~postSpace:true [lblBefore; atom "="] in + let sndHalf = makeSpacedBreakableInlineList (List.map self#constant_string vd.pval_prim) in + label ~space:true frstHalf sndHalf + in match vd.pval_attributes with | [] -> primDecl | attrs ->