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

Introduce 'getTypeOfExpression' function #12396

Merged
merged 3 commits into from
Nov 22, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 40 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9000,7 +9000,7 @@ namespace ts {

function getTypeWithDefault(type: Type, defaultExpression: Expression) {
if (defaultExpression) {
const defaultType = checkExpression(defaultExpression);
const defaultType = getTypeOfExpression(defaultExpression);
return getUnionType([getTypeWithFacts(type, TypeFacts.NEUndefined), defaultType]);
}
return type;
Expand All @@ -9027,7 +9027,7 @@ namespace ts {
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
return node.parent.kind === SyntaxKind.ArrayLiteralExpression || node.parent.kind === SyntaxKind.PropertyAssignment ?
getTypeWithDefault(getAssignedType(node), node.right) :
checkExpression(node.right);
getTypeOfExpression(node.right);
}

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

function getInitialTypeOfVariableDeclaration(node: VariableDeclaration) {
Expand Down Expand Up @@ -9145,7 +9145,7 @@ namespace ts {

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

Expand Down Expand Up @@ -9311,7 +9311,7 @@ namespace ts {
(<BinaryExpression>parent.parent).operatorToken.kind === SyntaxKind.EqualsToken &&
(<BinaryExpression>parent.parent).left === parent &&
!isAssignmentTarget(parent.parent) &&
isTypeAnyOrAllConstituentTypesHaveKind(checkExpression((<ElementAccessExpression>parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined);
isTypeAnyOrAllConstituentTypesHaveKind(getTypeOfExpression((<ElementAccessExpression>parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined);
return isLengthPushOrUnshift || isElementAssignment;
}

Expand Down Expand Up @@ -9473,7 +9473,7 @@ namespace ts {
}
}
else {
const indexType = checkExpression((<ElementAccessExpression>(<BinaryExpression>node).left).argumentExpression);
const indexType = getTypeOfExpression((<ElementAccessExpression>(<BinaryExpression>node).left).argumentExpression);
if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, TypeFlags.NumberLike | TypeFlags.Undefined)) {
evolvedType = addEvolvingArrayElementType(evolvedType, (<BinaryExpression>node).right);
}
Expand Down Expand Up @@ -9698,7 +9698,7 @@ namespace ts {
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
assumeTrue = !assumeTrue;
}
const valueType = checkExpression(value);
const valueType = getTypeOfExpression(value);
if (valueType.flags & TypeFlags.Nullable) {
if (!strictNullChecks) {
return type;
Expand Down Expand Up @@ -9785,7 +9785,7 @@ namespace ts {
}

// Check that right operand is a function type with a prototype property
const rightType = checkExpression(expr.right);
const rightType = getTypeOfExpression(expr.right);
if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
return type;
}
Expand Down Expand Up @@ -9926,7 +9926,7 @@ namespace ts {
location = location.parent;
}
if (isPartOfExpression(location) && !isAssignmentTarget(location)) {
const type = checkExpression(<Expression>location);
const type = getTypeOfExpression(<Expression>location);
if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) {
return type;
}
Expand Down Expand Up @@ -10794,15 +10794,15 @@ namespace ts {

// In an assignment expression, the right operand is contextually typed by the type of the left operand.
if (node === binaryExpression.right) {
return checkExpression(binaryExpression.left);
return getTypeOfExpression(binaryExpression.left);
}
}
else if (operator === SyntaxKind.BarBarToken) {
// When an || expression has a contextual type, the operands are contextually typed by that type. When an ||
// expression has no contextual type, the right operand is contextually typed by the type of the left operand.
let type = getContextualType(binaryExpression);
if (!type && node === binaryExpression.right) {
type = checkExpression(binaryExpression.left);
type = getTypeOfExpression(binaryExpression.left);
}
return type;
}
Expand Down Expand Up @@ -12173,7 +12173,7 @@ namespace ts {
if (node.kind === SyntaxKind.ForInStatement &&
child === (<ForInStatement>node).statement &&
getForInVariableSymbol(<ForInStatement>node) === symbol &&
hasNumericPropertyNames(checkExpression((<ForInStatement>node).expression))) {
hasNumericPropertyNames(getTypeOfExpression((<ForInStatement>node).expression))) {
return true;
}
child = node;
Expand Down Expand Up @@ -13809,7 +13809,7 @@ namespace ts {
if (!node.possiblyExhaustive) {
return false;
}
const type = checkExpression(node.expression);
const type = getTypeOfExpression(node.expression);
if (!isLiteralType(type)) {
return false;
}
Expand Down Expand Up @@ -14901,6 +14901,24 @@ namespace ts {
return type;
}

// Returns the type of an expression. Unlike checkExpression, this function is simply concerned
// with computing the type and may not fully check all contained sub-expressions for errors.
function getTypeOfExpression(node: Expression) {
// Optimize for the common case of a call to a function with a single non-generic call
// signature where we can just fetch the return type without checking the arguments.
if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword) {
const funcType = checkNonNullExpression((<CallExpression>node).expression);
const signature = getSingleCallSignature(funcType);
if (signature && !signature.typeParameters) {
return getReturnTypeOfSignature(signature);
}
}
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
// should have a parameter that indicates whether full error checking is required such that
// we can perform the optimizations locally.
return checkExpression(node);
}

// Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When
// contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the
// expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in
Expand Down Expand Up @@ -18283,7 +18301,7 @@ namespace ts {
}
}

enumType = checkExpression(expression);
enumType = getTypeOfExpression(expression);
// allow references to constant members of other enums
if (!(enumType.symbol && (enumType.symbol.flags & SymbolFlags.Enum))) {
return undefined;
Expand Down Expand Up @@ -19453,7 +19471,7 @@ namespace ts {
// fallthrough

case SyntaxKind.SuperKeyword:
const type = isPartOfExpression(node) ? checkExpression(<Expression>node) : getTypeFromTypeNode(<TypeNode>node);
const type = isPartOfExpression(node) ? getTypeOfExpression(<Expression>node) : getTypeFromTypeNode(<TypeNode>node);
return type.symbol;

case SyntaxKind.ThisType:
Expand Down Expand Up @@ -19483,7 +19501,7 @@ namespace ts {
case SyntaxKind.NumericLiteral:
// index access
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
const objectType = checkExpression((<ElementAccessExpression>node.parent).expression);
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
if (objectType === unknownType) return undefined;
const apparentType = getApparentType(objectType);
if (apparentType === unknownType) return undefined;
Expand Down Expand Up @@ -19522,7 +19540,7 @@ namespace ts {
}

if (isPartOfExpression(node)) {
return getTypeOfExpression(<Expression>node);
return getRegularTypeOfExpression(<Expression>node);
}

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

function getTypeOfExpression(expr: Expression): Type {
function getRegularTypeOfExpression(expr: Expression): Type {
if (isRightSideOfQualifiedNameOrPropertyAccess(expr)) {
expr = <Expression>expr.parent;
}
return getRegularTypeOfLiteralType(checkExpression(expr));
return getRegularTypeOfLiteralType(getTypeOfExpression(expr));
}

/**
Expand Down Expand Up @@ -20045,7 +20063,7 @@ namespace ts {
}

function writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) {
const type = getWidenedType(getTypeOfExpression(expr));
const type = getWidenedType(getRegularTypeOfExpression(expr));
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientRequireFunction.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const fs = require("fs");
>fs : typeof "fs"
>require("fs") : typeof "fs"
>require("fs") : any
>require : (moduleName: string) => any
>"fs" : "fs"

Expand Down
Loading