Unexpected 'Unexpected Token' error in apparently correct file


Using the latest version of Acorn (5.1.2, and also previous v4) we’re encountering a very odd problem.
For what appears to be a valid JS file, we get an “Unexpected Token” error pointing at the last line of the file.
It is always on the last line

There are no odd hex characters at the end, or other unusual line endings.

The odd thing is that this is not a reliable problem. If I try to reproduce this outside of a full system test then there is no problem. Adding extra debug lines can make the problem go away. Even with the system test, there’s about a 90% chance of reproduction.

I’ve confirmed (printint in hex) that the input is the same to Acorn each time.

Based on past experience, in same the Java world, this would indicate to me a timing or threading issues. But this is in Node (version independant it would seem).

Any thoughts on how to debug this further would be appreciated.



A stack trace might give you an idea of what the parser is expecting.


I’ve managed to get this stack trace by instrumenting the Acorn.js code.

      at Parser.pp$3.parseExprAtom (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:2097:10)
  at Parser.pp$3.parseExprSubscripts (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:1957:19)
  at Parser.pp$3.parseMaybeUnary (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:1934:17)
  at Parser.pp$3.parseExprOps (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:1876:19)
  at Parser.pp$3.parseMaybeConditional (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:1859:19)
  at Parser.pp$3.parseMaybeAssign (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:1834:19)
  at Parser.pp$3.parseExpression (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:1805:19)
  at Parser.pp$1.parseStatement (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:799:45)
  at Parser.pp$1.parseTopLevel (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:692:23)
  at Parser.parse (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:543:15)
  at Object.parse (/home/matthew/github/composer/node_modules/acorn/dist/acorn.js:3672:37)

In addition, I managed to get this as the Token that was triggering the issue.

TokenType {
  label: 'eof',
  keyword: undefined,
  beforeExpr: false,
  startsExpr: false,
  isLoop: false,
  isAssign: false,
  prefix: false,
  postfix: false,
  binop: null,
  updateContext: null }

I’ve just dumped the entire Parser object as well… will see if there’s anything obvious in that - any specific part might be worth looking at?