Skip to content

Commit

Permalink
[flow] Add support for backup flowconfig specification in flow ls jso…
Browse files Browse the repository at this point in the history
…n output

Summary:
Adds a "backup" field to the explain object when a backup is provided for an explicitly ignored file.

Changelog: [internal]

Reviewed By: SamChou19815

Differential Revision: D69864802

fbshipit-source-id: 45f5b1d2ec2ba1c76f2333b23136b9a8fe170ce9
  • Loading branch information
jbrown215 authored and facebook-github-bot committed Feb 21, 2025
1 parent 8732aa9 commit e0b550f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/codemods/utils/codemod_runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let get_target_filename_set ~options ~libs ~all filename_set =
(fun f ->
let s = File_key.to_string f in
Files.is_valid_path ~options s
&& (all || not (Files.is_ignored options s))
&& (all || not (fst (Files.is_ignored options s)))
&& not (SSet.mem s libs))
filename_set

Expand Down
16 changes: 13 additions & 3 deletions src/commands/lsCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ let explain ~flowconfig_name ~root ~options ~libs raw_file =
let file = raw_file |> File_path.make |> File_path.to_string in
let root_str = File_path.to_string root in
let result =
let (is_ignored, backup) = Files.is_ignored options file in
if SSet.mem file libs then
(* This is a lib file *)
let flowtyped_path = Files.get_flowtyped_path root in
Expand All @@ -88,8 +89,8 @@ let explain ~flowconfig_name ~root ~options ~libs raw_file =
ExplicitLib
else if Server_files_js.config_file flowconfig_name root = file then
ConfigFile
else if Files.is_ignored options file then
ExplicitlyIgnored None
else if is_ignored then
ExplicitlyIgnored backup
else if String.starts_with ~prefix:root_str file then
ImplicitlyIncluded
else if Files.is_included options file then
Expand All @@ -104,7 +105,16 @@ let json_of_files_with_explanations files =
let properties =
Base.List.map
~f:(fun (file, res) ->
(file, JSON_Object [("explanation", JSON_String (string_of_file_result res))]))
( file,
match res with
| ExplicitlyIgnored (Some backup) ->
JSON_Object
[
("explanation", JSON_String (string_of_file_result res));
("backup", JSON_String backup);
]
| _ -> JSON_Object [("explanation", JSON_String (string_of_file_result res))]
))
files
in
JSON_Object properties
Expand Down
29 changes: 22 additions & 7 deletions src/common/files.ml
Original file line number Diff line number Diff line change
Expand Up @@ -593,22 +593,37 @@ let ordered_and_unordered_lib_paths (options : options) =
in
(libs, SSet.of_list libs)

let is_matching_path path pattern rx current =
if String.starts_with ~prefix:"!" pattern then
current && not (Str.string_match rx path 0)
else
current || Str.string_match rx path 0

let is_matching path pattern_list =
List.fold_left
(fun current (pattern, rx) ->
if String.starts_with ~prefix:"!" pattern then
current && not (Str.string_match rx path 0)
else
current || Str.string_match rx path 0)
(fun current (pattern, rx) -> is_matching_path path pattern rx current)
false
pattern_list

let is_matching_ignore path pattern_list =
List.fold_left
(fun (matched_already, current_backup) ((pattern, backup), rx) ->
let matches = is_matching_path path pattern rx matched_already in
( matches,
if matches && current_backup = None then
backup
else
current_backup
))
(false, None)
pattern_list

(* true if a file path matches an [ignore] entry in config *)
let is_ignored (options : options) 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
is_matching path (options.ignores |> List.map (fun ((x, _y), z) -> (x, z)))
is_matching_ignore path options.ignores

(* true if a file path matches an [untyped] entry in config *)
let is_untyped (options : options) path =
Expand All @@ -628,7 +643,7 @@ let is_declaration (options : options) path =
let is_included options f = Path_matcher.matches options.includes f

let wanted ~options ~include_libdef lib_fileset =
let is_ignored_ = is_ignored options in
let is_ignored_ path = fst (is_ignored options path) in
if include_libdef then
fun path ->
(not (is_ignored_ path)) || SSet.mem path lib_fileset
Expand Down
6 changes: 4 additions & 2 deletions src/common/files.mli
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ val is_json_file : string -> bool

val is_flow_file : options:options -> string -> bool

(* true if a file path matches an [ignore] entry in config *)
val is_ignored : options -> string -> bool
(* true if a file path matches an [ignore] entry in config, also returns the backup flowconfig if
* one is specified
*)
val is_ignored : options -> string -> bool * string option

(* true if a file path matches an [untyped] entry in config *)
val is_untyped : options -> string -> bool
Expand Down
1 change: 1 addition & 0 deletions tests/ls/files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ src/explicitly_ignored.js
other/implicitly_ignored.js
src/explicit_lib.js
src/flow-typed/implicit_lib.js
src/explicitly_ignored_with_backup.js
10 changes: 8 additions & 2 deletions tests/ls/ls.exp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ implicitly_included.js
.flowconfig
explicit_lib.js
explicitly_ignored.js
explicitly_ignored_with_backup.js
flow-typed/implicit_lib.js
implicitly_included.js

Expand All @@ -50,6 +51,7 @@ Implicit/Explicit Included/Ignored/Lib should be correct
================================================================================
ExplicitLib explicit_lib.js
ExplicitlyIgnored explicitly_ignored.js
ExplicitlyIgnored explicitly_ignored_with_backup.js
ExplicitlyIncluded ../other/explicitly_included.js
ImplicitLib flow-typed/implicit_lib.js
ImplicitlyIgnored ../other/implicitly_ignored.js
Expand All @@ -58,12 +60,12 @@ ImplicitlyIncluded implicitly_included.js
================================================================================
JSON output without --explain should be an array
================================================================================
["../other/explicitly_included.js","../other/implicitly_ignored.js",".flowconfig","explicit_lib.js","explicitly_ignored.js","flow-typed/implicit_lib.js","implicitly_included.js"]
["../other/explicitly_included.js","../other/implicitly_ignored.js",".flowconfig","explicit_lib.js","explicitly_ignored.js","explicitly_ignored_with_backup.js","flow-typed/implicit_lib.js","implicitly_included.js"]

================================================================================
JSON output with --explain should be JSON object
================================================================================
{"../other/explicitly_included.js":{"explanation":"ExplicitlyIncluded"},"../other/implicitly_ignored.js":{"explanation":"ImplicitlyIgnored"},".flowconfig":{"explanation":"ConfigFile"},"explicit_lib.js":{"explanation":"ExplicitLib"},"explicitly_ignored.js":{"explanation":"ExplicitlyIgnored"},"flow-typed/implicit_lib.js":{"explanation":"ImplicitLib"},"implicitly_included.js":{"explanation":"ImplicitlyIncluded"}}
{"../other/explicitly_included.js":{"explanation":"ExplicitlyIncluded"},"../other/implicitly_ignored.js":{"explanation":"ImplicitlyIgnored"},".flowconfig":{"explanation":"ConfigFile"},"explicit_lib.js":{"explanation":"ExplicitLib"},"explicitly_ignored.js":{"explanation":"ExplicitlyIgnored"},"explicitly_ignored_with_backup.js":{"explanation":"ExplicitlyIgnored","backup":"other/.flowconfig"},"flow-typed/implicit_lib.js":{"explanation":"ImplicitLib"},"implicitly_included.js":{"explanation":"ImplicitlyIncluded"}}

================================================================================
Listing files over stdin
Expand All @@ -73,6 +75,7 @@ Listing files over stdin
.flowconfig
explicit_lib.js
explicitly_ignored.js
explicitly_ignored_with_backup.js
flow-typed/implicit_lib.js
implicitly_included.js

Expand All @@ -82,6 +85,7 @@ implicitly_included.js
.flowconfig
explicit_lib.js
explicitly_ignored.js
explicitly_ignored_with_backup.js
flow-typed/implicit_lib.js
implicitly_included.js

Expand All @@ -93,6 +97,7 @@ Input file on disk
.flowconfig
explicit_lib.js
explicitly_ignored.js
explicitly_ignored_with_backup.js
flow-typed/implicit_lib.js
implicitly_included.js

Expand All @@ -102,6 +107,7 @@ implicitly_included.js
.flowconfig
explicit_lib.js
explicitly_ignored.js
explicitly_ignored_with_backup.js
flow-typed/implicit_lib.js
implicitly_included.js

Expand Down
1 change: 1 addition & 0 deletions tests/ls/src/.flowconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[ignore]
'<PROJECT_ROOT>/explicitly_ignored_with_backup.js' -> 'other/.flowconfig'
<PROJECT_ROOT>/explicitly_ignored.js

[include]
Expand Down
Empty file.

0 comments on commit e0b550f

Please sign in to comment.