New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added flow to babel cli #10219
Added flow to babel cli #10219
Changes from 5 commits
d7714ad
a0ec5e4
a254447
af50714
a122deb
a16b3d4
3d484d3
de384ff
dbb53b4
2cd51e2
3f759bc
d97a1f7
6667330
5e567d9
c67e611
6d2f111
a4889a4
01e08c1
f569385
3a680ac
859bdaf
ce1fc7b
ad6454d
daaae15
588ec1a
14859a3
6de54cc
ce351a1
e20ce35
3c31947
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
// @flow | ||
|
||
import commander from "commander"; | ||
import isString from "lodash/isString"; | ||
import { buildExternalHelpers } from "@babel/core"; | ||
|
||
function collect(value, previousValue): Array<string> { | ||
function collect( | ||
value: String | any, | ||
previousValue: Array<string>, | ||
): Array<string> { | ||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It isn't 🙈 |
||
|
||
const values = value.split(","); | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,15 +1,28 @@ | ||||
// @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"; | ||||
|
||||
export default async function({ cliOptions, babelOptions }) { | ||||
function buildResult(fileResults) { | ||||
const map = new sourceMap.SourceMapGenerator({ | ||||
import { type CmdOptions } from "./options"; | ||||
|
||||
type CompilationOutput = { | ||||
code: string, | ||||
map: Object, | ||||
}; | ||||
|
||||
export default async function({ | ||||
cliOptions, | ||||
babelOptions, | ||||
}: CmdOptions): Promise<void> { | ||||
function buildResult(fileResults: Array<Object>): CompilationOutput { | ||||
const map: Object = new sourceMap.SourceMapGenerator({ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't flow already infer this type to the babel/lib/third-party-libs.js.flow Line 147 in 7dc5fdb
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does, but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking something like this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright. I will make that change. |
||||
file: | ||||
cliOptions.sourceMapTarget || | ||||
path.basename(cliOptions.outFile || "") || | ||||
|
@@ -74,7 +87,7 @@ export default async function({ cliOptions, babelOptions }) { | |||
}; | ||||
} | ||||
|
||||
function output(fileResults) { | ||||
function output(fileResults: Array<string>): void { | ||||
const result = buildResult(fileResults); | ||||
|
||||
if (cliOptions.outFile) { | ||||
|
@@ -91,25 +104,27 @@ export default async function({ cliOptions, babelOptions }) { | |||
} | ||||
} | ||||
|
||||
function readStdin() { | ||||
return new Promise((resolve, reject) => { | ||||
let code = ""; | ||||
function readStdin(): Promise<string> { | ||||
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); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need Note that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright. |
||||
}); | ||||
|
||||
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() { | ||||
async function stdin(): Promise<void> { | ||||
const code = await readStdin(); | ||||
|
||||
const res = await util.transform( | ||||
|
@@ -126,7 +141,7 @@ export default async function({ cliOptions, babelOptions }) { | |||
output([res]); | ||||
} | ||||
|
||||
async function walk(filenames) { | ||||
async function walk(filenames: Array<string>): Promise<void> { | ||||
const _filenames = []; | ||||
|
||||
filenames.forEach(function(filename) { | ||||
|
@@ -151,7 +166,7 @@ export default async function({ cliOptions, babelOptions }) { | |||
}); | ||||
|
||||
const results = await Promise.all( | ||||
_filenames.map(async function(filename) { | ||||
_filenames.map(async function(filename: string): Promise<Object> { | ||||
let sourceFilename = filename; | ||||
if (cliOptions.outFile) { | ||||
sourceFilename = path.relative( | ||||
|
@@ -168,7 +183,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,15 +199,14 @@ export default async function({ cliOptions, babelOptions }) { | |||
} | ||||
|
||||
console.error(err); | ||||
return null; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest leave Note that you can use union type to specify this function returns either a _filenames.map(async function(filename: string): Promise<Object> | null { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I'll make the fix. |
||||
} | ||||
}), | ||||
); | ||||
|
||||
output(results); | ||||
} | ||||
|
||||
async function files(filenames) { | ||||
async function files(filenames: Array<string>): Promise<void> { | ||||
if (!cliOptions.skipInitialBuild) { | ||||
await walk(filenames); | ||||
} | ||||
|
@@ -208,7 +222,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; | ||||
} | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a string object, it's a
string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes. Sorry, that is my mistake. Will fix