question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle default parameters with flow types

See original GitHub issue

Input:

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:closed
  • Created 7 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ForbesLindesaycommented, Jul 6, 2016

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.

1reaction
hzoocommented, Jul 6, 2016

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)

screen shot 2016-07-06 at 10 48 58 am
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found