From 30d507c915dcdca4cedf50a089025a8b041942f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 26 Apr 2019 14:19:53 +0200 Subject: [PATCH] Add TS support to @babel/parser's Scope (#9766) * [parser] Allow plugins to extend ScopeHandler * Directly extend Scope * Don't use new.target to get the ScopeHandler * [parser] Add TS enum support to the Scope * Remove duplicated options in tests * Fix * Fix flow * Rename tests * Add tests * Full typescript support in scope * Remove BIND_SIMPLE_CATCH SCOPE_SIMPLE_CATCH was used instead * Export TS types * Register function declarations * Fix body-less functions and namespaces 1) Move this.scope.exit() for functions from parseFunctionBody to the callers. Otherwise the scope of body-less functions was never closed. Also, it is easier to track scope.exit() if it is near to scope.enter() 2) Register namespace ids for export * Disallow redeclaration of enum with const enum --- packages/babel-parser/src/parser/base.js | 2 +- .../babel-parser/src/parser/expression.js | 3 +- packages/babel-parser/src/parser/index.js | 7 + packages/babel-parser/src/parser/lval.js | 6 +- packages/babel-parser/src/parser/statement.js | 55 +++--- packages/babel-parser/src/plugins/estree.js | 2 +- packages/babel-parser/src/plugins/flow.js | 2 +- .../{typescript.js => typescript/index.js} | 73 +++++++- .../src/plugins/typescript/scope.js | 105 +++++++++++ packages/babel-parser/src/util/scope.js | 117 ++++++++---- packages/babel-parser/src/util/scopeflags.js | 51 +++++- .../estree/typescript/literals/input.js | 6 +- .../estree/typescript/literals/output.json | 12 +- .../scope/enum-block-scoped/input.js | 2 + .../scope/enum-block-scoped/output.json | 137 ++++++++++++++ .../export-declare-function-after/input.js | 3 + .../export-declare-function-after/output.json | 169 +++++++++++++++++ .../export-declare-function-before/input.js | 3 + .../output.json | 169 +++++++++++++++++ .../scope/export-enum-after/input.js | 5 + .../scope/export-enum-after/output.json | 172 ++++++++++++++++++ .../scope/export-enum-before/input.js | 3 + .../scope/export-enum-before/output.json | 136 ++++++++++++++ .../scope/export-interface-after/input.js | 2 + .../scope/export-interface-after/output.json | 151 +++++++++++++++ .../scope/export-interface-before/input.js | 2 + .../scope/export-interface-before/output.json | 151 +++++++++++++++ .../scope/export-namespace/input.js | 3 + .../scope/export-namespace/output.json | 151 +++++++++++++++ .../scope/export-type-after/input.js | 2 + .../scope/export-type-after/output.json | 150 +++++++++++++++ .../scope/export-type-before/input.js | 2 + .../scope/export-type-before/output.json | 150 +++++++++++++++ .../function-type-before-declaration/index.js | 4 + .../scope/redeclaration-class-class/input.js | 2 + .../redeclaration-class-class/options.json | 3 + .../scope/redeclaration-class-enum/input.js | 2 + .../redeclaration-class-enum/options.json | 3 + .../redeclaration-class-interface/input.js | 2 + .../redeclaration-class-interface/output.json | 132 ++++++++++++++ .../scope/redeclaration-class-type/input.js | 2 + .../redeclaration-class-type/options.json | 3 + .../input.js | 0 .../output.json | 0 .../input.js | 2 + .../output.json | 103 +++++++++++ .../redeclaration-constenum-enum/input.js | 2 + .../redeclaration-constenum-enum/options.json | 3 + .../scope/redeclaration-enum-class/input.js | 2 + .../redeclaration-enum-class/options.json | 3 + .../redeclaration-enum-constenum/input.js | 2 + .../redeclaration-enum-constenum/options.json | 3 + .../scope/redeclaration-enum-enum/input.js | 2 + .../scope/redeclaration-enum-enum/output.json | 101 ++++++++++ .../redeclaration-enum-function/input.js | 2 + .../redeclaration-enum-function/options.json | 3 + .../redeclaration-enum-interface/input.js | 2 + .../redeclaration-enum-interface/options.json | 3 + .../scope/redeclaration-enum-let/input.js | 2 + .../scope/redeclaration-enum-let/options.json | 3 + .../scope/redeclaration-enum-type/input.js | 2 + .../redeclaration-enum-type/options.json | 3 + .../scope/redeclaration-enum-var/input.js | 2 + .../scope/redeclaration-enum-var/options.json | 3 + .../redeclaration-function-enum/input.js | 2 + .../redeclaration-function-enum/options.json | 3 + .../redeclaration-function-interface/input.js | 2 + .../output.json | 135 ++++++++++++++ .../redeclaration-function-type/input.js | 2 + .../redeclaration-function-type/output.json | 134 ++++++++++++++ .../redeclaration-interface-class/input.js | 2 + .../redeclaration-interface-class/output.json | 132 ++++++++++++++ .../redeclaration-interface-enum/input.js | 2 + .../redeclaration-interface-enum/options.json | 3 + .../redeclaration-interface-function/input.js | 2 + .../output.json | 135 ++++++++++++++ .../input.js | 2 + .../output.json | 131 +++++++++++++ .../redeclaration-interface-let/input.js | 2 + .../redeclaration-interface-let/output.json | 134 ++++++++++++++ .../redeclaration-interface-var/input.js | 2 + .../redeclaration-interface-var/output.json | 134 ++++++++++++++ .../scope/redeclaration-let-enum/input.js | 2 + .../scope/redeclaration-let-enum/options.json | 3 + .../redeclaration-let-interface/input.js | 2 + .../redeclaration-let-interface/output.json | 134 ++++++++++++++ .../input.js | 0 .../output.json | 0 .../scope/redeclaration-type-class/input.js | 2 + .../redeclaration-type-class/options.json | 3 + .../scope/redeclaration-type-enum/input.js | 2 + .../redeclaration-type-enum/options.json | 3 + .../redeclaration-type-function/input.js | 2 + .../redeclaration-type-function/output.json | 134 ++++++++++++++ .../redeclaration-type-interface/input.js | 2 + .../redeclaration-type-interface/options.json | 3 + .../scope/redeclaration-type-let/input.js | 2 + .../scope/redeclaration-type-let/output.json | 133 ++++++++++++++ .../scope/redeclaration-type-type/input.js | 2 + .../redeclaration-type-type/options.json | 3 + .../scope/redeclaration-type-var/input.js | 2 + .../scope/redeclaration-type-var/output.json | 133 ++++++++++++++ .../scope/redeclaration-var-enum/input.js | 2 + .../scope/redeclaration-var-enum/options.json | 3 + .../redeclaration-var-interface/input.js | 2 + .../redeclaration-var-interface/output.json | 134 ++++++++++++++ .../input.js | 0 .../output.json | 0 108 files changed, 3974 insertions(+), 98 deletions(-) rename packages/babel-parser/src/plugins/{typescript.js => typescript/index.js} (97%) create mode 100644 packages/babel-parser/src/plugins/typescript/scope.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-namespace/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-namespace/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-type-after/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-type-after/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-type-before/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/export-type-before/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/function-type-before-declaration/index.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/options.json rename packages/babel-parser/test/fixtures/typescript/scope/{no-dupl-decl-type-const => redeclaration-const-type}/input.js (100%) rename packages/babel-parser/test/fixtures/typescript/scope/{no-dupl-decl-type-const => redeclaration-const-type}/output.json (100%) create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/output.json rename packages/babel-parser/test/fixtures/typescript/scope/{no-dupl-decl-type-let => redeclaration-let-type}/input.js (100%) rename packages/babel-parser/test/fixtures/typescript/scope/{no-dupl-decl-type-let => redeclaration-let-type}/output.json (100%) create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/output.json rename packages/babel-parser/test/fixtures/typescript/scope/{no-dupl-decl-type-var => redeclaration-var-type}/input.js (100%) rename packages/babel-parser/test/fixtures/typescript/scope/{no-dupl-decl-type-var => redeclaration-var-type}/output.json (100%) diff --git a/packages/babel-parser/src/parser/base.js b/packages/babel-parser/src/parser/base.js index f2eeaaa01465..1e451794158c 100644 --- a/packages/babel-parser/src/parser/base.js +++ b/packages/babel-parser/src/parser/base.js @@ -9,7 +9,7 @@ export default class BaseParser { // Properties set by constructor in index.js options: Options; inModule: boolean; - scope: ScopeHandler; + scope: ScopeHandler<*>; plugins: PluginsMap; filename: ?string; sawUnambiguousESM: boolean = false; diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index f9a75fbe38e4..3978ad226d91 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1767,6 +1767,7 @@ export default class ExpressionParser extends LValParser { this.parseFunctionParams((node: any), allowModifiers); this.checkYieldAwaitInDefaultParams(); this.parseFunctionBodyAndFinish(node, type, true); + this.scope.exit(); this.state.yieldPos = oldYieldPos; this.state.awaitPos = oldAwaitPos; @@ -1795,6 +1796,7 @@ export default class ExpressionParser extends LValParser { if (params) this.setArrowFunctionParameters(node, params); this.parseFunctionBody(node, true); + this.scope.exit(); this.state.maybeInArrowParameters = oldMaybeInArrowParameters; this.state.yieldPos = oldYieldPos; this.state.awaitPos = oldAwaitPos; @@ -1890,7 +1892,6 @@ export default class ExpressionParser extends LValParser { node.body = this.parseBlock(true, false); this.state.labels = oldLabels; } - this.scope.exit(); this.state.inParameters = oldInParameters; // Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval' diff --git a/packages/babel-parser/src/parser/index.js b/packages/babel-parser/src/parser/index.js index b92ecb8203ad..9e75bf6044d3 100644 --- a/packages/babel-parser/src/parser/index.js +++ b/packages/babel-parser/src/parser/index.js @@ -20,6 +20,8 @@ export default class Parser extends StatementParser { options = getOptions(options); super(options, input); + const ScopeHandler = this.getScopeHandler(); + this.options = options; this.inModule = this.options.sourceType === "module"; this.scope = new ScopeHandler(this.raise.bind(this), this.inModule); @@ -27,6 +29,11 @@ export default class Parser extends StatementParser { this.filename = options.sourceFilename; } + // This can be overwritten, for example, by the TypeScript plugin. + getScopeHandler(): Class> { + return ScopeHandler; + } + parse(): File { this.scope.enter(SCOPE_PROGRAM); const file = this.startNode(); diff --git a/packages/babel-parser/src/parser/lval.js b/packages/babel-parser/src/parser/lval.js index 9bb0769294ab..c50e5cc1414b 100644 --- a/packages/babel-parser/src/parser/lval.js +++ b/packages/babel-parser/src/parser/lval.js @@ -16,7 +16,7 @@ import type { import type { Pos, Position } from "../util/location"; import { isStrictBindReservedWord } from "../util/identifier"; import { NodeUtils } from "./node"; -import { type BindingTypes, BIND_NONE, BIND_OUTSIDE } from "../util/scopeflags"; +import { type BindingTypes, BIND_NONE } from "../util/scopeflags"; export default class LValParser extends NodeUtils { // Forward-declaration: defined in expression.js @@ -325,7 +325,7 @@ export default class LValParser extends NodeUtils { checkLVal( expr: Expression, - bindingType: ?BindingTypes = BIND_NONE, + bindingType: BindingTypes = BIND_NONE, checkClashes: ?{ [key: string]: boolean }, contextDescription: string, ): void { @@ -363,7 +363,7 @@ export default class LValParser extends NodeUtils { checkClashes[key] = true; } } - if (bindingType !== BIND_NONE && bindingType !== BIND_OUTSIDE) { + if (!(bindingType & BIND_NONE)) { this.scope.declareName(expr.name, bindingType, expr.start); } break; diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 103a5172c427..0e83b088cc15 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -11,7 +11,7 @@ import { import { lineBreak, skipWhiteSpace } from "../util/whitespace"; import * as charCodes from "charcodes"; import { - BIND_SIMPLE_CATCH, + BIND_CLASS, BIND_LEXICAL, BIND_VAR, BIND_FUNCTION, @@ -662,12 +662,7 @@ export default class StatementParser extends ExpressionParser { clause.param = this.parseBindingAtom(); const simple = clause.param.type === "Identifier"; this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0); - this.checkLVal( - clause.param, - simple ? BIND_SIMPLE_CATCH : BIND_LEXICAL, - null, - "catch clause", - ); + this.checkLVal(clause.param, BIND_LEXICAL, null, "catch clause"); this.expect(tt.parenR); } else { clause.param = null; @@ -1056,22 +1051,6 @@ export default class StatementParser extends ExpressionParser { if (isStatement) { node.id = this.parseFunctionId(requireId); - if (node.id && !isHangingStatement) { - // If it is a regular function declaration in sloppy mode, then it is - // subject to Annex B semantics (BIND_FUNCTION). Otherwise, the binding - // mode depends on properties of the current scope (see - // treatFunctionsAsVar). - this.checkLVal( - node.id, - this.state.strict || node.generator || node.async - ? this.scope.treatFunctionsAsVar - ? BIND_VAR - : BIND_LEXICAL - : BIND_FUNCTION, - null, - "function name", - ); - } } const oldInClassProperty = this.state.inClassProperty; @@ -1099,6 +1078,15 @@ export default class StatementParser extends ExpressionParser { ); }); + this.scope.exit(); + + if (isStatement && !isHangingStatement) { + // We need to validate this _after_ parsing the function body + // because of TypeScript body-less function declarations, + // which shouldn't be added to the scope. + this.checkFunctionStatementId(node); + } + this.state.inClassProperty = oldInClassProperty; this.state.yieldPos = oldYieldPos; this.state.awaitPos = oldAwaitPos; @@ -1125,6 +1113,25 @@ export default class StatementParser extends ExpressionParser { this.checkYieldAwaitInDefaultParams(); } + checkFunctionStatementId(node: N.Function): void { + if (!node.id) return; + + // If it is a regular function declaration in sloppy mode, then it is + // subject to Annex B semantics (BIND_FUNCTION). Otherwise, the binding + // mode depends on properties of the current scope (see + // treatFunctionsAsVar). + this.checkLVal( + node.id, + this.state.strict || node.generator || node.async + ? this.scope.treatFunctionsAsVar + ? BIND_VAR + : BIND_LEXICAL + : BIND_FUNCTION, + null, + "function name", + ); + } + // Parse a class declaration or literal (depending on the // `isStatement` parameter). @@ -1612,7 +1619,7 @@ export default class StatementParser extends ExpressionParser { if (this.match(tt.name)) { node.id = this.parseIdentifier(); if (isStatement) { - this.checkLVal(node.id, BIND_LEXICAL, undefined, "class name"); + this.checkLVal(node.id, BIND_CLASS, undefined, "class name"); } } else { if (optionalId || !isStatement) { diff --git a/packages/babel-parser/src/plugins/estree.js b/packages/babel-parser/src/plugins/estree.js index d36e254bffeb..a407b8ac84ca 100644 --- a/packages/babel-parser/src/plugins/estree.js +++ b/packages/babel-parser/src/plugins/estree.js @@ -105,7 +105,7 @@ export default (superClass: Class): Class => checkLVal( expr: N.Expression, - bindingType: ?BindingTypes = BIND_NONE, + bindingType: BindingTypes = BIND_NONE, checkClashes: ?{ [key: string]: boolean }, contextDescription: string, ): void { diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 715b9cc1e008..88acb476665c 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -2016,7 +2016,7 @@ export default (superClass: Class): Class => checkLVal( expr: N.Expression, - bindingType: ?BindingTypes = BIND_NONE, + bindingType: BindingTypes = BIND_NONE, checkClashes: ?{ [key: string]: boolean }, contextDescription: string, ): void { diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript/index.js similarity index 97% rename from packages/babel-parser/src/plugins/typescript.js rename to packages/babel-parser/src/plugins/typescript/index.js index c9e3d1fa0753..80f41544f4ff 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1,12 +1,23 @@ // @flow -import type { TokenType } from "../tokenizer/types"; -import { types as tt } from "../tokenizer/types"; -import { types as ct } from "../tokenizer/context"; -import * as N from "../types"; -import type { Pos, Position } from "../util/location"; -import Parser from "../parser"; -import { type BindingTypes, BIND_NONE, SCOPE_OTHER } from "../util/scopeflags"; +import type { TokenType } from "../../tokenizer/types"; +import { types as tt } from "../../tokenizer/types"; +import { types as ct } from "../../tokenizer/context"; +import * as N from "../../types"; +import type { Pos, Position } from "../../util/location"; +import type Parser from "../../parser"; +import { + type BindingTypes, + BIND_NONE, + SCOPE_OTHER, + BIND_TS_ENUM, + BIND_TS_CONST_ENUM, + BIND_TS_TYPE, + BIND_TS_INTERFACE, + BIND_TS_FN_TYPE, + BIND_TS_NAMESPACE, +} from "../../util/scopeflags"; +import TypeScriptScopeHandler from "./scope"; type TsModifier = | "readonly" @@ -69,6 +80,10 @@ function keywordTypeFromName( export default (superClass: Class): Class => class extends superClass { + getScopeHandler(): Class { + return TypeScriptScopeHandler; + } + tsIsIdentifier(): boolean { // TODO: actually a bit more complex in TypeScript, but shouldn't matter. // See https://github.com/Microsoft/TypeScript/issues/15008 @@ -1017,6 +1032,12 @@ export default (superClass: Class): Class => node: N.TsInterfaceDeclaration, ): N.TsInterfaceDeclaration { node.id = this.parseIdentifier(); + this.checkLVal( + node.id, + BIND_TS_INTERFACE, + undefined, + "typescript interface declaration", + ); node.typeParameters = this.tsTryParseTypeParameters(); if (this.eat(tt._extends)) { node.extends = this.tsParseHeritageClause("extends"); @@ -1031,6 +1052,8 @@ export default (superClass: Class): Class => node: N.TsTypeAliasDeclaration, ): N.TsTypeAliasDeclaration { node.id = this.parseIdentifier(); + this.checkLVal(node.id, BIND_TS_TYPE, undefined, "typescript type alias"); + node.typeParameters = this.tsTryParseTypeParameters(); node.typeAnnotation = this.tsExpectThenParseType(tt.eq); this.semicolon(); @@ -1099,6 +1122,13 @@ export default (superClass: Class): Class => ): N.TsEnumDeclaration { if (isConst) node.const = true; node.id = this.parseIdentifier(); + this.checkLVal( + node.id, + isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM, + undefined, + "typescript enum declaration", + ); + this.expect(tt.braceL); node.members = this.tsParseDelimitedList( "EnumMembers", @@ -1126,11 +1156,22 @@ export default (superClass: Class): Class => tsParseModuleOrNamespaceDeclaration( node: N.TsModuleDeclaration, + nested?: boolean = false, ): N.TsModuleDeclaration { node.id = this.parseIdentifier(); + + if (!nested) { + this.checkLVal( + node.id, + BIND_TS_NAMESPACE, + null, + "module or namespace declaration", + ); + } + if (this.eat(tt.dot)) { const inner = this.startNode(); - this.tsParseModuleOrNamespaceDeclaration(inner); + this.tsParseModuleOrNamespaceDeclaration(inner, true); node.body = inner; } else { node.body = this.tsParseModuleBlock(); @@ -1260,7 +1301,11 @@ export default (superClass: Class): Class => switch (starttype) { case tt._function: - return this.parseFunctionStatement(nany); + return this.parseFunctionStatement( + nany, + /* async */ false, + /* declarationPosition */ true, + ); case tt._class: return this.parseClass( nany, @@ -1531,6 +1576,14 @@ export default (superClass: Class): Class => super.parseFunctionBodyAndFinish(node, type, isMethod); } + checkFunctionStatementId(node: N.Function): void { + if (!node.body && node.id) { + this.checkLVal(node.id, BIND_TS_FN_TYPE, null, "function name"); + } else { + super.checkFunctionStatementId(...arguments); + } + } + parseSubscript( base: N.Expression, startPos: number, @@ -2214,7 +2267,7 @@ export default (superClass: Class): Class => checkLVal( expr: N.Expression, - bindingType: ?BindingTypes = BIND_NONE, + bindingType: BindingTypes = BIND_NONE, checkClashes: ?{ [key: string]: boolean }, contextDescription: string, ): void { diff --git a/packages/babel-parser/src/plugins/typescript/scope.js b/packages/babel-parser/src/plugins/typescript/scope.js new file mode 100644 index 000000000000..f706f1e88e99 --- /dev/null +++ b/packages/babel-parser/src/plugins/typescript/scope.js @@ -0,0 +1,105 @@ +// @flow + +import ScopeHandler, { Scope } from "../../util/scope"; +import { + BIND_KIND_TYPE, + BIND_FLAGS_TS_ENUM, + BIND_FLAGS_TS_CONST_ENUM, + BIND_FLAGS_TS_EXPORT_ONLY, + BIND_KIND_VALUE, + BIND_FLAGS_CLASS, + type ScopeFlags, + type BindingTypes, +} from "../../util/scopeflags"; +import * as N from "../../types"; + +class TypeScriptScope extends Scope { + types: string[] = []; + + // enums (which are also in .types) + enums: string[] = []; + + // const enums (which are also in .enums and .types) + constEnums: string[] = []; + + // classes (which are also in .lexical) and interface (which are also in .types) + classes: string[] = []; + + // namespaces and bodyless-functions are too difficult to track, + // especially without type analysis. + // We need to track them anyway, to avoid "X is not defined" errors + // when exporting them. + exportOnlyBindings: string[] = []; +} + +// See https://github.com/babel/babel/pull/9766#discussion_r268920730 for an +// explanation of how typescript handles scope. + +export default class TypeScriptScopeHandler extends ScopeHandler { + createScope(flags: ScopeFlags): TypeScriptScope { + return new TypeScriptScope(flags); + } + + declareName(name: string, bindingType: BindingTypes, pos: number) { + const scope = this.currentScope(); + if (bindingType & BIND_FLAGS_TS_EXPORT_ONLY) { + this.maybeExportDefined(scope, name); + scope.exportOnlyBindings.push(name); + return; + } + + super.declareName(...arguments); + + if (bindingType & BIND_KIND_TYPE) { + if (!(bindingType & BIND_KIND_VALUE)) { + // "Value" bindings have already been registered by the superclass. + this.checkRedeclarationInScope(scope, name, bindingType, pos); + this.maybeExportDefined(scope, name); + } + scope.types.push(name); + } + if (bindingType & BIND_FLAGS_TS_ENUM) scope.enums.push(name); + if (bindingType & BIND_FLAGS_TS_CONST_ENUM) scope.constEnums.push(name); + if (bindingType & BIND_FLAGS_CLASS) scope.classes.push(name); + } + + isRedeclaredInScope( + scope: TypeScriptScope, + name: string, + bindingType: BindingTypes, + ): boolean { + if (scope.enums.indexOf(name) > -1) { + if (bindingType & BIND_FLAGS_TS_ENUM) { + // Enums can be merged with other enums if they are both + // const or both non-const. + const isConst = !!(bindingType & BIND_FLAGS_TS_CONST_ENUM); + const wasConst = scope.constEnums.indexOf(name) > -1; + return isConst !== wasConst; + } + return true; + } + if (bindingType & BIND_FLAGS_CLASS && scope.classes.indexOf(name) > -1) { + if (scope.lexical.indexOf(name) > -1) { + // Classes can be merged with interfaces + return !!(bindingType & BIND_KIND_VALUE); + } else { + // Interface can be merged with other classes or interfaces + return false; + } + } + if (bindingType & BIND_KIND_TYPE && scope.types.indexOf(name) > -1) { + return true; + } + + return super.isRedeclaredInScope(...arguments); + } + + checkLocalExport(id: N.Identifier) { + if ( + this.scopeStack[0].types.indexOf(id.name) === -1 && + this.scopeStack[0].exportOnlyBindings.indexOf(id.name) === -1 + ) { + super.checkLocalExport(id); + } + } +} diff --git a/packages/babel-parser/src/util/scope.js b/packages/babel-parser/src/util/scope.js index 30627c9605ee..e40cb53e3e33 100644 --- a/packages/babel-parser/src/util/scope.js +++ b/packages/babel-parser/src/util/scope.js @@ -9,17 +9,18 @@ import { SCOPE_SUPER, SCOPE_PROGRAM, SCOPE_VAR, - BIND_SIMPLE_CATCH, - BIND_LEXICAL, - BIND_FUNCTION, + SCOPE_CLASS, + BIND_SCOPE_FUNCTION, + BIND_SCOPE_VAR, + BIND_SCOPE_LEXICAL, + BIND_KIND_VALUE, type ScopeFlags, type BindingTypes, - SCOPE_CLASS, } from "./scopeflags"; import * as N from "../types"; // Start an AST node, attaching a start offset. -class Scope { +export class Scope { flags: ScopeFlags; // A list of var-declared names in the current lexical scope var: string[] = []; @@ -37,8 +38,8 @@ type raiseFunction = (number, string) => void; // The functions in this module keep track of declared variables in the // current scope in order to detect duplicate variable names. -export default class ScopeHandler { - scopeStack: Array = []; +export default class ScopeHandler { + scopeStack: Array = []; raise: raiseFunction; inModule: boolean; undefinedExports: Map = new Map(); @@ -70,8 +71,14 @@ export default class ScopeHandler { return this.treatFunctionsAsVarInScope(this.currentScope()); } + createScope(flags: ScopeFlags): Scope { + return new Scope(flags); + } + // This method will be overwritten by subclasses + +createScope: (flags: ScopeFlags) => IScope; + enter(flags: ScopeFlags) { - this.scopeStack.push(new Scope(flags)); + this.scopeStack.push(this.createScope(flags)); } exit() { @@ -81,46 +88,33 @@ export default class ScopeHandler { // The spec says: // > At the top level of a function, or script, function declarations are // > treated like var declarations rather than like lexical declarations. - treatFunctionsAsVarInScope(scope: Scope): boolean { + treatFunctionsAsVarInScope(scope: IScope): boolean { return !!( scope.flags & SCOPE_FUNCTION || (!this.inModule && scope.flags & SCOPE_PROGRAM) ); } - declareName(name: string, bindingType: ?BindingTypes, pos: number) { - let redeclared = false; + declareName(name: string, bindingType: BindingTypes, pos: number) { let scope = this.currentScope(); + if (bindingType & BIND_SCOPE_LEXICAL || bindingType & BIND_SCOPE_FUNCTION) { + this.checkRedeclarationInScope(scope, name, bindingType, pos); - if (bindingType === BIND_LEXICAL) { - redeclared = - scope.lexical.indexOf(name) > -1 || - scope.functions.indexOf(name) > -1 || - scope.var.indexOf(name) > -1; - scope.lexical.push(name); - } else if (bindingType === BIND_SIMPLE_CATCH) { - scope.lexical.push(name); - } else if (bindingType === BIND_FUNCTION) { - if (this.treatFunctionsAsVar) { - redeclared = scope.lexical.indexOf(name) > -1; + if (bindingType & BIND_SCOPE_FUNCTION) { + scope.functions.push(name); } else { - redeclared = - scope.lexical.indexOf(name) > -1 || scope.var.indexOf(name) > -1; + scope.lexical.push(name); } - scope.functions.push(name); - } else { + + if (bindingType & BIND_SCOPE_LEXICAL) { + this.maybeExportDefined(scope, name); + } + } else if (bindingType & BIND_SCOPE_VAR) { for (let i = this.scopeStack.length - 1; i >= 0; --i) { scope = this.scopeStack[i]; - if ( - (scope.lexical.indexOf(name) > -1 && - !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name)) || - (!this.treatFunctionsAsVarInScope(scope) && - scope.functions.indexOf(name) > -1) - ) { - redeclared = true; - break; - } + this.checkRedeclarationInScope(scope, name, bindingType, pos); scope.var.push(name); + this.maybeExportDefined(scope, name); if (scope.flags & SCOPE_VAR) break; } @@ -128,11 +122,56 @@ export default class ScopeHandler { if (this.inModule && scope.flags & SCOPE_PROGRAM) { this.undefinedExports.delete(name); } - if (redeclared) { + } + + maybeExportDefined(scope: IScope, name: string) { + if (this.inModule && scope.flags & SCOPE_PROGRAM) { + this.undefinedExports.delete(name); + } + } + + checkRedeclarationInScope( + scope: IScope, + name: string, + bindingType: BindingTypes, + pos: number, + ) { + if (this.isRedeclaredInScope(scope, name, bindingType)) { this.raise(pos, `Identifier '${name}' has already been declared`); } } + isRedeclaredInScope( + scope: IScope, + name: string, + bindingType: BindingTypes, + ): boolean { + if (!(bindingType & BIND_KIND_VALUE)) return false; + + if (bindingType & BIND_SCOPE_LEXICAL) { + return ( + scope.lexical.indexOf(name) > -1 || + scope.functions.indexOf(name) > -1 || + scope.var.indexOf(name) > -1 + ); + } + + if (bindingType & BIND_SCOPE_FUNCTION) { + return ( + scope.lexical.indexOf(name) > -1 || + (!this.treatFunctionsAsVarInScope(scope) && + scope.var.indexOf(name) > -1) + ); + } + + return ( + (scope.lexical.indexOf(name) > -1 && + !(scope.flags & SCOPE_SIMPLE_CATCH && scope.lexical[0] === name)) || + (!this.treatFunctionsAsVarInScope(scope) && + scope.functions.indexOf(name) > -1) + ); + } + checkLocalExport(id: N.Identifier) { if ( this.scopeStack[0].lexical.indexOf(id.name) === -1 && @@ -146,12 +185,12 @@ export default class ScopeHandler { } } - currentScope(): Scope { + currentScope(): IScope { return this.scopeStack[this.scopeStack.length - 1]; } // $FlowIgnore - currentVarScope(): Scope { + currentVarScope(): IScope { for (let i = this.scopeStack.length - 1; ; i--) { const scope = this.scopeStack[i]; if (scope.flags & SCOPE_VAR) { @@ -162,7 +201,7 @@ export default class ScopeHandler { // Could be useful for `this`, `new.target`, `super()`, `super.property`, and `super[property]`. // $FlowIgnore - currentThisScope(): Scope { + currentThisScope(): IScope { for (let i = this.scopeStack.length - 1; ; i--) { const scope = this.scopeStack[i]; if ( diff --git a/packages/babel-parser/src/util/scopeflags.js b/packages/babel-parser/src/util/scopeflags.js index 35ba7fe30301..8c9697b3a416 100644 --- a/packages/babel-parser/src/util/scopeflags.js +++ b/packages/babel-parser/src/util/scopeflags.js @@ -35,18 +35,51 @@ export function functionFlags(isAsync: boolean, isGenerator: boolean) { ); } -// Used in checkLVal and declareName to determine the type of a binding -export const BIND_NONE = 0, // Not a binding - BIND_VAR = 1, // Var-style binding - BIND_LEXICAL = 2, // Let- or const-style binding - BIND_FUNCTION = 3, // Function declaration - BIND_SIMPLE_CATCH = 4, // Simple (identifier pattern) catch binding - BIND_OUTSIDE = 5; // Special case for function names as bound inside the function +// These flags are meant to be _only_ used inside the Scope class (or subclasses). +// prettier-ignore +export const BIND_KIND_VALUE = 0b00000_0000_01, + BIND_KIND_TYPE = 0b00000_0000_10, + // Used in checkLVal and declareName to determine the type of a binding + BIND_SCOPE_VAR = 0b00000_0001_00, // Var-style binding + BIND_SCOPE_LEXICAL = 0b00000_0010_00, // Let- or const-style binding + BIND_SCOPE_FUNCTION = 0b00000_0100_00, // Function declaration + BIND_SCOPE_OUTSIDE = 0b00000_1000_00, // Special case for function names as + // bound inside the function + // Misc flags + BIND_FLAGS_NONE = 0b00001_0000_00, + BIND_FLAGS_CLASS = 0b00010_0000_00, + BIND_FLAGS_TS_ENUM = 0b00100_0000_00, + BIND_FLAGS_TS_CONST_ENUM = 0b01000_0000_00, + BIND_FLAGS_TS_EXPORT_ONLY = 0b10000_0000_00; + +// These flags are meant to be _only_ used by Scope consumers +// prettier-ignore +/* = is value? | is type? | scope | misc flags */ +export const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_CLASS , + BIND_LEXICAL = BIND_KIND_VALUE | 0 | BIND_SCOPE_LEXICAL | 0 , + BIND_VAR = BIND_KIND_VALUE | 0 | BIND_SCOPE_VAR | 0 , + BIND_FUNCTION = BIND_KIND_VALUE | 0 | BIND_SCOPE_FUNCTION | 0 , + BIND_TS_INTERFACE = 0 | BIND_KIND_TYPE | 0 | BIND_FLAGS_CLASS , + BIND_TS_TYPE = 0 | BIND_KIND_TYPE | 0 | 0 , + BIND_TS_ENUM = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_LEXICAL | BIND_FLAGS_TS_ENUM, + BIND_TS_FN_TYPE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY, + // These bindings don't introduce anything in the scope. They are used for assignments and + // function expressions IDs. + BIND_NONE = 0 | 0 | 0 | BIND_FLAGS_NONE , + BIND_OUTSIDE = BIND_KIND_VALUE | 0 | 0 | BIND_FLAGS_NONE , + + BIND_TS_CONST_ENUM = BIND_TS_ENUM | BIND_FLAGS_TS_CONST_ENUM, + BIND_TS_NAMESPACE = BIND_TS_FN_TYPE; export type BindingTypes = | typeof BIND_NONE + | typeof BIND_OUTSIDE | typeof BIND_VAR | typeof BIND_LEXICAL + | typeof BIND_CLASS | typeof BIND_FUNCTION - | typeof BIND_SIMPLE_CATCH - | typeof BIND_OUTSIDE; + | typeof BIND_TS_INTERFACE + | typeof BIND_TS_TYPE + | typeof BIND_TS_ENUM + | typeof BIND_TS_FN_TYPE + | typeof BIND_TS_NAMESPACE; diff --git a/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js b/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js index 59171967bb32..ba1ece47de5d 100644 --- a/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js +++ b/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js @@ -2,6 +2,6 @@ type Foo = false; type Foo2 = true; type Foo3 = "string"; type Foo4 = 123; -type Foo4 = 123.4; -type Foo5 = -123; -type Foo5 = -123.5; +type Foo5 = 123.4; +type Foo6 = -123; +type Foo7 = -123.5; diff --git a/packages/babel-parser/test/fixtures/estree/typescript/literals/output.json b/packages/babel-parser/test/fixtures/estree/typescript/literals/output.json index 833304ea89d6..e08c9b31db6d 100644 --- a/packages/babel-parser/test/fixtures/estree/typescript/literals/output.json +++ b/packages/babel-parser/test/fixtures/estree/typescript/literals/output.json @@ -312,9 +312,9 @@ "line": 5, "column": 9 }, - "identifierName": "Foo4" + "identifierName": "Foo5" }, - "name": "Foo4" + "name": "Foo5" }, "typeAnnotation": { "type": "TSLiteralType", @@ -376,9 +376,9 @@ "line": 6, "column": 9 }, - "identifierName": "Foo5" + "identifierName": "Foo6" }, - "name": "Foo5" + "name": "Foo6" }, "typeAnnotation": { "type": "TSLiteralType", @@ -457,9 +457,9 @@ "line": 7, "column": 9 }, - "identifierName": "Foo5" + "identifierName": "Foo7" }, - "name": "Foo5" + "name": "Foo7" }, "typeAnnotation": { "type": "TSLiteralType", diff --git a/packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/input.js b/packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/input.js new file mode 100644 index 000000000000..50183fe6db62 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/input.js @@ -0,0 +1,2 @@ +{ enum Foo {} } +let Foo; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/output.json b/packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/output.json new file mode 100644 index 000000000000..ddaa635e8f86 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/enum-block-scoped/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/input.js new file mode 100644 index 000000000000..a746b10360a1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/input.js @@ -0,0 +1,3 @@ +declare function foo(): void; + +export { foo }; diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/output.json new file mode 100644 index 000000000000..c207baa1677a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-after/output.json @@ -0,0 +1,169 @@ +{ + "type": "File", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSDeclareFunction", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeAnnotation", + "start": 22, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 28 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 28 + } + } + } + }, + "declare": true + }, + { + "type": "ExportNamedDeclaration", + "start": 31, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 40, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "local": { + "type": "Identifier", + "start": 40, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start": 40, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/input.js new file mode 100644 index 000000000000..cfd46f8a2f9b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/input.js @@ -0,0 +1,3 @@ +export { foo }; + +declare function foo(): void; diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/output.json new file mode 100644 index 000000000000..b7071b10e90f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-declare-function-before/output.json @@ -0,0 +1,169 @@ +{ + "type": "File", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "local": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + } + } + ], + "source": null, + "declaration": null + }, + { + "type": "TSDeclareFunction", + "start": 17, + "end": 46, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 29 + } + }, + "id": { + "type": "Identifier", + "start": 34, + "end": 37, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 20 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "returnType": { + "type": "TSTypeAnnotation", + "start": 39, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 22 + }, + "end": { + "line": 3, + "column": 28 + } + }, + "typeAnnotation": { + "type": "TSVoidKeyword", + "start": 41, + "end": 45, + "loc": { + "start": { + "line": 3, + "column": 24 + }, + "end": { + "line": 3, + "column": 28 + } + } + } + }, + "declare": true + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/input.js new file mode 100644 index 000000000000..0149829f731a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/input.js @@ -0,0 +1,5 @@ +// https://github.com/babel/babel/issues/9763 + +enum Test {} + +export { Test as default } diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/output.json new file mode 100644 index 000000000000..c8738ce00309 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-after/output.json @@ -0,0 +1,172 @@ +{ + "type": "File", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 87, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSEnumDeclaration", + "start": 47, + "end": 59, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 52, + "end": 56, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 9 + }, + "identifierName": "Test" + }, + "name": "Test" + }, + "members": [], + "leadingComments": [ + { + "type": "CommentLine", + "value": " https://github.com/babel/babel/issues/9763", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + } + } + ] + }, + { + "type": "ExportNamedDeclaration", + "start": 61, + "end": 87, + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 26 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 70, + "end": 85, + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "local": { + "type": "Identifier", + "start": 70, + "end": 74, + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 13 + }, + "identifierName": "Test" + }, + "name": "Test" + }, + "exported": { + "type": "Identifier", + "start": 78, + "end": 85, + "loc": { + "start": { + "line": 5, + "column": 17 + }, + "end": { + "line": 5, + "column": 24 + }, + "identifierName": "default" + }, + "name": "default" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " https://github.com/babel/babel/issues/9763", + "start": 0, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/input.js new file mode 100644 index 000000000000..c159ad2975fb --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/input.js @@ -0,0 +1,3 @@ +export { Test as default } + +enum Test {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/output.json new file mode 100644 index 000000000000..43ce8b85cb55 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-enum-before/output.json @@ -0,0 +1,136 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 26, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 26 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 9, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "local": { + "type": "Identifier", + "start": 9, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "Test" + }, + "name": "Test" + }, + "exported": { + "type": "Identifier", + "start": 17, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "default" + }, + "name": "default" + } + } + ], + "source": null, + "declaration": null + }, + { + "type": "TSEnumDeclaration", + "start": 28, + "end": 40, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "id": { + "type": "Identifier", + "start": 33, + "end": 37, + "loc": { + "start": { + "line": 3, + "column": 5 + }, + "end": { + "line": 3, + "column": 9 + }, + "identifierName": "Test" + }, + "name": "Test" + }, + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/input.js new file mode 100644 index 000000000000..65fbb4ee3942 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/input.js @@ -0,0 +1,2 @@ +interface A {} +export { A }; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/output.json new file mode 100644 index 000000000000..9e575d18d39e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-after/output.json @@ -0,0 +1,151 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "ExportNamedDeclaration", + "start": 15, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "A" + }, + "name": "A" + }, + "exported": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/input.js new file mode 100644 index 000000000000..26bd9bebfe1f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/input.js @@ -0,0 +1,2 @@ +export { A }; +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/output.json new file mode 100644 index 000000000000..dd48538d82d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-interface-before/output.json @@ -0,0 +1,151 @@ +{ + "type": "File", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 28, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "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": "TSInterfaceDeclaration", + "start": 14, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 26, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-namespace/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-namespace/input.js new file mode 100644 index 000000000000..905dda6dc297 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-namespace/input.js @@ -0,0 +1,3 @@ +namespace N {} + +export { N }; diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-namespace/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-namespace/output.json new file mode 100644 index 000000000000..ec2b4c8349b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-namespace/output.json @@ -0,0 +1,151 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSModuleDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "N" + }, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "ExportNamedDeclaration", + "start": 16, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + }, + "identifierName": "N" + }, + "name": "N" + }, + "exported": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + }, + "identifierName": "N" + }, + "name": "N" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-type-after/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-type-after/input.js new file mode 100644 index 000000000000..f971b8e6dad4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-type-after/input.js @@ -0,0 +1,2 @@ +type A = number; +export { A }; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-type-after/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-type-after/output.json new file mode 100644 index 000000000000..d4259d15a749 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-type-after/output.json @@ -0,0 +1,150 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + }, + { + "type": "ExportNamedDeclaration", + "start": 17, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "local": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "A" + }, + "name": "A" + }, + "exported": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "A" + }, + "name": "A" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-type-before/input.js b/packages/babel-parser/test/fixtures/typescript/scope/export-type-before/input.js new file mode 100644 index 000000000000..1e925d4d28de --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-type-before/input.js @@ -0,0 +1,2 @@ +export { A }; +type A = number; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/export-type-before/output.json b/packages/babel-parser/test/fixtures/typescript/scope/export-type-before/output.json new file mode 100644 index 000000000000..5086f1f05376 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/export-type-before/output.json @@ -0,0 +1,150 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "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": "TSTypeAliasDeclaration", + "start": 14, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 23, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/function-type-before-declaration/index.js b/packages/babel-parser/test/fixtures/typescript/scope/function-type-before-declaration/index.js new file mode 100644 index 000000000000..9f9694abe675 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/function-type-before-declaration/index.js @@ -0,0 +1,4 @@ +function f(): void; +function f() {} + +export { f }; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/input.js new file mode 100644 index 000000000000..b27d4a43b2e5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/input.js @@ -0,0 +1,2 @@ +class A {} +class A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/options.json new file mode 100644 index 000000000000..336686170eb0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-class/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:6)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/input.js new file mode 100644 index 000000000000..beafeb8dcb86 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/input.js @@ -0,0 +1,2 @@ +class A {} +enum A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/options.json new file mode 100644 index 000000000000..1eed93f49a0e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/input.js new file mode 100644 index 000000000000..1628d05ed1a4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/input.js @@ -0,0 +1,2 @@ +class A {} +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/output.json new file mode 100644 index 000000000000..b44cc7caf564 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-interface/output.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 8, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "body": [] + } + }, + { + "type": "TSInterfaceDeclaration", + "start": 11, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/input.js new file mode 100644 index 000000000000..4361a93e710b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/input.js @@ -0,0 +1,2 @@ +class A {} +type A = number; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/options.json new file mode 100644 index 000000000000..1eed93f49a0e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-class-type/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-const/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-const-type/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-const/input.js rename to packages/babel-parser/test/fixtures/typescript/scope/redeclaration-const-type/input.js diff --git a/packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-const/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-const-type/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-const/output.json rename to packages/babel-parser/test/fixtures/typescript/scope/redeclaration-const-type/output.json diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/input.js new file mode 100644 index 000000000000..cb487f9b6b22 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/input.js @@ -0,0 +1,2 @@ +const enum Foo {} +const enum Foo {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/output.json new file mode 100644 index 000000000000..2120fa78e381 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-constenum/output.json @@ -0,0 +1,103 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "const": true, + "id": { + "type": "Identifier", + "start": 11, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "members": [] + }, + { + "type": "TSEnumDeclaration", + "start": 18, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 17 + } + }, + "const": true, + "id": { + "type": "Identifier", + "start": 29, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 14 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/input.js new file mode 100644 index 000000000000..860bf512749f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/input.js @@ -0,0 +1,2 @@ +const enum X {} +enum X {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/options.json new file mode 100644 index 000000000000..ee82975e59a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-constenum-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'X' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/input.js new file mode 100644 index 000000000000..4ecea177edbc --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/input.js @@ -0,0 +1,2 @@ +enum X {} +class X {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/options.json new file mode 100644 index 000000000000..69462b29a995 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-class/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'X' has already been declared (2:6)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/input.js new file mode 100644 index 000000000000..1b224627f1e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/input.js @@ -0,0 +1,2 @@ +enum X {} +const enum X {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/options.json new file mode 100644 index 000000000000..c77fcb7927ac --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-constenum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'X' has already been declared (2:11)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/input.js new file mode 100644 index 000000000000..30775d873feb --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/input.js @@ -0,0 +1,2 @@ +enum Foo {} +enum Foo {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/output.json new file mode 100644 index 000000000000..a270131731fb --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-enum/output.json @@ -0,0 +1,101 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSEnumDeclaration", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "members": [] + }, + { + "type": "TSEnumDeclaration", + "start": 12, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "members": [] + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/input.js new file mode 100644 index 000000000000..124177c8a916 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/input.js @@ -0,0 +1,2 @@ +enum Foo {} +function Foo() {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/options.json new file mode 100644 index 000000000000..ee47ddce75a8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-function/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'Foo' has already been declared (2:9)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/input.js new file mode 100644 index 000000000000..f4284204e36f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/input.js @@ -0,0 +1,2 @@ +enum X {} +interface X {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/options.json new file mode 100644 index 000000000000..a5f623f4e212 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-interface/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'X' has already been declared (2:10)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/input.js new file mode 100644 index 000000000000..408ec7d99305 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/input.js @@ -0,0 +1,2 @@ +enum Foo {} +let Foo; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/options.json new file mode 100644 index 000000000000..4172f917bdb1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-let/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'Foo' has already been declared (2:4)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/input.js new file mode 100644 index 000000000000..14a488bc4bf8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/input.js @@ -0,0 +1,2 @@ +enum X {} +type X = number; diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/options.json new file mode 100644 index 000000000000..ee82975e59a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-type/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'X' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/input.js new file mode 100644 index 000000000000..408ec7d99305 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/input.js @@ -0,0 +1,2 @@ +enum Foo {} +let Foo; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/options.json new file mode 100644 index 000000000000..4172f917bdb1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-enum-var/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'Foo' has already been declared (2:4)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/input.js new file mode 100644 index 000000000000..9cc11112f632 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/input.js @@ -0,0 +1,2 @@ +function Foo() {} +enum Foo {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/options.json new file mode 100644 index 000000000000..92349410e8cf --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'Foo' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/input.js new file mode 100644 index 000000000000..723647a42528 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/input.js @@ -0,0 +1,2 @@ +function A() {} +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/output.json new file mode 100644 index 000000000000..13e97e869b2d --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-interface/output.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "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": "TSInterfaceDeclaration", + "start": 16, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 28, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/input.js new file mode 100644 index 000000000000..13c1a11b111c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/input.js @@ -0,0 +1,2 @@ +function A() {} +type A = number; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/output.json new file mode 100644 index 000000000000..4258090807ff --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-function-type/output.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "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": "TSTypeAliasDeclaration", + "start": 16, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 6 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/input.js new file mode 100644 index 000000000000..42dc0ec40d98 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/input.js @@ -0,0 +1,2 @@ +interface A {} +class A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/output.json new file mode 100644 index 000000000000..5b0af1060cbb --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-class/output.json @@ -0,0 +1,132 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "ClassDeclaration", + "start": 15, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "A" + }, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 10 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/input.js new file mode 100644 index 000000000000..9fe5fc6a0b98 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/input.js @@ -0,0 +1,2 @@ +interface X {} +enum X {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/options.json new file mode 100644 index 000000000000..ee82975e59a3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'X' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/input.js new file mode 100644 index 000000000000..953d2b186c4c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/input.js @@ -0,0 +1,2 @@ +interface A {} +function A() {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/output.json new file mode 100644 index 000000000000..166403881b86 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-function/output.json @@ -0,0 +1,135 @@ +{ + "type": "File", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 30, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 15, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 24, + "end": 25, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "A" + }, + "name": "A" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 28, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/input.js new file mode 100644 index 000000000000..96072b8fa3d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/input.js @@ -0,0 +1,2 @@ +interface A {} +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/output.json new file mode 100644 index 000000000000..598f4999cfbc --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-interface/output.json @@ -0,0 +1,131 @@ +{ + "type": "File", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 29, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "TSInterfaceDeclaration", + "start": 15, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 25, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 27, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/input.js new file mode 100644 index 000000000000..1c40c966835e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/input.js @@ -0,0 +1,2 @@ +interface A {} +let A; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/output.json new file mode 100644 index 000000000000..0c96b1cd892a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-let/output.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "VariableDeclaration", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/input.js new file mode 100644 index 000000000000..d8dba1e43619 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/input.js @@ -0,0 +1,2 @@ +interface A {} +var A; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/output.json new file mode 100644 index 000000000000..e353b5cd0b83 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-interface-var/output.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSInterfaceDeclaration", + "start": 0, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [] + } + }, + { + "type": "VariableDeclaration", + "start": 15, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "init": null + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/input.js new file mode 100644 index 000000000000..fdac34ea9209 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/input.js @@ -0,0 +1,2 @@ +let Foo; +enum Foo {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/options.json new file mode 100644 index 000000000000..92349410e8cf --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'Foo' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/input.js new file mode 100644 index 000000000000..60fd53cfea0e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/input.js @@ -0,0 +1,2 @@ +let A; +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/output.json new file mode 100644 index 000000000000..d1833f641360 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-interface/output.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "TSInterfaceDeclaration", + "start": 7, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-let/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-type/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-let/input.js rename to packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-type/input.js diff --git a/packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-let/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-type/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-let/output.json rename to packages/babel-parser/test/fixtures/typescript/scope/redeclaration-let-type/output.json diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/input.js new file mode 100644 index 000000000000..b0d3dd1355cd --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/input.js @@ -0,0 +1,2 @@ +type A = number; +class A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/options.json new file mode 100644 index 000000000000..336686170eb0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-class/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:6)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/input.js new file mode 100644 index 000000000000..c35f067241e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/input.js @@ -0,0 +1,2 @@ +type A = number; +enum A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/options.json new file mode 100644 index 000000000000..1eed93f49a0e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/input.js new file mode 100644 index 000000000000..a26333d0f21b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/input.js @@ -0,0 +1,2 @@ +type A = number; +function A() {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/output.json new file mode 100644 index 000000000000..805839ca3c76 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-function/output.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + }, + { + "type": "FunctionDeclaration", + "start": 17, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "id": { + "type": "Identifier", + "start": 26, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 10 + }, + "identifierName": "A" + }, + "name": "A" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 30, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 13 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/input.js new file mode 100644 index 000000000000..e8c0c5b91811 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/input.js @@ -0,0 +1,2 @@ +type A = number; +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/options.json new file mode 100644 index 000000000000..04242bd1565b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-interface/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:10)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/input.js new file mode 100644 index 000000000000..00cfb4b05eca --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/input.js @@ -0,0 +1,2 @@ +type A = number; +let A; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/output.json new file mode 100644 index 000000000000..fb4fc242eac9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-let/output.json @@ -0,0 +1,133 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + }, + { + "type": "VariableDeclaration", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "init": null + } + ], + "kind": "let" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/input.js new file mode 100644 index 000000000000..ade472fa3747 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/input.js @@ -0,0 +1,2 @@ +type A = number; +type A = string; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/options.json new file mode 100644 index 000000000000..1eed93f49a0e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-type/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'A' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/input.js new file mode 100644 index 000000000000..8d6c5741d857 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/input.js @@ -0,0 +1,2 @@ +type A = number; +var A; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/output.json new file mode 100644 index 000000000000..8a2375272f10 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-type-var/output.json @@ -0,0 +1,133 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSTypeAliasDeclaration", + "start": 0, + "end": 16, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 16 + } + }, + "id": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeAnnotation": { + "type": "TSNumberKeyword", + "start": 9, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 15 + } + } + } + }, + { + "type": "VariableDeclaration", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "init": null + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/input.js new file mode 100644 index 000000000000..fdac34ea9209 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/input.js @@ -0,0 +1,2 @@ +let Foo; +enum Foo {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/options.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/options.json new file mode 100644 index 000000000000..92349410e8cf --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-enum/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Identifier 'Foo' has already been declared (2:5)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/input.js new file mode 100644 index 000000000000..80f8b82d0eae --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/input.js @@ -0,0 +1,2 @@ +var A; +interface A {} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/output.json new file mode 100644 index 000000000000..068b9e0779a4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-interface/output.json @@ -0,0 +1,134 @@ +{ + "type": "File", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 5, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + }, + "identifierName": "A" + }, + "name": "A" + }, + "init": null + } + ], + "kind": "var" + }, + { + "type": "TSInterfaceDeclaration", + "start": 7, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 18, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "A" + }, + "name": "A" + }, + "body": { + "type": "TSInterfaceBody", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 14 + } + }, + "body": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-var/input.js b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-type/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-var/input.js rename to packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-type/input.js diff --git a/packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-var/output.json b/packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-type/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/typescript/scope/no-dupl-decl-type-var/output.json rename to packages/babel-parser/test/fixtures/typescript/scope/redeclaration-var-type/output.json