babel-plugin-contracts icon indicating copy to clipboard operation
babel-plugin-contracts copied to clipboard

Unexpected token (2:9)

Open ghost opened this issue 6 years ago • 2 comments

Hello,

I used to use contracts in Racket of all languages, so glad to see something similar exists for JS!

Anyway, every time I try to compile anything with the contracts plugin, I get Unexpected token (2:9). It doesn't matter what is at position 2:9, even comments will trigger the same error.

.babelrc

{
  "plugins": [
    ["contracts", {
      "names": {
        "assert": "assert",
        "precondition": "pre",
        "postcondition": "post",
        "invariant": "invariant",
        "return": "it",
        "old": "old"
      }
    }]
  ]
}

test.js

const add1 = n => {
	pre: typeof n === 'number';
	return n + 1
}

babel --version

6.26.0 (babel-core 6.26.3)

Do I need babel 7 for this? 6.26.3 is the latest version in npm.

https://www.npmjs.com/package/babel-core

ghost avatar May 07 '19 07:05 ghost

@LewisAndrewCampbell yes please test with babel 7, if it doesn't work let me know.

phpnode avatar May 13 '19 10:05 phpnode

Hi,

No luck with 7 either. Can I get a minimal working example?

$ cat test.js 
const add1 = n => {
	
	pre: typeof n === 'number';
	return n + 1
}
$ cat .babelrc 
{
  "plugins": [
    ["@babel/plugin-transform-react-jsx", { "pragma":"h" }],
    ["contracts", {
      "env": {
        "production": {
          "strip": true
        }
      },
      "names": {
        "assert": "assert",
        "precondition": "pre",
        "postcondition": "post",
        "invariant": "invariant",
        "return": "it",
        "old": "old"
      }
    }]
  ]
}
$ babel --version
7.4.4 (@babel/core 7.2.2)
$ babel test.js
{ SyntaxError: Unexpected token (3:9)
  1 | /* @babel/template */;
  2 | 
> 3 |     if (!%%condition%%) {
    |         ^
  4 |       throw new Error(%%message%%);
  5 |     }
  6 |   
    at Parser.raise (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:4051:15)
    at Parser.unexpected (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:5382:16)
    at Parser.parseExprAtom (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:6541:20)
    at Parser.parseExprSubscripts (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:6104:21)
    at Parser.parseMaybeUnary (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:6083:21)
    at Parser.parseMaybeUnary (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:6060:30)
    at Parser.parseExprOps (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:5968:21)
    at Parser.parseMaybeConditional (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:5940:21)
    at Parser.parseMaybeAssign (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:5887:21)
    at Parser.parseExpression (/home/lewis/.config/yarn/global/node_modules/@babel/parser/lib/index.js:5840:21)
    =============
    at _default (/home/lewis/code/lingo-engine/node_modules/babel-plugin-contracts/lib/index.js:36:15)
    at loadDescriptor (/home/lewis/.config/yarn/global/node_modules/@babel/core/lib/config/full.js:165:14)
    at cachedFunction (/home/lewis/.config/yarn/global/node_modules/@babel/core/lib/config/caching.js:33:19)
    at loadPluginDescriptor (/home/lewis/.config/yarn/global/node_modules/@babel/core/lib/config/full.js:200:28)
    at config.plugins.reduce (/home/lewis/.config/yarn/global/node_modules/@babel/core/lib/config/full.js:69:20)
    at Array.reduce (<anonymous>)
    at recurseDescriptors (/home/lewis/.config/yarn/global/node_modules/@babel/core/lib/config/full.js:67:38)
    at loadFullConfig (/home/lewis/.config/yarn/global/node_modules/@babel/core/lib/config/full.js:108:6)
  pos: 33,
  loc: Position { line: 3, column: 9 },
  code: 'BABEL_TEMPLATE_PARSE_ERROR' }

ghost avatar May 19 '19 03:05 ghost