-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[parser] Add TS enum support to the Scope
- Loading branch information
1 parent
cb578d9
commit ea374e1
Showing
24 changed files
with
717 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,45 @@ | ||
import ScopeHandler, { Scope } from "../../util/scope"; | ||
|
||
class TypeScriptScope extends Scope {} | ||
class TypeScriptScope extends Scope { | ||
tsEnum: string = []; | ||
} | ||
|
||
export default class TypeScriptScopeHandler extends ScopeHandler { | ||
scopeStack: Array<TypeScriptScope>; | ||
|
||
createScope(flags: ScopeFlags): Scope { | ||
return new TypeScriptScope(flags); | ||
} | ||
|
||
declareName(name: string, bindingType: ?BindingTypes, pos: number) { | ||
if (bindingType === BIND_TS_ENUM) { | ||
const scope = this.currentScope(); | ||
this.checkRedeclarationInScope(scope, name, bindingType, pos); | ||
scope.tsEnum.push(name); | ||
this.maybeExportDefined(scope, name); | ||
} else { | ||
super.declareName(...arguments); | ||
} | ||
} | ||
|
||
isRedeclaredInScope( | ||
scope: Scope, | ||
name: string, | ||
bindingType: ?BindingTypes, | ||
): boolean { | ||
if (bindingType === BIND_TS_ENUM) { | ||
// This checks for var/let/function. An enum can redeclare an enum. | ||
return super.isRedeclaredInScope(scope, name, BIND_LEXICAL); | ||
} | ||
|
||
return ( | ||
scope.tsEnum.indexOf(name) > -1 || super.isRedeclaredInScope(...arguments) | ||
); | ||
} | ||
|
||
checkLocalExport(id: N.Identifier) { | ||
if (this.scopeStack[0].tsEnum.indexOf(id.name) === -1) { | ||
super.checkLocalExport(...arguments); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ enum Foo {} } | ||
let Foo; |
137 changes: 137 additions & 0 deletions
137
packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
{ | ||
"type": "File", | ||
"start": 0, | ||
"end": 24, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 8 | ||
} | ||
}, | ||
"program": { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 24, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 8 | ||
} | ||
}, | ||
"sourceType": "module", | ||
"interpreter": null, | ||
"body": [ | ||
{ | ||
"type": "BlockStatement", | ||
"start": 0, | ||
"end": 15, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 15 | ||
} | ||
}, | ||
"body": [ | ||
{ | ||
"type": "TSEnumDeclaration", | ||
"start": 2, | ||
"end": 13, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 2 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 13 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 7, | ||
"end": 10, | ||
"loc": { | ||
"start": { | ||
"line": 1, | ||
"column": 7 | ||
}, | ||
"end": { | ||
"line": 1, | ||
"column": 10 | ||
}, | ||
"identifierName": "Foo" | ||
}, | ||
"name": "Foo" | ||
}, | ||
"members": [] | ||
} | ||
], | ||
"directives": [] | ||
}, | ||
{ | ||
"type": "VariableDeclaration", | ||
"start": 16, | ||
"end": 24, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 0 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 8 | ||
} | ||
}, | ||
"declarations": [ | ||
{ | ||
"type": "VariableDeclarator", | ||
"start": 20, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 7 | ||
} | ||
}, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 20, | ||
"end": 23, | ||
"loc": { | ||
"start": { | ||
"line": 2, | ||
"column": 4 | ||
}, | ||
"end": { | ||
"line": 2, | ||
"column": 7 | ||
}, | ||
"identifierName": "Foo" | ||
}, | ||
"name": "Foo" | ||
}, | ||
"init": null | ||
} | ||
], | ||
"kind": "let" | ||
} | ||
], | ||
"directives": [] | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-parser/test/fixtures/typescript/scope/enum-exported-after/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// https://github.com/babel/babel/issues/9763 | ||
|
||
enum Test {} | ||
|
||
export { Test as default } |
Oops, something went wrong.