Call getTypeOfSymbol in getNarrowedTypeOfSymbol to avoid running into circularities when computing types (#51914)

This commit is contained in:
Gabriela Araujo Britto
2023-01-05 13:12:56 -08:00
committed by GitHub
parent 3c7660a37b
commit aae3115838
5 changed files with 48 additions and 1 deletions
+2 -1
View File
@@ -27083,6 +27083,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function getNarrowedTypeOfSymbol(symbol: Symbol, location: Identifier) {
const type = getTypeOfSymbol(symbol);
const declaration = symbol.valueDeclaration;
if (declaration) {
// If we have a non-rest binding element with no initializer declared as a const variable or a const-like
@@ -27163,7 +27164,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
}
return getTypeOfSymbol(symbol);
return type;
}
function checkIdentifier(node: Identifier, checkMode: CheckMode | undefined): Type {
+11
View File
@@ -1144,6 +1144,17 @@ export class TestState {
}
}
public verifyTypeAtLocation(range: Range, expected: string): void {
const node = this.goToAndGetNode(range);
const checker = this.getChecker();
const type = checker.getTypeAtLocation(node);
const actual = checker.typeToString(type);
if (actual !== expected) {
this.raiseError(displayExpectedAndActualString(expected, actual));
}
}
public verifyBaselineFindAllReferences(...markerNames: string[]) {
ts.Debug.assert(markerNames.length > 0, "Must pass at least one marker name to `verifyBaselineFindAllReferences()`");
this.verifyBaselineFindAllReferencesWorker("", markerNames);
+4
View File
@@ -354,6 +354,10 @@ export class Verify extends VerifyNegatable {
this.state.verifyTypeOfSymbolAtLocation(range, symbol, expected);
}
public typeAtLocation(range: FourSlash.Range, expected: string) {
this.state.verifyTypeAtLocation(range, expected);
}
public baselineFindAllReferences(...markerNames: string[]) {
this.state.verifyBaselineFindAllReferences(...markerNames);
}
@@ -0,0 +1,30 @@
/// <reference path='fourslash.ts' />
// Issue #48313
// @strict: true
// @target: esnext
// @Filename: /file.tsx
//// export function working(baseVersion?: string): number[] {
//// const toRelease: number[] = [];
//// const baseRelease: number[] = [];
//// return baseRelease.map((_, index) => {
//// const toPart = toRelease[index] ?? 0;
//// [|toPart|]; // this is the "working" log
//// return 0;
//// });
//// }
////
//// export function broken(baseVersion?: string): number[] {
//// const toRelease: number[] = [];
//// const baseRelease: number[] = [];
//// return baseRelease.map((_, index) => {
//// const toPart = toRelease[index] ?? 0;
//// [|toPart|]; // this is the "broken" log
//// return toPart + (baseVersion === undefined ? 0 : 1);
//// });
//// }
const [r_ok, r_bad] = test.ranges();
verify.typeAtLocation(r_ok, "number");
verify.typeAtLocation(r_bad, "number");
+1
View File
@@ -335,6 +335,7 @@ declare namespace FourSlashInterface {
baselineGetFileReferences(fileName: string): void;
symbolAtLocation(startRange: Range, ...declarationRanges: Range[]): void;
typeOfSymbolAtLocation(range: Range, symbol: any, expected: string): void;
typeAtLocation(range: Range, expected: string): void;
/** @deprecated Use baselineFindAllReferences instead */
singleReferenceGroup(definition: ReferencesDefinition, ranges?: Range[] | string): void;
rangesAreOccurrences(isWriteAccess?: boolean, ranges?: Range[]): void;