Handle default parameters with flow types
See original GitHub issueInput:
const myObj = {
method: function (foo: number = 10) {
return foo;
},
};
Tranform:
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const printOptions = options.printOptions || {quote: 'single'};
const root = j(file.source);
const canBeSimplified = (key, value) => {
return key.type === 'Identifier' && value.type === 'FunctionExpression';
};
root
.find(j.Property)
.filter(p => canBeSimplified(p.value.key, p.value.value))
.forEach(p => {
p.value.method = true;
});
return root.toSource(printOptions);
};
Current (broken) Output:
const myObj = {
method(= 10) {
return foo;
},
};
Expected output:
const myObj = {
method(foo: number = 10) {
return foo;
},
};
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to set a default value for a Flow type? - Stack Overflow
I want the type parameter to default as 1 if no value is present. However, Flow is complaining with Unexpected token = ....
Read more >Function Types | Flow
Functions have two places where types are applied: Parameters (input) and the return value (output). // @flow function concat(a: string, b: string): string...
Read more >Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Read more >JavaScript Type Checking With Flow | by John Au-Yeung
We can add optional parameters by adding a question mark after the parameter name. ... Then, we can pass in undefined , nothing,...
Read more >Parameterizing mapping data flows - Azure - Microsoft Learn
When assigning a pipeline expression parameter of type string, by default quotes will be added and the value will be evaluated as a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, you can see what I’m working on at https://github.com/cpojer/js-codemod/pull/52
I’m trying to get it accurate enough to run at Facebook.
lol what! I encountered this issue too and mentioned it in the babel slack - made https://github.com/babel/babylon/issues/67
Oh probably since we are making the same thing! (except with babel/jscodeshift)