Skip to content

Commit aa33df7

Browse files
mochjafacebook-github-bot
authored andcommitted
[lsp] consider Options.all for typeCoverage command
Summary: Enables type coverage trough LSP in non pragma workspaces with `all=true` option. Previously the files were ignored and type coverage was only working on pragma files. flow/flow-for-vscode#138 (comment) <!-- If this is a change to library defintions, please include links to relevant documentation. If this is a documentation change, please prefix the title with [DOCS]. If this is neither, ensure you opened a discussion issue and link it in the PR description. --> Pull Request resolved: #8522 Reviewed By: nmote Differential Revision: D24890501 Pulled By: mroch fbshipit-source-id: 566b79c4c7ef33a20fbb954a21a9124c0fe52c99
1 parent 27ea321 commit aa33df7

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

src/server/command_handler/commandHandler.ml

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,62 @@ let handle_persistent_document_highlight
17151715
Lwt.return ((), LspProt.LspFromServer (Some response), metadata)
17161716
| Error reason -> mk_lsp_error_response ~ret:() ~id:(Some id) ~reason metadata
17171717

1718+
(* This tries to simulate the logic from elsewhere which determines whether we would report
1719+
* errors for a given file. The criteria are
1720+
*
1721+
* 1) The file must be either implicitly included (be in the same dir structure as .flowconfig)
1722+
* or explicitly included
1723+
* 2) The file must not be ignored
1724+
* 3) The file path must be a Flow file (e.g foo.js and not foo.php or foo/)
1725+
* 4) The file must either have `// @flow` or all=true must be set in the .flowconfig or CLI
1726+
*)
1727+
let check_that_we_care_about_this_file =
1728+
let check_file_not_ignored ~file_options ~env ~file_path () =
1729+
if Files.wanted ~options:file_options env.ServerEnv.libs file_path then
1730+
Ok ()
1731+
else
1732+
Error "File is ignored"
1733+
in
1734+
let check_file_included ~options ~file_options ~file_path () =
1735+
let file_is_implicitly_included =
1736+
let root_str = spf "%s%s" (Path.to_string (Options.root options)) Filename.dir_sep in
1737+
String_utils.string_starts_with file_path root_str
1738+
in
1739+
if file_is_implicitly_included then
1740+
Ok ()
1741+
else if Files.is_included file_options file_path then
1742+
Ok ()
1743+
else
1744+
Error "File is not implicitly or explicitly included"
1745+
in
1746+
let check_is_flow_file ~file_options ~file_path () =
1747+
if Files.is_flow_file ~options:file_options file_path then
1748+
Ok ()
1749+
else
1750+
Error "File is not a Flow file"
1751+
in
1752+
let check_flow_pragma ~options ~content ~file_path () =
1753+
if Options.all options then
1754+
Ok ()
1755+
else
1756+
let (_, docblock) =
1757+
Parsing_service_js.(
1758+
parse_docblock docblock_max_tokens (File_key.SourceFile file_path) content)
1759+
in
1760+
if Docblock.is_flow docblock then
1761+
Ok ()
1762+
else
1763+
Error "File is missing @flow pragma and `all` is not set to `true`"
1764+
in
1765+
fun ~options ~env ~file_path ~content ->
1766+
let file_path = Files.imaginary_realpath file_path in
1767+
let file_options = Options.file_options options in
1768+
Ok ()
1769+
>>= check_file_not_ignored ~file_options ~env ~file_path
1770+
>>= check_file_included ~options ~file_options ~file_path
1771+
>>= check_is_flow_file ~file_options ~file_path
1772+
>>= check_flow_pragma ~options ~content ~file_path
1773+
17181774
let handle_persistent_coverage ~options ~id ~params ~file ~metadata ~client ~profiling ~env =
17191775
let textDocument = params.TypeCoverage.textDocument in
17201776
let file =
@@ -1726,20 +1782,21 @@ let handle_persistent_coverage ~options ~id ~params ~file ~metadata ~client ~pro
17261782
Flow_lsp_conversions.lsp_DocumentIdentifier_to_flow textDocument ~client
17271783
in
17281784
(* if it isn't a flow file (i.e. lacks a @flow directive) then we won't do anything *)
1729-
let fkey = File_key.SourceFile (File_input.filename_of_file_input file) in
17301785
let content = File_input.content_of_file_input file in
1786+
let file_path = File_input.filename_of_file_input file in
17311787
let is_flow =
17321788
match content with
17331789
| Ok content ->
1734-
let (_, docblock) = Parsing_service_js.(parse_docblock docblock_max_tokens fkey content) in
1735-
Docblock.is_flow docblock
1790+
(match check_that_we_care_about_this_file ~options ~env ~file_path ~content with
1791+
| Ok () -> true
1792+
| Error _ -> false)
17361793
| Error _ -> false
17371794
in
17381795
let%lwt result =
17391796
if is_flow then
1740-
let force = false in
1741-
let type_contents_cache = Some (Persistent_connection.type_contents_cache client) in
17421797
(* 'true' makes it report "unknown" for all exprs in non-flow files *)
1798+
let force = Options.all options in
1799+
let type_contents_cache = Some (Persistent_connection.type_contents_cache client) in
17431800
coverage ~options ~env ~profiling ~type_contents_cache ~force ~trust:false file
17441801
else
17451802
Lwt.return (Ok [])
@@ -1955,62 +2012,6 @@ let handle_persistent_unsupported ?id ~unhandled ~metadata ~client:_ ~profiling:
19552012
let reason = Printf.sprintf "not implemented: %s" (Lsp_fmt.message_name_to_string unhandled) in
19562013
mk_lsp_error_response ~ret:() ~id ~reason metadata
19572014

1958-
(* This tries to simulate the logic from elsewhere which determines whether we would report
1959-
* errors for a given file. The criteria are
1960-
*
1961-
* 1) The file must be either implicitly included (be in the same dir structure as .flowconfig)
1962-
* or explicitly included
1963-
* 2) The file must not be ignored
1964-
* 3) The file path must be a Flow file (e.g foo.js and not foo.php or foo/)
1965-
* 4) The file must either have `// @flow` or all=true must be set in the .flowconfig or CLI
1966-
*)
1967-
let check_that_we_care_about_this_file =
1968-
let check_file_not_ignored ~file_options ~env ~file_path () =
1969-
if Files.wanted ~options:file_options env.ServerEnv.libs file_path then
1970-
Ok ()
1971-
else
1972-
Error "File is ignored"
1973-
in
1974-
let check_file_included ~options ~file_options ~file_path () =
1975-
let file_is_implicitly_included =
1976-
let root_str = spf "%s%s" (Path.to_string (Options.root options)) Filename.dir_sep in
1977-
String_utils.string_starts_with file_path root_str
1978-
in
1979-
if file_is_implicitly_included then
1980-
Ok ()
1981-
else if Files.is_included file_options file_path then
1982-
Ok ()
1983-
else
1984-
Error "File is not implicitly or explicitly included"
1985-
in
1986-
let check_is_flow_file ~file_options ~file_path () =
1987-
if Files.is_flow_file ~options:file_options file_path then
1988-
Ok ()
1989-
else
1990-
Error "File is not a Flow file"
1991-
in
1992-
let check_flow_pragma ~options ~content ~file_path () =
1993-
if Options.all options then
1994-
Ok ()
1995-
else
1996-
let (_, docblock) =
1997-
Parsing_service_js.(
1998-
parse_docblock docblock_max_tokens (File_key.SourceFile file_path) content)
1999-
in
2000-
if Docblock.is_flow docblock then
2001-
Ok ()
2002-
else
2003-
Error "File is missing @flow pragma and `all` is not set to `true`"
2004-
in
2005-
fun ~options ~env ~file_path ~content ->
2006-
let file_path = Files.imaginary_realpath file_path in
2007-
let file_options = Options.file_options options in
2008-
Ok ()
2009-
>>= check_file_not_ignored ~file_options ~env ~file_path
2010-
>>= check_file_included ~options ~file_options ~file_path
2011-
>>= check_is_flow_file ~file_options ~file_path
2012-
>>= check_flow_pragma ~options ~content ~file_path
2013-
20142015
(* What should we do if we get multiple requests for the same URI? Each request wants the most
20152016
* up-to-date live errors, so if we have 10 pending requests then we would want to send the same
20162017
* response to each. And we could do that, but it might have some weird side effects:

0 commit comments

Comments
 (0)