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
Conversation
import { buildExternalHelpers } from "@babel/core"; | ||
|
||
function collect(value, previousValue): Array<string> { | ||
function collect( | ||
value: String | any, |
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
// 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't 🙈
Will fix.
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/11155/ |
packages/babel-cli/src/babel/file.js
Outdated
}); | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need isString
and toString
here.
Note that chunk
could also be a Buffer
(docs), so isString(chunk)
is not equivalent to chunk !== null
.
code += chunk
will implicitly convert chunk
to string since code
is 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.
I can remove isString
, but without toString
flow will throw an error because it sees Buffer
being incompatible with 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.
We can // $FlowIgnore
it
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.
Alright.
packages/babel-cli/src/babel/file.js
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest leave return null
here so that we can make this PR focus on adding flow type annotation without changing any internal logic.
Note that you can use union type to specify this function returns either a Promise<Object>
or null
.
_filenames.map(async function(filename: string): Promise<Object> | null {
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.
Good point. I'll make the fix.
packages/babel-cli/src/babel/file.js
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't flow already infer this type to the SourceMapGenerator
class? (defined at
babel/lib/third-party-libs.js.flow
Line 147 in 7dc5fdb
declare class SourceMapGenerator { |
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.
It does, but the fromObject
method expects a SourceMap
but we pass it SourceMapGenerator
and so causes errors, so maybe I should add SourceMap | SourceMapGenerator
to that module declaration?
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.
I'm thinking something like this fromObject(obj: SourceMap | SourceMapGenerator)
. This works without errors.
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.
I think that fromObject(obj: SourceMap)
was correct, and it isn't intended to be called with a SourceMapGenerator
object.
It should be .fromObject(map.toJSON())
.
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.
Alright. I will make that change.
… add-flow-to-babel-cli
@letladi Could you rebase your branch? Test fails because we have moved our fork of lerna into babel organization. |
@JLHwung, will do. |
* removed duplicate tests for @babel/highlight getChalk method * changed second 'getChalk' test case for when colors are not supported
* 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
…e possibility of chunk being a Buffer
… add-flow-to-babel-cli
Sorry, I'm still working on the rebase (haven't done this before) |
I'm closing this. I will send in a new pull request shortly. |
I suggest squashing this commits before rebasing; it makes it easier to do. |
I actually deleted everything. forked the repo again and added my changes; #10244 |
added flow to the babel-cli package code.