Skip to content

Commit 91cecdc

Browse files
authored
Lookup retained type nodes in the node builder using correct, specific SymbolFlags meaning (#57887)
1 parent ecb6eb2 commit 91cecdc

9 files changed

+169
-59
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6308,7 +6308,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
63086308
}
63096309
}
63106310

6311-
function isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult {
6311+
function getMeaningOfEntityNameReference(entityName: EntityNameOrEntityNameExpression): SymbolFlags {
63126312
// get symbol of the first identifier of the entityName
63136313
let meaning: SymbolFlags;
63146314
if (
@@ -6321,7 +6321,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
63216321
}
63226322
else if (
63236323
entityName.kind === SyntaxKind.QualifiedName || entityName.kind === SyntaxKind.PropertyAccessExpression ||
6324-
entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration
6324+
entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration ||
6325+
(entityName.parent.kind === SyntaxKind.QualifiedName && (entityName.parent as QualifiedName).left === entityName) ||
6326+
(entityName.parent.kind === SyntaxKind.PropertyAccessExpression && (entityName.parent as PropertyAccessExpression).expression === entityName) ||
6327+
(entityName.parent.kind === SyntaxKind.ElementAccessExpression && (entityName.parent as ElementAccessExpression).expression === entityName)
63256328
) {
63266329
// Left identifier from type reference or TypeAlias
63276330
// Entity name of the import declaration
@@ -6331,7 +6334,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
63316334
// Type Reference or TypeAlias entity = Identifier
63326335
meaning = SymbolFlags.Type;
63336336
}
6337+
return meaning;
6338+
}
63346339

6340+
function isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult {
6341+
const meaning = getMeaningOfEntityNameReference(entityName);
63356342
const firstIdentifier = getFirstIdentifier(entityName);
63366343
const symbol = resolveName(enclosingDeclaration, firstIdentifier.escapedText, meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false);
63376344
if (symbol && symbol.flags & SymbolFlags.TypeParameter && meaning & SymbolFlags.Type) {
@@ -8576,15 +8583,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
85768583
introducesError = true;
85778584
return { introducesError, node };
85788585
}
8579-
const sym = resolveEntityName(leftmost, SymbolFlags.All, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
8586+
const meaning = getMeaningOfEntityNameReference(node);
8587+
const sym = resolveEntityName(leftmost, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
85808588
if (sym) {
8581-
if (isSymbolAccessible(sym, context.enclosingDeclaration, SymbolFlags.All, /*shouldComputeAliasesToMakeVisible*/ false).accessibility !== SymbolAccessibility.Accessible) {
8589+
if (isSymbolAccessible(sym, context.enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ false).accessibility !== SymbolAccessibility.Accessible) {
85828590
if (!isDeclarationName(node)) {
85838591
introducesError = true;
85848592
}
85858593
}
85868594
else {
8587-
context.tracker.trackSymbol(sym, context.enclosingDeclaration, SymbolFlags.All);
8595+
context.tracker.trackSymbol(sym, context.enclosingDeclaration, meaning);
85888596
includePrivateSymbol?.(sym);
85898597
}
85908598
if (isIdentifier(node)) {

tests/baselines/reference/declarationEmitGlobalThisPreserved.types

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/declarationEmitRetainedAnnotationRetainsImportInOutput.ts] ////
2+
3+
//// [index.d.ts]
4+
export type Whatever<T> = {x: T};
5+
export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;
6+
7+
//// [index.ts]
8+
import * as E from 'whatever';
9+
10+
export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);
11+
12+
//// [index.js]
13+
"use strict";
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.run = void 0;
16+
var E = require("whatever");
17+
var run = function (i) { return E.something(i); };
18+
exports.run = run;
19+
20+
21+
//// [index.d.ts]
22+
import * as E from 'whatever';
23+
export declare const run: <E>(i: () => E.Whatever<E>) => E.Whatever<E>;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [tests/cases/compiler/declarationEmitRetainedAnnotationRetainsImportInOutput.ts] ////
2+
3+
=== node_modules/whatever/index.d.ts ===
4+
export type Whatever<T> = {x: T};
5+
>Whatever : Symbol(Whatever, Decl(index.d.ts, 0, 0))
6+
>T : Symbol(T, Decl(index.d.ts, 0, 21))
7+
>x : Symbol(x, Decl(index.d.ts, 0, 27))
8+
>T : Symbol(T, Decl(index.d.ts, 0, 21))
9+
10+
export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;
11+
>something : Symbol(something, Decl(index.d.ts, 0, 33))
12+
>T : Symbol(T, Decl(index.d.ts, 1, 34))
13+
>cb : Symbol(cb, Decl(index.d.ts, 1, 37))
14+
>Whatever : Symbol(Whatever, Decl(index.d.ts, 0, 0))
15+
>T : Symbol(T, Decl(index.d.ts, 1, 34))
16+
>Whatever : Symbol(Whatever, Decl(index.d.ts, 0, 0))
17+
>T : Symbol(T, Decl(index.d.ts, 1, 34))
18+
19+
=== index.ts ===
20+
import * as E from 'whatever';
21+
>E : Symbol(E, Decl(index.ts, 0, 6))
22+
23+
export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);
24+
>run : Symbol(run, Decl(index.ts, 2, 12))
25+
>E : Symbol(E, Decl(index.ts, 2, 20))
26+
>i : Symbol(i, Decl(index.ts, 2, 23))
27+
>E : Symbol(E, Decl(index.ts, 0, 6))
28+
>Whatever : Symbol(E.Whatever, Decl(index.d.ts, 0, 0))
29+
>E : Symbol(E, Decl(index.ts, 2, 20))
30+
>E : Symbol(E, Decl(index.ts, 0, 6))
31+
>Whatever : Symbol(E.Whatever, Decl(index.d.ts, 0, 0))
32+
>E : Symbol(E, Decl(index.ts, 2, 20))
33+
>E.something : Symbol(E.something, Decl(index.d.ts, 0, 33))
34+
>E : Symbol(E, Decl(index.ts, 0, 6))
35+
>something : Symbol(E.something, Decl(index.d.ts, 0, 33))
36+
>i : Symbol(i, Decl(index.ts, 2, 23))
37+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/declarationEmitRetainedAnnotationRetainsImportInOutput.ts] ////
2+
3+
=== node_modules/whatever/index.d.ts ===
4+
export type Whatever<T> = {x: T};
5+
>Whatever : Whatever<T>
6+
>x : T
7+
8+
export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;
9+
>something : <T>(cb: () => Whatever<T>) => Whatever<T>
10+
>cb : () => Whatever<T>
11+
12+
=== index.ts ===
13+
import * as E from 'whatever';
14+
>E : typeof E
15+
16+
export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);
17+
>run : <E>(i: () => E.Whatever<E>) => E.Whatever<E>
18+
><E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i) : <E>(i: () => E.Whatever<E>) => E.Whatever<E>
19+
>i : () => E.Whatever<E>
20+
>E : any
21+
>E : any
22+
>E.something(i) : E.Whatever<E>
23+
>E.something : <T>(cb: () => E.Whatever<T>) => E.Whatever<T>
24+
>E : typeof E
25+
>something : <T>(cb: () => E.Whatever<T>) => E.Whatever<T>
26+
>i : () => E.Whatever<E>
27+

tests/baselines/reference/intraExpressionInferences.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ example({
620620
// Repro from #45255
621621

622622
declare const branch:
623-
>branch : <T, U extends T>(_: { test: T; if: (t: T) => t is U; then: (u: U) => void; }) => void
623+
>branch : <T, U extends T>(_: { test: T; if: (t: T) => t is U; then: (u: U) => void;}) => void
624624

625625
<T, U extends T>(_: { test: T, if: (t: T) => t is U, then: (u: U) => void }) => void
626626
>_ : { test: T; if: (t: T) => t is U; then: (u: U) => void; }

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespace.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,15 @@ export namespace myTypes {
6767
prop2: string;
6868
};
6969
type typeC = myTypes.typeB | Function;
70-
let myTypes: {
71-
[x: string]: any;
72-
};
7370
}
71+
/**
72+
* @namespace myTypes
73+
* @global
74+
* @type {Object<string,*>}
75+
*/
76+
export const myTypes: {
77+
[x: string]: any;
78+
};
7479
//// [file2.d.ts]
7580
export namespace testFnTypes {
7681
type input = boolean | myTypes.typeC;

tests/baselines/reference/reverseMappedUnionInference.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface Extractor<T, Result> {
2222
}
2323

2424
declare function createExtractor<T, Result>(params: {
25-
>createExtractor : <T, Result>(params: { matcher: (node: unknown) => node is T; extract: (node: T) => Result; }) => Extractor<T, Result>
25+
>createExtractor : <T, Result>(params: { matcher: (node: unknown) => node is T; extract: (node: T) => Result;}) => Extractor<T, Result>
2626
>params : { matcher: (node: unknown) => node is T; extract: (node: T) => Result; }
2727

2828
matcher: (node: unknown) => node is T;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @strict: true
2+
// @declaration: true
3+
// @filename: node_modules/whatever/index.d.ts
4+
export type Whatever<T> = {x: T};
5+
export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;
6+
7+
// @filename: index.ts
8+
import * as E from 'whatever';
9+
10+
export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);

0 commit comments

Comments
 (0)