Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract tt.lt and tt.gt from tt.relation #13892

Merged
merged 1 commit into from Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1211,23 +1211,24 @@ export default class ExpressionParser extends LValParser {

if (pipeProposal) {
return this.parseTopicReference(pipeProposal);
} else {
throw this.unexpected();
}
}

// fall through
case tt.relational: {
if (this.state.value === "<") {
const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
if (
isIdentifierStart(lookaheadCh) || // Element/Type Parameter <foo>
lookaheadCh === charCodes.greaterThan // Fragment <>
) {
this.expectOnePlugin(["jsx", "flow", "typescript"]);
}
case tt.lt: {
const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
if (
isIdentifierStart(lookaheadCh) || // Element/Type Parameter <foo>
lookaheadCh === charCodes.greaterThan // Fragment <>
) {
this.expectOnePlugin(["jsx", "flow", "typescript"]);
break;
} else {
throw this.unexpected();
}
}

// fall through
default:
if (tokenIsIdentifier(type)) {
if (
Expand Down
16 changes: 0 additions & 16 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -50,22 +50,6 @@ export default class UtilParser extends Tokenizer {
extra[key] = val;
}

// TODO

isRelational(op: "<" | ">"): boolean {
return this.match(tt.relational) && this.state.value === op;
}

// TODO

expectRelational(op: "<" | ">"): void {
if (this.isRelational(op)) {
this.next();
} else {
this.unexpected(null, tt.relational);
}
}

// Tests whether parsed token is a contextual keyword.

isContextual(token: TokenType): boolean {
Expand Down
103 changes: 48 additions & 55 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -316,7 +316,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const typeNode = this.startNode();
const typeContainer = this.startNode();

if (this.isRelational("<")) {
if (this.match(tt.lt)) {
typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
} else {
typeNode.typeParameters = null;
Expand Down Expand Up @@ -600,7 +600,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.id.start,
);

if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
} else {
node.typeParameters = null;
Expand Down Expand Up @@ -643,7 +643,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const node = this.startNode();

node.id = this.flowParseQualifiedTypeIdentifier();
if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterInstantiation();
} else {
node.typeParameters = null;
Expand Down Expand Up @@ -692,7 +692,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);
this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);

if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
} else {
node.typeParameters = null;
Expand All @@ -715,7 +715,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
);
this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);

if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
} else {
node.typeParameters = null;
Expand Down Expand Up @@ -770,7 +770,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.state.inType = true;

// istanbul ignore else: this condition is already checked at all call sites
if (this.isRelational("<") || this.match(tt.jsxTagStart)) {
if (this.match(tt.lt) || this.match(tt.jsxTagStart)) {
this.next();
} else {
this.unexpected();
Expand All @@ -787,11 +787,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
defaultRequired = true;
}

if (!this.isRelational(">")) {
if (!this.match(tt.gt)) {
this.expect(tt.comma);
}
} while (!this.isRelational(">"));
this.expectRelational(">");
} while (!this.match(tt.gt));
this.expect(tt.gt);

this.state.inType = oldInType;

Expand All @@ -805,17 +805,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>

this.state.inType = true;

this.expectRelational("<");
this.expect(tt.lt);
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
this.state.noAnonFunctionType = false;
while (!this.isRelational(">")) {
while (!this.match(tt.gt)) {
node.params.push(this.flowParseType());
if (!this.isRelational(">")) {
if (!this.match(tt.gt)) {
this.expect(tt.comma);
}
}
this.state.noAnonFunctionType = oldNoAnonFunctionType;
this.expectRelational(">");
this.expect(tt.gt);

this.state.inType = oldInType;

Expand All @@ -829,14 +829,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>

this.state.inType = true;

this.expectRelational("<");
while (!this.isRelational(">")) {
this.expect(tt.lt);
while (!this.match(tt.gt)) {
node.params.push(this.flowParseTypeOrImplicitInstantiation());
if (!this.isRelational(">")) {
if (!this.match(tt.gt)) {
this.expect(tt.comma);
}
}
this.expectRelational(">");
this.expect(tt.gt);

this.state.inType = oldInType;

Expand Down Expand Up @@ -902,7 +902,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.id = this.flowParseObjectPropertyKey();
this.expect(tt.bracketR);
this.expect(tt.bracketR);
if (this.isRelational("<") || this.match(tt.parenL)) {
if (this.match(tt.lt) || this.match(tt.parenL)) {
node.method = true;
node.optional = false;
node.value = this.flowParseObjectTypeMethodish(
Expand All @@ -926,7 +926,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeParameters = null;
node.this = null;

if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
}

Expand Down Expand Up @@ -1047,7 +1047,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.flowParseObjectTypeIndexer(node, isStatic, variance),
);
}
} else if (this.match(tt.parenL) || this.isRelational("<")) {
} else if (this.match(tt.parenL) || this.match(tt.lt)) {
if (protoStart != null) {
this.unexpected(protoStart);
}
Expand Down Expand Up @@ -1169,7 +1169,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.kind = kind;

let optional = false;
if (this.isRelational("<") || this.match(tt.parenL)) {
if (this.match(tt.lt) || this.match(tt.parenL)) {
// This is a method property
node.method = true;

Expand Down Expand Up @@ -1287,7 +1287,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.typeParameters = null;
node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id);

if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterInstantiation();
}

Expand Down Expand Up @@ -1453,23 +1453,20 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.state.noAnonFunctionType = oldNoAnonFunctionType;
return type;

case tt.relational:
if (this.state.value === "<") {
node.typeParameters = this.flowParseTypeParameterDeclaration();
this.expect(tt.parenL);
tmp = this.flowParseFunctionTypeParams();
node.params = tmp.params;
node.rest = tmp.rest;
node.this = tmp._this;
this.expect(tt.parenR);
case tt.lt:
node.typeParameters = this.flowParseTypeParameterDeclaration();
this.expect(tt.parenL);
tmp = this.flowParseFunctionTypeParams();
node.params = tmp.params;
node.rest = tmp.rest;
node.this = tmp._this;
this.expect(tt.parenR);

this.expect(tt.arrow);
this.expect(tt.arrow);

node.returnType = this.flowParseType();
node.returnType = this.flowParseType();

return this.finishNode(node, "FunctionTypeAnnotation");
}
break;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to break for other tt.relational tokens, they will not be handled by the big switch at all and eventually thrown as unexpected token.

return this.finishNode(node, "FunctionTypeAnnotation");

case tt.parenL:
this.next();
Expand Down Expand Up @@ -2178,7 +2175,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

parseClassId(node: N.Class, isStatement: boolean, optionalId: ?boolean) {
super.parseClassId(node, isStatement, optionalId);
if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
}
}
Expand Down Expand Up @@ -2241,7 +2238,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.state.inType &&
(code === charCodes.greaterThan || code === charCodes.lessThan)
) {
return this.finishOp(tt.relational, 1);
return this.finishOp(code === charCodes.greaterThan ? tt.gt : tt.lt, 1);
} else if (this.state.inType && code === charCodes.questionMark) {
if (next === charCodes.dot) {
return this.finishOp(tt.questionDot, 2);
Expand Down Expand Up @@ -2369,7 +2366,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

// determine whether or not we're currently in the position where a class method would appear
isClassMethod(): boolean {
return this.isRelational("<") || super.isClassMethod();
return this.match(tt.lt) || super.isClassMethod();
}

// determine whether or not we're currently in the position where a class property would appear
Expand All @@ -2394,7 +2391,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.unexpected((method: $FlowFixMe).variance.start);
}
delete (method: $FlowFixMe).variance;
if (this.isRelational("<")) {
if (this.match(tt.lt)) {
method.typeParameters = this.flowParseTypeParameterDeclaration();
}

Expand Down Expand Up @@ -2436,7 +2433,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
this.unexpected((method: $FlowFixMe).variance.start);
}
delete (method: $FlowFixMe).variance;
if (this.isRelational("<")) {
if (this.match(tt.lt)) {
method.typeParameters = this.flowParseTypeParameterDeclaration();
}

Expand All @@ -2446,7 +2443,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// parse a the super class type parameters and implements
parseClassSuper(node: N.Class): void {
super.parseClassSuper(node);
if (node.superClass && this.isRelational("<")) {
if (node.superClass && this.match(tt.lt)) {
node.superTypeParameters = this.flowParseTypeParameterInstantiation();
}
if (this.isContextual(tt._implements)) {
Expand All @@ -2455,7 +2452,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
do {
const node = this.startNode();
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ true);
if (this.isRelational("<")) {
if (this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterInstantiation();
} else {
node.typeParameters = null;
Expand Down Expand Up @@ -2508,7 +2505,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
let typeParameters;

// method shorthand
if (this.isRelational("<") && !isAccessor) {
if (this.match(tt.lt) && !isAccessor) {
typeParameters = this.flowParseTypeParameterDeclaration();
if (!this.match(tt.parenL)) this.unexpected();
}
Expand Down Expand Up @@ -2740,7 +2737,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
parseFunctionParams(node: N.Function, allowModifiers?: boolean): void {
// $FlowFixMe
const kind = node.kind;
if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
if (kind !== "get" && kind !== "set" && this.match(tt.lt)) {
node.typeParameters = this.flowParseTypeParameterDeclaration();
}
super.parseFunctionParams(node, allowModifiers);
Expand Down Expand Up @@ -2798,7 +2795,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

if (
this.hasPlugin("jsx") &&
(this.match(tt.jsxTagStart) || this.isRelational("<"))
(this.match(tt.jsxTagStart) || this.match(tt.lt))
) {
state = this.state.clone();

Expand All @@ -2823,7 +2820,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}

if (jsx?.error || this.isRelational("<")) {
if (jsx?.error || this.match(tt.lt)) {
state = state || this.state.clone();

let typeParameters;
Expand Down Expand Up @@ -3020,7 +3017,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
} else if (
base.type === "Identifier" &&
base.name === "async" &&
this.isRelational("<")
this.match(tt.lt)
) {
const state = this.state.clone();
const arrow = this.tryParse(
Expand Down Expand Up @@ -3081,11 +3078,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
node.optional = true;
return this.finishCallExpression(node, /* optional */ true);
} else if (
!noCalls &&
this.shouldParseTypes() &&
this.isRelational("<")
) {
} else if (!noCalls && this.shouldParseTypes() && this.match(tt.lt)) {
const node = this.startNodeAt(startPos, startLoc);
node.callee = base;

Expand Down Expand Up @@ -3118,7 +3111,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

parseNewArguments(node: N.NewExpression): void {
let targs = null;
if (this.shouldParseTypes() && this.isRelational("<")) {
if (this.shouldParseTypes() && this.match(tt.lt)) {
targs = this.tryParse(() =>
this.flowParseTypeParameterInstantiationCallOrNew(),
).node;
Expand Down Expand Up @@ -3665,7 +3658,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "EnumDeclaration");
}

// check if the next token is a tt.relation("<")
// check if the next token is a tt.lt
isLookaheadToken_lt(): boolean {
const next = this.nextTokenStart();
if (this.input.charCodeAt(next) === charCodes.lessThan) {
Expand Down