Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @flowtyped scope resolution before actual Node resolution #7758

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/services/inference/module/module_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,33 @@ module Node = struct
])
)

let flow_typed_node_module import_str =
let scoped_module_regex = Str.regexp "^@\\([a-zA-Z0-9$_.-]+\\)/\\(.*\\)$" in
if Str.string_match scoped_module_regex import_str 0 then
let scope = Str.matched_group 1 import_str in
let name = Str.matched_group 2 import_str in
scope ^ "__" ^ name
else
import_str

let rec node_module ~options ~reader node_modules_containers file loc resolution_acc dir r =
let file_options = Options.file_options options in
lazy_seq [
lazy (
let flow_typed_r = spf
"%s%s%s"
"@flowtyped"
Filename.dir_sep
(flow_typed_node_module r)
in
if String_utils.string_starts_with r "@flowtyped" then
None
else
node_module
~options ~reader node_modules_containers
file loc resolution_acc dir flow_typed_r
);

lazy (
if SSet.mem dir node_modules_containers then
lazy_seq (Files.node_resolver_dirnames file_options |> Core_list.map ~f:(fun dirname ->
Expand Down
7 changes: 7 additions & 0 deletions tests/declaration_files_node_flowtyped/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ignore]

[include]

[libs]

[options]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Error ------------------------------------------------------------------------------------ test_prefers_flowtyped.js:5:2

Cannot cast `A.fun()` to string because number [1] is incompatible with string [2].

test_prefers_flowtyped.js:5:2
5| (A.fun(): string); // Error number ~> string
^^^^^^^

References:
node_modules/@flowtyped/foo/index.js.flow:3:32
3| declare export function fun(): number;
^^^^^^ [1]
test_prefers_flowtyped.js:5:11
5| (A.fun(): string); // Error number ~> string
^^^^^^ [2]


Error ---------------------------------------------------------------------------------------------- test_regular.js:5:2

Cannot cast `A.fun()` to number because string [1] is incompatible with number [2].

test_regular.js:5:2
5| (A.fun(): number); // Error string ~> number
^^^^^^^

References:
node_modules/baz/index.js.flow:3:32
3| declare export function fun(): string;
^^^^^^ [1]
test_regular.js:5:11
5| (A.fun(): number); // Error string ~> number
^^^^^^ [2]


Error ----------------------------------------------------------------------------------------------- test_scoped.js:5:2

Cannot cast `A.fun()` to string because number [1] is incompatible with string [2].

test_scoped.js:5:2
5| (A.fun(): string); // Error number ~> string
^^^^^^^

References:
node_modules/@flowtyped/foo__bar/index.js.flow:3:32
3| declare export function fun(): number;
^^^^^^ [1]
test_scoped.js:5:11
5| (A.fun(): string); // Error number ~> string
^^^^^^ [2]


Error -------------------------------------------------------------------------------------------- test_with_main.js:5:2

Cannot cast `A.fun()` to string because number [1] is incompatible with string [2].

test_with_main.js:5:2
5| (A.fun(): string); // Error number ~> string
^^^^^^^

References:
node_modules/@flowtyped/bar/code.js.flow:3:32
3| declare export function fun(): number;
^^^^^^ [1]
test_with_main.js:5:11
5| (A.fun(): string); // Error number ~> string
^^^^^^ [2]



Found 4 errors

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@flow

// This will require ./node_modules/@flowtyped/foo/index.js.flow
var A = require('foo');
(A.fun(): string); // Error number ~> string
5 changes: 5 additions & 0 deletions tests/declaration_files_node_flowtyped/test_regular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@flow

// This will require ./node_modules/baz/index.js.flow
var A = require('baz');
(A.fun(): number); // Error string ~> number
5 changes: 5 additions & 0 deletions tests/declaration_files_node_flowtyped/test_scoped.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@flow

// This will require ./node_modules/@flowtyped/foo__bar/index.js.flow
var A = require('@foo/bar');
(A.fun(): string); // Error number ~> string
5 changes: 5 additions & 0 deletions tests/declaration_files_node_flowtyped/test_with_main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@flow

// This will require ./node_modules/@flowtyped/bar/code.js.flow
var A = require('bar');
(A.fun(): string); // Error number ~> string