Skip to content

Commit a87adee

Browse files
committed
improve style/names
1 parent 7e79bcd commit a87adee

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/compiler/checker.ts

+29-16
Original file line numberDiff line numberDiff line change
@@ -6655,25 +6655,31 @@ namespace ts {
66556655

66566656
function narrowTypeByValueExpression(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
66576657
assumeTrue = (expr.operatorToken.kind === SyntaxKind.EqualsEqualsEqualsToken) ? assumeTrue : !assumeTrue;
6658-
let lhs = skipParenthesizedNodes(expr.left);
6658+
let lhs = expr.left;
6659+
// selectors is the stack of property names used to select down into a type to get the member being narrowed
66596660
const selectors: string[] = [];
66606661
while (lhs.kind !== SyntaxKind.Identifier) {
66616662
switch (lhs.kind) {
6663+
case SyntaxKind.ParenthesizedExpression:
6664+
lhs = (lhs as ParenthesizedExpression).expression;
6665+
break;
66626666
case SyntaxKind.PropertyAccessExpression:
6663-
selectors.push((lhs as PropertyAccessExpression).name.text);
6667+
const name = (lhs as PropertyAccessExpression).name.text;
66646668
// If a name doesn't resolve, bail
6665-
if (selectors[selectors.length - 1] === undefined) {
6669+
if (name === undefined) {
66666670
return type;
66676671
}
6668-
lhs = skipParenthesizedNodes((lhs as PropertyAccessExpression).expression);
6672+
selectors.push(name);
6673+
lhs = (lhs as PropertyAccessExpression).expression;
66696674
break;
66706675
case SyntaxKind.Identifier:
66716676
break;
66726677
default:
66736678
// Unhandled control flow construct, don't narrow
66746679
return type;
66756680
}
6676-
};
6681+
}
6682+
66776683
const identifier = lhs as Identifier;
66786684
if (getResolvedSymbol(identifier) !== symbol) {
66796685
return type;
@@ -6683,52 +6689,59 @@ namespace ts {
66836689
return type;
66846690
}
66856691

6686-
// Descend into the type using the selectors we accumulated above and narrow any unions along the way
6687-
// If assumeTrue, we narrow by removing all types not compatible with the rhs type
6688-
// If not, we narrow only if the rhsType is a Value type (ie, StringLiteral) by removing all types compatible with that type (TODO)
66896692
if (assumeTrue) {
66906693
return narrowIntrospectively(type);
66916694
}
66926695
return type;
66936696

6697+
/**
6698+
* Descend into the type using the selectors we accumulated above and narrow any unions along the way
6699+
* If assumeTrue, we narrow by removing all types not compatible with the rhs type
6700+
* If not, we narrow only if the rhsType is a Value type (ie, StringLiteral) by removing all types compatible with that type (TODO)
6701+
*/
66946702
function narrowIntrospectively(type: Type) {
66956703
const propName = selectors.pop();
66966704
if (propName === undefined) {
66976705
// Selected all the way into the object, return the type for the property to be narrowed
6698-
if (isTypeAssignableTo(rhsType, type)) {
6706+
if (isTypeSubtypeOf(rhsType, type)) {
66996707
return rhsType;
67006708
}
67016709
else {
67026710
return type;
67036711
}
67046712
}
67056713
if (type.flags & TypeFlags.Union) {
6706-
const reducedUnion = getUnionType(filter((type as UnionType).types, t => isMemberAssignable(t, rhsType, [...selectors, propName])), /*noSubtypeReduction*/ true);
6714+
const reducedUnion = getUnionType(
6715+
filter((type as UnionType).types, t => isMemberSubtype(t, rhsType, [...selectors, propName])),
6716+
/*noSubtypeReduction*/ true
6717+
);
6718+
67076719
if (reducedUnion !== emptyUnionType) {
6708-
return narrowMatchingProperty(reducedUnion, propName);
6720+
return narrowBasedOnMatchingProperty(reducedUnion, propName);
67096721
}
67106722
else {
67116723
return type;
67126724
}
67136725
}
67146726

6715-
return narrowMatchingProperty(type, propName);
6727+
return narrowBasedOnMatchingProperty(type, propName);
67166728
}
67176729

6718-
function isMemberAssignable(type: Type, check: Type, selectors: string[]): boolean {
6730+
function isMemberSubtype(type: Type, check: Type, selectors: string[]): boolean {
67196731
if (!selectors.length) {
6720-
return isTypeAssignableTo(type, check);
6732+
return isTypeSubtypeOf(type, check);
67216733
}
67226734
const name = selectors.pop();
67236735
const childProp = getPropertyOfType(type, name);
67246736
const propType = childProp && getTypeOfSymbol(childProp);
6725-
return propType && isMemberAssignable(propType, check, selectors);
6737+
return propType && isMemberSubtype(propType, check, selectors);
67266738
}
67276739

6728-
function narrowMatchingProperty(type: Type, name: string): Type {
6740+
function narrowBasedOnMatchingProperty(type: Type, name: string): Type {
67296741
const childProp = getPropertyOfType(type, name);
67306742
const propType = childProp && getTypeOfSymbol(childProp);
67316743
const narrowedType = propType && narrowIntrospectively(propType);
6744+
67326745
if (narrowedType && !isTypeIdenticalTo(propType, narrowedType)) {
67336746
const symbols = cloneSymbolTable(resolveStructuredTypeMembers(type as ObjectType).members);
67346747
const temp = createSymbol(childProp.flags, name);

0 commit comments

Comments
 (0)