From e4b5166ead7af44f3ac16eba110d1975282cab7f Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Wed, 1 Mar 2023 09:22:26 +0100 Subject: [PATCH] Support `@gentype.import` as an alias to `@genType.import` in the compiler See https://github.com/rescript-lang/rescript-compiler/issues/6018 --- CHANGELOG.md | 1 + jscomp/ext/literals.ml | 3 ++- jscomp/frontend/ast_attributes.ml | 3 ++- jscomp/frontend/ast_external_process.ml | 2 +- .../typescript-react-example/src/ImportJsValue.bs.js | 5 +++++ .../typescript-react-example/src/ImportJsValue.gen.tsx | 8 ++++++++ .../typescript-react-example/src/ImportJsValue.res | 5 +++++ .../gentype_tests/typescript-react-example/src/MyMath.ts | 2 ++ lib/4.06.1/bsb_helper.ml | 3 ++- lib/4.06.1/rescript.ml | 3 ++- lib/4.06.1/unstable/all_ounit_tests.ml | 3 ++- lib/4.06.1/unstable/js_compiler.ml | 8 +++++--- lib/4.06.1/unstable/js_playground_compiler.ml | 8 +++++--- lib/4.06.1/whole_compiler.ml | 8 +++++--- 14 files changed, 47 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 475d309112..7db015bbc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ - Fix location issue for the treatment of `async` functions where hovering on the body with a type error would show `'a => promise<'a>` everywhere https://github.com/rescript-lang/rescript-compiler/pull/6014 - Fix formatting of `switch` expressions that contain braced `cases` inside https://github.com/rescript-lang/syntax/pull/735 - Fix formatting of props spread for multiline JSX expression https://github.com/rescript-lang/syntax/pull/736 +- Support `@gentype.import` as an alias to `@genType.import` in the compiler https://github.com/rescript-lang/rescript-compiler/pull/6021 #### :rocket: New Feature diff --git a/jscomp/ext/literals.ml b/jscomp/ext/literals.ml index 1d67343220..c0714ba40e 100644 --- a/jscomp/ext/literals.ml +++ b/jscomp/ext/literals.ml @@ -152,7 +152,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" diff --git a/jscomp/frontend/ast_attributes.ml b/jscomp/frontend/ast_attributes.ml index e64a32ae2e..47530db43e 100644 --- a/jscomp/frontend/ast_attributes.ml +++ b/jscomp/frontend/ast_attributes.ml @@ -119,7 +119,8 @@ let external_attrs = "send"; "new"; "set_index"; - Literals.gentype_import; + Literals.gentype_import1; + Literals.gentype_import2; |] let first_char_special (x : string) = diff --git a/jscomp/frontend/ast_external_process.ml b/jscomp/frontend/ast_external_process.ml index d381a8a876..7168c84395 100644 --- a/jscomp/frontend/ast_external_process.ml +++ b/jscomp/frontend/ast_external_process.ml @@ -230,7 +230,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string) Ext_list.fold_left prim_attributes ([], init_st) (fun (attrs, st) (({ txt; loc }, payload) as attr) -> - if txt = Literals.gentype_import then + if txt = Literals.gentype_import1 || txt = Literals.gentype_import2 then let bundle = "./" ^ Ext_filename.new_extension diff --git a/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.bs.js b/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.bs.js index 02086f500b..64a21c9857 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.bs.js +++ b/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.bs.js @@ -7,6 +7,10 @@ function round(prim) { return ImportJsValueGen$1.round(prim); } +function round2(prim) { + return ImportJsValueGen$1.round2(prim); +} + function area(prim) { return ImportJsValueGen$1.area(prim); } @@ -62,6 +66,7 @@ var $$default = ImportJsValueGen; export { round , + round2 , area , returnMixedArray , roundedNumber , diff --git a/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.gen.tsx b/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.gen.tsx index 205508536a..51a8cf56c7 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.gen.tsx +++ b/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.gen.tsx @@ -4,6 +4,8 @@ import {round as roundNotChecked} from './MyMath'; +import {round2 as round2NotChecked} from './MyMath'; + import {area as areaNotChecked} from './MyMath'; import {returnMixedArray as returnMixedArrayNotChecked} from './MyMath'; @@ -28,6 +30,12 @@ export const roundTypeChecked: (_1:number) => number = roundNotChecked; // Export 'round' early to allow circular import from the '.bs.js' file. export const round: unknown = roundTypeChecked as (_1:number) => number; +// In case of type error, check the type of 'round2' in 'ImportJsValue.re' and './MyMath'. +export const round2TypeChecked: (_1:number) => number = round2NotChecked; + +// Export 'round2' early to allow circular import from the '.bs.js' file. +export const round2: unknown = round2TypeChecked as (_1:number) => number; + // In case of type error, check the type of 'area' in 'ImportJsValue.re' and './MyMath'. export const areaTypeChecked: (_1:point) => number = areaNotChecked; diff --git a/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.res b/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.res index a7f6ee2f5b..91dd17680c 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.res +++ b/jscomp/gentype_tests/typescript-react-example/src/ImportJsValue.res @@ -6,6 +6,11 @@ external /* This is the module to import from. */ /* Name and type of the JS value to bind to. */ round: float => float = "round" +@gentype.import("./MyMath") +external round2: float => float = "round2" + +let _ = round2 + @genType type point = { x: int, diff --git a/jscomp/gentype_tests/typescript-react-example/src/MyMath.ts b/jscomp/gentype_tests/typescript-react-example/src/MyMath.ts index 1728f7e57e..5e4c987b26 100644 --- a/jscomp/gentype_tests/typescript-react-example/src/MyMath.ts +++ b/jscomp/gentype_tests/typescript-react-example/src/MyMath.ts @@ -2,6 +2,8 @@ export const round: (_: number) => number = Math.round; +export const round2 = round; + // tslint:disable-next-line:only-arrow-functions export const area = function(point: { x: number; y?: number }): number { return point.x * (point.y === undefined ? 1 : point.y); diff --git a/lib/4.06.1/bsb_helper.ml b/lib/4.06.1/bsb_helper.ml index dfcdf0d5b6..15a7d054d5 100644 --- a/lib/4.06.1/bsb_helper.ml +++ b/lib/4.06.1/bsb_helper.ml @@ -1340,7 +1340,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" diff --git a/lib/4.06.1/rescript.ml b/lib/4.06.1/rescript.ml index 0c8fbc206b..38e30ea68b 100644 --- a/lib/4.06.1/rescript.ml +++ b/lib/4.06.1/rescript.ml @@ -5618,7 +5618,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" diff --git a/lib/4.06.1/unstable/all_ounit_tests.ml b/lib/4.06.1/unstable/all_ounit_tests.ml index 58375b6183..9975f8632a 100644 --- a/lib/4.06.1/unstable/all_ounit_tests.ml +++ b/lib/4.06.1/unstable/all_ounit_tests.ml @@ -5833,7 +5833,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" diff --git a/lib/4.06.1/unstable/js_compiler.ml b/lib/4.06.1/unstable/js_compiler.ml index 6637e9aaa9..e6d80e7204 100644 --- a/lib/4.06.1/unstable/js_compiler.ml +++ b/lib/4.06.1/unstable/js_compiler.ml @@ -6530,7 +6530,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" @@ -266200,7 +266201,8 @@ let external_attrs = "send"; "new"; "set_index"; - Literals.gentype_import; + Literals.gentype_import1; + Literals.gentype_import2; |] let first_char_special (x : string) = @@ -270046,7 +270048,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string) Ext_list.fold_left prim_attributes ([], init_st) (fun (attrs, st) (({ txt; loc }, payload) as attr) -> - if txt = Literals.gentype_import then + if txt = Literals.gentype_import1 || txt = Literals.gentype_import2 then let bundle = "./" ^ Ext_filename.new_extension diff --git a/lib/4.06.1/unstable/js_playground_compiler.ml b/lib/4.06.1/unstable/js_playground_compiler.ml index 307098deb7..8a49318e94 100644 --- a/lib/4.06.1/unstable/js_playground_compiler.ml +++ b/lib/4.06.1/unstable/js_playground_compiler.ml @@ -6530,7 +6530,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" @@ -266200,7 +266201,8 @@ let external_attrs = "send"; "new"; "set_index"; - Literals.gentype_import; + Literals.gentype_import1; + Literals.gentype_import2; |] let first_char_special (x : string) = @@ -270046,7 +270048,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string) Ext_list.fold_left prim_attributes ([], init_st) (fun (attrs, st) (({ txt; loc }, payload) as attr) -> - if txt = Literals.gentype_import then + if txt = Literals.gentype_import1 || txt = Literals.gentype_import2 then let bundle = "./" ^ Ext_filename.new_extension diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 464a3dff11..a8ee9b105d 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -181378,7 +181378,8 @@ let node_parent = ".." let node_current = "." -let gentype_import = "genType.import" +let gentype_import1 = "genType.import" +let gentype_import2 = "gentype.import" let bsbuild_cache = ".bsbuild" @@ -276587,7 +276588,8 @@ let external_attrs = "send"; "new"; "set_index"; - Literals.gentype_import; + Literals.gentype_import1; + Literals.gentype_import2; |] let first_char_special (x : string) = @@ -280433,7 +280435,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string) Ext_list.fold_left prim_attributes ([], init_st) (fun (attrs, st) (({ txt; loc }, payload) as attr) -> - if txt = Literals.gentype_import then + if txt = Literals.gentype_import1 || txt = Literals.gentype_import2 then let bundle = "./" ^ Ext_filename.new_extension