Skip to content

Commit

Permalink
Add TS support to @babel/parser's Scope (#9766)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
nicolo-ribaudo committed Apr 26, 2019
1 parent 293f3c9 commit 30d507c
Show file tree
Hide file tree
Showing 108 changed files with 3,974 additions and 98 deletions.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/base.js
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-parser/src/parser/index.js
Expand Up @@ -20,13 +20,20 @@ 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);
this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename;
}

// This can be overwritten, for example, by the TypeScript plugin.
getScopeHandler(): Class<ScopeHandler<*>> {
return ScopeHandler;
}

parse(): File {
this.scope.enter(SCOPE_PROGRAM);
const file = this.startNode();
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-parser/src/parser/lval.js
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
55 changes: 31 additions & 24 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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).

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/estree.js
Expand Up @@ -105,7 +105,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

checkLVal(
expr: N.Expression,
bindingType: ?BindingTypes = BIND_NONE,
bindingType: BindingTypes = BIND_NONE,
checkClashes: ?{ [key: string]: boolean },
contextDescription: string,
): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2016,7 +2016,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

checkLVal(
expr: N.Expression,
bindingType: ?BindingTypes = BIND_NONE,
bindingType: BindingTypes = BIND_NONE,
checkClashes: ?{ [key: string]: boolean },
contextDescription: string,
): void {
Expand Down
@@ -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"
Expand Down Expand Up @@ -69,6 +80,10 @@ function keywordTypeFromName(

export default (superClass: Class<Parser>): Class<Parser> =>
class extends superClass {
getScopeHandler(): Class<TypeScriptScopeHandler> {
return TypeScriptScopeHandler;
}

tsIsIdentifier(): boolean {
// TODO: actually a bit more complex in TypeScript, but shouldn't matter.
// See https://github.com/Microsoft/TypeScript/issues/15008
Expand Down Expand Up @@ -1017,6 +1032,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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");
Expand All @@ -1031,6 +1052,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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();
Expand Down Expand Up @@ -1099,6 +1122,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): 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",
Expand Down Expand Up @@ -1126,11 +1156,22 @@ export default (superClass: Class<Parser>): Class<Parser> =>

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();
Expand Down Expand Up @@ -1260,7 +1301,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>

switch (starttype) {
case tt._function:
return this.parseFunctionStatement(nany);
return this.parseFunctionStatement(
nany,
/* async */ false,
/* declarationPosition */ true,
);
case tt._class:
return this.parseClass(
nany,
Expand Down Expand Up @@ -1531,6 +1576,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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,
Expand Down Expand Up @@ -2214,7 +2267,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

checkLVal(
expr: N.Expression,
bindingType: ?BindingTypes = BIND_NONE,
bindingType: BindingTypes = BIND_NONE,
checkClashes: ?{ [key: string]: boolean },
contextDescription: string,
): void {
Expand Down

0 comments on commit 30d507c

Please sign in to comment.