Skip to content

Commit

Permalink
[flow][get-def] Redo D67875259
Browse files Browse the repository at this point in the history
Summary:
With the guardrails enabled earlier in the stack, we can redo this without affecting find-refs.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D68175536

fbshipit-source-id: dcaffad8a787f0dc582984662b2c482b700d18db
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 15, 2025
1 parent 4d957a0 commit 0fc9c11
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
8 changes: 6 additions & 2 deletions src/services/get_def/get_def_process_location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,16 @@ class virtual ['T] searcher _cx ~is_local_use ~is_legit_require ~covers_target ~
Call.callee = (_, Identifier (_, { Identifier.name = "require"; _ }));
arguments =
( _,
{ ArgList.arguments = [Expression (source_annot, StringLiteral _)]; comments = _ }
{
ArgList.arguments =
[Expression (source_annot, (StringLiteral _ | TemplateLiteral _))];
comments = _;
}
);
_;
}
when this#is_legit_require source_annot ->
this#module_def (this#type_from_enclosing_node annot)
this#module_def (this#type_from_enclosing_node source_annot)
| _ -> super#expression (annot, expr)
else
(* it is tempting to not recurse here, but comments are not included in
Expand Down
32 changes: 15 additions & 17 deletions src/typing/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3650,21 +3650,7 @@ module Make
comments;
}
)
) ->
let (_def_loc_opt, t) =
Import_export.get_module_t
cx
(source_loc, module_name)
~perform_platform_validation:true
~import_kind_for_untyped_import_validation:(Some ImportValue)
|> Import_export.cjs_require_type
cx
(mk_reason (RModule module_name) loc)
~namespace_symbol:(mk_module_symbol ~name:module_name ~def_loc:loc)
~standard_cjs_esm_interop:false
~legacy_interop:false
in
(t, (args_loc, { ArgList.arguments = [Expression (expression cx lit_exp)]; comments }))
)
| ( None,
( args_loc,
{
Expand Down Expand Up @@ -3694,7 +3680,7 @@ module Make
}
)
) ->
let (_def_loc_opt, t) =
let (def_loc_opt, require_t) =
Import_export.get_module_t
cx
(source_loc, module_name)
Expand All @@ -3707,7 +3693,19 @@ module Make
~standard_cjs_esm_interop:false
~legacy_interop:false
in
(t, (args_loc, { ArgList.arguments = [Expression (expression cx lit_exp)]; comments }))
let lit_exp =
let ((l, t), e) = expression cx lit_exp in
let t =
TypeUtil.mod_reason_of_t
(fun _ ->
match def_loc_opt with
| Some def_loc -> mk_reason (RModule module_name) def_loc |> repos_reason loc
| None -> TypeUtil.reason_of_t require_t |> repos_reason loc)
t
in
((l, t), e)
in
(require_t, (args_loc, { ArgList.arguments = [Expression lit_exp]; comments }))
| (Some _, arguments) ->
ignore (arg_list cx arguments);
Flow.add_output
Expand Down
50 changes: 27 additions & 23 deletions tests/get_def/get_def.exp
Original file line number Diff line number Diff line change
Expand Up @@ -232,82 +232,86 @@ require.js:12:10
Flags:
[LIB] libs/lib.js:21:27,21:32

require.js:16:8
require.js:15:11
Flags:
require.js:16:13,16:31
[LIB] libs/lib.js:2:22,2:24

require.js:19:8
Flags:
require.js:19:13,19:31

require.js:18:9
require.js:21:9
Flags:
[LIB] libs/lib.js:2:22,2:24

require.js:20:9
require.js:23:9
Flags:
[LIB] libs/lib.js:2:22,2:24

require.js:22:14
require.js:25:14
Flags:
[LIB] libs/lib.js:2:22,2:24

require.js:24:9
require.js:27:9
Flags:
[LIB] libs/lib.js:3:22,3:24

require.js:26:9
require.js:29:9
Flags:
[LIB] libs/lib.js:3:22,3:24

require.js:28:14
require.js:31:14
Flags:
[LIB] libs/lib.js:3:22,3:24

require.js:30:15
require.js:33:15
Flags:
[LIB] libs/lib.js:3:28,3:30

require.js:32:20
require.js:35:20
Flags:
[LIB] libs/lib.js:3:28,3:30

require.js:34:17
require.js:37:17
Flags:
require.js:34:16,34:20
require.js:37:16,37:20

require.js:36:17
require.js:39:17
Flags:
require.js:36:16,36:22
require.js:39:16,39:22

require.js:38:16
require.js:41:16
Flags:

require.js:43:9
require.js:46:9
Flags:
[LIB] libs/lib.js:11:5,11:7

require.js:45:9
require.js:48:9
Flags:
[LIB] libs/lib.js:11:5,11:7

require.js:47:14
require.js:50:14
Flags:
[LIB] libs/lib.js:11:5,11:7

require.js:49:9
require.js:52:9
Flags:
[LIB] libs/lib.js:12:5,12:7

require.js:51:9
require.js:54:9
Flags:
[LIB] libs/lib.js:12:5,12:7

require.js:53:14
require.js:56:14
Flags:
[LIB] libs/lib.js:12:5,12:7

require.js:55:15
require.js:58:15
Flags:
[LIB] libs/lib.js:12:11,12:13

require.js:57:20
require.js:60:20
Flags:
[LIB] libs/lib.js:12:11,12:13

Expand Down
3 changes: 3 additions & 0 deletions tests/get_def/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ require("Untyped");
require("Untyped_LibDeclared");
// ^

require('test_lib');
// ^

{
const Foo = require('test_lib');
// ^
Expand Down

0 comments on commit 0fc9c11

Please sign in to comment.