mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #8390 from Microsoft/narrowingOfDottedNames
Fix narrowing of dotted names
This commit is contained in:
+41
-19
@@ -7335,27 +7335,29 @@ namespace ts {
|
||||
}
|
||||
|
||||
function containsMatchingReference(source: Node, target: Node) {
|
||||
while (true) {
|
||||
while (source.kind === SyntaxKind.PropertyAccessExpression) {
|
||||
source = (<PropertyAccessExpression>source).expression;
|
||||
if (isMatchingReference(source, target)) {
|
||||
return true;
|
||||
}
|
||||
if (source.kind !== SyntaxKind.PropertyAccessExpression) {
|
||||
return false;
|
||||
}
|
||||
source = (<PropertyAccessExpression>source).expression;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function hasMatchingArgument(callExpression: CallExpression, target: Node) {
|
||||
function isOrContainsMatchingReference(source: Node, target: Node) {
|
||||
return isMatchingReference(source, target) || containsMatchingReference(source, target);
|
||||
}
|
||||
|
||||
function hasMatchingArgument(callExpression: CallExpression, reference: Node) {
|
||||
if (callExpression.arguments) {
|
||||
for (const argument of callExpression.arguments) {
|
||||
if (isMatchingReference(argument, target)) {
|
||||
if (isOrContainsMatchingReference(reference, argument)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (callExpression.expression.kind === SyntaxKind.PropertyAccessExpression &&
|
||||
isMatchingReference((<PropertyAccessExpression>callExpression.expression).expression, target)) {
|
||||
isOrContainsMatchingReference(reference, (<PropertyAccessExpression>callExpression.expression).expression)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -7618,8 +7620,7 @@ namespace ts {
|
||||
// may be an assignment to a left hand part of the reference. For example, for a
|
||||
// reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case,
|
||||
// return the declared type.
|
||||
if (reference.kind === SyntaxKind.PropertyAccessExpression &&
|
||||
containsMatchingReference((<PropertyAccessExpression>reference).expression, node)) {
|
||||
if (containsMatchingReference(reference, node)) {
|
||||
return declaredType;
|
||||
}
|
||||
// Assignment doesn't affect reference
|
||||
@@ -7682,7 +7683,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function narrowTypeByTruthiness(type: Type, expr: Expression, assumeTrue: boolean): Type {
|
||||
return isMatchingReference(expr, reference) ? getTypeWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy) : type;
|
||||
return isMatchingReference(reference, expr) ? getTypeWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy) : type;
|
||||
}
|
||||
|
||||
function narrowTypeByBinaryExpression(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
|
||||
@@ -7714,7 +7715,7 @@ namespace ts {
|
||||
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
|
||||
assumeTrue = !assumeTrue;
|
||||
}
|
||||
if (!strictNullChecks || !isMatchingReference(expr.left, reference)) {
|
||||
if (!strictNullChecks || !isMatchingReference(reference, expr.left)) {
|
||||
return type;
|
||||
}
|
||||
const doubleEquals = operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken;
|
||||
@@ -7731,7 +7732,12 @@ namespace ts {
|
||||
// and string literal on the right
|
||||
const left = <TypeOfExpression>expr.left;
|
||||
const right = <LiteralExpression>expr.right;
|
||||
if (!isMatchingReference(left.expression, reference)) {
|
||||
if (!isMatchingReference(reference, left.expression)) {
|
||||
// For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the
|
||||
// narrowed type of 'y' to its declared type.
|
||||
if (containsMatchingReference(reference, left.expression)) {
|
||||
return declaredType;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
if (expr.operatorToken.kind === SyntaxKind.ExclamationEqualsToken ||
|
||||
@@ -7754,8 +7760,16 @@ namespace ts {
|
||||
}
|
||||
|
||||
function narrowTypeByInstanceof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
|
||||
// Check that type is not any, assumed result is true, and we have variable symbol on the left
|
||||
if (isTypeAny(type) || !isMatchingReference(expr.left, reference)) {
|
||||
if (!isMatchingReference(reference, expr.left)) {
|
||||
// For a reference of the form 'x.y', an 'x instanceof T' type guard resets the
|
||||
// narrowed type of 'y' to its declared type.
|
||||
if (containsMatchingReference(reference, expr.left)) {
|
||||
return declaredType;
|
||||
}
|
||||
return type;
|
||||
}
|
||||
// We never narrow type any in an instanceof guard
|
||||
if (isTypeAny(type)) {
|
||||
return type;
|
||||
}
|
||||
|
||||
@@ -7830,18 +7844,26 @@ namespace ts {
|
||||
}
|
||||
if (isIdentifierTypePredicate(predicate)) {
|
||||
const predicateArgument = callExpression.arguments[predicate.parameterIndex];
|
||||
if (predicateArgument && isMatchingReference(predicateArgument, reference)) {
|
||||
return getNarrowedType(type, predicate.type, assumeTrue);
|
||||
if (predicateArgument) {
|
||||
if (isMatchingReference(reference, predicateArgument)) {
|
||||
return getNarrowedType(type, predicate.type, assumeTrue);
|
||||
}
|
||||
if (containsMatchingReference(reference, predicateArgument)) {
|
||||
return declaredType;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
const invokedExpression = skipParenthesizedNodes(callExpression.expression);
|
||||
if (invokedExpression.kind === SyntaxKind.ElementAccessExpression || invokedExpression.kind === SyntaxKind.PropertyAccessExpression) {
|
||||
const accessExpression = invokedExpression as ElementAccessExpression | PropertyAccessExpression;
|
||||
const possibleReference= skipParenthesizedNodes(accessExpression.expression);
|
||||
if (isMatchingReference(possibleReference, reference)) {
|
||||
const possibleReference = skipParenthesizedNodes(accessExpression.expression);
|
||||
if (isMatchingReference(reference, possibleReference)) {
|
||||
return getNarrowedType(type, predicate.type, assumeTrue);
|
||||
}
|
||||
if (containsMatchingReference(reference, possibleReference)) {
|
||||
return declaredType;
|
||||
}
|
||||
}
|
||||
}
|
||||
return type;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
//// [narrowingOfDottedNames.ts]
|
||||
// Repro from #8383
|
||||
|
||||
class A {
|
||||
prop: { a: string; };
|
||||
}
|
||||
|
||||
class B {
|
||||
prop: { b: string; }
|
||||
}
|
||||
|
||||
function isA(x: any): x is A {
|
||||
return x instanceof A;
|
||||
}
|
||||
|
||||
function isB(x: any): x is B {
|
||||
return x instanceof B;
|
||||
}
|
||||
|
||||
function f1(x: A | B) {
|
||||
while (true) {
|
||||
if (x instanceof A) {
|
||||
x.prop.a;
|
||||
}
|
||||
else if (x instanceof B) {
|
||||
x.prop.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function f2(x: A | B) {
|
||||
while (true) {
|
||||
if (isA(x)) {
|
||||
x.prop.a;
|
||||
}
|
||||
else if (isB(x)) {
|
||||
x.prop.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//// [narrowingOfDottedNames.js]
|
||||
// Repro from #8383
|
||||
var A = (function () {
|
||||
function A() {
|
||||
}
|
||||
return A;
|
||||
}());
|
||||
var B = (function () {
|
||||
function B() {
|
||||
}
|
||||
return B;
|
||||
}());
|
||||
function isA(x) {
|
||||
return x instanceof A;
|
||||
}
|
||||
function isB(x) {
|
||||
return x instanceof B;
|
||||
}
|
||||
function f1(x) {
|
||||
while (true) {
|
||||
if (x instanceof A) {
|
||||
x.prop.a;
|
||||
}
|
||||
else if (x instanceof B) {
|
||||
x.prop.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
function f2(x) {
|
||||
while (true) {
|
||||
if (isA(x)) {
|
||||
x.prop.a;
|
||||
}
|
||||
else if (isB(x)) {
|
||||
x.prop.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
=== tests/cases/compiler/narrowingOfDottedNames.ts ===
|
||||
// Repro from #8383
|
||||
|
||||
class A {
|
||||
>A : Symbol(A, Decl(narrowingOfDottedNames.ts, 0, 0))
|
||||
|
||||
prop: { a: string; };
|
||||
>prop : Symbol(A.prop, Decl(narrowingOfDottedNames.ts, 2, 9))
|
||||
>a : Symbol(a, Decl(narrowingOfDottedNames.ts, 3, 11))
|
||||
}
|
||||
|
||||
class B {
|
||||
>B : Symbol(B, Decl(narrowingOfDottedNames.ts, 4, 1))
|
||||
|
||||
prop: { b: string; }
|
||||
>prop : Symbol(B.prop, Decl(narrowingOfDottedNames.ts, 6, 9))
|
||||
>b : Symbol(b, Decl(narrowingOfDottedNames.ts, 7, 11))
|
||||
}
|
||||
|
||||
function isA(x: any): x is A {
|
||||
>isA : Symbol(isA, Decl(narrowingOfDottedNames.ts, 8, 1))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 10, 13))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 10, 13))
|
||||
>A : Symbol(A, Decl(narrowingOfDottedNames.ts, 0, 0))
|
||||
|
||||
return x instanceof A;
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 10, 13))
|
||||
>A : Symbol(A, Decl(narrowingOfDottedNames.ts, 0, 0))
|
||||
}
|
||||
|
||||
function isB(x: any): x is B {
|
||||
>isB : Symbol(isB, Decl(narrowingOfDottedNames.ts, 12, 1))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 14, 13))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 14, 13))
|
||||
>B : Symbol(B, Decl(narrowingOfDottedNames.ts, 4, 1))
|
||||
|
||||
return x instanceof B;
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 14, 13))
|
||||
>B : Symbol(B, Decl(narrowingOfDottedNames.ts, 4, 1))
|
||||
}
|
||||
|
||||
function f1(x: A | B) {
|
||||
>f1 : Symbol(f1, Decl(narrowingOfDottedNames.ts, 16, 1))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 18, 12))
|
||||
>A : Symbol(A, Decl(narrowingOfDottedNames.ts, 0, 0))
|
||||
>B : Symbol(B, Decl(narrowingOfDottedNames.ts, 4, 1))
|
||||
|
||||
while (true) {
|
||||
if (x instanceof A) {
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 18, 12))
|
||||
>A : Symbol(A, Decl(narrowingOfDottedNames.ts, 0, 0))
|
||||
|
||||
x.prop.a;
|
||||
>x.prop.a : Symbol(a, Decl(narrowingOfDottedNames.ts, 3, 11))
|
||||
>x.prop : Symbol(A.prop, Decl(narrowingOfDottedNames.ts, 2, 9))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 18, 12))
|
||||
>prop : Symbol(A.prop, Decl(narrowingOfDottedNames.ts, 2, 9))
|
||||
>a : Symbol(a, Decl(narrowingOfDottedNames.ts, 3, 11))
|
||||
}
|
||||
else if (x instanceof B) {
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 18, 12))
|
||||
>B : Symbol(B, Decl(narrowingOfDottedNames.ts, 4, 1))
|
||||
|
||||
x.prop.b;
|
||||
>x.prop.b : Symbol(b, Decl(narrowingOfDottedNames.ts, 7, 11))
|
||||
>x.prop : Symbol(B.prop, Decl(narrowingOfDottedNames.ts, 6, 9))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 18, 12))
|
||||
>prop : Symbol(B.prop, Decl(narrowingOfDottedNames.ts, 6, 9))
|
||||
>b : Symbol(b, Decl(narrowingOfDottedNames.ts, 7, 11))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function f2(x: A | B) {
|
||||
>f2 : Symbol(f2, Decl(narrowingOfDottedNames.ts, 27, 1))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 29, 12))
|
||||
>A : Symbol(A, Decl(narrowingOfDottedNames.ts, 0, 0))
|
||||
>B : Symbol(B, Decl(narrowingOfDottedNames.ts, 4, 1))
|
||||
|
||||
while (true) {
|
||||
if (isA(x)) {
|
||||
>isA : Symbol(isA, Decl(narrowingOfDottedNames.ts, 8, 1))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 29, 12))
|
||||
|
||||
x.prop.a;
|
||||
>x.prop.a : Symbol(a, Decl(narrowingOfDottedNames.ts, 3, 11))
|
||||
>x.prop : Symbol(A.prop, Decl(narrowingOfDottedNames.ts, 2, 9))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 29, 12))
|
||||
>prop : Symbol(A.prop, Decl(narrowingOfDottedNames.ts, 2, 9))
|
||||
>a : Symbol(a, Decl(narrowingOfDottedNames.ts, 3, 11))
|
||||
}
|
||||
else if (isB(x)) {
|
||||
>isB : Symbol(isB, Decl(narrowingOfDottedNames.ts, 12, 1))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 29, 12))
|
||||
|
||||
x.prop.b;
|
||||
>x.prop.b : Symbol(b, Decl(narrowingOfDottedNames.ts, 7, 11))
|
||||
>x.prop : Symbol(B.prop, Decl(narrowingOfDottedNames.ts, 6, 9))
|
||||
>x : Symbol(x, Decl(narrowingOfDottedNames.ts, 29, 12))
|
||||
>prop : Symbol(B.prop, Decl(narrowingOfDottedNames.ts, 6, 9))
|
||||
>b : Symbol(b, Decl(narrowingOfDottedNames.ts, 7, 11))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
=== tests/cases/compiler/narrowingOfDottedNames.ts ===
|
||||
// Repro from #8383
|
||||
|
||||
class A {
|
||||
>A : A
|
||||
|
||||
prop: { a: string; };
|
||||
>prop : { a: string; }
|
||||
>a : string
|
||||
}
|
||||
|
||||
class B {
|
||||
>B : B
|
||||
|
||||
prop: { b: string; }
|
||||
>prop : { b: string; }
|
||||
>b : string
|
||||
}
|
||||
|
||||
function isA(x: any): x is A {
|
||||
>isA : (x: any) => x is A
|
||||
>x : any
|
||||
>x : any
|
||||
>A : A
|
||||
|
||||
return x instanceof A;
|
||||
>x instanceof A : boolean
|
||||
>x : any
|
||||
>A : typeof A
|
||||
}
|
||||
|
||||
function isB(x: any): x is B {
|
||||
>isB : (x: any) => x is B
|
||||
>x : any
|
||||
>x : any
|
||||
>B : B
|
||||
|
||||
return x instanceof B;
|
||||
>x instanceof B : boolean
|
||||
>x : any
|
||||
>B : typeof B
|
||||
}
|
||||
|
||||
function f1(x: A | B) {
|
||||
>f1 : (x: A | B) => void
|
||||
>x : A | B
|
||||
>A : A
|
||||
>B : B
|
||||
|
||||
while (true) {
|
||||
>true : boolean
|
||||
|
||||
if (x instanceof A) {
|
||||
>x instanceof A : boolean
|
||||
>x : A | B
|
||||
>A : typeof A
|
||||
|
||||
x.prop.a;
|
||||
>x.prop.a : string
|
||||
>x.prop : { a: string; }
|
||||
>x : A
|
||||
>prop : { a: string; }
|
||||
>a : string
|
||||
}
|
||||
else if (x instanceof B) {
|
||||
>x instanceof B : boolean
|
||||
>x : B
|
||||
>B : typeof B
|
||||
|
||||
x.prop.b;
|
||||
>x.prop.b : string
|
||||
>x.prop : { b: string; }
|
||||
>x : B
|
||||
>prop : { b: string; }
|
||||
>b : string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function f2(x: A | B) {
|
||||
>f2 : (x: A | B) => void
|
||||
>x : A | B
|
||||
>A : A
|
||||
>B : B
|
||||
|
||||
while (true) {
|
||||
>true : boolean
|
||||
|
||||
if (isA(x)) {
|
||||
>isA(x) : boolean
|
||||
>isA : (x: any) => x is A
|
||||
>x : A | B
|
||||
|
||||
x.prop.a;
|
||||
>x.prop.a : string
|
||||
>x.prop : { a: string; }
|
||||
>x : A
|
||||
>prop : { a: string; }
|
||||
>a : string
|
||||
}
|
||||
else if (isB(x)) {
|
||||
>isB(x) : boolean
|
||||
>isB : (x: any) => x is B
|
||||
>x : B
|
||||
|
||||
x.prop.b;
|
||||
>x.prop.b : string
|
||||
>x.prop : { b: string; }
|
||||
>x : B
|
||||
>prop : { b: string; }
|
||||
>b : string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Repro from #8383
|
||||
|
||||
class A {
|
||||
prop: { a: string; };
|
||||
}
|
||||
|
||||
class B {
|
||||
prop: { b: string; }
|
||||
}
|
||||
|
||||
function isA(x: any): x is A {
|
||||
return x instanceof A;
|
||||
}
|
||||
|
||||
function isB(x: any): x is B {
|
||||
return x instanceof B;
|
||||
}
|
||||
|
||||
function f1(x: A | B) {
|
||||
while (true) {
|
||||
if (x instanceof A) {
|
||||
x.prop.a;
|
||||
}
|
||||
else if (x instanceof B) {
|
||||
x.prop.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function f2(x: A | B) {
|
||||
while (true) {
|
||||
if (isA(x)) {
|
||||
x.prop.a;
|
||||
}
|
||||
else if (isB(x)) {
|
||||
x.prop.b;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user