Skip to content

GenType: ensure JS import works regardless moduleResolution #6541

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

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- GenType: support `@deriving(accessors)` outputs. https://github.com/rescript-lang/rescript-compiler/pull/6537
- Allow coercing ints and floats to unboxed variants that have a catch-all unboxed int or float case. https://github.com/rescript-lang/rescript-compiler/pull/6540

#### :bug: Bug Fix

- GenType: now emits full suffix on JS import path to be compatible with `.res.js`. https://github.com/rescript-lang/rescript-compiler/pull/6541

# 11.0.0-rc.8

#### :rocket: New Feature
Expand Down
6 changes: 3 additions & 3 deletions jscomp/gentype/EmitJs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
|> ModuleResolver.resolveModule ~config ~importExtension:config.suffix
~outputFileRelative ~resolver ~useBsDependencies:false
in
let fileNameBs = fileName |> ModuleName.forBsFile in
let fileNameJs = fileName |> ModuleName.forJsFile in
let envWithRequires =
fileNameBs |> requireModule ~import:false ~env ~importPath
fileNameJs |> requireModule ~import:false ~env ~importPath
in
let default = "default" in
let make = "make" in
Expand Down Expand Up @@ -358,7 +358,7 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
| _ -> emitters
in
let emitters =
(fileNameBs |> ModuleName.toString)
(fileNameJs |> ModuleName.toString)
^ "."
^ (moduleAccessPath |> Runtime.emitModuleAccessPath ~config)
|> EmitType.emitExportConst ~config ~docString ~early:false ~emitters
Expand Down
7 changes: 0 additions & 7 deletions jscomp/gentype/EmitType.ml
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,6 @@ let emitImportValueAsEarly ~emitters ~name ~nameAs importPath =

let emitRequire ~importedValueOrComponent ~early ~emitters ~(config : Config.t)
~moduleName importPath =
let importPath =
match config.moduleResolution with
| Node ->
importPath
|> ImportPath.chopExtensionSafe (* for backward compatibility *)
| _ -> importPath
in
let moduleNameString = ModuleName.toString moduleName in
let importPathString = ImportPath.emit importPath in
let output =
Expand Down
4 changes: 2 additions & 2 deletions jscomp/gentype/ModuleName.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ let sanitizeId s =
then s
else "_" ^ s

let forBsFile s = sanitizeId s ^ "BS"
let forJsFile s = sanitizeId s ^ "JS"

let forInnerModule ~fileName ~innerModuleName =
(fileName |> forBsFile) ^ "." ^ innerModuleName
(fileName |> forJsFile) ^ "." ^ innerModuleName

let fromStringUnsafe s = s
let toString s = s
Expand Down
2 changes: 1 addition & 1 deletion jscomp/gentype/ModuleName.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type t

val compare : t -> t -> int
val curry : t
val forBsFile : t -> t
val forJsFile : t -> t
val forInnerModule : fileName:t -> innerModuleName:string -> t

val fromStringUnsafe : string -> t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"ignorePatterns": ["src/**/*.bs.js"],
"ignorePatterns": ["src/**/*.res.js"],
"root": true
}
2 changes: 1 addition & 1 deletion jscomp/gentype_tests/typescript-react-example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test:
npm run lint

clean:
rm -rf node_modules lib src/*.bs.js src/*.gen.tsx
rm -rf node_modules lib src/*.res.js src/*.gen.tsx

.DEFAULT_GOAL := test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
"module": "es6",
"in-source": true
},
"suffix": ".bs.js"
"suffix": ".res.js"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable */
/* tslint:disable */

import * as CommentsBS from './Comments.bs';
import * as CommentsJS from './Comments.res.js';

export type DecideSubject_payload = {
/** A hint to use as a guide when thinking of your poem. */
Expand All @@ -23,8 +23,8 @@ export type DecideSubject_output = {
};

/** Decide on a subject matter for a poem. */
export const DecideSubject__placeholder: (run:string, times:number) => void = CommentsBS.DecideSubject._placeholder as any;
export const DecideSubject__placeholder: (run:string, times:number) => void = CommentsJS.DecideSubject._placeholder as any;

export const DecideSubject: {
/** Decide on a subject matter for a poem. */
_placeholder: (run:string, times:number) => void } = CommentsBS.DecideSubject as any;
_placeholder: (run:string, times:number) => void } = CommentsJS.DecideSubject as any;
46 changes: 23 additions & 23 deletions jscomp/gentype_tests/typescript-react-example/src/Core.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,54 @@ export const someFunWithNullUndefinedArgTypeChecked: (_1:(null | undefined | str
// Export 'someFunWithNullUndefinedArg' early to allow circular import from the '.bs.js' file.
export const someFunWithNullUndefinedArg: unknown = someFunWithNullUndefinedArgTypeChecked as (_1:(null | undefined | string), _2:number) => string as any;

const CoreBS = require('./Core.bs');
const CoreJS = require('./Core.res.js');

export type variant = "A" | { TAG: "B"; _0: string };

export type t1 = { readonly x?: string };

export type t2 = { readonly x: (undefined | string) };

export const null0: (x:(null | number)) => (null | number) = CoreBS.null0 as any;
export const null0: (x:(null | number)) => (null | number) = CoreJS.null0 as any;

export const null1: (x:(null | number)) => (null | number) = CoreBS.null1 as any;
export const null1: (x:(null | number)) => (null | number) = CoreJS.null1 as any;

export const nullable0: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable0 as any;
export const nullable0: (x:(null | undefined | number)) => (null | undefined | number) = CoreJS.nullable0 as any;

export const nullable1: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable1 as any;
export const nullable1: (x:(null | undefined | number)) => (null | undefined | number) = CoreJS.nullable1 as any;

export const undefined0: (x:(undefined | number)) => (undefined | number) = CoreBS.undefined0 as any;
export const undefined0: (x:(undefined | number)) => (undefined | number) = CoreJS.undefined0 as any;

export const undefined1: (x:(undefined | number)) => (undefined | number) = CoreBS.undefined1 as any;
export const undefined1: (x:(undefined | number)) => (undefined | number) = CoreJS.undefined1 as any;

export const dict0: (x:{[id: string]: string}) => {[id: string]: string} = CoreBS.dict0 as any;
export const dict0: (x:{[id: string]: string}) => {[id: string]: string} = CoreJS.dict0 as any;

export const dict1: (x:{[id: string]: string}) => {[id: string]: string} = CoreBS.dict1 as any;
export const dict1: (x:{[id: string]: string}) => {[id: string]: string} = CoreJS.dict1 as any;

export const promise0: (x:Promise<string>) => Promise<string> = CoreBS.promise0 as any;
export const promise0: (x:Promise<string>) => Promise<string> = CoreJS.promise0 as any;

export const promise1: (x:Promise<string>) => Promise<string> = CoreBS.promise1 as any;
export const promise1: (x:Promise<string>) => Promise<string> = CoreJS.promise1 as any;

export const date0: (x:Date) => Date = CoreBS.date0 as any;
export const date0: (x:Date) => Date = CoreJS.date0 as any;

export const date1: (x:Date) => Date = CoreBS.date1 as any;
export const date1: (x:Date) => Date = CoreJS.date1 as any;

export const bigint0: (x:BigInt) => BigInt = CoreBS.bigint0 as any;
export const bigint0: (x:BigInt) => BigInt = CoreJS.bigint0 as any;

export const bigint1: (x:BigInt) => BigInt = CoreBS.bigint1 as any;
export const bigint1: (x:BigInt) => BigInt = CoreJS.bigint1 as any;

export const regexp0: (x:RegExp) => RegExp = CoreBS.regexp0 as any;
export const regexp0: (x:RegExp) => RegExp = CoreJS.regexp0 as any;

export const regexp1: (x:RegExp) => RegExp = CoreBS.regexp1 as any;
export const regexp1: (x:RegExp) => RegExp = CoreJS.regexp1 as any;

export const map1: (x:Map<string,number>) => Map<string,number> = CoreBS.map1 as any;
export const map1: (x:Map<string,number>) => Map<string,number> = CoreJS.map1 as any;

export const weakmap1: (x:WeakMap<number[],number>) => WeakMap<number[],number> = CoreBS.weakmap1 as any;
export const weakmap1: (x:WeakMap<number[],number>) => WeakMap<number[],number> = CoreJS.weakmap1 as any;

export const set1: (x:Set<string>) => Set<string> = CoreBS.set1 as any;
export const set1: (x:Set<string>) => Set<string> = CoreJS.set1 as any;

export const weakset1: (x:WeakSet<number[]>) => WeakSet<number[]> = CoreBS.weakset1 as any;
export const weakset1: (x:WeakSet<number[]>) => WeakSet<number[]> = CoreJS.weakset1 as any;

export const option0: (x:(undefined | string)) => (undefined | string) = CoreBS.option0 as any;
export const option0: (x:(undefined | string)) => (undefined | string) = CoreJS.option0 as any;

export const option1: (x:(undefined | variant)) => (undefined | variant) = CoreBS.option1 as any;
export const option1: (x:(undefined | variant)) => (undefined | variant) = CoreJS.option1 as any;

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
Expand Up @@ -3,15 +3,15 @@
/* eslint-disable */
/* tslint:disable */

import * as DerivingsBS from './Derivings.bs';
import * as DerivingsJS from './Derivings.res.js';

export type action =
"Click"
| "Cancel"
| { TAG: "Submit"; _0: string };

export const click: action = DerivingsBS.click as any;
export const click: action = DerivingsJS.click as any;

export const submit: (_1:string) => action = DerivingsBS.submit as any;
export const submit: (_1:string) => action = DerivingsJS.submit as any;

export const cancel: action = DerivingsBS.cancel as any;
export const cancel: action = DerivingsJS.cancel as any;
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
/* eslint-disable */
/* tslint:disable */

import * as DocstringsBS from './Docstrings.bs';
import * as DocstringsJS from './Docstrings.res.js';

export type t = "A" | "B";

/** hello */
export const flat: number = DocstringsBS.flat as any;
export const flat: number = DocstringsJS.flat as any;

/** \n * Sign a message with a key.\n *\n * @param message - A message to be signed\n * @param key - The key with which to sign the message\n * @returns A signed message\n */
export const signMessage: (message:string, key:number) => string = DocstringsBS.signMessage as any;
export const signMessage: (message:string, key:number) => string = DocstringsJS.signMessage as any;

export const one: (a:number) => number = DocstringsBS.one as any;
export const one: (a:number) => number = DocstringsJS.one as any;

export const two: (a:number, b:number) => number = DocstringsBS.two as any;
export const two: (a:number, b:number) => number = DocstringsJS.two as any;

export const tree: (a:number, b:number, c:number) => number = DocstringsBS.tree as any;
export const tree: (a:number, b:number, c:number) => number = DocstringsJS.tree as any;

export const oneU: (a:number) => number = DocstringsBS.oneU as any;
export const oneU: (a:number) => number = DocstringsJS.oneU as any;

export const twoU: (a:number, b:number) => number = DocstringsBS.twoU as any;
export const twoU: (a:number, b:number) => number = DocstringsJS.twoU as any;

export const treeU: (a:number, b:number, c:number) => number = DocstringsBS.treeU as any;
export const treeU: (a:number, b:number, c:number) => number = DocstringsJS.treeU as any;

export const useParam: (param:number) => number = DocstringsBS.useParam as any;
export const useParam: (param:number) => number = DocstringsJS.useParam as any;

export const useParamU: (param:number) => number = DocstringsBS.useParamU as any;
export const useParamU: (param:number) => number = DocstringsJS.useParamU as any;

export const unnamed1: (param:number) => number = DocstringsBS.unnamed1 as any;
export const unnamed1: (param:number) => number = DocstringsJS.unnamed1 as any;

export const unnamed1U: (param:number) => number = DocstringsBS.unnamed1U as any;
export const unnamed1U: (param:number) => number = DocstringsJS.unnamed1U as any;

export const unnamed2: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2 as any;
export const unnamed2: (param_0:number, param_1:number) => number = DocstringsJS.unnamed2 as any;

export const unnamed2U: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2U as any;
export const unnamed2U: (param_0:number, param_1:number) => number = DocstringsJS.unnamed2U as any;

export const grouped: (x:number, y:number, a:number, b:number, c:number, z:number) => number = DocstringsBS.grouped as any;
export const grouped: (x:number, y:number, a:number, b:number, c:number, z:number) => number = DocstringsJS.grouped as any;

export const unitArgWithoutConversion: () => string = DocstringsBS.unitArgWithoutConversion as any;
export const unitArgWithoutConversion: () => string = DocstringsJS.unitArgWithoutConversion as any;

export const unitArgWithoutConversionU: () => string = DocstringsBS.unitArgWithoutConversionU as any;
export const unitArgWithoutConversionU: () => string = DocstringsJS.unitArgWithoutConversionU as any;

export const unitArgWithConversion: () => t = DocstringsBS.unitArgWithConversion as any;
export const unitArgWithConversion: () => t = DocstringsJS.unitArgWithConversion as any;

export const unitArgWithConversionU: () => t = DocstringsBS.unitArgWithConversionU as any;
export const unitArgWithConversionU: () => t = DocstringsJS.unitArgWithConversionU as any;
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
/* eslint-disable */
/* tslint:disable */

import * as EmitModuleIfNoConversionBS from './EmitModuleIfNoConversion.bs';
import * as EmitModuleIfNoConversionJS from './EmitModuleIfNoConversion.res.js';

export type t = "A" | { TAG: "B"; readonly name: string };

export const X_foo: (t:t) => void = EmitModuleIfNoConversionBS.X.foo as any;
export const X_foo: (t:t) => void = EmitModuleIfNoConversionJS.X.foo as any;

export const X_x: number = EmitModuleIfNoConversionBS.X.x as any;
export const X_x: number = EmitModuleIfNoConversionJS.X.x as any;

export const Y_x: string = EmitModuleIfNoConversionBS.Y.x as any;
export const Y_x: string = EmitModuleIfNoConversionJS.Y.x as any;

export const Y: { x: string } = EmitModuleIfNoConversionBS.Y as any;
export const Y: { x: string } = EmitModuleIfNoConversionJS.Y as any;

export const X: { x: number; foo: (t:t) => void } = EmitModuleIfNoConversionBS.X as any;
export const X: { x: number; foo: (t:t) => void } = EmitModuleIfNoConversionJS.X as any;
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import * as React from 'react';

import * as ExportWithRenameBS from './ExportWithRename.bs';
import * as ExportWithRenameJS from './ExportWithRename.res.js';

export type Props = { readonly s: string };

export const Renamed: React.ComponentType<{ readonly s: string }> = ExportWithRenameBS.make as any;
export const Renamed: React.ComponentType<{ readonly s: string }> = ExportWithRenameJS.make as any;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable */
/* tslint:disable */

import * as FirstClassModulesBS from './FirstClassModules.bs';
import * as FirstClassModulesJS from './FirstClassModules.res.js';

export type MT_t = number;

Expand All @@ -23,7 +23,7 @@ export type firstClassModule = {
readonly y: string
};

export const firstClassModule: firstClassModule = FirstClassModulesBS.firstClassModule as any;
export const firstClassModule: firstClassModule = FirstClassModulesJS.firstClassModule as any;

export const testConvert: (m:{
readonly x: number;
Expand All @@ -49,7 +49,7 @@ export const testConvert: (m:{
};
readonly Z: unknown;
readonly y: string
} = FirstClassModulesBS.testConvert as any;
} = FirstClassModulesJS.testConvert as any;

export const someFunctorAsFunction: (x:{
readonly x: number;
Expand All @@ -63,4 +63,4 @@ export const someFunctorAsFunction: (x:{
};
readonly Z: unknown;
readonly y: string
}) => { readonly ww: string } = FirstClassModulesBS.someFunctorAsFunction as any;
}) => { readonly ww: string } = FirstClassModulesJS.someFunctorAsFunction as any;
Loading