Skip to content

Commit 2b0503b

Browse files
Swap checkExpressionWorker to use an array of functions instead of a switch statement.
1 parent 71e8529 commit 2b0503b

File tree

1 file changed

+66
-104
lines changed

1 file changed

+66
-104
lines changed

src/compiler/checker.ts

Lines changed: 66 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,70 @@ namespace ts {
10681068
[".json", ".json"],
10691069
];
10701070

1071+
const checkExpressionWorkerTable = new Array<(node: any, checkMode: CheckMode | undefined, forceTuple: boolean | undefined) => Type>(SyntaxKind.Count);
1072+
checkExpressionWorkerTable[SyntaxKind.Identifier] = checkIdentifier;
1073+
checkExpressionWorkerTable[SyntaxKind.PrivateIdentifier] = checkPrivateIdentifierExpression;
1074+
checkExpressionWorkerTable[SyntaxKind.ThisKeyword] = checkThisExpression;
1075+
checkExpressionWorkerTable[SyntaxKind.SuperKeyword] = checkSuperExpression;
1076+
checkExpressionWorkerTable[SyntaxKind.NullKeyword] = (_node, _checkMode, _forceTuple) => nullWideningType;
1077+
checkExpressionWorkerTable[SyntaxKind.NoSubstitutionTemplateLiteral] = checkExpressionWorkerTable[SyntaxKind.StringLiteral] =
1078+
(node: StringLiteralLike, _checkMode) => getFreshTypeOfLiteralType(getStringLiteralType(node.text));
1079+
checkExpressionWorkerTable[SyntaxKind.NumericLiteral] = (node: NumericLiteral, _checkMode, _forceTuple) => {
1080+
checkGrammarNumericLiteral(node);
1081+
return getFreshTypeOfLiteralType(getNumberLiteralType(+(node).text));
1082+
};
1083+
checkExpressionWorkerTable[SyntaxKind.BigIntLiteral] = (node: BigIntLiteral, _checkMode, _forceTuple) => {
1084+
checkGrammarBigIntLiteral(node);
1085+
return getFreshTypeOfLiteralType(getBigIntLiteralType({
1086+
negative: false,
1087+
base10Value: parsePseudoBigInt((node).text)
1088+
}));
1089+
};
1090+
checkExpressionWorkerTable[SyntaxKind.TrueKeyword] = (_node, _checkMode) => trueType;
1091+
checkExpressionWorkerTable[SyntaxKind.FalseKeyword] = (_node, _checkMode) => falseType;
1092+
checkExpressionWorkerTable[SyntaxKind.TemplateExpression] = checkTemplateExpression;
1093+
checkExpressionWorkerTable[SyntaxKind.RegularExpressionLiteral] = (_node, _checkMode) => globalRegExpType;
1094+
checkExpressionWorkerTable[SyntaxKind.ArrayLiteralExpression] = checkArrayLiteral;
1095+
checkExpressionWorkerTable[SyntaxKind.ObjectLiteralExpression] = checkObjectLiteral;
1096+
checkExpressionWorkerTable[SyntaxKind.PropertyAccessExpression] = checkPropertyAccessExpression;
1097+
checkExpressionWorkerTable[SyntaxKind.QualifiedName] = checkQualifiedName;
1098+
checkExpressionWorkerTable[SyntaxKind.ElementAccessExpression] = checkIndexedAccess;
1099+
checkExpressionWorkerTable[SyntaxKind.CallExpression] = (node: CallExpression, checkMode) => {
1100+
if (node.expression.kind === SyntaxKind.ImportKeyword) {
1101+
return checkImportCallExpression(node as ImportCall);
1102+
};
1103+
return checkCallExpression(node, checkMode);
1104+
};
1105+
checkExpressionWorkerTable[SyntaxKind.NewExpression] = checkCallExpression;
1106+
checkExpressionWorkerTable[SyntaxKind.TaggedTemplateExpression] = checkTaggedTemplateExpression;
1107+
checkExpressionWorkerTable[SyntaxKind.ParenthesizedExpression] = checkParenthesizedExpression;
1108+
checkExpressionWorkerTable[SyntaxKind.ClassExpression] = checkClassExpression;
1109+
checkExpressionWorkerTable[SyntaxKind.FunctionExpression] =
1110+
checkExpressionWorkerTable[SyntaxKind.ArrowFunction] = checkFunctionExpressionOrObjectLiteralMethod;
1111+
checkExpressionWorkerTable[SyntaxKind.TypeOfExpression] = checkTypeOfExpression;
1112+
checkExpressionWorkerTable[SyntaxKind.TypeAssertionExpression] =
1113+
checkExpressionWorkerTable[SyntaxKind.AsExpression] = checkAssertion;
1114+
checkExpressionWorkerTable[SyntaxKind.NonNullExpression] = checkNonNullAssertion;
1115+
checkExpressionWorkerTable[SyntaxKind.ExpressionWithTypeArguments] = checkExpressionWithTypeArguments;
1116+
checkExpressionWorkerTable[SyntaxKind.MetaProperty] = checkMetaProperty;
1117+
checkExpressionWorkerTable[SyntaxKind.DeleteExpression] = checkDeleteExpression;
1118+
checkExpressionWorkerTable[SyntaxKind.VoidExpression] = checkVoidExpression;
1119+
checkExpressionWorkerTable[SyntaxKind.AwaitExpression] = checkAwaitExpression;
1120+
checkExpressionWorkerTable[SyntaxKind.PrefixUnaryExpression] = checkPrefixUnaryExpression;
1121+
checkExpressionWorkerTable[SyntaxKind.PostfixUnaryExpression] = checkPostfixUnaryExpression;
1122+
checkExpressionWorkerTable[SyntaxKind.BinaryExpression] = checkBinaryExpression;
1123+
checkExpressionWorkerTable[SyntaxKind.ConditionalExpression] = checkConditionalExpression;
1124+
checkExpressionWorkerTable[SyntaxKind.SpreadElement] = checkSpreadExpression;
1125+
checkExpressionWorkerTable[SyntaxKind.OmittedExpression] = (_node, _checkMode) => undefinedWideningType;
1126+
checkExpressionWorkerTable[SyntaxKind.YieldExpression] = checkYieldExpression;
1127+
checkExpressionWorkerTable[SyntaxKind.SyntheticExpression] = checkSyntheticExpression;
1128+
checkExpressionWorkerTable[SyntaxKind.JsxExpression] = checkJsxExpression;
1129+
checkExpressionWorkerTable[SyntaxKind.JsxElement] = checkJsxElement;
1130+
checkExpressionWorkerTable[SyntaxKind.JsxSelfClosingElement] = checkJsxSelfClosingElement;
1131+
checkExpressionWorkerTable[SyntaxKind.JsxFragment] = checkJsxFragment;
1132+
checkExpressionWorkerTable[SyntaxKind.JsxAttributes] = checkJsxAttributes;
1133+
checkExpressionWorkerTable[SyntaxKind.JsxOpeningElement] = (_node, _checkMode) => Debug.fail("Shouldn't ever directly check a JsxOpeningElement");
1134+
10711135
initializeTypeChecker();
10721136

10731137
return checker;
@@ -35105,110 +35169,8 @@ namespace ts {
3510535169
cancellationToken.throwIfCancellationRequested();
3510635170
}
3510735171
}
35108-
switch (kind) {
35109-
case SyntaxKind.Identifier:
35110-
return checkIdentifier(node as Identifier, checkMode);
35111-
case SyntaxKind.PrivateIdentifier:
35112-
return checkPrivateIdentifierExpression(node as PrivateIdentifier);
35113-
case SyntaxKind.ThisKeyword:
35114-
return checkThisExpression(node);
35115-
case SyntaxKind.SuperKeyword:
35116-
return checkSuperExpression(node);
35117-
case SyntaxKind.NullKeyword:
35118-
return nullWideningType;
35119-
case SyntaxKind.NoSubstitutionTemplateLiteral:
35120-
case SyntaxKind.StringLiteral:
35121-
return getFreshTypeOfLiteralType(getStringLiteralType((node as StringLiteralLike).text));
35122-
case SyntaxKind.NumericLiteral:
35123-
checkGrammarNumericLiteral(node as NumericLiteral);
35124-
return getFreshTypeOfLiteralType(getNumberLiteralType(+(node as NumericLiteral).text));
35125-
case SyntaxKind.BigIntLiteral:
35126-
checkGrammarBigIntLiteral(node as BigIntLiteral);
35127-
return getFreshTypeOfLiteralType(getBigIntLiteralType({
35128-
negative: false,
35129-
base10Value: parsePseudoBigInt((node as BigIntLiteral).text)
35130-
}));
35131-
case SyntaxKind.TrueKeyword:
35132-
return trueType;
35133-
case SyntaxKind.FalseKeyword:
35134-
return falseType;
35135-
case SyntaxKind.TemplateExpression:
35136-
return checkTemplateExpression(node as TemplateExpression);
35137-
case SyntaxKind.RegularExpressionLiteral:
35138-
return globalRegExpType;
35139-
case SyntaxKind.ArrayLiteralExpression:
35140-
return checkArrayLiteral(node as ArrayLiteralExpression, checkMode, forceTuple);
35141-
case SyntaxKind.ObjectLiteralExpression:
35142-
return checkObjectLiteral(node as ObjectLiteralExpression, checkMode);
35143-
case SyntaxKind.PropertyAccessExpression:
35144-
return checkPropertyAccessExpression(node as PropertyAccessExpression, checkMode);
35145-
case SyntaxKind.QualifiedName:
35146-
return checkQualifiedName(node as QualifiedName, checkMode);
35147-
case SyntaxKind.ElementAccessExpression:
35148-
return checkIndexedAccess(node as ElementAccessExpression, checkMode);
35149-
case SyntaxKind.CallExpression:
35150-
if ((node as CallExpression).expression.kind === SyntaxKind.ImportKeyword) {
35151-
return checkImportCallExpression(node as ImportCall);
35152-
}
35153-
// falls through
35154-
case SyntaxKind.NewExpression:
35155-
return checkCallExpression(node as CallExpression, checkMode);
35156-
case SyntaxKind.TaggedTemplateExpression:
35157-
return checkTaggedTemplateExpression(node as TaggedTemplateExpression);
35158-
case SyntaxKind.ParenthesizedExpression:
35159-
return checkParenthesizedExpression(node as ParenthesizedExpression, checkMode);
35160-
case SyntaxKind.ClassExpression:
35161-
return checkClassExpression(node as ClassExpression);
35162-
case SyntaxKind.FunctionExpression:
35163-
case SyntaxKind.ArrowFunction:
35164-
return checkFunctionExpressionOrObjectLiteralMethod(node as FunctionExpression | ArrowFunction, checkMode);
35165-
case SyntaxKind.TypeOfExpression:
35166-
return checkTypeOfExpression(node as TypeOfExpression);
35167-
case SyntaxKind.TypeAssertionExpression:
35168-
case SyntaxKind.AsExpression:
35169-
return checkAssertion(node as AssertionExpression);
35170-
case SyntaxKind.NonNullExpression:
35171-
return checkNonNullAssertion(node as NonNullExpression);
35172-
case SyntaxKind.ExpressionWithTypeArguments:
35173-
return checkExpressionWithTypeArguments(node as ExpressionWithTypeArguments);
35174-
case SyntaxKind.MetaProperty:
35175-
return checkMetaProperty(node as MetaProperty);
35176-
case SyntaxKind.DeleteExpression:
35177-
return checkDeleteExpression(node as DeleteExpression);
35178-
case SyntaxKind.VoidExpression:
35179-
return checkVoidExpression(node as VoidExpression);
35180-
case SyntaxKind.AwaitExpression:
35181-
return checkAwaitExpression(node as AwaitExpression);
35182-
case SyntaxKind.PrefixUnaryExpression:
35183-
return checkPrefixUnaryExpression(node as PrefixUnaryExpression);
35184-
case SyntaxKind.PostfixUnaryExpression:
35185-
return checkPostfixUnaryExpression(node as PostfixUnaryExpression);
35186-
case SyntaxKind.BinaryExpression:
35187-
return checkBinaryExpression(node as BinaryExpression, checkMode);
35188-
case SyntaxKind.ConditionalExpression:
35189-
return checkConditionalExpression(node as ConditionalExpression, checkMode);
35190-
case SyntaxKind.SpreadElement:
35191-
return checkSpreadExpression(node as SpreadElement, checkMode);
35192-
case SyntaxKind.OmittedExpression:
35193-
return undefinedWideningType;
35194-
case SyntaxKind.YieldExpression:
35195-
return checkYieldExpression(node as YieldExpression);
35196-
case SyntaxKind.SyntheticExpression:
35197-
return checkSyntheticExpression(node as SyntheticExpression);
35198-
case SyntaxKind.JsxExpression:
35199-
return checkJsxExpression(node as JsxExpression, checkMode);
35200-
case SyntaxKind.JsxElement:
35201-
return checkJsxElement(node as JsxElement, checkMode);
35202-
case SyntaxKind.JsxSelfClosingElement:
35203-
return checkJsxSelfClosingElement(node as JsxSelfClosingElement, checkMode);
35204-
case SyntaxKind.JsxFragment:
35205-
return checkJsxFragment(node as JsxFragment);
35206-
case SyntaxKind.JsxAttributes:
35207-
return checkJsxAttributes(node as JsxAttributes, checkMode);
35208-
case SyntaxKind.JsxOpeningElement:
35209-
Debug.fail("Shouldn't ever directly check a JsxOpeningElement");
35210-
}
35211-
return errorType;
35172+
const result = checkExpressionWorkerTable[kind]?.(node, checkMode, forceTuple);
35173+
return result ?? errorType;
3521235174
}
3521335175

3521435176
// DECLARATION AND STATEMENT TYPE CHECKING

0 commit comments

Comments
 (0)