Skip to content

Commit

Permalink
[flow] Slight haste_namespace config tweak and add tests
Browse files Browse the repository at this point in the history
Summary:
Previously, I reversed the list here because I think the lines in flowconfig are processed in order but they are prepended to the list in reverse order. However, that's my misunderstanding and flowconfig processing will reverse the lines first. This diff removes the reverse here to be consitent with other flowconfig options.

In addition, I also added the compatibility mode that allow common code to import 1-namespace only code (choosing the first namespace).

I also added test to test the namespace resolution behavior and the precedence rule change.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D66710892

fbshipit-source-id: 728f804ddb40032cce62401e8a93a8fb35948fcc
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Dec 4, 2024
1 parent 5a53109 commit 726a642
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/common/haste_namespaces.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let mk_options =
IMap.empty
in
let namespaces_path_mapping =
Base.List.rev_map haste_namespaces_path_mapping ~f:(fun (path, ns) ->
Base.List.map haste_namespaces_path_mapping ~f:(fun (path, ns) ->
(map_path path, list_to_bitset ~haste_namespaces ns)
)
in
Expand Down Expand Up @@ -69,6 +69,20 @@ let reachable_namespace_bitsets_from_namespace_bitset ~opts ns =
None
)
in
let additional =
match additional with
| Some _ -> additional
| None ->
if IMap.exists (fun _ b -> Bitset.equal b ns) opts.overlapping_namespaces_mapping then
Base.List.find_mapi (Nel.to_list opts.namespaces) ~f:(fun i _ ->
if Bitset.mem i ns then
Some (Bitset.all_zero size |> Bitset.set i)
else
None
)
else
None
in
match additional with
| Some ns' -> [ns; ns']
| None -> [ns]
9 changes: 9 additions & 0 deletions tests/haste_namespaces/.flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[options]
all=true
module.system=haste
module.system.haste.experimental.namespaces=web
module.system.haste.experimental.namespaces=native
module.system.haste.experimental.namespace_path_mapping='<PROJECT_ROOT>/common/web' -> 'web'
module.system.haste.experimental.namespace_path_mapping='<PROJECT_ROOT>/common/' -> 'web,native'
module.system.haste.experimental.namespace_path_mapping='<PROJECT_ROOT>/web/' -> 'web'
module.system.haste.experimental.namespace_path_mapping='<PROJECT_ROOT>/native/' -> 'native'
5 changes: 5 additions & 0 deletions tests/haste_namespaces/common/Common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { native } from "NativeOnly"; // error: common code cannot import native-only code
import { web } from "WebOnly"; // ok: common code can import web-only code, because web-only code is treated as implicit common interface. TODO: common interface validation is missing for now

web as empty; // error: string ~> empty
native as empty;
1 change: 1 addition & 0 deletions tests/haste_namespaces/common/CommonLib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare export const foo: string;
Empty file.
69 changes: 69 additions & 0 deletions tests/haste_namespaces/haste_namespaces.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Error -------------------------------------------------------------------------------------------- common/Common.js:1:24

Cannot resolve module `NativeOnly`. [cannot-resolve-module]

1| import { native } from "NativeOnly"; // error: common code cannot import native-only code
^^^^^^^^^^^^


Error --------------------------------------------------------------------------------------------- common/Common.js:4:1

Cannot cast `web` to empty because string [1] is incompatible with empty [2]. [incompatible-cast]

common/Common.js:4:1
4| web as empty; // error: string ~> empty
^^^

References:
web/WebOnly.js:6:27
6| declare export const web: string;
^^^^^^ [1]
common/Common.js:4:8
4| web as empty; // error: string ~> empty
^^^^^ [2]


Error ----------------------------------------------------------------------------------------- native/NativeOnly.js:4:1

Cannot cast `foo` to empty because string [1] is incompatible with empty [2]. [incompatible-cast]

native/NativeOnly.js:4:1
4| foo as empty; // error: string ~> empty
^^^

References:
common/CommonLib.js:1:27
1| declare export const foo: string;
^^^^^^ [1]
native/NativeOnly.js:4:8
4| foo as empty; // error: string ~> empty
^^^^^ [2]


Error ----------------------------------------------------------------------------------------- native/NativeOnly.js:8:8

Cannot resolve module `WebInCommon`. [cannot-resolve-module]

8| import 'WebInCommon' // error: native code cannot import web code. This one tests the precedence rule in flowconfig
^^^^^^^^^^^^^


Error ----------------------------------------------------------------------------------------------- web/WebOnly.js:4:1

Cannot cast `foo` to empty because string [1] is incompatible with empty [2]. [incompatible-cast]

web/WebOnly.js:4:1
4| foo as empty; // error: string ~> empty
^^^

References:
common/CommonLib.js:1:27
1| declare export const foo: string;
^^^^^^ [1]
web/WebOnly.js:4:8
4| foo as empty; // error: string ~> empty
^^^^^ [2]



Found 5 errors
Empty file.
8 changes: 8 additions & 0 deletions tests/haste_namespaces/native/NativeOnly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { foo } from "CommonLib";

foo as string;
foo as empty; // error: string ~> empty

declare export const native: string;

import 'WebInCommon' // error: native code cannot import web code. This one tests the precedence rule in flowconfig
Empty file.
6 changes: 6 additions & 0 deletions tests/haste_namespaces/web/WebOnly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { foo } from "CommonLib";

foo as string;
foo as empty; // error: string ~> empty

declare export const web: string;

0 comments on commit 726a642

Please sign in to comment.