Skip to content

Commit

Permalink
Always register global bindings as exportable (#9865)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 26, 2019
1 parent d7757f6 commit cf36687
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 11 deletions.
17 changes: 6 additions & 11 deletions packages/babel-parser/src/util/scope.js
Expand Up @@ -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 {
Expand All @@ -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)) ||
Expand All @@ -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`);
}
Expand Down
@@ -0,0 +1,2 @@
export { a };
function a() {}
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"strictMode": false
}
@@ -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": []
}
}

0 comments on commit cf36687

Please sign in to comment.