Skip to content
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

Lambda functions in short-circuits throw syntax-errors at build time #1757

Closed
joelgriffith opened this issue Jun 15, 2015 · 2 comments
Closed
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@joelgriffith
Copy link

let percentFormatter = this.props.percentFormatter || (val) => {
  return Math.floor(val * 100) + '%';
};

This chunk of code will throw an error on build (I'm using gulp, browserify, and gulp-babel):

SyntaxError: myFile.jsx: Unexpected token (50:1)
  48 |  }
  49 |
> 50 |  render() {
     |  ^
  51 |      let percentDelivered = this.percentFormatter(this.props.numerator/ this.props.denominator);
  52 |      let percentIntoFlight = this.percentFormatter(this.props.percentComplete);
  53 |
    at Parser.pp.raise (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/location.js:73:13)
    at Parser.pp.unexpected (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/parseutil.js:87:8)
    at Parser.pp.expect (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/parseutil.js:81:26)
    at Parser.pp.parseObj (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/expression.js:555:12)
    at Parser.pp.parseExprAtom (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/expression.js:369:19)
    at Parser.parseExprAtom (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/node_modules/acorn-jsx/inject.js:624:24)
    at Parser.pp.parseExprSubscripts (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/expression.js:217:19)
    at Parser.pp.parseMaybeUnary (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/expression.js:199:19)
    at Parser.pp.parseExprOps (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/expression.js:155:19)
    at Parser.pp.parseMaybeConditional (/Users/jgriffith/Projects/anx-react/node_modules/babelify/node_modules/babel-core/lib/acorn/src/expression.js:138:19)

Could be an issue in babelify, but wanted to check here to see if this is an issue anyone else has encountered, not seeing it in any previous issues.

I'm fairly certain that this is syntax is valid, but this may not be possible. Handing it an anonymous function works at build time, so I imagine lambda functions would work as well.

@joelgriffith joelgriffith changed the title Lambda functions in short-circuits throw type-errors at build-time Lambda functions in short-circuits throw syntax-errors at build time Jun 15, 2015
@fkling
Copy link
Contributor

fkling commented Jun 15, 2015

|| has a higher precedence than =>, i.e. this is evaluated as

(this.props.percentFormatter || (val)) => {...}

which is clearly an error.

Use the grouping operator:

this.props.percentFormatter || ((val) => {...})

Source: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-expressions

@joelgriffith
Copy link
Author

TIL!

Very good, thanks for the info.

@sebmck sebmck added the invalid label Jun 15, 2015
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 16, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants