Skip to content

Commit d718736

Browse files
committed
Reason V4 Feature [Lint Warnings Injected]
Summary: Test Plan: Reviewers: CC:
1 parent 7270daf commit d718736

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

formatTest/typeCheckedTests/expected_output/mlVariants.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[@reason.version 3.7];
12
/* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. */
23

34
type polyVariantsInMl = [

src/reason-parser/reason_attributes.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ type attributesPartition = {
1313
uncurried : bool
1414
}
1515

16+
let is_stylistic_attr = function
17+
| { attr_name = {txt="reason.raw_literal"}; _}
18+
(* Consider warnings to be "stylistic" attributes - attributes that do not
19+
* affect printing *)
20+
| { attr_name = {txt="ocaml.ppwarn"}; _}
21+
| { attr_name = {txt="reason.preserve_braces"}; _} -> true
22+
| _ -> false
23+
24+
1625
(** Partition attributes into kinds *)
1726
let rec partitionAttributes ?(partDoc=false) ?(allowUncurry=true) attrs : attributesPartition =
1827
match attrs with
@@ -36,10 +45,7 @@ let rec partitionAttributes ?(partDoc=false) ?(allowUncurry=true) attrs : attrib
3645
| ({ attr_name = {txt="ocaml.doc" | "ocaml.text"}; _} as doc)::atTl when partDoc = true ->
3746
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
3847
{partition with docAttrs=doc::partition.docAttrs}
39-
| ({ attr_name = {txt="reason.raw_literal"}; _} as attr) :: atTl ->
40-
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
41-
{partition with stylisticAttrs=attr::partition.stylisticAttrs}
42-
| ({ attr_name = {txt="reason.preserve_braces"}; _} as attr) :: atTl ->
48+
| attr :: atTl when is_stylistic_attr attr ->
4349
let partition = partitionAttributes ~partDoc ~allowUncurry atTl in
4450
{partition with stylisticAttrs=attr::partition.stylisticAttrs}
4551
| atHd :: atTl ->
@@ -63,8 +69,7 @@ let extract_raw_literal attrs =
6369

6470
let without_stylistic_attrs attrs =
6571
let rec loop acc = function
66-
| attr :: rest when (partitionAttributes [attr]).stylisticAttrs != [] ->
67-
loop acc rest
72+
| attr :: rest when is_stylistic_attr attr -> loop acc rest
6873
| [] -> List.rev acc
6974
| attr :: rest -> loop (attr :: acc) rest
7075
in

src/reason-parser/reason_pprint_ast.ml

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ let expandLocation pos ~expand:(startPos, endPos) =
294294
}
295295
}
296296

297+
let should_keep_floating_stylistic_structure_attr = function
298+
| {pstr_desc=Pstr_attribute a; _} -> not (Reason_attributes.is_stylistic_attr a)
299+
| _ -> true
300+
301+
let should_keep_floating_stylistic_sig_attr = function
302+
| {psig_desc=Psig_attribute a; _} -> not (Reason_attributes.is_stylistic_attr a)
303+
| _ -> true
304+
297305
(* Computes the location of the attribute with the lowest line number
298306
* that isn't ghost. Useful to determine the start location of an item
299307
* in the parsetree that has attributes.
@@ -1063,7 +1071,7 @@ let makeTup ?(wrap=("", ""))?(trailComma=true) ?(uncurried = false) l =
10631071
~sep:(if trailComma then commaTrail else commaSep)
10641072
~postSpace:true
10651073
~break:IfNeed l
1066-
1074+
10671075
(* Makes angle brackets < > *)
10681076
let typeParameterBookends ?(wrap=("", ""))?(trailComma=true) l =
10691077
let useAngle = Reason_version.supports Reason_version.AngleBracketTypes in
@@ -7582,7 +7590,7 @@ let printer = object(self:'self)
75827590
~xf:self#structure_item
75837591
~getLoc:(fun x -> x.pstr_loc)
75847592
~comments:self#comments
7585-
structureItems
7593+
(List.filter should_keep_floating_stylistic_structure_attr structureItems)
75867594
in
75877595
source_map ~loc:{loc_start; loc_end; loc_ghost = false}
75887596
(makeList
@@ -8321,10 +8329,28 @@ let record_version_mapper super =
83218329
in
83228330
{ super with Ast_mapper.structure_item; Ast_mapper.signature_item }
83238331
8332+
(* These won't get removed from partitioning since they are individual floating
8333+
* attributes *)
8334+
let remove_floating_style_attributes super =
8335+
let super_structure = super.Ast_mapper.structure in
8336+
let super_signature = super.Ast_mapper.signature in
8337+
let structure mapper structure =
8338+
super_structure
8339+
mapper
8340+
(List.filter should_keep_floating_stylistic_structure_attr structure)
8341+
in
8342+
let signature mapper signature =
8343+
super_signature
8344+
mapper
8345+
(List.filter should_keep_floating_stylistic_sig_attr signature)
8346+
in
8347+
{ super with Ast_mapper.structure; Ast_mapper.signature }
8348+
83248349
let preprocessing_mapper =
83258350
ml_to_reason_swap_operator_mapper
8326-
(record_version_mapper (escape_stars_slashes_mapper
8327-
(add_explicit_arity_mapper Ast_mapper.default_mapper)))
8351+
(remove_floating_style_attributes
8352+
(record_version_mapper (escape_stars_slashes_mapper
8353+
(add_explicit_arity_mapper Ast_mapper.default_mapper))))
83288354
83298355
let core_type ppf x =
83308356
format_layout ppf

src/reason-version/reason_version.ml

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type package_version = {
2222

2323
type feature =
2424
| AngleBracketTypes
25-
25+
2626
(**
2727
* Tracks the current package version of Reason parser/printer. This is
2828
* primarily for printing the version with `refmt --version`.
@@ -77,7 +77,7 @@ let within
7777

7878
let at_least (major, minor) =
7979
within ~inclusive:true (major, minor) ~inclusive:true (10000,0)
80-
80+
8181
let supports = function
8282
| AngleBracketTypes -> at_least (3, 8)
8383

@@ -102,6 +102,11 @@ let _split_on_char sep_char str =
102102
String.sub str 0 j.contents :: r.contents
103103

104104
module Ast_nodes = struct
105+
let mk_warning_attribute_payload ~loc msg =
106+
let exp = Exp.mk ~loc (Pexp_constant (Pconst_string(msg, None))) in
107+
let item = { pstr_desc = Pstr_eval (exp, []); pstr_loc = exp.pexp_loc } in
108+
PStr [item]
109+
105110
let mk_version_attr_payload major minor =
106111
let major, minor = string_of_int major, string_of_int minor in
107112
let loc = dummy_loc () in
@@ -122,7 +127,26 @@ module Ast_nodes = struct
122127
first :: created :: rest
123128
| _ -> created :: itms
124129
)
125-
| Some efv -> itms
130+
| Some efv -> begin
131+
if efv.major > package_version.major ||
132+
(efv.major == package_version.major && efv.minor > package_version.minor) then
133+
let efv_mjr = string_of_int efv.major in
134+
let efv_mnr = string_of_int efv.minor in
135+
let pkg_mjr = string_of_int package_version.major in
136+
let pkg_mnr = string_of_int package_version.minor in
137+
let msg =
138+
"This file specifies a reason.version " ^ efv_mjr ^ "." ^ efv_mnr ^
139+
" which is greater than the package version " ^ pkg_mjr ^ "." ^ pkg_mnr ^
140+
" Either upgrade the Reason package or lower the version specified in [@reason.version ]." in
141+
(* let loc = match itms with *)
142+
(* | hd :: _ -> hd.pstr_loc *)
143+
(* | [] -> loc *)
144+
(* in *)
145+
let attr_payload = mk_warning_attribute_payload ~loc msg in
146+
let created = (creator ~loc {attr_name={loc; txt="ocaml.ppwarn"}; attr_payload; attr_loc=loc}) in
147+
created :: itms
148+
else itms
149+
end
126150

127151
let inject_attr_from_version_impl itms =
128152
let insert_after = function
@@ -131,15 +155,15 @@ module Ast_nodes = struct
131155
in
132156
let creator = (fun ~loc x -> Str.mk ~loc (Pstr_attribute x)) in
133157
inject_attr_from_version itms ~insert_after ~creator
134-
158+
135159
let inject_attr_from_version_intf itms =
136160
let insert_after = function
137161
| {psig_desc = Psig_attribute {attr_name = {loc; txt="ocaml.doc"|"ocaml.text"}}} -> true
138162
| _ -> false
139163
in
140164
let creator = (fun ~loc x -> Sig.mk ~loc (Psig_attribute x)) in
141165
inject_attr_from_version itms ~insert_after ~creator
142-
166+
143167
let extract_version_attribute_structure_item structure_item =
144168
(match structure_item with
145169
| {pstr_desc=(Pstr_attribute {

0 commit comments

Comments
 (0)