Skip to content

Commit 4ed3225

Browse files
committed
Add optimized getTypeOfExpression function
1 parent f2a7434 commit 4ed3225

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/compiler/checker.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9000,7 +9000,7 @@ namespace ts {
90009000

90019001
function getTypeWithDefault(type: Type, defaultExpression: Expression) {
90029002
if (defaultExpression) {
9003-
const defaultType = checkExpression(defaultExpression);
9003+
const defaultType = getTypeOfExpression(defaultExpression);
90049004
return getUnionType([getTypeWithFacts(type, TypeFacts.NEUndefined), defaultType]);
90059005
}
90069006
return type;
@@ -9027,7 +9027,7 @@ namespace ts {
90279027
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
90289028
return node.parent.kind === SyntaxKind.ArrayLiteralExpression || node.parent.kind === SyntaxKind.PropertyAssignment ?
90299029
getTypeWithDefault(getAssignedType(node), node.right) :
9030-
checkExpression(node.right);
9030+
getTypeOfExpression(node.right);
90319031
}
90329032

90339033
function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
@@ -9085,7 +9085,7 @@ namespace ts {
90859085
// from its initializer, we'll already have cached the type. Otherwise we compute it now
90869086
// without caching such that transient types are reflected.
90879087
const links = getNodeLinks(node);
9088-
return links.resolvedType || checkExpression(node);
9088+
return links.resolvedType || getTypeOfExpression(node);
90899089
}
90909090

90919091
function getInitialTypeOfVariableDeclaration(node: VariableDeclaration) {
@@ -9145,7 +9145,7 @@ namespace ts {
91459145

91469146
function getTypeOfSwitchClause(clause: CaseClause | DefaultClause) {
91479147
if (clause.kind === SyntaxKind.CaseClause) {
9148-
const caseType = getRegularTypeOfLiteralType(checkExpression((<CaseClause>clause).expression));
9148+
const caseType = getRegularTypeOfLiteralType(getTypeOfExpression((<CaseClause>clause).expression));
91499149
return isUnitType(caseType) ? caseType : undefined;
91509150
}
91519151
return neverType;
@@ -9250,7 +9250,7 @@ namespace ts {
92509250
// we defer subtype reduction until the evolving array type is finalized into a manifest
92519251
// array type.
92529252
function addEvolvingArrayElementType(evolvingArrayType: EvolvingArrayType, node: Expression): EvolvingArrayType {
9253-
const elementType = getBaseTypeOfLiteralType(checkExpression(node));
9253+
const elementType = getBaseTypeOfLiteralType(getTypeOfExpression(node));
92549254
return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType]));
92559255
}
92569256

@@ -9311,7 +9311,7 @@ namespace ts {
93119311
(<BinaryExpression>parent.parent).operatorToken.kind === SyntaxKind.EqualsToken &&
93129312
(<BinaryExpression>parent.parent).left === parent &&
93139313
!isAssignmentTarget(parent.parent) &&
9314-
isTypeAnyOrAllConstituentTypesHaveKind(checkExpression((<ElementAccessExpression>parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined);
9314+
isTypeAnyOrAllConstituentTypesHaveKind(getTypeOfExpression((<ElementAccessExpression>parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined);
93159315
return isLengthPushOrUnshift || isElementAssignment;
93169316
}
93179317

@@ -9473,7 +9473,7 @@ namespace ts {
94739473
}
94749474
}
94759475
else {
9476-
const indexType = checkExpression((<ElementAccessExpression>(<BinaryExpression>node).left).argumentExpression);
9476+
const indexType = getTypeOfExpression((<ElementAccessExpression>(<BinaryExpression>node).left).argumentExpression);
94779477
if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, TypeFlags.NumberLike | TypeFlags.Undefined)) {
94789478
evolvedType = addEvolvingArrayElementType(evolvedType, (<BinaryExpression>node).right);
94799479
}
@@ -9698,7 +9698,7 @@ namespace ts {
96989698
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
96999699
assumeTrue = !assumeTrue;
97009700
}
9701-
const valueType = checkExpression(value);
9701+
const valueType = getTypeOfExpression(value);
97029702
if (valueType.flags & TypeFlags.Nullable) {
97039703
if (!strictNullChecks) {
97049704
return type;
@@ -9785,7 +9785,7 @@ namespace ts {
97859785
}
97869786

97879787
// Check that right operand is a function type with a prototype property
9788-
const rightType = checkExpression(expr.right);
9788+
const rightType = getTypeOfExpression(expr.right);
97899789
if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
97909790
return type;
97919791
}
@@ -9926,7 +9926,7 @@ namespace ts {
99269926
location = location.parent;
99279927
}
99289928
if (isPartOfExpression(location) && !isAssignmentTarget(location)) {
9929-
const type = checkExpression(<Expression>location);
9929+
const type = getTypeOfExpression(<Expression>location);
99309930
if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) {
99319931
return type;
99329932
}
@@ -10794,15 +10794,15 @@ namespace ts {
1079410794

1079510795
// In an assignment expression, the right operand is contextually typed by the type of the left operand.
1079610796
if (node === binaryExpression.right) {
10797-
return checkExpression(binaryExpression.left);
10797+
return getTypeOfExpression(binaryExpression.left);
1079810798
}
1079910799
}
1080010800
else if (operator === SyntaxKind.BarBarToken) {
1080110801
// When an || expression has a contextual type, the operands are contextually typed by that type. When an ||
1080210802
// expression has no contextual type, the right operand is contextually typed by the type of the left operand.
1080310803
let type = getContextualType(binaryExpression);
1080410804
if (!type && node === binaryExpression.right) {
10805-
type = checkExpression(binaryExpression.left);
10805+
type = getTypeOfExpression(binaryExpression.left);
1080610806
}
1080710807
return type;
1080810808
}
@@ -12173,7 +12173,7 @@ namespace ts {
1217312173
if (node.kind === SyntaxKind.ForInStatement &&
1217412174
child === (<ForInStatement>node).statement &&
1217512175
getForInVariableSymbol(<ForInStatement>node) === symbol &&
12176-
hasNumericPropertyNames(checkExpression((<ForInStatement>node).expression))) {
12176+
hasNumericPropertyNames(getTypeOfExpression((<ForInStatement>node).expression))) {
1217712177
return true;
1217812178
}
1217912179
child = node;
@@ -13809,7 +13809,7 @@ namespace ts {
1380913809
if (!node.possiblyExhaustive) {
1381013810
return false;
1381113811
}
13812-
const type = checkExpression(node.expression);
13812+
const type = getTypeOfExpression(node.expression);
1381313813
if (!isLiteralType(type)) {
1381413814
return false;
1381513815
}
@@ -14901,6 +14901,24 @@ namespace ts {
1490114901
return type;
1490214902
}
1490314903

14904+
// Returns the type of an expression. Unlike checkExpression, this function is simply concerned
14905+
// with computing the type and may not fully check all contained sub-expressions for errors.
14906+
function getTypeOfExpression(node: Expression) {
14907+
// Optimize for the common case of a call to a function with a single non-generic call
14908+
// signature where we can just fetch the return type without checking the arguments.
14909+
if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword) {
14910+
const funcType = checkNonNullExpression((<CallExpression>node).expression);
14911+
const signature = getSingleCallSignature(funcType);
14912+
if (signature && !signature.typeParameters) {
14913+
return getReturnTypeOfSignature(signature);
14914+
}
14915+
}
14916+
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
14917+
// should have a parameter that indicates whether full error checking is required such that
14918+
// we can perform the optimizations locally.
14919+
return checkExpression(node);
14920+
}
14921+
1490414922
// Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When
1490514923
// contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the
1490614924
// expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in
@@ -18283,7 +18301,7 @@ namespace ts {
1828318301
}
1828418302
}
1828518303

18286-
enumType = checkExpression(expression);
18304+
enumType = getTypeOfExpression(expression);
1828718305
// allow references to constant members of other enums
1828818306
if (!(enumType.symbol && (enumType.symbol.flags & SymbolFlags.Enum))) {
1828918307
return undefined;
@@ -19453,7 +19471,7 @@ namespace ts {
1945319471
// fallthrough
1945419472

1945519473
case SyntaxKind.SuperKeyword:
19456-
const type = isPartOfExpression(node) ? checkExpression(<Expression>node) : getTypeFromTypeNode(<TypeNode>node);
19474+
const type = isPartOfExpression(node) ? getTypeOfExpression(<Expression>node) : getTypeFromTypeNode(<TypeNode>node);
1945719475
return type.symbol;
1945819476

1945919477
case SyntaxKind.ThisType:
@@ -19483,7 +19501,7 @@ namespace ts {
1948319501
case SyntaxKind.NumericLiteral:
1948419502
// index access
1948519503
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
19486-
const objectType = checkExpression((<ElementAccessExpression>node.parent).expression);
19504+
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
1948719505
if (objectType === unknownType) return undefined;
1948819506
const apparentType = getApparentType(objectType);
1948919507
if (apparentType === unknownType) return undefined;
@@ -19522,7 +19540,7 @@ namespace ts {
1952219540
}
1952319541

1952419542
if (isPartOfExpression(node)) {
19525-
return getTypeOfExpression(<Expression>node);
19543+
return getRegularTypeOfExpression(<Expression>node);
1952619544
}
1952719545

1952819546
if (isExpressionWithTypeArgumentsInClassExtendsClause(node)) {
@@ -19584,7 +19602,7 @@ namespace ts {
1958419602
// If this is from "for" initializer
1958519603
// for ({a } = elems[0];.....) { }
1958619604
if (expr.parent.kind === SyntaxKind.BinaryExpression) {
19587-
const iteratedType = checkExpression((<BinaryExpression>expr.parent).right);
19605+
const iteratedType = getTypeOfExpression((<BinaryExpression>expr.parent).right);
1958819606
return checkDestructuringAssignment(expr, iteratedType || unknownType);
1958919607
}
1959019608
// If this is from nested object binding pattern
@@ -19614,11 +19632,11 @@ namespace ts {
1961419632
return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text);
1961519633
}
1961619634

19617-
function getTypeOfExpression(expr: Expression): Type {
19635+
function getRegularTypeOfExpression(expr: Expression): Type {
1961819636
if (isRightSideOfQualifiedNameOrPropertyAccess(expr)) {
1961919637
expr = <Expression>expr.parent;
1962019638
}
19621-
return getRegularTypeOfLiteralType(checkExpression(expr));
19639+
return getRegularTypeOfLiteralType(getTypeOfExpression(expr));
1962219640
}
1962319641

1962419642
/**
@@ -20045,7 +20063,7 @@ namespace ts {
2004520063
}
2004620064

2004720065
function writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) {
20048-
const type = getWidenedType(getTypeOfExpression(expr));
20066+
const type = getWidenedType(getRegularTypeOfExpression(expr));
2004920067
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
2005020068
}
2005120069

0 commit comments

Comments
 (0)