diff --git a/packages/babel-parser/src/util/scope.js b/packages/babel-parser/src/util/scope.js index a9cf9d89ed..759da8f1ce 100644 --- a/packages/babel-parser/src/util/scope.js +++ b/packages/babel-parser/src/util/scope.js @@ -139,10 +139,13 @@ export default class ScopeHandler { } checkLocalExport(id: N.Identifier) { - // scope.functions must be empty as Module code is always strict. if ( this.scopeStack[0].lexical.indexOf(id.name) === -1 && - this.scopeStack[0].var.indexOf(id.name) === -1 + this.scopeStack[0].var.indexOf(id.name) === -1 && + // In strict mode, scope.functions will always be empty. + // Modules are strict by default, but the `scriptMode` option + // can overwrite this behavior. + this.scopeStack[0].functions.indexOf(id.name) === -1 ) { this.undefinedExports.set(id.name, id.start); } diff --git a/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/input.js b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/input.js new file mode 100644 index 0000000000..87d3720d82 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/input.js @@ -0,0 +1,3 @@ +function a() {} + +export { a }; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/options.json b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/options.json new file mode 100644 index 0000000000..0580fdad26 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/options.json @@ -0,0 +1,4 @@ +{ + "sourceType": "module", + "strictMode": false +} diff --git a/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/output.json b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/output.json new file mode 100644 index 0000000000..e59b53b2e0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/output.json @@ -0,0 +1,155 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 13, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ExportNamedDeclaration", + "start": 17, + "end": 30, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + }, + "exported": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} \ No newline at end of file