google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.17k stars 578 forks source link

jsx: Unexpected token `this` #2092

Closed johnjbarton closed 8 years ago

johnjbarton commented 8 years ago

I tried an example from https://facebook.github.io/react/docs/reusable-components.html#es6-classes

export class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {count: props.initialCount};
  }
  tick() {
    this.setState({count: this.state.count + 1});
  }
  render() {
    return (
      <div onClick={this.tick.bind(this)}>
        Clicks: {this.state.count}  // <<< Unexpected token this
      </div>
    );
  }
}
Counter.propTypes = { initialCount: React.PropTypes.number };
Counter.defaultProps = { initialCount: 0 };

But the fails on Unexpected token this. To be sure I don't know JSX. Any hints?

arv commented 8 years ago

This is a bug in the parser. The way it works now is that the scanner has scanJsx/peekJs and that ends up treating this as a JsxIdentifier which leads to this bug. I think I should refactor the parser for jsx to work more like how we parse template strings with. The downside is that it introduces "a lot" of new AST nodes:

>text< >text{ }text{ }text<