natefaubion / sparkler

Native pattern matching for JavaScript
MIT License
694 stars 18 forks source link

Parse error with `match` in functions saved to constant variables #17

Closed Havvy closed 9 years ago

Havvy commented 9 years ago

This works:

var f = function () {
    match (num1, num2, num3) {
      case (Number, Number, Number):
        allNums = true;
      default:
        allNums = false;
    }
};

This fails:

const f = function () {
    match (num1, num2, num3) {
      case (Number, Number, Number):
        allNums = true;
      default:
        allNums = false;
    }
};

The error message:

/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:5213
            throw e;
                  ^
Error: Line 2: Unexpected token {
[... , num3 ) { case ( ...]
    at throwError (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:1887:21)
    at throwUnexpected (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:1939:9)
    at consumeSemicolon (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:1994:13)
    at parseStatement (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:3376:9)
    at /home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:4316:38
    at parseSourceElement (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:3790:20)
    at parseFunctionSourceElements (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:3426:29)
    at /home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:4316:38
    at parseConciseBody (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:3382:20)
    at parseFunctionExpression (/home/havvy/sweetjs/playground/node_modules/sweet.js/lib/parser.js:3611:16)

The only difference between working and not working is the var token changed to a const token. There's no other macro involved, and the code parses if the match statement is removed. The behavior seems to also ignore depth between the const and the match. E.g., the following also fails.

const f = function () {
    var g = function () {
        match (num1, num2, num3) {
          case (Number, Number, Number):
            allNums = true;
          default:
            allNums = false;
        }
    }
}
natefaubion commented 9 years ago

This is a sweet.js bug with let and const, so I'm going to close and move it over there.

https://github.com/mozilla/sweet.js/issues/410