Skip to content

Commit

Permalink
add format.bracket_spacing option
Browse files Browse the repository at this point in the history
Summary:
adds a flowconfig option to determine whether the code formatter includes spaces inside brackets, like `{ foo: 1 }` instead of `{foo: 1}`. defaults to `true`

this parallels the Prettier `bracketSpacing` option, which also defaults to true.

Reviewed By: gkz

Differential Revision: D27786100

fbshipit-source-id: fd458e83e28e7ef0aada4ba5684031b2350cefe0
  • Loading branch information
mroch authored and facebook-github-bot committed Apr 17, 2021
1 parent 90f33e2 commit 2d7614f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,9 @@ let make_options
in
let opt_format =
{
Options.opt_single_quotes =
Options.opt_bracket_spacing =
Base.Option.value (FlowConfig.format_bracket_spacing flowconfig) ~default:true;
opt_single_quotes =
Base.Option.value (FlowConfig.format_single_quotes flowconfig) ~default:false;
}
in
Expand Down
8 changes: 8 additions & 0 deletions src/commands/config/flowConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module Opts = struct
facebook_module_interop: bool;
file_watcher_timeout: int option;
file_watcher: file_watcher option;
format_bracket_spacing: bool option; (** print spaces between brackets in object literals *)
format_single_quotes: bool option; (** prefer single-quoted strings *)
gc_worker_custom_major_ratio: int option; (** Gc.control's custom_major_ratio *)
gc_worker_custom_minor_max_size: int option; (** Gc.control's custom_minor_max_size *)
Expand Down Expand Up @@ -185,6 +186,7 @@ module Opts = struct
facebook_module_interop = false;
file_watcher = None;
file_watcher_timeout = None;
format_bracket_spacing = None;
format_single_quotes = None;
gc_worker_custom_major_ratio = None;
gc_worker_custom_minor_max_size = None;
Expand Down Expand Up @@ -448,6 +450,9 @@ module Opts = struct
enum [("none", NoFileWatcher); ("dfind", DFind); ("watchman", Watchman)] (fun opts v ->
Ok { opts with file_watcher = Some v })

let format_bracket_spacing_parser =
boolean (fun opts v -> Ok { opts with format_bracket_spacing = Some v })

let format_single_quotes_parser =
boolean (fun opts v -> Ok { opts with format_single_quotes = Some v })

Expand Down Expand Up @@ -654,6 +659,7 @@ module Opts = struct
("file_watcher.watchman.mergebase_with", watchman_mergebase_with_parser);
("file_watcher.watchman.sync_timeout", watchman_sync_timeout_parser);
("file_watcher", file_watcher_parser);
("format.bracket_spacing", format_bracket_spacing_parser);
("format.single_quotes", format_single_quotes_parser);
("gc.worker.custom_major_ratio", gc_worker_custom_major_ratio_parser);
("gc.worker.custom_minor_max_size", gc_worker_custom_minor_max_size_parser);
Expand Down Expand Up @@ -1237,6 +1243,8 @@ let check_updates_against_providers c = c.options.Opts.check_updates_against_pro

let emoji c = c.options.Opts.emoji

let format_bracket_spacing c = c.options.Opts.format_bracket_spacing

let format_single_quotes c = c.options.Opts.format_single_quotes

let max_literal_length c = c.options.Opts.max_literal_length
Expand Down
2 changes: 2 additions & 0 deletions src/commands/config/flowConfig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ val file_watcher : config -> file_watcher option

val file_watcher_timeout : config -> int option

val format_bracket_spacing : config -> bool option

val format_single_quotes : config -> bool option

val gc_worker_custom_major_ratio : config -> int option
Expand Down
7 changes: 6 additions & 1 deletion src/common/options.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ type react_runtime =
| ReactRuntimeAutomatic
| ReactRuntimeClassic

type format = { opt_single_quotes: bool }
type format = {
opt_bracket_spacing: bool;
opt_single_quotes: bool;
}

type gc_control = {
gc_minor_heap_size: int option;
Expand Down Expand Up @@ -163,6 +166,8 @@ let enums opts = opts.opt_enums

let enums_with_unknown_members opts = opts.opt_enums_with_unknown_members

let format_bracket_spacing opts = opts.opt_format.opt_bracket_spacing

let format_single_quotes opts = opts.opt_format.opt_single_quotes

let this_annot opts = opts.opt_this_annot
Expand Down
37 changes: 35 additions & 2 deletions src/services/code_action/__tests__/autofix_imports_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,26 @@ let apply_patch contents patch =
^ String.sub contents end_offset (String.length contents - end_offset))
contents

let assert_patch ~ctxt ?(single_quotes = false) expected binding from contents =
let assert_patch ~ctxt ?bracket_spacing ?single_quotes expected binding from contents =
let expected = dedent_trim expected in
let contents = dedent_trim contents in
let options = Js_layout_generator.{ default_opts with single_quotes } in
let options =
let open Js_layout_generator in
let options = default_opts in
let options =
Base.Option.value_map
~default:options
~f:(fun bracket_spacing -> { options with bracket_spacing })
bracket_spacing
in
let options =
Base.Option.value_map
~default:options
~f:(fun single_quotes -> { options with single_quotes })
single_quotes
in
options
in
let patch = Autofix_imports.add_import ~options ~binding ~from (parse contents) in
let patched = apply_patch contents patch in
assert_equal ~ctxt ~printer:(fun x -> x) expected patched
Expand Down Expand Up @@ -455,6 +471,23 @@ let add_import_tests =
foo
|} in
assert_patch ~ctxt expected binding from contents );

( "bracket_spacing" >:: fun ctxt ->
let binding = (Export_index.Named, "foo") in
let from = "./foo" in
let contents = {|
foo
|} in
let expected = {|
import { foo } from "./foo";

foo
|} in
assert_patch ~ctxt ~bracket_spacing:true expected binding from contents;

let expected = Str.global_replace (Str.regexp_string "{ foo }") "{foo}" expected in
assert_patch ~ctxt ~bracket_spacing:false expected binding from contents );

]

let tests = "autofix_imports" >::: ["add_import" >::: add_import_tests]
7 changes: 6 additions & 1 deletion src/services/code_action/code_action_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
open Types_js_types

let layout_options options =
Js_layout_generator.{ default_opts with single_quotes = Options.format_single_quotes options }
Js_layout_generator.
{
default_opts with
bracket_spacing = Options.format_bracket_spacing options;
single_quotes = Options.format_single_quotes options;
}

let autofix_exports_code_actions
~options ~full_cx ~ast ~file_sig ~tolerable_errors ~typed_ast ~diagnostics uri loc =
Expand Down

0 comments on commit 2d7614f

Please sign in to comment.