Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3e7965d

Browse files
committedDec 6, 2023
Gentype: make output DCE-friendly
1 parent fc7b900 commit 3e7965d

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
-421
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

0 commit comments

Comments
 (0)
Please sign in to comment.