From d7714ad47b3bafebb9d948eb07923c74e18fc66c Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sat, 13 Jul 2019 22:38:23 +0200 Subject: [PATCH 01/28] added flow to directory cmd --- packages/babel-cli/src/babel/dir.js | 16 +++++++++++----- packages/babel-cli/src/babel/options.js | 9 ++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index 2eaca85750f8..ce1b7a3fe5be 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -1,3 +1,5 @@ +// @flow + import defaults from "lodash/defaults"; import outputFileSync from "output-file-sync"; import { sync as mkdirpSync } from "mkdirp"; @@ -6,11 +8,15 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; +import { type cmdOptions } from "./options"; -export default async function({ cliOptions, babelOptions }) { +export default async function({ + cliOptions, + babelOptions, +}: cmdOptions): Promise { const filenames = cliOptions.filenames; - async function write(src, base) { + async function write(src: string, base: string): Promise { let relative = path.relative(base, src); if (!util.isCompilableExtension(relative, cliOptions.extensions)) { @@ -65,14 +71,14 @@ export default async function({ cliOptions, babelOptions }) { } } - function getDest(filename, base) { + function getDest(filename: string, base: string): string { if (cliOptions.relative) { return path.join(base, cliOptions.outDir, filename); } return path.join(cliOptions.outDir, filename); } - async function handleFile(src, base) { + async function handleFile(src: string, base: string): Promise { const written = await write(src, base); if (!written && cliOptions.copyFiles) { @@ -84,7 +90,7 @@ export default async function({ cliOptions, babelOptions }) { return written; } - async function handle(filenameOrDir) { + async function handle(filenameOrDir: string): Promise { if (!fs.existsSync(filenameOrDir)) return 0; const stat = fs.statSync(filenameOrDir); diff --git a/packages/babel-cli/src/babel/options.js b/packages/babel-cli/src/babel/options.js index 41cc0fb83e7f..965177f391c7 100644 --- a/packages/babel-cli/src/babel/options.js +++ b/packages/babel-cli/src/babel/options.js @@ -1,3 +1,5 @@ +// @flow + import fs from "fs"; import commander from "commander"; @@ -151,7 +153,12 @@ commander.option( commander.version(pkg.version + " (@babel/core " + version + ")"); commander.usage("[options] "); -export default function parseArgv(args: Array) { +export type cmdOptions = { + babelOptions: Object, + cliOptions: Object, +}; + +export default function parseArgv(args: Array): cmdOptions { // commander.parse(args); From a0ec5e44387a8d89633edb55ae7c5196b33a55c5 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sat, 13 Jul 2019 23:13:20 +0200 Subject: [PATCH 02/28] added flow to file cmd --- packages/babel-cli/src/babel/file.js | 34 +++++++++++++++++----------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index f0678e27ed39..9d6ceeaaba76 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -6,9 +6,18 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; - -export default async function({ cliOptions, babelOptions }) { - function buildResult(fileResults) { +import { type cmdOptions } from "./options"; + +type compileOutput = { + code: string, + map: Object, +}; + +export default async function({ + cliOptions, + babelOptions, +}: cmdOptions): Promise { + function buildResult(fileResults): compileOutput { const map = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || @@ -74,7 +83,7 @@ export default async function({ cliOptions, babelOptions }) { }; } - function output(fileResults) { + function output(fileResults: Array): void { const result = buildResult(fileResults); if (cliOptions.outFile) { @@ -91,8 +100,8 @@ export default async function({ cliOptions, babelOptions }) { } } - function readStdin() { - return new Promise((resolve, reject) => { + function readStdin(): Promise { + return new Promise((resolve: Function, reject: Function) => { let code = ""; process.stdin.setEncoding("utf8"); @@ -109,7 +118,7 @@ export default async function({ cliOptions, babelOptions }) { }); } - async function stdin() { + async function stdin(): Promise { const code = await readStdin(); const res = await util.transform( @@ -126,7 +135,7 @@ export default async function({ cliOptions, babelOptions }) { output([res]); } - async function walk(filenames) { + async function walk(filenames: Array): Promise { const _filenames = []; filenames.forEach(function(filename) { @@ -151,7 +160,7 @@ export default async function({ cliOptions, babelOptions }) { }); const results = await Promise.all( - _filenames.map(async function(filename) { + _filenames.map(async function(filename): Promise { let sourceFilename = filename; if (cliOptions.outFile) { sourceFilename = path.relative( @@ -168,7 +177,7 @@ export default async function({ cliOptions, babelOptions }) { { sourceFileName: sourceFilename, // Since we're compiling everything to be merged together, - // "inline" applies to the final output file, but to the individual + // "inline" applies to the final output file, but not to the individual // files being concatenated. sourceMaps: babelOptions.sourceMaps === "inline" @@ -184,7 +193,6 @@ export default async function({ cliOptions, babelOptions }) { } console.error(err); - return null; } }), ); @@ -192,7 +200,7 @@ export default async function({ cliOptions, babelOptions }) { output(results); } - async function files(filenames) { + async function files(filenames: Array): Promise { if (!cliOptions.skipInitialBuild) { await walk(filenames); } @@ -208,7 +216,7 @@ export default async function({ cliOptions, babelOptions }) { pollInterval: 10, }, }) - .on("all", function(type, filename) { + .on("all", function(type: string, filename: string) { if (!util.isCompilableExtension(filename, cliOptions.extensions)) { return; } From a2544476d0353b6ffdd0def83e00ae9c2c849ac2 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sun, 14 Jul 2019 12:01:24 +0200 Subject: [PATCH 03/28] added flow to babel-cli utility file --- packages/babel-cli/src/babel/util.js | 32 +++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/babel-cli/src/babel/util.js b/packages/babel-cli/src/babel/util.js index 99c9ae9b00cd..daacba53f0d2 100644 --- a/packages/babel-cli/src/babel/util.js +++ b/packages/babel-cli/src/babel/util.js @@ -1,10 +1,12 @@ +// @flow + import readdirRecursive from "fs-readdir-recursive"; import * as babel from "@babel/core"; import includes from "lodash/includes"; import path from "path"; import fs from "fs"; -export function chmod(src, dest) { +export function chmod(src: string, dest: string): void { fs.chmodSync(dest, fs.statSync(src).mode); } @@ -13,8 +15,8 @@ type ReaddirFilter = (filename: string) => boolean; export function readdir( dirname: string, includeDotfiles: boolean, - filter: ReaddirFilter, -) { + filter?: ReaddirFilter, +): Array { return readdirRecursive(dirname, (filename, _index, currentDirectory) => { const stat = fs.statSync(path.join(currentDirectory, filename)); @@ -30,7 +32,7 @@ export function readdirForCompilable( dirname: string, includeDotfiles: boolean, altExts?: Array, -) { +): Array { return readdir(dirname, includeDotfiles, function(filename) { return isCompilableExtension(filename, altExts); }); @@ -48,7 +50,7 @@ export function isCompilableExtension( return includes(exts, ext); } -export function addSourceMappingUrl(code, loc) { +export function addSourceMappingUrl(code: string, loc: string): string { return code + "\n//# sourceMappingURL=" + path.basename(loc); } @@ -56,7 +58,11 @@ const CALLER = { name: "@babel/cli", }; -export function transform(filename, code, opts) { +export function transform( + filename: string, + code: string, + opts: Object, +): Promise { opts = { ...opts, caller: CALLER, @@ -71,7 +77,10 @@ export function transform(filename, code, opts) { }); } -export function compile(filename, opts) { +export function compile( + filename: string, + opts: Object | Function, +): Promise { opts = { ...opts, caller: CALLER, @@ -85,7 +94,7 @@ export function compile(filename, opts) { }); } -export function deleteDir(path) { +export function deleteDir(path: string): void { if (fs.existsSync(path)) { fs.readdirSync(path).forEach(function(file) { const curPath = path + "/" + file; @@ -106,7 +115,7 @@ process.on("uncaughtException", function(err) { process.exit(1); }); -export function requireChokidar() { +export function requireChokidar(): Object { try { return require("chokidar"); } catch (err) { @@ -118,7 +127,10 @@ export function requireChokidar() { } } -export function adjustRelative(relative, keepFileExtension) { +export function adjustRelative( + relative: string, + keepFileExtension: boolean, +): string { if (keepFileExtension) { return relative; } From af50714595c9a4ab28db27b13bdc999c84316c41 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sun, 14 Jul 2019 20:33:32 +0200 Subject: [PATCH 04/28] corrected errors in babel-cli file command --- packages/babel-cli/src/babel/dir.js | 4 +-- packages/babel-cli/src/babel/file.js | 42 ++++++++++++++----------- packages/babel-cli/src/babel/options.js | 4 +-- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index ce1b7a3fe5be..32136b78f02a 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -8,12 +8,12 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; -import { type cmdOptions } from "./options"; +import { type CmdOptions } from "./options"; export default async function({ cliOptions, babelOptions, -}: cmdOptions): Promise { +}: CmdOptions): Promise { const filenames = cliOptions.filenames; async function write(src: string, base: string): Promise { diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 9d6ceeaaba76..46cf3d7922fd 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -1,14 +1,18 @@ +// @flow + import convertSourceMap from "convert-source-map"; import defaults from "lodash/defaults"; +import isString from "lodash/isString"; +import toString from "lodash/toString"; import sourceMap from "source-map"; 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"; -type compileOutput = { +type CompilationOutput = { code: string, map: Object, }; @@ -16,9 +20,9 @@ type compileOutput = { export default async function({ cliOptions, babelOptions, -}: cmdOptions): Promise { - function buildResult(fileResults): compileOutput { - const map = new sourceMap.SourceMapGenerator({ +}: CmdOptions): Promise { + function buildResult(fileResults: Array): CompilationOutput { + const map: Object = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || path.basename(cliOptions.outFile || "") || @@ -101,21 +105,23 @@ export default async function({ } function readStdin(): Promise { - return new Promise((resolve: Function, reject: Function) => { - let code = ""; + return new Promise( + (resolve: Function, reject: Function): void => { + let code = ""; - process.stdin.setEncoding("utf8"); + process.stdin.setEncoding("utf8"); - process.stdin.on("readable", function() { - const chunk = process.stdin.read(); - if (chunk !== null) code += chunk; - }); + process.stdin.on("readable", function() { + const chunk = process.stdin.read(); + if (isString(chunk)) code += toString(chunk); + }); - process.stdin.on("end", function() { - resolve(code); - }); - process.stdin.on("error", reject); - }); + process.stdin.on("end", function() { + resolve(code); + }); + process.stdin.on("error", reject); + }, + ); } async function stdin(): Promise { @@ -160,7 +166,7 @@ export default async function({ }); const results = await Promise.all( - _filenames.map(async function(filename): 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/options.js b/packages/babel-cli/src/babel/options.js index 965177f391c7..19ae7bfce878 100644 --- a/packages/babel-cli/src/babel/options.js +++ b/packages/babel-cli/src/babel/options.js @@ -153,12 +153,12 @@ commander.option( commander.version(pkg.version + " (@babel/core " + version + ")"); commander.usage("[options] "); -export type cmdOptions = { +export type CmdOptions = { babelOptions: Object, cliOptions: Object, }; -export default function parseArgv(args: Array): cmdOptions { +export default function parseArgv(args: Array): CmdOptions { // commander.parse(args); From a122deb582ed4a251eb15adf39180f970594af2a Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sun, 14 Jul 2019 20:46:38 +0200 Subject: [PATCH 05/28] added flow to babel-cli babel-external-helpers --- packages/babel-cli/src/babel-external-helpers.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel-external-helpers.js b/packages/babel-cli/src/babel-external-helpers.js index c2560dfa6f37..e2a1a0d3429f 100755 --- a/packages/babel-cli/src/babel-external-helpers.js +++ b/packages/babel-cli/src/babel-external-helpers.js @@ -1,9 +1,15 @@ +// @flow + import commander from "commander"; +import isString from "lodash/isString"; import { buildExternalHelpers } from "@babel/core"; -function collect(value, previousValue): Array { +function collect( + value: String | any, + previousValue: Array, +): Array { // If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing. - if (typeof value !== "string") return previousValue; + if (!isString(value)) return previousValue; const values = value.split(","); From a16b3d410367fba51c1598dace1835adf23910ac Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Mon, 15 Jul 2019 07:09:44 +0200 Subject: [PATCH 06/28] added flow to babel-cli babel-external-helpers. Iteration #2 --- packages/babel-cli/src/babel-external-helpers.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/babel-cli/src/babel-external-helpers.js b/packages/babel-cli/src/babel-external-helpers.js index e2a1a0d3429f..fef536187c41 100755 --- a/packages/babel-cli/src/babel-external-helpers.js +++ b/packages/babel-cli/src/babel-external-helpers.js @@ -1,15 +1,14 @@ // @flow import commander from "commander"; -import isString from "lodash/isString"; import { buildExternalHelpers } from "@babel/core"; function collect( - value: String | any, + value: string | any, previousValue: Array, ): Array { // If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing. - if (!isString(value)) return previousValue; + if (typeof value !== "string") return previousValue; const values = value.split(","); From 3d484d37016c88776bce292e6840f04317247576 Mon Sep 17 00:00:00 2001 From: Letladi Date: Mon, 15 Jul 2019 15:23:17 +0200 Subject: [PATCH 07/28] changed check inside readStdin inside babel file command to accomodate possibility of chunk being a Buffer --- packages/babel-cli/src/babel/file.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 46cf3d7922fd..7f2533960dbf 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -2,7 +2,6 @@ import convertSourceMap from "convert-source-map"; import defaults from "lodash/defaults"; -import isString from "lodash/isString"; import toString from "lodash/toString"; import sourceMap from "source-map"; import slash from "slash"; @@ -113,7 +112,7 @@ export default async function({ process.stdin.on("readable", function() { const chunk = process.stdin.read(); - if (isString(chunk)) code += toString(chunk); + if (chunk !== null) code += toString(chunk); }); process.stdin.on("end", function() { From de384ff25ca74cfb7b556db2678e9a6e4601359e Mon Sep 17 00:00:00 2001 From: Letladi Date: Mon, 15 Jul 2019 15:37:47 +0200 Subject: [PATCH 08/28] removed toString method inside readStdin of babel-cli file command --- packages/babel-cli/src/babel/file.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 7f2533960dbf..4f231f5344a9 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -2,7 +2,6 @@ import convertSourceMap from "convert-source-map"; import defaults from "lodash/defaults"; -import toString from "lodash/toString"; import sourceMap from "source-map"; import slash from "slash"; import path from "path"; @@ -112,7 +111,8 @@ export default async function({ process.stdin.on("readable", function() { const chunk = process.stdin.read(); - if (chunk !== null) code += toString(chunk); + // $FlowIgnore + if (chunk !== null) code += chunk; }); process.stdin.on("end", function() { From dbb53b42deea1b56e60690f2983f4938f621678d Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Mon, 15 Jul 2019 20:06:35 +0200 Subject: [PATCH 09/28] fixed file command to only include flow changes --- packages/babel-cli/src/babel/file.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 4f231f5344a9..74f90aa18c73 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -198,6 +198,7 @@ export default async function({ } console.error(err); + return null; } }), ); From 2cd51e201508513085fad32c1e965ea3d827867f Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Tue, 16 Jul 2019 05:33:02 +0200 Subject: [PATCH 10/28] added SourceMapGenerator as an arg type to fromObject --- lib/third-party-libs.js.flow | 4 ++-- packages/babel-cli/src/babel/file.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index c21a2797acfe..884b1e68bbfb 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -165,7 +165,7 @@ declare module "source-map" { } declare module "convert-source-map" { - import type { SourceMap } from "source-map"; + import type { SourceMap, SourceMapGenerator } from "source-map"; declare class Converter { toJSON(): string; @@ -177,7 +177,7 @@ declare module "convert-source-map" { declare module.exports: { SourceMap: SourceMap, Converter: Converter, - fromObject(obj: SourceMap): Converter, + fromObject(obj: SourceMap | SourceMapGenerator): Converter, fromJSON(str: string): Converter, fromBase64(str: string): Converter, fromComment(str: string): Converter, diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 74f90aa18c73..14cf4c562971 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -20,7 +20,7 @@ export default async function({ babelOptions, }: CmdOptions): Promise { function buildResult(fileResults: Array): CompilationOutput { - const map: Object = new sourceMap.SourceMapGenerator({ + const map = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || path.basename(cliOptions.outFile || "") || From 3f759bc96a259ec8eda439da59a551537fe64a92 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Tue, 16 Jul 2019 07:26:06 +0200 Subject: [PATCH 11/28] reverted to only using SourceMap inside fromObject method --- lib/third-party-libs.js.flow | 4 ++-- packages/babel-cli/src/babel/file.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index 884b1e68bbfb..a4eed0a1da6c 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -165,7 +165,7 @@ declare module "source-map" { } declare module "convert-source-map" { - import type { SourceMap, SourceMapGenerator } from "source-map"; + import type { SourceMap } from "source-map"; declare class Converter { toJSON(): string; @@ -177,7 +177,7 @@ declare module "convert-source-map" { declare module.exports: { SourceMap: SourceMap, Converter: Converter, - fromObject(obj: SourceMap | SourceMapGenerator): Converter, + fromObject(obj: SourceMap ): Converter, fromJSON(str: string): Converter, fromBase64(str: string): Converter, fromComment(str: string): Converter, diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 14cf4c562971..8ed5e5263fd6 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -76,7 +76,7 @@ export default async function({ babelOptions.sourceMaps === "inline" || (!cliOptions.outFile && babelOptions.sourceMaps) ) { - code += "\n" + convertSourceMap.fromObject(map).toComment(); + code += "\n" + convertSourceMap.fromObject(map.toJSON()).toComment(); } return { From d97a1f7f47eb502933c9512e2732498853b17159 Mon Sep 17 00:00:00 2001 From: Letladi Date: Wed, 17 Jul 2019 14:30:51 +0200 Subject: [PATCH 12/28] went back to returning 'null' from the async function that compiles file list --- packages/babel-cli/src/babel/file.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 4f231f5344a9..74f90aa18c73 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -198,6 +198,7 @@ export default async function({ } console.error(err); + return null; } }), ); From 5e567d94155c0b73bb6d936a94e772797b3152d9 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Thu, 18 Jul 2019 20:35:46 +0200 Subject: [PATCH 13/28] added null promise return type to method in file command --- packages/babel-cli/src/babel/file.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 8ed5e5263fd6..3e281471cba1 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -165,7 +165,9 @@ export default async function({ }); const results = await Promise.all( - _filenames.map(async function(filename: string): Promise { + _filenames.map(async function( + filename: string, + ): Promise | Promise { let sourceFilename = filename; if (cliOptions.outFile) { sourceFilename = path.relative( From c67e611e797fedfa28fc5de07b2bbc63f8af0560 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sat, 15 Jun 2019 09:04:35 +0200 Subject: [PATCH 14/28] Change duplicate tests for @babel/highlight getChalk method (#10093) * removed duplicate tests for @babel/highlight getChalk method * changed second 'getChalk' test case for when colors are not supported --- packages/babel-highlight/test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-highlight/test/index.js b/packages/babel-highlight/test/index.js index 2aeab67a9b3a..a33f79fd5041 100644 --- a/packages/babel-highlight/test/index.js +++ b/packages/babel-highlight/test/index.js @@ -94,8 +94,8 @@ describe("@babel/highlight", function() { }); }); - describe("when colors are supported", function() { - stubColorSupport(true); + describe("when colors are not supported", function() { + stubColorSupport(false); describe("when forceColor is not passed", function() { it("returns a Chalk instance", function() { From 6d2f111d6047b93741f351b3fa34f7159dda1bb7 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Sat, 15 Jun 2019 18:31:12 +0800 Subject: [PATCH 15/28] flow - allow type parameter defaults in function declarations (#10084) * flow - allow type parameter defaults in function declarations * fix flow test * add intern_comments option * fix flow parser test * remove allowdefault from flowParseTypeParameterDeclaration * rename test cases --- Makefile | 2 +- packages/babel-parser/src/plugins/flow.js | 48 +- .../default-invalid-1/input.js | 3 - .../default-invalid-1/options.json | 3 - .../default-invalid-2/input.js | 1 - .../default-invalid-2/options.json | 3 - .../default-invalid-3/input.js | 1 - .../default-invalid-3/options.json | 3 - .../default-invalid-4/input.js | 3 - .../default-invalid-4/options.json | 3 - .../default-invalid-5/input.js | 1 - .../default-invalid-5/options.json | 3 - .../default-invalid-class-extends/input.js | 1 + .../options.json | 3 + .../default-invalid-declare-func/input.js | 1 + .../default-invalid-declare-func/options.json | 3 + .../default-invalid-var-type/input.js | 1 + .../default-invalid-var-type/options.json | 3 + .../default/input.js | 10 + .../default/output.json | 887 +++++++++++++++++- scripts/tests/flow/flow_tests_whitelist.txt | 19 +- .../tests/flow/run_babel_parser_flow_tests.js | 10 +- 22 files changed, 938 insertions(+), 74 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/input.js delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/options.json delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/input.js delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/options.json delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/input.js delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/options.json delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/input.js delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/options.json delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/input.js delete mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/options.json create mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/options.json create mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/options.json create mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/options.json diff --git a/Makefile b/Makefile index dc858ab2ee46..5fa5cb0cfdbf 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ MAKEFLAGS = -j1 -FLOW_COMMIT = 2ac56861e3ceff9ca406ae586fbafb3480c6c0b7 +FLOW_COMMIT = 09669846b7a7ca5a6c23c12d56bb3bebdafd67e9 TEST262_COMMIT = de567d3aa5de4eaa11e00131d26b9fe77997dfb0 # Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967 diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index d6f3e06a906f..e9b4cc32c37f 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -588,16 +588,7 @@ export default (superClass: Class): Class => // Type annotations - flowParseTypeParameter( - allowDefault?: boolean = true, - requireDefault?: boolean = false, - ): N.TypeParameter { - if (!allowDefault && requireDefault) { - throw new Error( - "Cannot disallow a default value (`allowDefault`) while also requiring it (`requireDefault`).", - ); - } - + flowParseTypeParameter(requireDefault?: boolean = false): N.TypeParameter { const nodeStart = this.state.start; const node = this.startNode(); @@ -610,12 +601,8 @@ export default (superClass: Class): Class => node.bound = ident.typeAnnotation; if (this.match(tt.eq)) { - if (allowDefault) { - this.eat(tt.eq); - node.default = this.flowParseType(); - } else { - this.unexpected(); - } + this.eat(tt.eq); + node.default = this.flowParseType(); } else { if (requireDefault) { this.unexpected( @@ -629,9 +616,7 @@ export default (superClass: Class): Class => return this.finishNode(node, "TypeParameter"); } - flowParseTypeParameterDeclaration( - allowDefault?: boolean = true, - ): N.TypeParameterDeclaration { + flowParseTypeParameterDeclaration(): N.TypeParameterDeclaration { const oldInType = this.state.inType; const node = this.startNode(); node.params = []; @@ -648,10 +633,7 @@ export default (superClass: Class): Class => let defaultRequired = false; do { - const typeParameter = this.flowParseTypeParameter( - allowDefault, - defaultRequired, - ); + const typeParameter = this.flowParseTypeParameter(defaultRequired); node.params.push(typeParameter); @@ -798,9 +780,7 @@ export default (superClass: Class): Class => node.typeParameters = null; if (this.isRelational("<")) { - node.typeParameters = this.flowParseTypeParameterDeclaration( - /* allowDefault */ false, - ); + node.typeParameters = this.flowParseTypeParameterDeclaration(); } this.expect(tt.parenL); @@ -1287,9 +1267,7 @@ export default (superClass: Class): Class => case tt.relational: if (this.state.value === "<") { - node.typeParameters = this.flowParseTypeParameterDeclaration( - /* allowDefault */ false, - ); + node.typeParameters = this.flowParseTypeParameterDeclaration(); this.expect(tt.parenL); tmp = this.flowParseFunctionTypeParams(); node.params = tmp.params; @@ -2092,9 +2070,7 @@ export default (superClass: Class): Class => } delete (method: $FlowFixMe).variance; if (this.isRelational("<")) { - method.typeParameters = this.flowParseTypeParameterDeclaration( - /* allowDefault */ false, - ); + method.typeParameters = this.flowParseTypeParameterDeclaration(); } super.pushClassMethod( @@ -2176,9 +2152,7 @@ export default (superClass: Class): Class => // method shorthand if (this.isRelational("<")) { - typeParameters = this.flowParseTypeParameterDeclaration( - /* allowDefault */ false, - ); + typeParameters = this.flowParseTypeParameterDeclaration(); if (!this.match(tt.parenL)) this.unexpected(); } @@ -2386,9 +2360,7 @@ export default (superClass: Class): Class => // $FlowFixMe const kind = node.kind; if (kind !== "get" && kind !== "set" && this.isRelational("<")) { - node.typeParameters = this.flowParseTypeParameterDeclaration( - /* allowDefault */ false, - ); + node.typeParameters = this.flowParseTypeParameterDeclaration(); } super.parseFunctionParams(node, allowModifiers); } diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/input.js deleted file mode 100644 index e85753f1bf25..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/input.js +++ /dev/null @@ -1,3 +0,0 @@ -(class A { - foo() {} -}); diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/options.json deleted file mode 100644 index ef2b7c682fba..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-1/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token (2:8)" -} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/input.js deleted file mode 100644 index 088051643fa2..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/input.js +++ /dev/null @@ -1 +0,0 @@ -({ foo() {} }); diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/options.json deleted file mode 100644 index 2a28555f76db..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-2/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token (1:9)" -} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/input.js deleted file mode 100644 index 742685c460ec..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/input.js +++ /dev/null @@ -1 +0,0 @@ -declare class A { foo(): void } diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/options.json deleted file mode 100644 index fa579aa83145..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-3/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token (1:24)" -} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/input.js deleted file mode 100644 index a35315333a49..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/input.js +++ /dev/null @@ -1,3 +0,0 @@ -class A { - foo() {} -} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/options.json deleted file mode 100644 index ef2b7c682fba..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-4/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token (2:8)" -} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/input.js deleted file mode 100644 index 3223246d3979..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/input.js +++ /dev/null @@ -1 +0,0 @@ -function foo() {} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/options.json deleted file mode 100644 index 98d712379010..000000000000 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-5/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token (1:15)" -} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/input.js new file mode 100644 index 000000000000..2b40a1ba03fc --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/input.js @@ -0,0 +1 @@ +class A extends B {} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/options.json new file mode 100644 index 000000000000..6f017e5a5919 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-class-extends/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \",\" (1:20)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/input.js new file mode 100644 index 000000000000..018ada1c550e --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/input.js @@ -0,0 +1 @@ +declare function foo() {} diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/options.json new file mode 100644 index 000000000000..161afd296dc4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-declare-func/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \":\" (1:35)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/input.js new file mode 100644 index 000000000000..646522dea3d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/input.js @@ -0,0 +1 @@ +var x: Array diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/options.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/options.json new file mode 100644 index 000000000000..fbfad415eebf --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default-invalid-var-type/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \",\" (1:15)" +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/input.js b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/input.js index c434fce2a03d..4feffe1038d6 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/input.js +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/input.js @@ -20,3 +20,13 @@ interface A19 {} interface A20 {} interface A21 {} type A22 = T +function A26() {} +;({ A28() {} }); +class A29 { + foo() {} +} +;(class A30 { + foo() {} +}); +declare class A31 { foo(): void } +() => 123; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json index e97815d406f0..8b2db5858c66 100644 --- a/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json +++ b/packages/babel-parser/test/fixtures/flow/type-parameter-declaration/default/output.json @@ -1,28 +1,28 @@ { "type": "File", "start": 0, - "end": 803, + "end": 1009, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 22, + "line": 32, "column": 22 } }, "program": { "type": "Program", "start": 0, - "end": 803, + "end": 1009, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 22, + "line": 32, "column": 22 } }, @@ -3339,6 +3339,885 @@ "name": "T" } } + }, + { + "type": "FunctionDeclaration", + "start": 804, + "end": 833, + "loc": { + "start": { + "line": 23, + "column": 0 + }, + "end": { + "line": 23, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 813, + "end": 816, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 12 + }, + "identifierName": "A26" + }, + "name": "A26" + }, + "generator": false, + "async": false, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 816, + "end": 828, + "loc": { + "start": { + "line": 23, + "column": 12 + }, + "end": { + "line": 23, + "column": 24 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 817, + "end": 827, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 23 + } + }, + "name": "T", + "variance": null, + "default": { + "type": "StringTypeAnnotation", + "start": 821, + "end": 827, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 23 + } + } + } + } + ] + }, + "params": [], + "body": { + "type": "BlockStatement", + "start": 831, + "end": 833, + "loc": { + "start": { + "line": 23, + "column": 27 + }, + "end": { + "line": 23, + "column": 29 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "EmptyStatement", + "start": 834, + "end": 835, + "loc": { + "start": { + "line": 24, + "column": 0 + }, + "end": { + "line": 24, + "column": 1 + } + } + }, + { + "type": "ExpressionStatement", + "start": 835, + "end": 862, + "loc": { + "start": { + "line": 24, + "column": 1 + }, + "end": { + "line": 24, + "column": 28 + } + }, + "expression": { + "type": "ObjectExpression", + "start": 836, + "end": 860, + "loc": { + "start": { + "line": 24, + "column": 2 + }, + "end": { + "line": 24, + "column": 26 + } + }, + "properties": [ + { + "type": "ObjectMethod", + "start": 838, + "end": 858, + "loc": { + "start": { + "line": 24, + "column": 4 + }, + "end": { + "line": 24, + "column": 24 + } + }, + "method": true, + "key": { + "type": "Identifier", + "start": 838, + "end": 841, + "loc": { + "start": { + "line": 24, + "column": 4 + }, + "end": { + "line": 24, + "column": 7 + }, + "identifierName": "A28" + }, + "name": "A28" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 856, + "end": 858, + "loc": { + "start": { + "line": 24, + "column": 22 + }, + "end": { + "line": 24, + "column": 24 + } + }, + "body": [], + "directives": [] + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 841, + "end": 853, + "loc": { + "start": { + "line": 24, + "column": 7 + }, + "end": { + "line": 24, + "column": 19 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 842, + "end": 852, + "loc": { + "start": { + "line": 24, + "column": 8 + }, + "end": { + "line": 24, + "column": 18 + } + }, + "name": "T", + "variance": null, + "default": { + "type": "StringTypeAnnotation", + "start": 846, + "end": 852, + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 18 + } + } + } + } + ] + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 835 + } + } + }, + { + "type": "ClassDeclaration", + "start": 863, + "end": 899, + "loc": { + "start": { + "line": 25, + "column": 0 + }, + "end": { + "line": 27, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 869, + "end": 872, + "loc": { + "start": { + "line": 25, + "column": 6 + }, + "end": { + "line": 25, + "column": 9 + }, + "identifierName": "A29" + }, + "name": "A29" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 873, + "end": 899, + "loc": { + "start": { + "line": 25, + "column": 10 + }, + "end": { + "line": 27, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 877, + "end": 897, + "loc": { + "start": { + "line": 26, + "column": 2 + }, + "end": { + "line": 26, + "column": 22 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 877, + "end": 880, + "loc": { + "start": { + "line": 26, + "column": 2 + }, + "end": { + "line": 26, + "column": 5 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "computed": false, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 880, + "end": 892, + "loc": { + "start": { + "line": 26, + "column": 5 + }, + "end": { + "line": 26, + "column": 17 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 881, + "end": 891, + "loc": { + "start": { + "line": 26, + "column": 6 + }, + "end": { + "line": 26, + "column": 16 + } + }, + "name": "T", + "variance": null, + "default": { + "type": "StringTypeAnnotation", + "start": 885, + "end": 891, + "loc": { + "start": { + "line": 26, + "column": 10 + }, + "end": { + "line": 26, + "column": 16 + } + } + } + } + ] + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 895, + "end": 897, + "loc": { + "start": { + "line": 26, + "column": 20 + }, + "end": { + "line": 26, + "column": 22 + } + }, + "body": [], + "directives": [] + } + } + ] + } + }, + { + "type": "EmptyStatement", + "start": 900, + "end": 901, + "loc": { + "start": { + "line": 28, + "column": 0 + }, + "end": { + "line": 28, + "column": 1 + } + } + }, + { + "type": "ExpressionStatement", + "start": 901, + "end": 940, + "loc": { + "start": { + "line": 28, + "column": 1 + }, + "end": { + "line": 30, + "column": 3 + } + }, + "expression": { + "type": "ClassExpression", + "start": 902, + "end": 938, + "loc": { + "start": { + "line": 28, + "column": 2 + }, + "end": { + "line": 30, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 908, + "end": 911, + "loc": { + "start": { + "line": 28, + "column": 8 + }, + "end": { + "line": 28, + "column": 11 + }, + "identifierName": "A30" + }, + "name": "A30" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 912, + "end": 938, + "loc": { + "start": { + "line": 28, + "column": 12 + }, + "end": { + "line": 30, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 916, + "end": 936, + "loc": { + "start": { + "line": 29, + "column": 2 + }, + "end": { + "line": 29, + "column": 22 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 916, + "end": 919, + "loc": { + "start": { + "line": 29, + "column": 2 + }, + "end": { + "line": 29, + "column": 5 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "computed": false, + "kind": "method", + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 919, + "end": 931, + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 17 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 920, + "end": 930, + "loc": { + "start": { + "line": 29, + "column": 6 + }, + "end": { + "line": 29, + "column": 16 + } + }, + "name": "T", + "variance": null, + "default": { + "type": "StringTypeAnnotation", + "start": 924, + "end": 930, + "loc": { + "start": { + "line": 29, + "column": 10 + }, + "end": { + "line": 29, + "column": 16 + } + } + } + } + ] + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 934, + "end": 936, + "loc": { + "start": { + "line": 29, + "column": 20 + }, + "end": { + "line": 29, + "column": 22 + } + }, + "body": [], + "directives": [] + } + } + ] + }, + "extra": { + "parenthesized": true, + "parenStart": 901 + } + } + }, + { + "type": "DeclareClass", + "start": 941, + "end": 986, + "loc": { + "start": { + "line": 31, + "column": 0 + }, + "end": { + "line": 31, + "column": 45 + } + }, + "id": { + "type": "Identifier", + "start": 955, + "end": 958, + "loc": { + "start": { + "line": 31, + "column": 14 + }, + "end": { + "line": 31, + "column": 17 + }, + "identifierName": "A31" + }, + "name": "A31" + }, + "typeParameters": null, + "extends": [], + "implements": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start": 959, + "end": 986, + "loc": { + "start": { + "line": 31, + "column": 18 + }, + "end": { + "line": 31, + "column": 45 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 961, + "end": 984, + "loc": { + "start": { + "line": 31, + "column": 20 + }, + "end": { + "line": 31, + "column": 43 + } + }, + "key": { + "type": "Identifier", + "start": 961, + "end": 964, + "loc": { + "start": { + "line": 31, + "column": 20 + }, + "end": { + "line": 31, + "column": 23 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "static": false, + "proto": false, + "kind": "init", + "method": true, + "value": { + "type": "FunctionTypeAnnotation", + "start": 961, + "end": 984, + "loc": { + "start": { + "line": 31, + "column": 20 + }, + "end": { + "line": 31, + "column": 43 + } + }, + "params": [], + "rest": null, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 964, + "end": 976, + "loc": { + "start": { + "line": 31, + "column": 23 + }, + "end": { + "line": 31, + "column": 35 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 965, + "end": 975, + "loc": { + "start": { + "line": 31, + "column": 24 + }, + "end": { + "line": 31, + "column": 34 + } + }, + "name": "T", + "variance": null, + "default": { + "type": "StringTypeAnnotation", + "start": 969, + "end": 975, + "loc": { + "start": { + "line": 31, + "column": 28 + }, + "end": { + "line": 31, + "column": 34 + } + } + } + } + ] + }, + "returnType": { + "type": "VoidTypeAnnotation", + "start": 980, + "end": 984, + "loc": { + "start": { + "line": 31, + "column": 39 + }, + "end": { + "line": 31, + "column": 43 + } + } + } + }, + "optional": false + } + ], + "indexers": [], + "internalSlots": [], + "exact": false + } + }, + { + "type": "ExpressionStatement", + "start": 987, + "end": 1009, + "loc": { + "start": { + "line": 32, + "column": 0 + }, + "end": { + "line": 32, + "column": 22 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 987, + "end": 1008, + "loc": { + "start": { + "line": 32, + "column": 0 + }, + "end": { + "line": 32, + "column": 21 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "NumericLiteral", + "start": 1005, + "end": 1008, + "loc": { + "start": { + "line": 32, + "column": 18 + }, + "end": { + "line": 32, + "column": 21 + } + }, + "extra": { + "rawValue": 123, + "raw": "123" + }, + "value": 123 + }, + "typeParameters": { + "type": "TypeParameterDeclaration", + "start": 987, + "end": 999, + "loc": { + "start": { + "line": 32, + "column": 0 + }, + "end": { + "line": 32, + "column": 12 + } + }, + "params": [ + { + "type": "TypeParameter", + "start": 988, + "end": 998, + "loc": { + "start": { + "line": 32, + "column": 1 + }, + "end": { + "line": 32, + "column": 11 + } + }, + "name": "T", + "variance": null, + "default": { + "type": "StringTypeAnnotation", + "start": 992, + "end": 998, + "loc": { + "start": { + "line": 32, + "column": 5 + }, + "end": { + "line": 32, + "column": 11 + } + } + } + } + ] + } + } } ], "directives": [] diff --git a/scripts/tests/flow/flow_tests_whitelist.txt b/scripts/tests/flow/flow_tests_whitelist.txt index 60ee5e0e6e98..81758912b0f4 100644 --- a/scripts/tests/flow/flow_tests_whitelist.txt +++ b/scripts/tests/flow/flow_tests_whitelist.txt @@ -9,24 +9,33 @@ # Entries should be removed incrementally as the babel parser is improved. JSX_invalid/migrated_0000.js +arrow_function/tuple_return_type.js arrow_function_invalid/migrated_0002.js async_await/migrated_0007.js async_await/migrated_0020.js async_await/migrated_0024.js async_await/migrated_0027.js async_generators/migrated_0007.js -class_properties/migrated_0000.js -class_properties/migrated_0005.js -class_properties/migrated_0011.js -class_properties/migrated_0016.js class_properties/migrated_0021.js class_properties/migrated_0026.js decorators/migrated_0003.js decorators/migrated_0007.js private_class_properties/multiple.js private_class_properties/super.js +private_class_properties/getter_and_field.js +private_class_properties/getter_duplicate.js +private_class_properties/setter_and_field.js +private_class_properties/setter_duplicate.js types/member/reserved_words.js -types/parameter_defaults/migrated_0032.js +types/bigint_literal/migrated_0000.js +types/bigint_literal/migrated_0002.js +types/bigint_literal/migrated_0003.js +types/bigint_literal/migrated_0004.js +types/bigint_literal/migrated_0005.js +types/bigint_literal/migrated_0006.js +types/bigint_literal/migrated_0007.js +types/bigint_literal/migrated_0008.js +types/bigint_literal/migrated_0009.js class_method_kinds/polymorphic_getter.js numbers/underscored_bin.js numbers/underscored_float.js diff --git a/scripts/tests/flow/run_babel_parser_flow_tests.js b/scripts/tests/flow/run_babel_parser_flow_tests.js index 0b75218968fa..ffad821ba1d5 100644 --- a/scripts/tests/flow/run_babel_parser_flow_tests.js +++ b/scripts/tests/flow/run_babel_parser_flow_tests.js @@ -113,7 +113,10 @@ const options = { ["flow", { all: true }], "flowComments", "jsx", + "classProperties", "classPrivateProperties", + "classPrivateMethods", + "bigInt", ], sourceType: "module", ranges: true, @@ -127,6 +130,7 @@ const flowOptionsMapping = { esproposal_nullish_coalescing: "nullishCoalescingOperator", esproposal_optional_chaining: "optionalChaining", types: "flowComments", + intern_comments: false, }; const summary = { @@ -169,10 +173,12 @@ tests.forEach(section => { } return; } - if (!flowOptionsMapping[option]) { + if (!(option in flowOptionsMapping)) { throw new Error("Parser options not mapped " + option); } - babelParserOptions.plugins.push(flowOptionsMapping[option]); + if (flowOptionsMapping[option]) { + babelParserOptions.plugins.push(flowOptionsMapping[option]); + } }); } From a4889a424365f0afc2db5dd2c68713460c189900 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sat, 13 Jul 2019 22:38:23 +0200 Subject: [PATCH 16/28] added flow to directory cmd --- packages/babel-cli/src/babel/dir.js | 16 +++++++++++----- packages/babel-cli/src/babel/options.js | 9 ++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index 2eaca85750f8..ce1b7a3fe5be 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -1,3 +1,5 @@ +// @flow + import defaults from "lodash/defaults"; import outputFileSync from "output-file-sync"; import { sync as mkdirpSync } from "mkdirp"; @@ -6,11 +8,15 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; +import { type cmdOptions } from "./options"; -export default async function({ cliOptions, babelOptions }) { +export default async function({ + cliOptions, + babelOptions, +}: cmdOptions): Promise { const filenames = cliOptions.filenames; - async function write(src, base) { + async function write(src: string, base: string): Promise { let relative = path.relative(base, src); if (!util.isCompilableExtension(relative, cliOptions.extensions)) { @@ -65,14 +71,14 @@ export default async function({ cliOptions, babelOptions }) { } } - function getDest(filename, base) { + function getDest(filename: string, base: string): string { if (cliOptions.relative) { return path.join(base, cliOptions.outDir, filename); } return path.join(cliOptions.outDir, filename); } - async function handleFile(src, base) { + async function handleFile(src: string, base: string): Promise { const written = await write(src, base); if (!written && cliOptions.copyFiles) { @@ -84,7 +90,7 @@ export default async function({ cliOptions, babelOptions }) { return written; } - async function handle(filenameOrDir) { + async function handle(filenameOrDir: string): Promise { if (!fs.existsSync(filenameOrDir)) return 0; const stat = fs.statSync(filenameOrDir); diff --git a/packages/babel-cli/src/babel/options.js b/packages/babel-cli/src/babel/options.js index 41cc0fb83e7f..965177f391c7 100644 --- a/packages/babel-cli/src/babel/options.js +++ b/packages/babel-cli/src/babel/options.js @@ -1,3 +1,5 @@ +// @flow + import fs from "fs"; import commander from "commander"; @@ -151,7 +153,12 @@ commander.option( commander.version(pkg.version + " (@babel/core " + version + ")"); commander.usage("[options] "); -export default function parseArgv(args: Array) { +export type cmdOptions = { + babelOptions: Object, + cliOptions: Object, +}; + +export default function parseArgv(args: Array): cmdOptions { // commander.parse(args); From 01e08c110a17594669dfee632397eb2156959bba Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sat, 13 Jul 2019 23:13:20 +0200 Subject: [PATCH 17/28] added flow to file cmd --- packages/babel-cli/src/babel/file.js | 34 +++++++++++++++++----------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index f0678e27ed39..9d6ceeaaba76 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -6,9 +6,18 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; - -export default async function({ cliOptions, babelOptions }) { - function buildResult(fileResults) { +import { type cmdOptions } from "./options"; + +type compileOutput = { + code: string, + map: Object, +}; + +export default async function({ + cliOptions, + babelOptions, +}: cmdOptions): Promise { + function buildResult(fileResults): compileOutput { const map = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || @@ -74,7 +83,7 @@ export default async function({ cliOptions, babelOptions }) { }; } - function output(fileResults) { + function output(fileResults: Array): void { const result = buildResult(fileResults); if (cliOptions.outFile) { @@ -91,8 +100,8 @@ export default async function({ cliOptions, babelOptions }) { } } - function readStdin() { - return new Promise((resolve, reject) => { + function readStdin(): Promise { + return new Promise((resolve: Function, reject: Function) => { let code = ""; process.stdin.setEncoding("utf8"); @@ -109,7 +118,7 @@ export default async function({ cliOptions, babelOptions }) { }); } - async function stdin() { + async function stdin(): Promise { const code = await readStdin(); const res = await util.transform( @@ -126,7 +135,7 @@ export default async function({ cliOptions, babelOptions }) { output([res]); } - async function walk(filenames) { + async function walk(filenames: Array): Promise { const _filenames = []; filenames.forEach(function(filename) { @@ -151,7 +160,7 @@ export default async function({ cliOptions, babelOptions }) { }); const results = await Promise.all( - _filenames.map(async function(filename) { + _filenames.map(async function(filename): Promise { let sourceFilename = filename; if (cliOptions.outFile) { sourceFilename = path.relative( @@ -168,7 +177,7 @@ export default async function({ cliOptions, babelOptions }) { { sourceFileName: sourceFilename, // Since we're compiling everything to be merged together, - // "inline" applies to the final output file, but to the individual + // "inline" applies to the final output file, but not to the individual // files being concatenated. sourceMaps: babelOptions.sourceMaps === "inline" @@ -184,7 +193,6 @@ export default async function({ cliOptions, babelOptions }) { } console.error(err); - return null; } }), ); @@ -192,7 +200,7 @@ export default async function({ cliOptions, babelOptions }) { output(results); } - async function files(filenames) { + async function files(filenames: Array): Promise { if (!cliOptions.skipInitialBuild) { await walk(filenames); } @@ -208,7 +216,7 @@ export default async function({ cliOptions, babelOptions }) { pollInterval: 10, }, }) - .on("all", function(type, filename) { + .on("all", function(type: string, filename: string) { if (!util.isCompilableExtension(filename, cliOptions.extensions)) { return; } From f569385b641a43242fe696f094302aa600aa6eeb Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sun, 14 Jul 2019 12:01:24 +0200 Subject: [PATCH 18/28] added flow to babel-cli utility file --- packages/babel-cli/src/babel/util.js | 32 +++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/babel-cli/src/babel/util.js b/packages/babel-cli/src/babel/util.js index 99c9ae9b00cd..daacba53f0d2 100644 --- a/packages/babel-cli/src/babel/util.js +++ b/packages/babel-cli/src/babel/util.js @@ -1,10 +1,12 @@ +// @flow + import readdirRecursive from "fs-readdir-recursive"; import * as babel from "@babel/core"; import includes from "lodash/includes"; import path from "path"; import fs from "fs"; -export function chmod(src, dest) { +export function chmod(src: string, dest: string): void { fs.chmodSync(dest, fs.statSync(src).mode); } @@ -13,8 +15,8 @@ type ReaddirFilter = (filename: string) => boolean; export function readdir( dirname: string, includeDotfiles: boolean, - filter: ReaddirFilter, -) { + filter?: ReaddirFilter, +): Array { return readdirRecursive(dirname, (filename, _index, currentDirectory) => { const stat = fs.statSync(path.join(currentDirectory, filename)); @@ -30,7 +32,7 @@ export function readdirForCompilable( dirname: string, includeDotfiles: boolean, altExts?: Array, -) { +): Array { return readdir(dirname, includeDotfiles, function(filename) { return isCompilableExtension(filename, altExts); }); @@ -48,7 +50,7 @@ export function isCompilableExtension( return includes(exts, ext); } -export function addSourceMappingUrl(code, loc) { +export function addSourceMappingUrl(code: string, loc: string): string { return code + "\n//# sourceMappingURL=" + path.basename(loc); } @@ -56,7 +58,11 @@ const CALLER = { name: "@babel/cli", }; -export function transform(filename, code, opts) { +export function transform( + filename: string, + code: string, + opts: Object, +): Promise { opts = { ...opts, caller: CALLER, @@ -71,7 +77,10 @@ export function transform(filename, code, opts) { }); } -export function compile(filename, opts) { +export function compile( + filename: string, + opts: Object | Function, +): Promise { opts = { ...opts, caller: CALLER, @@ -85,7 +94,7 @@ export function compile(filename, opts) { }); } -export function deleteDir(path) { +export function deleteDir(path: string): void { if (fs.existsSync(path)) { fs.readdirSync(path).forEach(function(file) { const curPath = path + "/" + file; @@ -106,7 +115,7 @@ process.on("uncaughtException", function(err) { process.exit(1); }); -export function requireChokidar() { +export function requireChokidar(): Object { try { return require("chokidar"); } catch (err) { @@ -118,7 +127,10 @@ export function requireChokidar() { } } -export function adjustRelative(relative, keepFileExtension) { +export function adjustRelative( + relative: string, + keepFileExtension: boolean, +): string { if (keepFileExtension) { return relative; } From 3a680acd412d4e6fa1d1e1f3146f9bcbaf1be8c9 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sun, 14 Jul 2019 20:33:32 +0200 Subject: [PATCH 19/28] corrected errors in babel-cli file command --- packages/babel-cli/src/babel/dir.js | 4 +-- packages/babel-cli/src/babel/file.js | 42 ++++++++++++++----------- packages/babel-cli/src/babel/options.js | 4 +-- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index ce1b7a3fe5be..32136b78f02a 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -8,12 +8,12 @@ import path from "path"; import fs from "fs"; import * as util from "./util"; -import { type cmdOptions } from "./options"; +import { type CmdOptions } from "./options"; export default async function({ cliOptions, babelOptions, -}: cmdOptions): Promise { +}: CmdOptions): Promise { const filenames = cliOptions.filenames; async function write(src: string, base: string): Promise { diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 9d6ceeaaba76..46cf3d7922fd 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -1,14 +1,18 @@ +// @flow + import convertSourceMap from "convert-source-map"; import defaults from "lodash/defaults"; +import isString from "lodash/isString"; +import toString from "lodash/toString"; import sourceMap from "source-map"; 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"; -type compileOutput = { +type CompilationOutput = { code: string, map: Object, }; @@ -16,9 +20,9 @@ type compileOutput = { export default async function({ cliOptions, babelOptions, -}: cmdOptions): Promise { - function buildResult(fileResults): compileOutput { - const map = new sourceMap.SourceMapGenerator({ +}: CmdOptions): Promise { + function buildResult(fileResults: Array): CompilationOutput { + const map: Object = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || path.basename(cliOptions.outFile || "") || @@ -101,21 +105,23 @@ export default async function({ } function readStdin(): Promise { - return new Promise((resolve: Function, reject: Function) => { - let code = ""; + return new Promise( + (resolve: Function, reject: Function): void => { + let code = ""; - process.stdin.setEncoding("utf8"); + process.stdin.setEncoding("utf8"); - process.stdin.on("readable", function() { - const chunk = process.stdin.read(); - if (chunk !== null) code += chunk; - }); + process.stdin.on("readable", function() { + const chunk = process.stdin.read(); + if (isString(chunk)) code += toString(chunk); + }); - process.stdin.on("end", function() { - resolve(code); - }); - process.stdin.on("error", reject); - }); + process.stdin.on("end", function() { + resolve(code); + }); + process.stdin.on("error", reject); + }, + ); } async function stdin(): Promise { @@ -160,7 +166,7 @@ export default async function({ }); const results = await Promise.all( - _filenames.map(async function(filename): 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/options.js b/packages/babel-cli/src/babel/options.js index 965177f391c7..19ae7bfce878 100644 --- a/packages/babel-cli/src/babel/options.js +++ b/packages/babel-cli/src/babel/options.js @@ -153,12 +153,12 @@ commander.option( commander.version(pkg.version + " (@babel/core " + version + ")"); commander.usage("[options] "); -export type cmdOptions = { +export type CmdOptions = { babelOptions: Object, cliOptions: Object, }; -export default function parseArgv(args: Array): cmdOptions { +export default function parseArgv(args: Array): CmdOptions { // commander.parse(args); From 859bdafe41563289c5c5a790a2fc96400569f786 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Sun, 14 Jul 2019 20:46:38 +0200 Subject: [PATCH 20/28] added flow to babel-cli babel-external-helpers --- packages/babel-cli/src/babel-external-helpers.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel-external-helpers.js b/packages/babel-cli/src/babel-external-helpers.js index c2560dfa6f37..e2a1a0d3429f 100755 --- a/packages/babel-cli/src/babel-external-helpers.js +++ b/packages/babel-cli/src/babel-external-helpers.js @@ -1,9 +1,15 @@ +// @flow + import commander from "commander"; +import isString from "lodash/isString"; import { buildExternalHelpers } from "@babel/core"; -function collect(value, previousValue): Array { +function collect( + value: String | any, + previousValue: Array, +): Array { // If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing. - if (typeof value !== "string") return previousValue; + if (!isString(value)) return previousValue; const values = value.split(","); From ce1fc7b3f5292626c79b3d8a933dfc1677ee675d Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Mon, 15 Jul 2019 07:09:44 +0200 Subject: [PATCH 21/28] added flow to babel-cli babel-external-helpers. Iteration #2 --- packages/babel-cli/src/babel-external-helpers.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/babel-cli/src/babel-external-helpers.js b/packages/babel-cli/src/babel-external-helpers.js index e2a1a0d3429f..fef536187c41 100755 --- a/packages/babel-cli/src/babel-external-helpers.js +++ b/packages/babel-cli/src/babel-external-helpers.js @@ -1,15 +1,14 @@ // @flow import commander from "commander"; -import isString from "lodash/isString"; import { buildExternalHelpers } from "@babel/core"; function collect( - value: String | any, + value: string | any, previousValue: Array, ): Array { // If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing. - if (!isString(value)) return previousValue; + if (typeof value !== "string") return previousValue; const values = value.split(","); From ad6454d67e9d8d0092f4511b719c03fb5ca46571 Mon Sep 17 00:00:00 2001 From: Letladi Date: Mon, 15 Jul 2019 15:23:17 +0200 Subject: [PATCH 22/28] changed check inside readStdin inside babel file command to accomodate possibility of chunk being a Buffer --- packages/babel-cli/src/babel/file.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 46cf3d7922fd..7f2533960dbf 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -2,7 +2,6 @@ import convertSourceMap from "convert-source-map"; import defaults from "lodash/defaults"; -import isString from "lodash/isString"; import toString from "lodash/toString"; import sourceMap from "source-map"; import slash from "slash"; @@ -113,7 +112,7 @@ export default async function({ process.stdin.on("readable", function() { const chunk = process.stdin.read(); - if (isString(chunk)) code += toString(chunk); + if (chunk !== null) code += toString(chunk); }); process.stdin.on("end", function() { From daaae15bc1129fbf00f60462c91721320c9055c4 Mon Sep 17 00:00:00 2001 From: Letladi Date: Mon, 15 Jul 2019 15:37:47 +0200 Subject: [PATCH 23/28] removed toString method inside readStdin of babel-cli file command --- packages/babel-cli/src/babel/file.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 7f2533960dbf..4f231f5344a9 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -2,7 +2,6 @@ import convertSourceMap from "convert-source-map"; import defaults from "lodash/defaults"; -import toString from "lodash/toString"; import sourceMap from "source-map"; import slash from "slash"; import path from "path"; @@ -112,7 +111,8 @@ export default async function({ process.stdin.on("readable", function() { const chunk = process.stdin.read(); - if (chunk !== null) code += toString(chunk); + // $FlowIgnore + if (chunk !== null) code += chunk; }); process.stdin.on("end", function() { From 588ec1a8476f458aa0c8eb408067795f617ed9ca Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Mon, 15 Jul 2019 20:06:35 +0200 Subject: [PATCH 24/28] fixed file command to only include flow changes --- packages/babel-cli/src/babel/file.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 4f231f5344a9..74f90aa18c73 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -198,6 +198,7 @@ export default async function({ } console.error(err); + return null; } }), ); From 14859a34f63e40ba45e0f56c1ccc9522341fe175 Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Tue, 16 Jul 2019 05:33:02 +0200 Subject: [PATCH 25/28] added SourceMapGenerator as an arg type to fromObject --- lib/third-party-libs.js.flow | 4 ++-- packages/babel-cli/src/babel/file.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index c21a2797acfe..884b1e68bbfb 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -165,7 +165,7 @@ declare module "source-map" { } declare module "convert-source-map" { - import type { SourceMap } from "source-map"; + import type { SourceMap, SourceMapGenerator } from "source-map"; declare class Converter { toJSON(): string; @@ -177,7 +177,7 @@ declare module "convert-source-map" { declare module.exports: { SourceMap: SourceMap, Converter: Converter, - fromObject(obj: SourceMap): Converter, + fromObject(obj: SourceMap | SourceMapGenerator): Converter, fromJSON(str: string): Converter, fromBase64(str: string): Converter, fromComment(str: string): Converter, diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 74f90aa18c73..14cf4c562971 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -20,7 +20,7 @@ export default async function({ babelOptions, }: CmdOptions): Promise { function buildResult(fileResults: Array): CompilationOutput { - const map: Object = new sourceMap.SourceMapGenerator({ + const map = new sourceMap.SourceMapGenerator({ file: cliOptions.sourceMapTarget || path.basename(cliOptions.outFile || "") || From 6de54ccc134deeb17dbd10c1526b556afb0d667f Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Tue, 16 Jul 2019 07:26:06 +0200 Subject: [PATCH 26/28] reverted to only using SourceMap inside fromObject method --- lib/third-party-libs.js.flow | 4 ++-- packages/babel-cli/src/babel/file.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/third-party-libs.js.flow b/lib/third-party-libs.js.flow index 884b1e68bbfb..a4eed0a1da6c 100644 --- a/lib/third-party-libs.js.flow +++ b/lib/third-party-libs.js.flow @@ -165,7 +165,7 @@ declare module "source-map" { } declare module "convert-source-map" { - import type { SourceMap, SourceMapGenerator } from "source-map"; + import type { SourceMap } from "source-map"; declare class Converter { toJSON(): string; @@ -177,7 +177,7 @@ declare module "convert-source-map" { declare module.exports: { SourceMap: SourceMap, Converter: Converter, - fromObject(obj: SourceMap | SourceMapGenerator): Converter, + fromObject(obj: SourceMap ): Converter, fromJSON(str: string): Converter, fromBase64(str: string): Converter, fromComment(str: string): Converter, diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 14cf4c562971..8ed5e5263fd6 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -76,7 +76,7 @@ export default async function({ babelOptions.sourceMaps === "inline" || (!cliOptions.outFile && babelOptions.sourceMaps) ) { - code += "\n" + convertSourceMap.fromObject(map).toComment(); + code += "\n" + convertSourceMap.fromObject(map.toJSON()).toComment(); } return { From ce351a1928233b8b4b9d93d896bef7882c56966b Mon Sep 17 00:00:00 2001 From: Letladi Sebesho Date: Thu, 18 Jul 2019 20:35:46 +0200 Subject: [PATCH 27/28] added null promise return type to method in file command --- packages/babel-cli/src/babel/file.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 8ed5e5263fd6..3e281471cba1 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -165,7 +165,9 @@ export default async function({ }); const results = await Promise.all( - _filenames.map(async function(filename: string): Promise { + _filenames.map(async function( + filename: string, + ): Promise | Promise { let sourceFilename = filename; if (cliOptions.outFile) { sourceFilename = path.relative( From 3c31947f2fa07dd8c4c65c3bb2a71d41b8abf72a Mon Sep 17 00:00:00 2001 From: Letladi Date: Fri, 19 Jul 2019 11:04:40 +0200 Subject: [PATCH 28/28] changed walk file Promise call to only an object promise --- packages/babel-cli/src/babel/file.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 3e281471cba1..8ed5e5263fd6 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -165,9 +165,7 @@ export default async function({ }); const results = await Promise.all( - _filenames.map(async function( - filename: string, - ): Promise | Promise { + _filenames.map(async function(filename: string): Promise { let sourceFilename = filename; if (cliOptions.outFile) { sourceFilename = path.relative(