From bb70ea47f51c985adb678c93cfaf2ee549e55668 Mon Sep 17 00:00:00 2001 From: Bogdan Savluk Date: Fri, 14 May 2021 09:32:38 +0200 Subject: [PATCH] convert @babel/cli to typescript (#13213) * babel-cli flowts rename * babel-cli flowts convert * babel-cli * yarn install --- ...rnal-helpers.js => babel-external-helpers.ts} | 2 -- packages/babel-cli/src/babel/{dir.js => dir.ts} | 8 +++----- .../babel-cli/src/babel/{file.js => file.ts} | 16 +++++++--------- .../babel-cli/src/babel/{index.js => index.ts} | 0 .../src/babel/{options.js => options.ts} | 7 +++---- .../babel-cli/src/babel/{util.js => util.ts} | 15 +++++---------- 6 files changed, 18 insertions(+), 30 deletions(-) rename packages/babel-cli/src/{babel-external-helpers.js => babel-external-helpers.ts} (98%) rename packages/babel-cli/src/babel/{dir.js => dir.ts} (98%) rename packages/babel-cli/src/babel/{file.js => file.ts} (95%) rename packages/babel-cli/src/babel/{index.js => index.ts} (100%) rename packages/babel-cli/src/babel/{options.js => options.ts} (98%) rename packages/babel-cli/src/babel/{util.js => util.ts} (94%) diff --git a/packages/babel-cli/src/babel-external-helpers.js b/packages/babel-cli/src/babel-external-helpers.ts similarity index 98% rename from packages/babel-cli/src/babel-external-helpers.js rename to packages/babel-cli/src/babel-external-helpers.ts index fef536187c..5c6ef24de6 100755 --- a/packages/babel-cli/src/babel-external-helpers.js +++ b/packages/babel-cli/src/babel-external-helpers.ts @@ -1,5 +1,3 @@ -// @flow - import commander from "commander"; import { buildExternalHelpers } from "@babel/core"; diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.ts similarity index 98% rename from packages/babel-cli/src/babel/dir.js rename to packages/babel-cli/src/babel/dir.ts index 7eb6db97f1..2ab0652afb 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.ts @@ -1,18 +1,16 @@ -// @flow - import slash from "slash"; import path from "path"; import fs from "fs"; import * as util from "./util"; -import { type CmdOptions } from "./options"; +import type { CmdOptions } from "./options"; const FILE_TYPE = Object.freeze({ NON_COMPILABLE: "NON_COMPILABLE", COMPILED: "COMPILED", IGNORED: "IGNORED", ERR_COMPILATION: "ERR_COMPILATION", -}); +} as const); function outputFileSync(filePath: string, data: string | Buffer): void { fs.mkdirSync(path.dirname(filePath), { recursive: true }); @@ -28,7 +26,7 @@ export default async function ({ async function write( src: string, base: string, - ): Promise<$Keys> { + ): Promise { let relative = path.relative(base, src); if (!util.isCompilableExtension(relative, cliOptions.extensions)) { diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.ts similarity index 95% rename from packages/babel-cli/src/babel/file.js rename to packages/babel-cli/src/babel/file.ts index 71478fafdb..e05f664979 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.ts @@ -1,5 +1,3 @@ -// @flow - import convertSourceMap from "convert-source-map"; import sourceMap from "source-map"; import slash from "slash"; @@ -7,18 +5,18 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; -import { type CmdOptions } from "./options"; +import type { CmdOptions } from "./options"; type CompilationOutput = { - code: string, - map: Object, + code: string; + map: any; }; export default async function ({ cliOptions, babelOptions, }: CmdOptions): Promise { - function buildResult(fileResults: Array): CompilationOutput { + function buildResult(fileResults: Array): CompilationOutput { const map = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || @@ -37,7 +35,7 @@ export default async function ({ if (result.map) { const consumer = new sourceMap.SourceMapConsumer(result.map); - const sources = new Set(); + const sources = new Set(); consumer.eachMapping(function (mapping) { if (mapping.source != null) sources.add(mapping.source); @@ -104,7 +102,7 @@ export default async function ({ } function readStdin(): Promise { - return new Promise((resolve: Function, reject: Function): void => { + return new Promise((resolve, reject): void => { let code = ""; process.stdin.setEncoding("utf8"); @@ -158,7 +156,7 @@ export default async function ({ }); const results = await Promise.all( - _filenames.map(async function (filename: string): Promise { + _filenames.map(async function (filename: string): Promise { let sourceFilename = filename; if (cliOptions.outFile) { sourceFilename = path.relative( diff --git a/packages/babel-cli/src/babel/index.js b/packages/babel-cli/src/babel/index.ts similarity index 100% rename from packages/babel-cli/src/babel/index.js rename to packages/babel-cli/src/babel/index.ts diff --git a/packages/babel-cli/src/babel/options.js b/packages/babel-cli/src/babel/options.ts similarity index 98% rename from packages/babel-cli/src/babel/options.js rename to packages/babel-cli/src/babel/options.ts index 3e6cd45c46..7253d64b43 100644 --- a/packages/babel-cli/src/babel/options.js +++ b/packages/babel-cli/src/babel/options.ts @@ -1,5 +1,3 @@ -// @flow - import fs from "fs"; import commander from "commander"; @@ -170,6 +168,7 @@ commander.option( "Use a specific extension for the output files", ); +declare const PACKAGE_JSON: { name: string; version: string }; commander.version(PACKAGE_JSON.version + " (@babel/core " + version + ")"); commander.usage("[options] "); // register an empty action handler so that commander.js can throw on @@ -178,8 +177,8 @@ commander.usage("[options] "); commander.action(() => {}); export type CmdOptions = { - babelOptions: Object, - cliOptions: Object, + babelOptions: any; + cliOptions: any; }; export default function parseArgv(args: Array): CmdOptions | null { diff --git a/packages/babel-cli/src/babel/util.js b/packages/babel-cli/src/babel/util.ts similarity index 94% rename from packages/babel-cli/src/babel/util.js rename to packages/babel-cli/src/babel/util.ts index 0a90de40d4..992a77d4a2 100644 --- a/packages/babel-cli/src/babel/util.js +++ b/packages/babel-cli/src/babel/util.ts @@ -1,5 +1,3 @@ -// @flow - import readdirRecursive from "fs-readdir-recursive"; import * as babel from "@babel/core"; import path from "path"; @@ -47,7 +45,7 @@ export function readdirForCompilable( */ export function isCompilableExtension( filename: string, - altExts?: Array, + altExts?: readonly string[], ): boolean { const exts = altExts || babel.DEFAULT_EXTENSIONS; const ext = path.extname(filename); @@ -65,8 +63,8 @@ const CALLER = { export function transform( filename: string, code: string, - opts: Object, -): Promise { + opts: any, +): Promise { opts = { ...opts, caller: CALLER, @@ -81,10 +79,7 @@ export function transform( }); } -export function compile( - filename: string, - opts: Object | Function, -): Promise { +export function compile(filename: string, opts: any | Function): Promise { opts = { ...opts, caller: CALLER, @@ -119,7 +114,7 @@ process.on("uncaughtException", function (err) { process.exitCode = 1; }); -export function requireChokidar(): Object { +export function requireChokidar(): any { // $FlowIgnore - https://github.com/facebook/flow/issues/6913#issuecomment-662787504 const require = createRequire(import /*::("")*/.meta.url);