Skip to content

Commit 9596f55

Browse files
committed
Gentype: make output DCE-friendly
1 parent 63ef9e5 commit 9596f55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+364
-420
lines changed

jscomp/gentype/EmitJs.ml

+5-7
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
206206
in
207207
let valueNameTypeChecked = valueName ^ "TypeChecked" in
208208
let emitters =
209-
(importedAsName ^ restOfPath) ^ ";"
209+
importedAsName ^ restOfPath
210210
|> EmitType.emitExportConst ~config
211211
~comment:
212212
("In case of type error, check the type of '" ^ valueName
@@ -224,9 +224,8 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
224224
| false -> valueName
225225
in
226226
let emitters =
227-
(valueNameTypeChecked
228-
|> EmitType.emitTypeCast ~config ~type_ ~typeNameIsInterface)
229-
^ ";"
227+
valueNameTypeChecked
228+
|> EmitType.emitTypeCast ~config ~type_ ~typeNameIsInterface
230229
|> EmitType.emitExportConst
231230
~comment:
232231
("Export '" ^ valueNameNotDefault
@@ -359,10 +358,9 @@ let emitCodeItem ~config ~emitters ~moduleItemsEmitter ~env ~fileName
359358
| _ -> emitters
360359
in
361360
let emitters =
362-
((fileNameBs |> ModuleName.toString)
361+
(fileNameBs |> ModuleName.toString)
363362
^ "."
364-
^ (moduleAccessPath |> Runtime.emitModuleAccessPath ~config))
365-
^ ";"
363+
^ (moduleAccessPath |> Runtime.emitModuleAccessPath ~config)
366364
|> EmitType.emitExportConst ~config ~docString ~early:false ~emitters
367365
~name ~type_ ~typeNameIsInterface
368366
in

jscomp/gentype/EmitType.ml

+17-27
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,15 @@ and renderFunType ~config ~indent ~inFunType ~typeNameIsInterface ~typeVars
308308
let typeToString ~config ~typeNameIsInterface type_ =
309309
type_ |> renderType ~config ~typeNameIsInterface ~inFunType:false
310310

311-
let ofType ~config ~typeNameIsInterface ~type_ s =
312-
s ^ ": " ^ (type_ |> typeToString ~config ~typeNameIsInterface)
313-
314311
let emitExportConst ~early ?(comment = "") ~config
315312
?(docString = DocString.empty) ~emitters ~name ~type_ ~typeNameIsInterface
316313
line =
314+
let typeString = type_ |> typeToString ~config ~typeNameIsInterface in
317315
(match comment = "" with
318316
| true -> comment
319317
| false -> "// " ^ comment ^ "\n")
320-
^ DocString.render docString ^ "export const "
321-
^ (name |> ofType ~config ~typeNameIsInterface ~type_)
322-
^ " = " ^ line
318+
^ DocString.render docString ^ "export const " ^ name ^ ": " ^ typeString
319+
^ " = " ^ line ^ " as any;"
323320
|> (match early with
324321
| true -> Emitters.exportEarly
325322
| false -> Emitters.export)
@@ -388,27 +385,20 @@ let emitRequire ~importedValueOrComponent ~early ~emitters ~(config : Config.t)
388385
|> ImportPath.chopExtensionSafe (* for backward compatibility *)
389386
| _ -> importPath
390387
in
391-
match config.module_ with
392-
| ES6 when not importedValueOrComponent ->
393-
let moduleNameString = ModuleName.toString moduleName in
394-
(let es6ImportModule = moduleNameString ^ "__Es6Import" in
395-
"import * as " ^ es6ImportModule ^ " from '"
396-
^ (importPath |> ImportPath.emit)
397-
^ "';\n" ^ "const " ^ moduleNameString ^ ": any = " ^ es6ImportModule ^ ";")
398-
|> (match early with
399-
| true -> Emitters.requireEarly
400-
| false -> Emitters.require)
401-
~emitters
402-
| _ ->
403-
"const "
404-
^ ModuleName.toString moduleName
405-
^ " = require('"
406-
^ (importPath |> ImportPath.emit)
407-
^ "');"
408-
|> (match early with
409-
| true -> Emitters.requireEarly
410-
| false -> Emitters.require)
411-
~emitters
388+
let moduleNameString = ModuleName.toString moduleName in
389+
let importPathString = ImportPath.emit importPath in
390+
let output =
391+
match config.module_ with
392+
| ES6 when not importedValueOrComponent ->
393+
"import * as " ^ moduleNameString ^ " from '" ^ importPathString ^ "';"
394+
| _ ->
395+
"const " ^ moduleNameString ^ " = require('" ^ importPathString ^ "');"
396+
in
397+
output
398+
|> (match early with
399+
| true -> Emitters.requireEarly
400+
| false -> Emitters.require)
401+
~emitters
412402

413403
let require ~early =
414404
match early with

jscomp/gentype_tests/typescript-react-example/src/Comments.gen.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/* eslint-disable */
44
/* tslint:disable */
55

6-
import * as CommentsBS__Es6Import from './Comments.bs';
7-
const CommentsBS: any = CommentsBS__Es6Import;
6+
import * as CommentsBS from './Comments.bs';
87

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

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

2928
export const DecideSubject: {
3029
/** Decide on a subject matter for a poem. */
31-
_placeholder: (run:string, times:number) => void } = CommentsBS.DecideSubject
30+
_placeholder: (run:string, times:number) => void } = CommentsBS.DecideSubject as any;

jscomp/gentype_tests/typescript-react-example/src/Core.gen.tsx

+26-26
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import {someFunWithNullThenOptionalArgs as someFunWithNullThenOptionalArgsNotChe
88
import {someFunWithNullUndefinedArg as someFunWithNullUndefinedArgNotChecked} from './CoreTS';
99

1010
// In case of type error, check the type of 'someFunWithNullThenOptionalArgs' in 'Core.res' and './CoreTS'.
11-
export const someFunWithNullThenOptionalArgsTypeChecked: (_1:(null | string), _2:(undefined | string)) => string = someFunWithNullThenOptionalArgsNotChecked;
11+
export const someFunWithNullThenOptionalArgsTypeChecked: (_1:(null | string), _2:(undefined | string)) => string = someFunWithNullThenOptionalArgsNotChecked as any;
1212

1313
// Export 'someFunWithNullThenOptionalArgs' early to allow circular import from the '.bs.js' file.
14-
export const someFunWithNullThenOptionalArgs: unknown = someFunWithNullThenOptionalArgsTypeChecked as (_1:(null | string), _2:(undefined | string)) => string;
14+
export const someFunWithNullThenOptionalArgs: unknown = someFunWithNullThenOptionalArgsTypeChecked as (_1:(null | string), _2:(undefined | string)) => string as any;
1515

1616
// In case of type error, check the type of 'someFunWithNullUndefinedArg' in 'Core.res' and './CoreTS'.
17-
export const someFunWithNullUndefinedArgTypeChecked: (_1:(null | undefined | string), _2:number) => string = someFunWithNullUndefinedArgNotChecked;
17+
export const someFunWithNullUndefinedArgTypeChecked: (_1:(null | undefined | string), _2:number) => string = someFunWithNullUndefinedArgNotChecked as any;
1818

1919
// Export 'someFunWithNullUndefinedArg' early to allow circular import from the '.bs.js' file.
20-
export const someFunWithNullUndefinedArg: unknown = someFunWithNullUndefinedArgTypeChecked as (_1:(null | undefined | string), _2:number) => string;
20+
export const someFunWithNullUndefinedArg: unknown = someFunWithNullUndefinedArgTypeChecked as (_1:(null | undefined | string), _2:number) => string as any;
2121

2222
const CoreBS = require('./Core.bs');
2323

@@ -27,46 +27,46 @@ export type t1 = { readonly x?: string };
2727

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

30-
export const null0: (x:(null | number)) => (null | number) = CoreBS.null0;
30+
export const null0: (x:(null | number)) => (null | number) = CoreBS.null0 as any;
3131

32-
export const null1: (x:(null | number)) => (null | number) = CoreBS.null1;
32+
export const null1: (x:(null | number)) => (null | number) = CoreBS.null1 as any;
3333

34-
export const nullable0: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable0;
34+
export const nullable0: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable0 as any;
3535

36-
export const nullable1: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable1;
36+
export const nullable1: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable1 as any;
3737

38-
export const undefined0: (x:(undefined | number)) => (undefined | number) = CoreBS.undefined0;
38+
export const undefined0: (x:(undefined | number)) => (undefined | number) = CoreBS.undefined0 as any;
3939

40-
export const undefined1: (x:(undefined | number)) => (undefined | number) = CoreBS.undefined1;
40+
export const undefined1: (x:(undefined | number)) => (undefined | number) = CoreBS.undefined1 as any;
4141

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

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

46-
export const promise0: (x:Promise<string>) => Promise<string> = CoreBS.promise0;
46+
export const promise0: (x:Promise<string>) => Promise<string> = CoreBS.promise0 as any;
4747

48-
export const promise1: (x:Promise<string>) => Promise<string> = CoreBS.promise1;
48+
export const promise1: (x:Promise<string>) => Promise<string> = CoreBS.promise1 as any;
4949

50-
export const date0: (x:Date) => Date = CoreBS.date0;
50+
export const date0: (x:Date) => Date = CoreBS.date0 as any;
5151

52-
export const date1: (x:Date) => Date = CoreBS.date1;
52+
export const date1: (x:Date) => Date = CoreBS.date1 as any;
5353

54-
export const bigint0: (x:BigInt) => BigInt = CoreBS.bigint0;
54+
export const bigint0: (x:BigInt) => BigInt = CoreBS.bigint0 as any;
5555

56-
export const bigint1: (x:BigInt) => BigInt = CoreBS.bigint1;
56+
export const bigint1: (x:BigInt) => BigInt = CoreBS.bigint1 as any;
5757

58-
export const regexp0: (x:RegExp) => RegExp = CoreBS.regexp0;
58+
export const regexp0: (x:RegExp) => RegExp = CoreBS.regexp0 as any;
5959

60-
export const regexp1: (x:RegExp) => RegExp = CoreBS.regexp1;
60+
export const regexp1: (x:RegExp) => RegExp = CoreBS.regexp1 as any;
6161

62-
export const map1: (x:Map<string,number>) => Map<string,number> = CoreBS.map1;
62+
export const map1: (x:Map<string,number>) => Map<string,number> = CoreBS.map1 as any;
6363

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

66-
export const set1: (x:Set<string>) => Set<string> = CoreBS.set1;
66+
export const set1: (x:Set<string>) => Set<string> = CoreBS.set1 as any;
6767

68-
export const weakset1: (x:WeakSet<number[]>) => WeakSet<number[]> = CoreBS.weakset1;
68+
export const weakset1: (x:WeakSet<number[]>) => WeakSet<number[]> = CoreBS.weakset1 as any;
6969

70-
export const option0: (x:(undefined | string)) => (undefined | string) = CoreBS.option0;
70+
export const option0: (x:(undefined | string)) => (undefined | string) = CoreBS.option0 as any;
7171

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

jscomp/gentype_tests/typescript-react-example/src/Docstrings.gen.tsx

+20-21
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,46 @@
33
/* eslint-disable */
44
/* tslint:disable */
55

6-
import * as DocstringsBS__Es6Import from './Docstrings.bs';
7-
const DocstringsBS: any = DocstringsBS__Es6Import;
6+
import * as DocstringsBS from './Docstrings.bs';
87

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

1110
/** hello */
12-
export const flat: number = DocstringsBS.flat;
11+
export const flat: number = DocstringsBS.flat as any;
1312

1413
/** \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 */
15-
export const signMessage: (message:string, key:number) => string = DocstringsBS.signMessage;
14+
export const signMessage: (message:string, key:number) => string = DocstringsBS.signMessage as any;
1615

17-
export const one: (a:number) => number = DocstringsBS.one;
16+
export const one: (a:number) => number = DocstringsBS.one as any;
1817

19-
export const two: (a:number, b:number) => number = DocstringsBS.two;
18+
export const two: (a:number, b:number) => number = DocstringsBS.two as any;
2019

21-
export const tree: (a:number, b:number, c:number) => number = DocstringsBS.tree;
20+
export const tree: (a:number, b:number, c:number) => number = DocstringsBS.tree as any;
2221

23-
export const oneU: (a:number) => number = DocstringsBS.oneU;
22+
export const oneU: (a:number) => number = DocstringsBS.oneU as any;
2423

25-
export const twoU: (a:number, b:number) => number = DocstringsBS.twoU;
24+
export const twoU: (a:number, b:number) => number = DocstringsBS.twoU as any;
2625

27-
export const treeU: (a:number, b:number, c:number) => number = DocstringsBS.treeU;
26+
export const treeU: (a:number, b:number, c:number) => number = DocstringsBS.treeU as any;
2827

29-
export const useParam: (param:number) => number = DocstringsBS.useParam;
28+
export const useParam: (param:number) => number = DocstringsBS.useParam as any;
3029

31-
export const useParamU: (param:number) => number = DocstringsBS.useParamU;
30+
export const useParamU: (param:number) => number = DocstringsBS.useParamU as any;
3231

33-
export const unnamed1: (param:number) => number = DocstringsBS.unnamed1;
32+
export const unnamed1: (param:number) => number = DocstringsBS.unnamed1 as any;
3433

35-
export const unnamed1U: (param:number) => number = DocstringsBS.unnamed1U;
34+
export const unnamed1U: (param:number) => number = DocstringsBS.unnamed1U as any;
3635

37-
export const unnamed2: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2;
36+
export const unnamed2: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2 as any;
3837

39-
export const unnamed2U: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2U;
38+
export const unnamed2U: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2U as any;
4039

41-
export const grouped: (x:number, y:number, a:number, b:number, c:number, z:number) => number = DocstringsBS.grouped;
40+
export const grouped: (x:number, y:number, a:number, b:number, c:number, z:number) => number = DocstringsBS.grouped as any;
4241

43-
export const unitArgWithoutConversion: () => string = DocstringsBS.unitArgWithoutConversion;
42+
export const unitArgWithoutConversion: () => string = DocstringsBS.unitArgWithoutConversion as any;
4443

45-
export const unitArgWithoutConversionU: () => string = DocstringsBS.unitArgWithoutConversionU;
44+
export const unitArgWithoutConversionU: () => string = DocstringsBS.unitArgWithoutConversionU as any;
4645

47-
export const unitArgWithConversion: () => t = DocstringsBS.unitArgWithConversion;
46+
export const unitArgWithConversion: () => t = DocstringsBS.unitArgWithConversion as any;
4847

49-
export const unitArgWithConversionU: () => t = DocstringsBS.unitArgWithConversionU;
48+
export const unitArgWithConversionU: () => t = DocstringsBS.unitArgWithConversionU as any;

jscomp/gentype_tests/typescript-react-example/src/EmitModuleIfNoConversion.gen.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
/* eslint-disable */
44
/* tslint:disable */
55

6-
import * as EmitModuleIfNoConversionBS__Es6Import from './EmitModuleIfNoConversion.bs';
7-
const EmitModuleIfNoConversionBS: any = EmitModuleIfNoConversionBS__Es6Import;
6+
import * as EmitModuleIfNoConversionBS from './EmitModuleIfNoConversion.bs';
87

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

11-
export const X_foo: (t:t) => void = EmitModuleIfNoConversionBS.X.foo;
10+
export const X_foo: (t:t) => void = EmitModuleIfNoConversionBS.X.foo as any;
1211

13-
export const X_x: number = EmitModuleIfNoConversionBS.X.x;
12+
export const X_x: number = EmitModuleIfNoConversionBS.X.x as any;
1413

15-
export const Y_x: string = EmitModuleIfNoConversionBS.Y.x;
14+
export const Y_x: string = EmitModuleIfNoConversionBS.Y.x as any;
1615

17-
export const Y: { x: string } = EmitModuleIfNoConversionBS.Y
16+
export const Y: { x: string } = EmitModuleIfNoConversionBS.Y as any;
1817

19-
export const X: { x: number; foo: (t:t) => void } = EmitModuleIfNoConversionBS.X
18+
export const X: { x: number; foo: (t:t) => void } = EmitModuleIfNoConversionBS.X as any;

jscomp/gentype_tests/typescript-react-example/src/ExportWithRename.gen.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
import * as React from 'react';
77

8-
import * as ExportWithRenameBS__Es6Import from './ExportWithRename.bs';
9-
const ExportWithRenameBS: any = ExportWithRenameBS__Es6Import;
8+
import * as ExportWithRenameBS from './ExportWithRename.bs';
109

1110
export type Props = { readonly s: string };
1211

13-
export const Renamed: React.ComponentType<{ readonly s: string }> = ExportWithRenameBS.make;
12+
export const Renamed: React.ComponentType<{ readonly s: string }> = ExportWithRenameBS.make as any;

jscomp/gentype_tests/typescript-react-example/src/FirstClassModules.gen.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/* eslint-disable */
44
/* tslint:disable */
55

6-
import * as FirstClassModulesBS__Es6Import from './FirstClassModules.bs';
7-
const FirstClassModulesBS: any = FirstClassModulesBS__Es6Import;
6+
import * as FirstClassModulesBS from './FirstClassModules.bs';
87

98
export type MT_t = number;
109

@@ -24,7 +23,7 @@ export type firstClassModule = {
2423
readonly y: string
2524
};
2625

27-
export const firstClassModule: firstClassModule = FirstClassModulesBS.firstClassModule;
26+
export const firstClassModule: firstClassModule = FirstClassModulesBS.firstClassModule as any;
2827

2928
export const testConvert: (m:{
3029
readonly x: number;
@@ -50,7 +49,7 @@ export const testConvert: (m:{
5049
};
5150
readonly Z: unknown;
5251
readonly y: string
53-
} = FirstClassModulesBS.testConvert;
52+
} = FirstClassModulesBS.testConvert as any;
5453

5554
export const someFunctorAsFunction: (x:{
5655
readonly x: number;
@@ -64,4 +63,4 @@ export const someFunctorAsFunction: (x:{
6463
};
6564
readonly Z: unknown;
6665
readonly y: string
67-
}) => { readonly ww: string } = FirstClassModulesBS.someFunctorAsFunction;
66+
}) => { readonly ww: string } = FirstClassModulesBS.someFunctorAsFunction as any;

0 commit comments

Comments
 (0)