Do not register ambient classes to the TS scope (#10352)

This commit is contained in:
Nicolò Ribaudo
2019-08-21 00:22:47 +02:00
committed by Brian Ng
parent 11ed2e2bf5
commit 15aa511b8e
10 changed files with 468 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import {
SCOPE_OTHER,
SCOPE_SIMPLE_CATCH,
SCOPE_SUPER,
type BindingTypes,
} from "../util/scopeflags";
const loopLabel = { kind: "loop" },
@@ -1611,11 +1612,12 @@ export default class StatementParser extends ExpressionParser {
node: N.Class,
isStatement: boolean,
optionalId: ?boolean,
bindingType: BindingTypes = BIND_CLASS,
): void {
if (this.match(tt.name)) {
node.id = this.parseIdentifier();
if (isStatement) {
this.checkLVal(node.id, BIND_CLASS, undefined, "class name");
this.checkLVal(node.id, bindingType, undefined, "class name");
}
} else {
if (optionalId || !isStatement) {

View File

@@ -14,8 +14,9 @@ import {
BIND_TS_CONST_ENUM,
BIND_TS_TYPE,
BIND_TS_INTERFACE,
BIND_TS_FN_TYPE,
BIND_TS_AMBIENT,
BIND_TS_NAMESPACE,
BIND_CLASS,
} from "../../util/scopeflags";
import TypeScriptScopeHandler from "./scope";
@@ -1278,6 +1279,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
/* declarationPosition */ true,
);
case tt._class:
// While this is also set by tsParseExpressionStatement, we need to set it
// before parsing the class declaration to now how to register it in the scope.
nany.declare = true;
return this.parseClass(
nany,
/* isStatement */ true,
@@ -1552,7 +1556,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
checkFunctionStatementId(node: N.Function): void {
if (!node.body && node.id) {
this.checkLVal(node.id, BIND_TS_FN_TYPE, null, "function name");
this.checkLVal(node.id, BIND_TS_AMBIENT, null, "function name");
} else {
super.checkFunctionStatementId(...arguments);
}
@@ -1988,7 +1992,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return;
}
super.parseClassId(...arguments);
super.parseClassId(
node,
isStatement,
optionalId,
(node: any).declare ? BIND_TS_AMBIENT : BIND_CLASS,
);
const typeParameters = this.tsTryParseTypeParameters();
if (typeParameters) node.typeParameters = typeParameters;
}

View File

@@ -25,7 +25,7 @@ class TypeScriptScope extends Scope {
// classes (which are also in .lexical) and interface (which are also in .types)
classes: string[] = [];
// namespaces and bodyless-functions are too difficult to track,
// namespaces and ambient functions (or classes) 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.

View File

@@ -62,14 +62,14 @@ export const BIND_CLASS = BIND_KIND_VALUE | BIND_KIND_TYPE | BIND_SCOPE_
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,
BIND_TS_AMBIENT = 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;
BIND_TS_NAMESPACE = 0 | 0 | 0 | BIND_FLAGS_TS_EXPORT_ONLY;
export type BindingTypes =
| typeof BIND_NONE
@@ -81,5 +81,5 @@ export type BindingTypes =
| typeof BIND_TS_INTERFACE
| typeof BIND_TS_TYPE
| typeof BIND_TS_ENUM
| typeof BIND_TS_FN_TYPE
| typeof BIND_TS_AMBIENT
| typeof BIND_TS_NAMESPACE;