diff --git a/packages/babel-parser/src/util/scope.js b/packages/babel-parser/src/util/scope.js index 759da8f1cef7..30627c9605ee 100644 --- a/packages/babel-parser/src/util/scope.js +++ b/packages/babel-parser/src/util/scope.js @@ -90,21 +90,17 @@ export default class ScopeHandler { declareName(name: string, bindingType: ?BindingTypes, pos: number) { let redeclared = false; + let scope = this.currentScope(); + if (bindingType === BIND_LEXICAL) { - const scope = this.currentScope(); redeclared = scope.lexical.indexOf(name) > -1 || scope.functions.indexOf(name) > -1 || scope.var.indexOf(name) > -1; scope.lexical.push(name); - if (this.inModule && scope.flags & SCOPE_PROGRAM) { - this.undefinedExports.delete(name); - } } else if (bindingType === BIND_SIMPLE_CATCH) { - const scope = this.currentScope(); scope.lexical.push(name); } else if (bindingType === BIND_FUNCTION) { - const scope = this.currentScope(); if (this.treatFunctionsAsVar) { redeclared = scope.lexical.indexOf(name) > -1; } else { @@ -114,7 +110,7 @@ export default class ScopeHandler { scope.functions.push(name); } else { for (let i = this.scopeStack.length - 1; i >= 0; --i) { - const scope = this.scopeStack[i]; + scope = this.scopeStack[i]; if ( (scope.lexical.indexOf(name) > -1 && !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name)) || @@ -126,13 +122,12 @@ export default class ScopeHandler { } scope.var.push(name); - if (this.inModule && scope.flags & SCOPE_PROGRAM) { - this.undefinedExports.delete(name); - } - if (scope.flags & SCOPE_VAR) break; } } + if (this.inModule && scope.flags & SCOPE_PROGRAM) { + this.undefinedExports.delete(name); + } if (redeclared) { this.raise(pos, `Identifier '${name}' has already been declared`); } 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-1/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/input.js rename to packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-1/input.js 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-1/options.json similarity index 100% rename from packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/options.json rename to packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-1/options.json 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-1/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode/output.json rename to packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-1/output.json diff --git a/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/input.js b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/input.js new file mode 100644 index 000000000000..aacd737fb7b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/input.js @@ -0,0 +1,2 @@ +export { a }; +function a() {} diff --git a/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/options.json b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/options.json new file mode 100644 index 000000000000..0580fdad26f5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/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-2/output.json b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/output.json new file mode 100644 index 000000000000..16b50c7b3875 --- /dev/null +++ b/packages/babel-parser/test/fixtures/core/scope/undecl-export-function-loose-mode-2/output.json @@ -0,0 +1,155 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + }, + "exported": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + } + } + ], + "source": null, + "declaration": null + }, + { + "type": "FunctionDeclaration", + "start": 14, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "a" + }, + "name": "a" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 27, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file