Skip to content
Closed
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
23 changes: 19 additions & 4 deletions src/commands/commandUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ let assert_version flowconfig =
type flowconfig_params = {
ignores: string list;
untyped: string list;
declarations: string list;
includes: string list;
libs: string list;
(* We store raw_lint_severities as a string list instead of as a LintSettings.t so we
Expand All @@ -394,13 +395,14 @@ let list_of_string_arg = function
| None -> []
| Some arg_str -> Str.split (Str.regexp ",") arg_str

let collect_flowconfig_flags main ignores_str untyped_str includes_str lib_str lints_str =
let collect_flowconfig_flags main ignores_str untyped_str declarations_str includes_str lib_str lints_str =
let ignores = list_of_string_arg ignores_str in
let untyped = list_of_string_arg untyped_str in
let declarations = list_of_string_arg declarations_str in
let includes = list_of_string_arg includes_str in
let libs = list_of_string_arg lib_str in
let raw_lint_severities = list_of_string_arg lints_str in
main { ignores; includes; libs; raw_lint_severities; untyped; }
main { ignores; includes; libs; raw_lint_severities; untyped; declarations; }

let file_options =
let default_lib_dir ~no_flowlib tmp_dir =
Expand Down Expand Up @@ -466,7 +468,7 @@ let file_options =
| [] -> config_libs
| _ -> config_libs @ (List.map (Files.make_path_absolute root) extras)
in
fun ~root ~no_flowlib ~temp_dir ~includes ~ignores ~libs ~untyped flowconfig ->
fun ~root ~no_flowlib ~temp_dir ~includes ~ignores ~libs ~untyped ~declarations flowconfig ->
let default_lib_dir =
let no_flowlib = no_flowlib || FlowConfig.no_flowlib flowconfig in
Some (default_lib_dir ~no_flowlib temp_dir)
Expand All @@ -479,6 +481,10 @@ let file_options =
root
(FlowConfig.untyped flowconfig)
untyped in
let declarations = ignores_of_arg
root
(FlowConfig.declarations flowconfig)
declarations in
let lib_paths = lib_paths ~root flowconfig libs in
let includes =
includes
Expand All @@ -488,6 +494,7 @@ let file_options =
default_lib_dir;
ignores;
untyped;
declarations;
includes;
lib_paths;
module_file_exts = FlowConfig.module_file_exts flowconfig;
Expand All @@ -507,6 +514,12 @@ let untyped_flag prev = CommandSpec.ArgSpec.(
~doc:"Specify one or more patterns, comma separated, for files to treat as untyped"
)

let declaration_flag prev = CommandSpec.ArgSpec.(
prev
|> flag "--declaration" (optional string)
~doc:"Specify one or more patterns, comma separated, for files to treat as declarations"
)

let include_flag prev = CommandSpec.ArgSpec.(
prev
|> flag "--include" (optional string)
Expand Down Expand Up @@ -536,6 +549,7 @@ let flowconfig_flags prev = CommandSpec.ArgSpec.(
|> collect collect_flowconfig_flags
|> ignore_flag
|> untyped_flag
|> declaration_flag
|> include_flag
|> lib_flag
|> lints_flag
Expand Down Expand Up @@ -782,8 +796,9 @@ let make_options ~flowconfig ~lazy_mode ~root (options_flags: Options_flags.t) =
libs;
raw_lint_severities=_;
untyped;
declarations;
} = options_flags.flowconfig_flags in
file_options ~root ~no_flowlib ~temp_dir ~includes ~ignores ~libs ~untyped flowconfig
file_options ~root ~no_flowlib ~temp_dir ~includes ~ignores ~libs ~untyped ~declarations flowconfig
in
let lint_severities = parse_lints_flag
(FlowConfig.lint_severities flowconfig) options_flags.flowconfig_flags.raw_lint_severities
Expand Down
18 changes: 17 additions & 1 deletion src/commands/config/flowConfig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ type config = {
ignores: string list;
(* files that should be treated as untyped *)
untyped: string list;
(* files that should be treated as declarations *)
declarations: string list;
(* non-root include paths *)
includes: string list;
(* library paths. no wildcards *)
Expand All @@ -339,6 +341,9 @@ end = struct
let untyped o untyped =
List.iter (fun ex -> (fprintf o "%s\n" ex)) untyped

let declarations o declarations =
List.iter (fun ex -> (fprintf o "%s\n" ex)) declarations

let includes o includes =
List.iter (fun inc -> (fprintf o "%s\n" inc)) includes

Expand Down Expand Up @@ -400,6 +405,7 @@ end = struct
ignores o config.ignores;
fprintf o "\n";
section_if_nonempty o "untyped" untyped config.untyped;
section_if_nonempty o "declarations" declarations config.declarations;
section_header o "include";
includes o config.includes;
fprintf o "\n";
Expand All @@ -419,6 +425,7 @@ end
let empty_config = {
ignores = [];
untyped = [];
declarations = [];
includes = [];
libs = [];
lint_severities = LintSettings.empty_severities;
Expand Down Expand Up @@ -472,6 +479,10 @@ let parse_untyped config lines =
let untyped = trim_lines lines in
{ config with untyped; }

let parse_declarations config lines =
let declarations = trim_lines lines in
{ config with declarations; }

let parse_options config lines =
let open Opts in
let (>>=) = Core_result.(>>=) in
Expand Down Expand Up @@ -948,6 +959,7 @@ let parse_section config ((section_ln, section), lines) =
| "ignore", _ -> parse_ignores config lines
| "libs", _ -> parse_libs config lines
| "lints", _ -> parse_lints config lines
| "declarations", _ -> parse_declarations config lines
| "strict", _ -> parse_strict config lines
| "options", _ -> parse_options config lines
| "untyped", _ -> parse_untyped config lines
Expand Down Expand Up @@ -986,15 +998,17 @@ let read filename =
} in
parse config lines

let init ~ignores ~untyped ~includes ~libs ~options ~lints =
let init ~ignores ~untyped ~declarations ~includes ~libs ~options ~lints =
let ignores_lines = List.map (fun s -> (1, s)) ignores in
let untyped_lines = List.map (fun s -> (1, s)) untyped in
let declarations_lines = List.map (fun s -> (1, s)) declarations in
let includes_lines = List.map (fun s -> (1, s)) includes in
let options_lines = List.map (fun s -> (1, s)) options in
let lib_lines = List.map (fun s -> (1, s)) libs in
let lint_lines = List.map (fun s -> (1, s)) lints in
let config = parse_ignores empty_config ignores_lines in
let config = parse_untyped config untyped_lines in
let config = parse_declarations config declarations_lines in
let config = parse_includes config includes_lines in
let config = parse_options config options_lines in
let config = parse_libs config lib_lines in
Expand Down Expand Up @@ -1025,6 +1039,8 @@ let restore (filename, config) = cache := Some (filename, config)
let ignores config = config.ignores
(* files that should be treated as untyped *)
let untyped config = config.untyped
(* files that should be treated as declarations *)
let declarations config = config.declarations
(* non-root include paths *)
let includes config = config.includes
(* library paths. no wildcards *)
Expand Down
3 changes: 3 additions & 0 deletions src/commands/config/flowConfig.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ val empty_config: config
val init:
ignores: string list ->
untyped: string list ->
declarations: string list ->
includes: string list ->
libs: string list ->
options: string list ->
Expand All @@ -28,6 +29,8 @@ val restore: string * config -> unit
val ignores: config -> string list
(* files that should be treated as untyped *)
val untyped: config -> string list
(* files that should be treated as declarations *)
val declarations: config -> string list
(* non-root include paths *)
val includes: config -> string list
(* library paths. no wildcards *)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/genFlowFilesCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ let spec = {
|> ignore_flag
|> include_flag
|> untyped_flag
|> declaration_flag
|> from_flag
|> anon "src" (required string)
|> flag "--out-dir" string
Expand Down Expand Up @@ -85,7 +86,7 @@ let write_file strip_root root content perm src_file_path dest_file_path =
Unix.close fd

let main option_values root error_flags strip_root ignore_flag
include_flag untyped_flag from src out_dir () = (
include_flag untyped_flag declaration_flag from src out_dir () = (
FlowEventLogger.set_from from;
let src = expand_path src in
let root = guess_root (
Expand All @@ -111,7 +112,7 @@ let main option_values root error_flags strip_root ignore_flag
FlowExitStatus.exit ~msg FlowExitStatus.Commandline_usage_error
);

let options = LsCommand.make_options ~root ~ignore_flag ~include_flag ~untyped_flag in
let options = LsCommand.make_options ~root ~ignore_flag ~include_flag ~untyped_flag ~declaration_flag in
let _, libs = Files.init options in
let next_files =
LsCommand.get_ls_files ~root ~all:false ~options ~libs ~imaginary:false (Some src)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/initCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ let main from flowconfig_flags options root () =
in
let ignores = flowconfig_flags.CommandUtils.ignores in
let untyped = flowconfig_flags.CommandUtils.untyped in
let declarations = flowconfig_flags.CommandUtils.declarations in
let includes = flowconfig_flags.CommandUtils.includes in
let libs = flowconfig_flags.CommandUtils.libs in
let lints = flowconfig_flags.CommandUtils.raw_lint_severities in
Expand All @@ -61,7 +62,7 @@ let main from flowconfig_flags options root () =
FlowExitStatus.(exit ~msg Invalid_flowconfig)
end;

let config = FlowConfig.init ~ignores ~untyped ~includes ~libs ~options ~lints in
let config = FlowConfig.init ~ignores ~untyped ~declarations ~includes ~libs ~options ~lints in

let out = Sys_utils.open_out_no_fail file in
FlowConfig.write config out;
Expand Down
10 changes: 6 additions & 4 deletions src/commands/lsCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let spec = {
|> ignore_flag
|> include_flag
|> untyped_flag
|> declaration_flag
|> root_flag
|> json_flags
|> from_flag
Expand Down Expand Up @@ -106,15 +107,16 @@ let rec iter_get_next ~f get_next =
List.iter f result;
iter_get_next ~f get_next

let make_options ~root ~ignore_flag ~include_flag ~untyped_flag =
let make_options ~root ~ignore_flag ~include_flag ~untyped_flag ~declaration_flag =
let flowconfig = FlowConfig.get (Server_files_js.config_file root) in
let temp_dir = FlowConfig.temp_dir flowconfig in
let includes = CommandUtils.list_of_string_arg include_flag in
let ignores = CommandUtils.list_of_string_arg ignore_flag in
let untyped = CommandUtils.list_of_string_arg untyped_flag in
let declarations = CommandUtils.list_of_string_arg declaration_flag in
let libs = [] in
CommandUtils.file_options flowconfig
~root ~no_flowlib:true ~temp_dir ~ignores ~includes ~libs ~untyped
~root ~no_flowlib:true ~temp_dir ~ignores ~includes ~libs ~untyped ~declarations

(* The problem with Files.wanted is that it says yes to everything except ignored files and libs.
* So implicitly ignored files (like files in another directory) pass the Files.wanted check *)
Expand Down Expand Up @@ -174,7 +176,7 @@ let get_next_append_const get_next const =
ret

let main
strip_root ignore_flag include_flag untyped_flag root_flag json pretty from all imaginary reason
strip_root ignore_flag include_flag untyped_flag declaration_flag root_flag json pretty from all imaginary reason
input_file root_or_files () =

let files_or_dirs = get_filenames_from_input ~allow_imaginary:true input_file root_or_files in
Expand All @@ -194,7 +196,7 @@ let main
| _ -> None)
) in

let options = make_options ~root ~ignore_flag ~include_flag ~untyped_flag in
let options = make_options ~root ~ignore_flag ~include_flag ~untyped_flag ~declaration_flag in
(* Turn on --no-flowlib by default, so that flow ls never reports flowlib files *)
let options = { options with Files.default_lib_dir = None; } in
let _, libs = Files.init options in
Expand Down
11 changes: 11 additions & 0 deletions src/common/files.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type options = {
default_lib_dir: Path.t option;
ignores: (string * Str.regexp) list;
untyped: (string * Str.regexp) list;
declarations: (string * Str.regexp) list;
includes: Path_matcher.t;
lib_paths: Path.t list;
module_file_exts: SSet.t;
Expand All @@ -21,6 +22,7 @@ type options = {
let default_lib_dir options = options.default_lib_dir
let ignores options = options.ignores
let untyped options = options.untyped
let declarations options = options.declarations
let includes options = options.includes
let lib_paths options = options.lib_paths
let module_file_exts options = options.module_file_exts
Expand Down Expand Up @@ -300,6 +302,15 @@ let is_untyped (options: options) =
let path = Sys_utils.normalize_filename_dir_sep path in
List.exists (fun rx -> Str.string_match rx path 0) list

(* true if a file path matches a [declarations] entry in config *)
let is_declaration (options: options) =
let list = List.map snd options.declarations in
fun path ->
(* On Windows, the path may use \ instead of /, but let's standardize the
* ignore regex to use / *)
let path = Sys_utils.normalize_filename_dir_sep path in
List.exists (fun rx -> Str.string_match rx path 0) list

(* true if a file path matches an [include] path in config *)
let is_included options f =
Path_matcher.matches options.includes f
Expand Down
4 changes: 4 additions & 0 deletions src/common/files.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type options = {
default_lib_dir: Path.t option;
ignores: (string * Str.regexp) list;
untyped: (string * Str.regexp) list;
declarations: (string * Str.regexp) list;
includes: Path_matcher.t;
lib_paths: Path.t list;
module_file_exts: SSet.t;
Expand All @@ -21,6 +22,7 @@ type options = {
val default_lib_dir: options -> Path.t option
val ignores: options -> (string * Str.regexp) list
val untyped: options -> (string * Str.regexp) list
val declarations: options -> (string * Str.regexp) list
val includes: options -> Path_matcher.t
val lib_paths: options -> Path.t list
val module_file_exts: options -> SSet.t
Expand All @@ -42,6 +44,8 @@ val is_flow_file: options: options -> string -> bool
val is_ignored: options -> string -> bool
(* true if a file path matches an [untyped] entry in config *)
val is_untyped: options -> string -> bool
(* true if a file path matches a [declarations] entry in config *)
val is_declaration: options -> string -> bool
(* true if a file path matches an [include] path in config *)
val is_included: options -> string -> bool

Expand Down
4 changes: 2 additions & 2 deletions src/flow_dot_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let load_lib_files ~master_cx ~metadata files
let cx = Context.make sig_cx metadata lib_file Files.lib_module_ref in
Flow_js.mk_builtins cx;
let syms = Type_inference_js.infer_lib_file cx ast
~exclude_syms ~file_sig ~lint_severities:LintSettings.empty_severities
~exclude_syms ~file_sig ~lint_severities:LintSettings.empty_severities ~file_options:None
in

Context.merge_into (Context.sig_cx master_cx) sig_cx;
Expand Down Expand Up @@ -204,7 +204,7 @@ let infer_and_merge ~root filename ast file_sig =
let strict_mode = StrictModeSettings.empty in
let file_sigs = Utils_js.FilenameMap.singleton filename file_sig in
let cx, _other_cxs = Merge_js.merge_component_strict
~metadata ~lint_severities ~strict_mode ~file_sigs
~metadata ~lint_severities ~file_options:None ~strict_mode ~file_sigs
~get_ast_unsafe:(fun _ -> ast)
~get_docblock_unsafe:(fun _ -> stub_docblock)
(Nel.one filename) reqs [] (Context.sig_cx master_cx)
Expand Down
3 changes: 2 additions & 1 deletion src/services/inference/init_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ let load_lib_files ~master_cx ~options files =

let lib_file = File_key.LibFile file in
let lint_severities = options.Options.opt_lint_severities in
let file_options = Options.file_options options in
let%lwt result = parse_lib_file options file in
Lwt.return (match result with
| Parsing.Parse_ok (ast, file_sig) ->
Expand All @@ -89,7 +90,7 @@ let load_lib_files ~master_cx ~options files =
Flow.mk_builtins cx;

let syms = Infer.infer_lib_file cx ast
~exclude_syms ~lint_severities ~file_sig
~exclude_syms ~lint_severities ~file_options:(Some file_options) ~file_sig
in

Context.merge_into (Context.sig_cx master_cx) sig_cx;
Expand Down
6 changes: 4 additions & 2 deletions src/services/inference/merge_service.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ let merge_strict_context ~options component =

let metadata = Context.metadata_of_options options in
let lint_severities = Options.lint_severities options in
let file_options = Some (Options.file_options options) in
let strict_mode = Options.strict_mode options in
let cx, other_cxs = Merge_js.merge_component_strict
~metadata ~lint_severities ~strict_mode ~file_sigs
~metadata ~lint_severities ~file_options ~strict_mode ~file_sigs
~get_ast_unsafe:Parsing_service_js.get_ast_unsafe
~get_docblock_unsafe:Parsing_service_js.get_docblock_unsafe
~do_gc:(Options.is_debug_mode options)
Expand Down Expand Up @@ -140,9 +141,10 @@ let merge_contents_context options file ast info file_sig ~ensure_checked_depend

let metadata = Context.metadata_of_options options in
let lint_severities = Options.lint_severities options in
let file_options = Some (Options.file_options options) in
let strict_mode = Options.strict_mode options in
let cx, _ = Merge_js.merge_component_strict
~metadata ~lint_severities ~strict_mode ~file_sigs
~metadata ~lint_severities ~file_options ~strict_mode ~file_sigs
~get_ast_unsafe:(fun _ -> ast)
~get_docblock_unsafe:(fun _ -> info)
component file_reqs dep_cxs master_cx
Expand Down
Loading