refactor: reorder checkLVal parameters (#12346)

* refactor: reorder checkLVal parameters

* update allow list

* Apply suggestions from code review
This commit is contained in:
Huáng Jùnliàng
2020-11-17 09:07:32 -05:00
committed by GitHub
parent 0f838be944
commit b564368d6e
7 changed files with 87 additions and 123 deletions

View File

@@ -5,7 +5,7 @@ import type Parser from "../parser";
import type { ExpressionErrors } from "../parser/util";
import * as N from "../types";
import type { Position } from "../util/location";
import { type BindingTypes, BIND_NONE } from "../util/scopeflags";
import { type BindingTypes } from "../util/scopeflags";
import { Errors } from "../parser/error";
function isSimpleProperty(node: N.Node): boolean {
@@ -112,31 +112,26 @@ export default (superClass: Class<Parser>): Class<Parser> =>
checkLVal(
expr: N.Expression,
bindingType: BindingTypes = BIND_NONE,
checkClashes: ?{ [key: string]: boolean },
contextDescription: string,
disallowLetBinding?: boolean,
...args: [
BindingTypes | void,
?Set<string>,
boolean | void,
boolean | void,
]
): void {
switch (expr.type) {
case "ObjectPattern":
expr.properties.forEach(prop => {
this.checkLVal(
prop.type === "Property" ? prop.value : prop,
bindingType,
checkClashes,
"object destructuring pattern",
disallowLetBinding,
...args,
);
});
break;
default:
super.checkLVal(
expr,
bindingType,
checkClashes,
contextDescription,
disallowLetBinding,
);
super.checkLVal(expr, contextDescription, ...args);
}
}

View File

@@ -16,7 +16,6 @@ import * as charCodes from "charcodes";
import { isIteratorStart, isKeyword } from "../util/identifier";
import {
type BindingTypes,
BIND_NONE,
BIND_LEXICAL,
BIND_VAR,
BIND_FUNCTION,
@@ -2264,17 +2263,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
checkLVal(
expr: N.Expression,
bindingType: BindingTypes = BIND_NONE,
checkClashes: ?{ [key: string]: boolean },
contextDescription: string,
...args:
| [string, BindingTypes | void]
| [
string,
BindingTypes | void,
?Set<string>,
boolean | void,
boolean | void,
]
): void {
if (expr.type !== "TypeCastExpression") {
return super.checkLVal(
expr,
bindingType,
checkClashes,
contextDescription,
);
return super.checkLVal(expr, ...args);
}
}
@@ -2481,12 +2481,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
)
: this.parseIdentifier();
this.checkLVal(
specifier.local,
BIND_LEXICAL,
undefined,
contextDescription,
);
this.checkLVal(specifier.local, contextDescription, BIND_LEXICAL);
node.specifiers.push(this.finishNode(specifier, type));
}
@@ -2608,12 +2603,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);
}
this.checkLVal(
specifier.local,
BIND_LEXICAL,
undefined,
"import specifier",
);
this.checkLVal(specifier.local, "import specifier", BIND_LEXICAL);
node.specifiers.push(this.finishNode(specifier, "ImportSpecifier"));
}

View File

@@ -14,7 +14,6 @@ import type { Pos, Position } from "../../util/location";
import type Parser from "../../parser";
import {
type BindingTypes,
BIND_NONE,
SCOPE_TS_MODULE,
SCOPE_OTHER,
BIND_TS_ENUM,
@@ -1221,9 +1220,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.id = this.parseIdentifier();
this.checkLVal(
node.id,
BIND_TS_INTERFACE,
undefined,
"typescript interface declaration",
BIND_TS_INTERFACE,
);
node.typeParameters = this.tsTryParseTypeParameters();
if (this.eat(tt._extends)) {
@@ -1239,7 +1237,7 @@ 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");
this.checkLVal(node.id, "typescript type alias", BIND_TS_TYPE);
node.typeParameters = this.tsTryParseTypeParameters();
node.typeAnnotation = this.tsInType(() => {
@@ -1325,9 +1323,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.id = this.parseIdentifier();
this.checkLVal(
node.id,
isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM,
undefined,
"typescript enum declaration",
isConst ? BIND_TS_CONST_ENUM : BIND_TS_ENUM,
);
this.expect(tt.braceL);
@@ -1364,9 +1361,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!nested) {
this.checkLVal(
node.id,
BIND_TS_NAMESPACE,
null,
"module or namespace declaration",
BIND_TS_NAMESPACE,
);
}
@@ -1414,12 +1410,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): N.TsImportEqualsDeclaration {
node.isExport = isExport || false;
node.id = this.parseIdentifier();
this.checkLVal(
node.id,
BIND_LEXICAL,
undefined,
"import equals declaration",
);
this.checkLVal(node.id, "import equals declaration", BIND_LEXICAL);
this.expect(tt.eq);
node.moduleReference = this.tsParseModuleReference();
this.semicolon();
@@ -1805,7 +1796,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!node.body && node.id) {
// Function ids are validated after parsing their body.
// For bodyless function, we need to do it here.
this.checkLVal(node.id, BIND_TS_AMBIENT, null, "function name");
this.checkLVal(node.id, "function name", BIND_TS_AMBIENT);
} else {
super.registerFunctionStatementId(...arguments);
}
@@ -2615,9 +2606,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
checkLVal(
expr: N.Expression,
bindingType: BindingTypes = BIND_NONE,
checkClashes: ?{ [key: string]: boolean },
contextDescription: string,
...args:
| [BindingTypes | void]
| [BindingTypes | void, ?Set<string>, boolean | void, boolean | void]
): void {
switch (expr.type) {
case "TSTypeCastExpression":
@@ -2626,25 +2618,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// e.g. `const f = (foo: number = 0) => foo;`
return;
case "TSParameterProperty":
this.checkLVal(
expr.parameter,
bindingType,
checkClashes,
"parameter property",
);
this.checkLVal(expr.parameter, "parameter property", ...args);
return;
case "TSAsExpression":
case "TSNonNullExpression":
case "TSTypeAssertion":
this.checkLVal(
expr.expression,
bindingType,
checkClashes,
contextDescription,
);
this.checkLVal(expr.expression, contextDescription, ...args);
return;
default:
super.checkLVal(expr, bindingType, checkClashes, contextDescription);
super.checkLVal(expr, contextDescription, ...args);
return;
}
}