Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SyntaxError: Illegal return statement #4

Merged
merged 1 commit into from
Dec 17, 2019

Conversation

gitim
Copy link

@gitim gitim commented Dec 16, 2019

Faced with SyntaxError in this package, because some package in my project depend from this package.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a syntax error whatsoever; this is a node module and top level return is allowed in it.

What bundler are you using that breaks on valid node modules?

@ljharb ljharb added wontfix This will not be worked on invalid This doesn't seem right labels Dec 16, 2019
@Jessidhia
Copy link

Jessidhia commented Dec 17, 2019

Babel doesn't allow top level return by default; if you use babel-loader to process your dependencies (as recommended by babel(!) for @babel/preset-env) you're going to get this error.

> require('@babel/parser').parse(require('fs').readFileSync(require.resolve('is-map'), 'utf8'))
Thrown:
[SyntaxError: 'return' outside of function (12:1)
] {
  pos: 315,
  loc: Position { line: 12, column: 1 }
}

Top level return is a specific quirk of commonjs modules because of the function wrapper, not actually something that is allowed by script or module (or unambiguous) parse goals, so that on its own doesn't enable parsing it.

This is an example of the whole error stack when building storybook:

ERR! => Failed to build the preview
ERR! ../node_modules/is-map/index.js
ERR! Module build failed (from ../node_modules/thread-loader/dist/cjs.js):
ERR! Thread Loader (Worker 0)
ERR! /builds/afQUGiFh/0/node_modules/is-map/index.js: 'return' outside of function (12:1)
ERR! 
ERR!   10 | 		return false;
ERR!   11 | 	};
ERR! > 12 | 	return;
ERR!      | 	^
ERR!   13 | }
ERR!   14 | 
ERR!   15 | var $mapHas = $Map ? Map.prototype.has : null;
ERR!     at PoolWorker.fromErrorObj (/builds/afQUGiFh/0/node_modules/thread-loader/dist/WorkerPool.js:262:12)
ERR!     at /builds/afQUGiFh/0/node_modules/thread-loader/dist/WorkerPool.js:204:29
ERR!     at mapSeries (/builds/afQUGiFh/0/node_modules/neo-async/async.js:3625:14)
ERR!     at PoolWorker.onWorkerMessage (/builds/afQUGiFh/0/node_modules/thread-loader/dist/WorkerPool.js:170:35)
ERR!     at /builds/afQUGiFh/0/node_modules/thread-loader/dist/WorkerPool.js:152:14
ERR!     at Socket.onChunk (/builds/afQUGiFh/0/node_modules/thread-loader/dist/readBuffer.js:36:9)
ERR!     at Socket.emit (events.js:210:5)
ERR!     at Socket.Readable.read (_stream_readable.js:546:10)
ERR!     at Object.raise (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:6983:17)
ERR!     at Object.parseReturnStatement (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11027:12)
ERR!     at Object.parseStatementContent (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10714:21)
ERR!     at Object.parseStatement (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10666:17)
ERR!     at Object.parseBlockOrModuleBlockBody (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11242:25)
ERR!     at Object.parseBlockBody (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11229:10)
ERR!     at Object.parseBlock (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11213:10)
ERR!     at Object.parseStatementContent (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10742:21)
ERR!     at Object.parseStatement (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10666:17)
ERR!     at Object.parseIfStatement (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11020:28)
ERR!     at Object.parseStatementContent (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10711:21)
ERR!     at Object.parseStatement (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10666:17)
ERR!     at Object.parseBlockOrModuleBlockBody (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11242:25)
ERR!     at Object.parseBlockBody (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:11229:10)
ERR!     at Object.parseTopLevel (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:10597:10)
ERR!     at Object.parse (/builds/afQUGiFh/0/node_modules/@babel/parser/lib/index.js:12107:10)
ERR!  @ ../node_modules/es-get-iterator/index.js 145:16-33
ERR!  @ ../node_modules/iterate-value/index.js
ERR!  @ ../node_modules/promise.allsettled/implementation.js
ERR!  @ ../node_modules/promise.allsettled/polyfill.js
ERR!  @ ../node_modules/promise.allsettled/shim.js
ERR!  @ ../node_modules/promise.allsettled/auto.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es2020.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es2019.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es2018.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es2017.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es2016.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es2015.js
ERR!  @ ../node_modules/airbnb-js-shims/target/es5.js
ERR!  @ ../node_modules/airbnb-js-shims/index.js
ERR!  @ ../node_modules/@storybook/core/dist/server/common/polyfills.js
ERR!  @ multi ../node_modules/@storybook/core/dist/server/common/polyfills.js ../node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/config.js

@ljharb
Copy link
Member

ljharb commented Dec 17, 2019

It's highly unsafe to transpile code you didn't author; is this perhaps because you're trying to process third party code as if it's an ESM Module, instead of a CJS Script? If so, that will break things as well by forcing strict mode.

It's certainly something only allowed because of CJS's function wrapper, but it has always had the function wrapper and that's what makes it valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right wontfix This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants