Skip to content

Commit 1fa759f

Browse files
authored
refactor: extract tt.lt and tt.gt from tt.relation (#13892)
1 parent 05af38c commit 1fa759f

File tree

7 files changed

+143
-137
lines changed

7 files changed

+143
-137
lines changed

packages/babel-parser/src/parser/expression.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,23 +1211,24 @@ export default class ExpressionParser extends LValParser {
12111211

12121212
if (pipeProposal) {
12131213
return this.parseTopicReference(pipeProposal);
1214+
} else {
1215+
throw this.unexpected();
12141216
}
12151217
}
12161218

1217-
// fall through
1218-
case tt.relational: {
1219-
if (this.state.value === "<") {
1220-
const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
1221-
if (
1222-
isIdentifierStart(lookaheadCh) || // Element/Type Parameter <foo>
1223-
lookaheadCh === charCodes.greaterThan // Fragment <>
1224-
) {
1225-
this.expectOnePlugin(["jsx", "flow", "typescript"]);
1226-
}
1219+
case tt.lt: {
1220+
const lookaheadCh = this.input.codePointAt(this.nextTokenStart());
1221+
if (
1222+
isIdentifierStart(lookaheadCh) || // Element/Type Parameter <foo>
1223+
lookaheadCh === charCodes.greaterThan // Fragment <>
1224+
) {
1225+
this.expectOnePlugin(["jsx", "flow", "typescript"]);
1226+
break;
1227+
} else {
1228+
throw this.unexpected();
12271229
}
12281230
}
12291231

1230-
// fall through
12311232
default:
12321233
if (tokenIsIdentifier(type)) {
12331234
if (

packages/babel-parser/src/parser/util.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,6 @@ export default class UtilParser extends Tokenizer {
5050
extra[key] = val;
5151
}
5252

53-
// TODO
54-
55-
isRelational(op: "<" | ">"): boolean {
56-
return this.match(tt.relational) && this.state.value === op;
57-
}
58-
59-
// TODO
60-
61-
expectRelational(op: "<" | ">"): void {
62-
if (this.isRelational(op)) {
63-
this.next();
64-
} else {
65-
this.unexpected(null, tt.relational);
66-
}
67-
}
68-
6953
// Tests whether parsed token is a contextual keyword.
7054

7155
isContextual(token: TokenType): boolean {

packages/babel-parser/src/plugins/flow/index.js

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
316316
const typeNode = this.startNode();
317317
const typeContainer = this.startNode();
318318

319-
if (this.isRelational("<")) {
319+
if (this.match(tt.lt)) {
320320
typeNode.typeParameters = this.flowParseTypeParameterDeclaration();
321321
} else {
322322
typeNode.typeParameters = null;
@@ -600,7 +600,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
600600
node.id.start,
601601
);
602602

603-
if (this.isRelational("<")) {
603+
if (this.match(tt.lt)) {
604604
node.typeParameters = this.flowParseTypeParameterDeclaration();
605605
} else {
606606
node.typeParameters = null;
@@ -643,7 +643,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
643643
const node = this.startNode();
644644

645645
node.id = this.flowParseQualifiedTypeIdentifier();
646-
if (this.isRelational("<")) {
646+
if (this.match(tt.lt)) {
647647
node.typeParameters = this.flowParseTypeParameterInstantiation();
648648
} else {
649649
node.typeParameters = null;
@@ -692,7 +692,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
692692
);
693693
this.scope.declareName(node.id.name, BIND_LEXICAL, node.id.start);
694694

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

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

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

790-
if (!this.isRelational(">")) {
790+
if (!this.match(tt.gt)) {
791791
this.expect(tt.comma);
792792
}
793-
} while (!this.isRelational(">"));
794-
this.expectRelational(">");
793+
} while (!this.match(tt.gt));
794+
this.expect(tt.gt);
795795

796796
this.state.inType = oldInType;
797797

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

806806
this.state.inType = true;
807807

808-
this.expectRelational("<");
808+
this.expect(tt.lt);
809809
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
810810
this.state.noAnonFunctionType = false;
811-
while (!this.isRelational(">")) {
811+
while (!this.match(tt.gt)) {
812812
node.params.push(this.flowParseType());
813-
if (!this.isRelational(">")) {
813+
if (!this.match(tt.gt)) {
814814
this.expect(tt.comma);
815815
}
816816
}
817817
this.state.noAnonFunctionType = oldNoAnonFunctionType;
818-
this.expectRelational(">");
818+
this.expect(tt.gt);
819819

820820
this.state.inType = oldInType;
821821

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

830830
this.state.inType = true;
831831

832-
this.expectRelational("<");
833-
while (!this.isRelational(">")) {
832+
this.expect(tt.lt);
833+
while (!this.match(tt.gt)) {
834834
node.params.push(this.flowParseTypeOrImplicitInstantiation());
835-
if (!this.isRelational(">")) {
835+
if (!this.match(tt.gt)) {
836836
this.expect(tt.comma);
837837
}
838838
}
839-
this.expectRelational(">");
839+
this.expect(tt.gt);
840840

841841
this.state.inType = oldInType;
842842

@@ -902,7 +902,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
902902
node.id = this.flowParseObjectPropertyKey();
903903
this.expect(tt.bracketR);
904904
this.expect(tt.bracketR);
905-
if (this.isRelational("<") || this.match(tt.parenL)) {
905+
if (this.match(tt.lt) || this.match(tt.parenL)) {
906906
node.method = true;
907907
node.optional = false;
908908
node.value = this.flowParseObjectTypeMethodish(
@@ -926,7 +926,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
926926
node.typeParameters = null;
927927
node.this = null;
928928

929-
if (this.isRelational("<")) {
929+
if (this.match(tt.lt)) {
930930
node.typeParameters = this.flowParseTypeParameterDeclaration();
931931
}
932932

@@ -1047,7 +1047,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
10471047
this.flowParseObjectTypeIndexer(node, isStatic, variance),
10481048
);
10491049
}
1050-
} else if (this.match(tt.parenL) || this.isRelational("<")) {
1050+
} else if (this.match(tt.parenL) || this.match(tt.lt)) {
10511051
if (protoStart != null) {
10521052
this.unexpected(protoStart);
10531053
}
@@ -1169,7 +1169,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
11691169
node.kind = kind;
11701170

11711171
let optional = false;
1172-
if (this.isRelational("<") || this.match(tt.parenL)) {
1172+
if (this.match(tt.lt) || this.match(tt.parenL)) {
11731173
// This is a method property
11741174
node.method = true;
11751175

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

1290-
if (this.isRelational("<")) {
1290+
if (this.match(tt.lt)) {
12911291
node.typeParameters = this.flowParseTypeParameterInstantiation();
12921292
}
12931293

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

1456-
case tt.relational:
1457-
if (this.state.value === "<") {
1458-
node.typeParameters = this.flowParseTypeParameterDeclaration();
1459-
this.expect(tt.parenL);
1460-
tmp = this.flowParseFunctionTypeParams();
1461-
node.params = tmp.params;
1462-
node.rest = tmp.rest;
1463-
node.this = tmp._this;
1464-
this.expect(tt.parenR);
1456+
case tt.lt:
1457+
node.typeParameters = this.flowParseTypeParameterDeclaration();
1458+
this.expect(tt.parenL);
1459+
tmp = this.flowParseFunctionTypeParams();
1460+
node.params = tmp.params;
1461+
node.rest = tmp.rest;
1462+
node.this = tmp._this;
1463+
this.expect(tt.parenR);
14651464

1466-
this.expect(tt.arrow);
1465+
this.expect(tt.arrow);
14671466

1468-
node.returnType = this.flowParseType();
1467+
node.returnType = this.flowParseType();
14691468

1470-
return this.finishNode(node, "FunctionTypeAnnotation");
1471-
}
1472-
break;
1469+
return this.finishNode(node, "FunctionTypeAnnotation");
14731470

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

21792176
parseClassId(node: N.Class, isStatement: boolean, optionalId: ?boolean) {
21802177
super.parseClassId(node, isStatement, optionalId);
2181-
if (this.isRelational("<")) {
2178+
if (this.match(tt.lt)) {
21822179
node.typeParameters = this.flowParseTypeParameterDeclaration();
21832180
}
21842181
}
@@ -2241,7 +2238,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
22412238
this.state.inType &&
22422239
(code === charCodes.greaterThan || code === charCodes.lessThan)
22432240
) {
2244-
return this.finishOp(tt.relational, 1);
2241+
return this.finishOp(code === charCodes.greaterThan ? tt.gt : tt.lt, 1);
22452242
} else if (this.state.inType && code === charCodes.questionMark) {
22462243
if (next === charCodes.dot) {
22472244
return this.finishOp(tt.questionDot, 2);
@@ -2369,7 +2366,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
23692366

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

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

@@ -2436,7 +2433,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
24362433
this.unexpected((method: $FlowFixMe).variance.start);
24372434
}
24382435
delete (method: $FlowFixMe).variance;
2439-
if (this.isRelational("<")) {
2436+
if (this.match(tt.lt)) {
24402437
method.typeParameters = this.flowParseTypeParameterDeclaration();
24412438
}
24422439

@@ -2446,7 +2443,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
24462443
// parse a the super class type parameters and implements
24472444
parseClassSuper(node: N.Class): void {
24482445
super.parseClassSuper(node);
2449-
if (node.superClass && this.isRelational("<")) {
2446+
if (node.superClass && this.match(tt.lt)) {
24502447
node.superTypeParameters = this.flowParseTypeParameterInstantiation();
24512448
}
24522449
if (this.isContextual(tt._implements)) {
@@ -2455,7 +2452,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
24552452
do {
24562453
const node = this.startNode();
24572454
node.id = this.flowParseRestrictedIdentifier(/*liberal*/ true);
2458-
if (this.isRelational("<")) {
2455+
if (this.match(tt.lt)) {
24592456
node.typeParameters = this.flowParseTypeParameterInstantiation();
24602457
} else {
24612458
node.typeParameters = null;
@@ -2508,7 +2505,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
25082505
let typeParameters;
25092506

25102507
// method shorthand
2511-
if (this.isRelational("<") && !isAccessor) {
2508+
if (this.match(tt.lt) && !isAccessor) {
25122509
typeParameters = this.flowParseTypeParameterDeclaration();
25132510
if (!this.match(tt.parenL)) this.unexpected();
25142511
}
@@ -2740,7 +2737,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
27402737
parseFunctionParams(node: N.Function, allowModifiers?: boolean): void {
27412738
// $FlowFixMe
27422739
const kind = node.kind;
2743-
if (kind !== "get" && kind !== "set" && this.isRelational("<")) {
2740+
if (kind !== "get" && kind !== "set" && this.match(tt.lt)) {
27442741
node.typeParameters = this.flowParseTypeParameterDeclaration();
27452742
}
27462743
super.parseFunctionParams(node, allowModifiers);
@@ -2798,7 +2795,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
27982795

27992796
if (
28002797
this.hasPlugin("jsx") &&
2801-
(this.match(tt.jsxTagStart) || this.isRelational("<"))
2798+
(this.match(tt.jsxTagStart) || this.match(tt.lt))
28022799
) {
28032800
state = this.state.clone();
28042801

@@ -2823,7 +2820,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
28232820
}
28242821
}
28252822

2826-
if (jsx?.error || this.isRelational("<")) {
2823+
if (jsx?.error || this.match(tt.lt)) {
28272824
state = state || this.state.clone();
28282825

28292826
let typeParameters;
@@ -3020,7 +3017,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
30203017
} else if (
30213018
base.type === "Identifier" &&
30223019
base.name === "async" &&
3023-
this.isRelational("<")
3020+
this.match(tt.lt)
30243021
) {
30253022
const state = this.state.clone();
30263023
const arrow = this.tryParse(
@@ -3081,11 +3078,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
30813078
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
30823079
node.optional = true;
30833080
return this.finishCallExpression(node, /* optional */ true);
3084-
} else if (
3085-
!noCalls &&
3086-
this.shouldParseTypes() &&
3087-
this.isRelational("<")
3088-
) {
3081+
} else if (!noCalls && this.shouldParseTypes() && this.match(tt.lt)) {
30893082
const node = this.startNodeAt(startPos, startLoc);
30903083
node.callee = base;
30913084

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

31193112
parseNewArguments(node: N.NewExpression): void {
31203113
let targs = null;
3121-
if (this.shouldParseTypes() && this.isRelational("<")) {
3114+
if (this.shouldParseTypes() && this.match(tt.lt)) {
31223115
targs = this.tryParse(() =>
31233116
this.flowParseTypeParameterInstantiationCallOrNew(),
31243117
).node;
@@ -3665,7 +3658,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
36653658
return this.finishNode(node, "EnumDeclaration");
36663659
}
36673660

3668-
// check if the next token is a tt.relation("<")
3661+
// check if the next token is a tt.lt
36693662
isLookaheadToken_lt(): boolean {
36703663
const next = this.nextTokenStart();
36713664
if (this.input.charCodeAt(next) === charCodes.lessThan) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy