babel: "'return' outside of function" errors in babel 5.x

Create a script test.js:

'use strict';

if (true) {
  console.log('true');
  return;
}

Run it with:

$ babel-node test

At version 4.x it works just fine and outputs true. At version 5.1.10 you get this error:

/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/babel/helpers/parse.js:70
    throw err;
          ^
SyntaxError: /Users/maximilianschmitt/Desktop/test.js: 'return' outside of function (5:2)
  3 | if (true) {
  4 |   console.log('true');
> 5 |   return;
    |   ^
  6 | }
  7 |
    at Parser.pp.raise (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/location.js:68:13)
    at Parser.pp.parseReturnStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/statement.js:247:74)
    at Parser.pp.parseStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/statement.js:77:19)
    at Parser.parseStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/plugins/flow.js:631:22)
    at Parser.pp.parseBlock (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/statement.js:387:21)
    at Parser.pp.parseStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/statement.js:93:19)
    at Parser.parseStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/plugins/flow.js:631:22)
    at Parser.pp.parseIfStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/statement.js:241:26)
    at Parser.pp.parseStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/src/statement.js:75:19)
    at Parser.parseStatement (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/acorn/plugins/flow.js:631:22)

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 19 (13 by maintainers)

Most upvoted comments

This issue was created for Babel 5 which isn’t supported anymore. If you need it babylon has a option for it https://github.com/babel/babylon#options but doesn’t mean we support the transform just the parsing.