Skip to content

Commit

Permalink
Lookup retained type nodes in the node builder using correct, specifi…
Browse files Browse the repository at this point in the history
…c SymbolFlags meaning (microsoft#57887)
  • Loading branch information
weswigham authored and jakebailey committed Mar 21, 2024
1 parent 6ea273c commit a9375fd
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 59 deletions.
18 changes: 13 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6306,7 +6306,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult {
function getMeaningOfEntityNameReference(entityName: EntityNameOrEntityNameExpression): SymbolFlags {
// get symbol of the first identifier of the entityName
let meaning: SymbolFlags;
if (
Expand All @@ -6319,7 +6319,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
else if (
entityName.kind === SyntaxKind.QualifiedName || entityName.kind === SyntaxKind.PropertyAccessExpression ||
entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration
entityName.parent.kind === SyntaxKind.ImportEqualsDeclaration ||
(entityName.parent.kind === SyntaxKind.QualifiedName && (entityName.parent as QualifiedName).left === entityName) ||
(entityName.parent.kind === SyntaxKind.PropertyAccessExpression && (entityName.parent as PropertyAccessExpression).expression === entityName) ||
(entityName.parent.kind === SyntaxKind.ElementAccessExpression && (entityName.parent as ElementAccessExpression).expression === entityName)
) {
// Left identifier from type reference or TypeAlias
// Entity name of the import declaration
Expand All @@ -6329,7 +6332,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Type Reference or TypeAlias entity = Identifier
meaning = SymbolFlags.Type;
}
return meaning;
}

function isEntityNameVisible(entityName: EntityNameOrEntityNameExpression, enclosingDeclaration: Node): SymbolVisibilityResult {
const meaning = getMeaningOfEntityNameReference(entityName);
const firstIdentifier = getFirstIdentifier(entityName);
const symbol = resolveName(enclosingDeclaration, firstIdentifier.escapedText, meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false);
if (symbol && symbol.flags & SymbolFlags.TypeParameter && meaning & SymbolFlags.Type) {
Expand Down Expand Up @@ -8512,13 +8519,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
introducesError = true;
return { introducesError, node };
}
const sym = resolveEntityName(leftmost, SymbolFlags.All, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
const meaning = getMeaningOfEntityNameReference(node);
const sym = resolveEntityName(leftmost, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
if (sym) {
if (isSymbolAccessible(sym, context.enclosingDeclaration, SymbolFlags.All, /*shouldComputeAliasesToMakeVisible*/ false).accessibility !== SymbolAccessibility.Accessible) {
if (isSymbolAccessible(sym, context.enclosingDeclaration, meaning, /*shouldComputeAliasesToMakeVisible*/ false).accessibility !== SymbolAccessibility.Accessible) {
introducesError = true;
}
else {
context.tracker.trackSymbol(sym, context.enclosingDeclaration, SymbolFlags.All);
context.tracker.trackSymbol(sym, context.enclosingDeclaration, meaning);
includePrivateSymbol?.(sym);
}
if (isIdentifier(node)) {
Expand Down
98 changes: 49 additions & 49 deletions tests/baselines/reference/declarationEmitGlobalThisPreserved.types

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/declarationEmitRetainedAnnotationRetainsImportInOutput.ts] ////

//// [index.d.ts]
export type Whatever<T> = {x: T};
export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;

//// [index.ts]
import * as E from 'whatever';

export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);

//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
var E = require("whatever");
var run = function (i) { return E.something(i); };
exports.run = run;


//// [index.d.ts]
import * as E from 'whatever';
export declare const run: <E>(i: () => E.Whatever<E>) => E.Whatever<E>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [tests/cases/compiler/declarationEmitRetainedAnnotationRetainsImportInOutput.ts] ////

=== node_modules/whatever/index.d.ts ===
export type Whatever<T> = {x: T};
>Whatever : Symbol(Whatever, Decl(index.d.ts, 0, 0))
>T : Symbol(T, Decl(index.d.ts, 0, 21))
>x : Symbol(x, Decl(index.d.ts, 0, 27))
>T : Symbol(T, Decl(index.d.ts, 0, 21))

export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;
>something : Symbol(something, Decl(index.d.ts, 0, 33))
>T : Symbol(T, Decl(index.d.ts, 1, 34))
>cb : Symbol(cb, Decl(index.d.ts, 1, 37))
>Whatever : Symbol(Whatever, Decl(index.d.ts, 0, 0))
>T : Symbol(T, Decl(index.d.ts, 1, 34))
>Whatever : Symbol(Whatever, Decl(index.d.ts, 0, 0))
>T : Symbol(T, Decl(index.d.ts, 1, 34))

=== index.ts ===
import * as E from 'whatever';
>E : Symbol(E, Decl(index.ts, 0, 6))

export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);
>run : Symbol(run, Decl(index.ts, 2, 12))
>E : Symbol(E, Decl(index.ts, 2, 20))
>i : Symbol(i, Decl(index.ts, 2, 23))
>E : Symbol(E, Decl(index.ts, 0, 6))
>Whatever : Symbol(E.Whatever, Decl(index.d.ts, 0, 0))
>E : Symbol(E, Decl(index.ts, 2, 20))
>E : Symbol(E, Decl(index.ts, 0, 6))
>Whatever : Symbol(E.Whatever, Decl(index.d.ts, 0, 0))
>E : Symbol(E, Decl(index.ts, 2, 20))
>E.something : Symbol(E.something, Decl(index.d.ts, 0, 33))
>E : Symbol(E, Decl(index.ts, 0, 6))
>something : Symbol(E.something, Decl(index.d.ts, 0, 33))
>i : Symbol(i, Decl(index.ts, 2, 23))

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/declarationEmitRetainedAnnotationRetainsImportInOutput.ts] ////

=== node_modules/whatever/index.d.ts ===
export type Whatever<T> = {x: T};
>Whatever : Whatever<T>
>x : T

export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;
>something : <T>(cb: () => Whatever<T>) => Whatever<T>
>cb : () => Whatever<T>

=== index.ts ===
import * as E from 'whatever';
>E : typeof E

export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);
>run : <E>(i: () => E.Whatever<E>) => E.Whatever<E>
><E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i) : <E>(i: () => E.Whatever<E>) => E.Whatever<E>
>i : () => E.Whatever<E>
>E : any
>E : any
>E.something(i) : E.Whatever<E>
>E.something : <T>(cb: () => E.Whatever<T>) => E.Whatever<T>
>E : typeof E
>something : <T>(cb: () => E.Whatever<T>) => E.Whatever<T>
>i : () => E.Whatever<E>

2 changes: 1 addition & 1 deletion tests/baselines/reference/intraExpressionInferences.types
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ example({
// Repro from #45255

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

<T, U extends T>(_: { test: T, if: (t: T) => t is U, then: (u: U) => void }) => void
>_ : { test: T; if: (t: T) => t is U; then: (u: U) => void; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ export namespace myTypes {
prop2: string;
};
type typeC = myTypes.typeB | Function;
let myTypes: {
[x: string]: any;
};
}
/**
* @namespace myTypes
* @global
* @type {Object<string,*>}
*/
export const myTypes: {
[x: string]: any;
};
//// [file2.d.ts]
export namespace testFnTypes {
type input = boolean | myTypes.typeC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface Extractor<T, Result> {
}

declare function createExtractor<T, Result>(params: {
>createExtractor : <T, Result>(params: { matcher: (node: unknown) => node is T; extract: (node: T) => Result; }) => Extractor<T, Result>
>createExtractor : <T, Result>(params: { matcher: (node: unknown) => node is T; extract: (node: T) => Result;}) => Extractor<T, Result>
>params : { matcher: (node: unknown) => node is T; extract: (node: T) => Result; }

matcher: (node: unknown) => node is T;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @strict: true
// @declaration: true
// @filename: node_modules/whatever/index.d.ts
export type Whatever<T> = {x: T};
export declare function something<T>(cb: () => Whatever<T>): Whatever<T>;

// @filename: index.ts
import * as E from 'whatever';

export const run = <E>(i: () => E.Whatever<E>): E.Whatever<E> => E.something(i);

0 comments on commit a9375fd

Please sign in to comment.