diff --git a/lib/Parser.js b/lib/Parser.js index a5b444eeb8f..3df20ce7a72 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -2238,6 +2238,8 @@ class Parser extends Tapable { if (type === "auto") { parserOptions.sourceType = "module"; + } else if (parserOptions.sourceType === "script") { + parserOptions.allowReturnOutsideFunction = true; } let ast; @@ -2252,6 +2254,7 @@ class Parser extends Tapable { if (threw && type === "auto") { parserOptions.sourceType = "script"; + parserOptions.allowReturnOutsideFunction = true; if (Array.isArray(parserOptions.onComment)) { parserOptions.onComment.length = 0; } diff --git a/test/fixtures/b.js b/test/fixtures/b.js index 0900b0b4558..7e6505aa822 100644 --- a/test/fixtures/b.js +++ b/test/fixtures/b.js @@ -1,3 +1,6 @@ module.exports = function b() { return "This is b"; -}; \ No newline at end of file +}; + +// Test CJS top-level return +return;