Skip to content

Commit

Permalink
Always consider parameters in scope visible to node builder (#58075)
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Apr 4, 2024
1 parent 386cc0f commit f2bd592
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 80 deletions.
12 changes: 11 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6329,7 +6329,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (
entityName.parent.kind === SyntaxKind.TypeQuery ||
entityName.parent.kind === SyntaxKind.ExpressionWithTypeArguments && !isPartOfTypeNode(entityName.parent) ||
entityName.parent.kind === SyntaxKind.ComputedPropertyName
entityName.parent.kind === SyntaxKind.ComputedPropertyName ||
entityName.parent.kind === SyntaxKind.TypePredicate && (entityName.parent as TypePredicateNode).parameterName === entityName
) {
// Typeof value
meaning = SymbolFlags.Value | SymbolFlags.ExportValue;
Expand Down Expand Up @@ -8672,6 +8673,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const meaning = getMeaningOfEntityNameReference(node);
const sym = resolveEntityName(leftmost, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true);
if (sym) {
// If a parameter is resolvable in the current context it is also visible, so no need to go to symbol accesibility
if (
sym.flags & SymbolFlags.FunctionScopedVariable
&& sym.valueDeclaration
) {
if (isParameterDeclaration(sym.valueDeclaration)) {
return { introducesError, node: attachSymbolToLeftmostIdentifier(node) as T };
}
}
if (
!(sym.flags & SymbolFlags.TypeParameter) && // Type parameters are visible in the curent context if they are are resolvable
!isDeclarationName(node) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,24 @@ var out = foo((x, y) => {
> : ^^^^^^^
>foo : { (func: (x: string, y: string) => any): boolean; (func: (x: string, y: number) => any): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>(x, y) => { function bar(a: typeof x): void; function bar(b: typeof y): void; function bar() { } return bar;} : (x: string, y: string) => { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
>(x, y) => { function bar(a: typeof x): void; function bar(b: typeof y): void; function bar() { } return bar;} : (x: string, y: string) => { (a: typeof x): void; (b: typeof y): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^
>x : string
> : ^^^^^^
>y : string
> : ^^^^^^

function bar(a: typeof x): void;
>bar : { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
>bar : { (a: typeof x): void; (b: string): void; }
> : ^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^
>a : string
> : ^^^^^^
>x : string
> : ^^^^^^

function bar(b: typeof y): void;
>bar : { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
>bar : { (a: string): void; (b: typeof y): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^
>b : string
> : ^^^^^^
>y : string
Expand Down Expand Up @@ -88,16 +88,16 @@ var out2 = foo2((x, y) => {
> : ^^^^^^^
>foo2 : { (func: (x: string, y: string) => any): boolean; (func: (x: string, y: number) => any): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>(x, y) => { var bar: { (a: typeof x): void; (b: typeof y): void; }; return bar;} : (x: string, y: string) => { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
>(x, y) => { var bar: { (a: typeof x): void; (b: typeof y): void; }; return bar;} : (x: string, y: string) => { (a: typeof x): void; (b: typeof y): void; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^
>x : string
> : ^^^^^^
>y : string
> : ^^^^^^

var bar: {
>bar : { (a: string): void; (b: string): void; }
> : ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
>bar : { (a: typeof x): void; (b: typeof y): void; }
> : ^^^^^^ ^^^ ^^^^^^ ^^^ ^^^

(a: typeof x): void;
>a : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ interface I {
declare function f1({ a: string }: O): void;
declare const f2: ({ a: string }: O) => void;
declare const f3: ({ a: string, b, c }: O) => void;
declare const f4: ({ a: string }: O) => string;
declare const f5: ({ a: string, b, c }: O) => string;
declare const f4: ({ a: string }: O) => typeof string;
declare const f5: ({ a: string, b, c }: O) => typeof string;
declare const obj1: {
method({ a: string }: O): void;
};
declare const obj2: {
method({ a: string }: O): string;
method({ a: string }: O): typeof string;
};
declare function f6({ a: string }: O): void;
declare const f7: ({ a: string, b, c }: O) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ const f3 = ({ a: string, b, c }: O) => { };
> : ^^^^^^

const f4 = function({ a: string }: O): typeof string { return string; };
>f4 : ({ a: string }: O) => string
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>function({ a: string }: O): typeof string { return string; } : ({ a: string }: O) => string
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>f4 : ({ a: string }: O) => typeof string
> : ^^^^^^^^^^^^^^^^ ^^^^^
>function({ a: string }: O): typeof string { return string; } : ({ a: string }: O) => typeof string
> : ^^^^^^^^^^^^^^^^ ^^^^^
>a : any
> : ^^^
>string : string
Expand All @@ -339,10 +339,10 @@ const f4 = function({ a: string }: O): typeof string { return string; };
> : ^^^^^^

const f5 = ({ a: string, b, c }: O): typeof string => '';
>f5 : ({ a: string, b, c }: O) => string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>({ a: string, b, c }: O): typeof string => '' : ({ a: string, b, c }: O) => string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>f5 : ({ a: string, b, c }: O) => typeof string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
>({ a: string, b, c }: O): typeof string => '' : ({ a: string, b, c }: O) => typeof string
> : ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
>a : any
> : ^^^
>string : string
Expand Down Expand Up @@ -372,14 +372,14 @@ const obj1 = {

};
const obj2 = {
>obj2 : { method({ a: string }: O): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
>{ method({ a: string }: O): typeof string { return string; }} : { method({ a: string }: O): string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^
>obj2 : { method({ a: string }: O): typeof string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^
>{ method({ a: string }: O): typeof string { return string; }} : { method({ a: string }: O): typeof string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^ ^^^

method({ a: string }: O): typeof string { return string; }
>method : ({ a: string }: O) => string
> : ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
>method : ({ a: string }: O) => typeof string
> : ^^^^^^^^^^^^^^^^ ^^^^^
>a : any
> : ^^^
>string : string
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/subtypingWithCallSignatures.types
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module CallSignature {
> : ^^^^^^^^^^^^^^^^^^^^

declare function foo1(cb: (x: number) => void): typeof cb;
>foo1 : { (cb: (x: number) => void): (x: number) => void; (cb: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo1 : { (cb: (x: number) => void): typeof cb; (cb: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>cb : (x: number) => void
> : ^^^^ ^^^^^
>x : number
Expand Down Expand Up @@ -49,8 +49,8 @@ module CallSignature {
> : ^^

declare function foo2(cb: (x: number, y: number) => void): typeof cb;
>foo2 : { (cb: (x: number, y: number) => void): (x: number, y: number) => void; (cb: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (cb: (x: number, y: number) => void): typeof cb; (cb: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>cb : (x: number, y: number) => void
> : ^^^^ ^^^^^ ^^^^^
>x : number
Expand Down
44 changes: 22 additions & 22 deletions tests/baselines/reference/subtypingWithCallSignatures3.types
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ module Errors {
> : ^^^^^^

declare function foo2(a2: (x: number) => string[]): typeof a2;
>foo2 : { (a2: (x: number) => string[]): (x: number) => string[]; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (a2: (x: number) => string[]): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: number) => string[]
> : ^^^^ ^^^^^
>x : number
Expand All @@ -54,8 +54,8 @@ module Errors {
>a2 : any

declare function foo7(a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): typeof a2;
>foo7 : { (a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): (x: (arg: Base) => Derived) => (r: Base) => Derived2; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo7 : { (a2: (x: (arg: Base) => Derived) => (r: Base) => Derived2): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: (arg: Base) => Derived) => (r: Base) => Derived2
> : ^^^^ ^^^^^
>x : (arg: Base) => Derived
Expand All @@ -73,8 +73,8 @@ module Errors {
>a2 : any

declare function foo8(a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): typeof a2;
>foo8 : { (a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo8 : { (a2: (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived
> : ^^^^ ^^^^^ ^^^^^
>x : (arg: Base) => Derived
Expand All @@ -96,8 +96,8 @@ module Errors {
>a2 : any

declare function foo10(a2: (...x: Base[]) => Base): typeof a2;
>foo10 : { (a2: (...x: Base[]) => Base): (...x: Base[]) => Base; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo10 : { (a2: (...x: Base[]) => Base): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (...x: Base[]) => Base
> : ^^^^^^^ ^^^^^
>x : Base[]
Expand All @@ -111,8 +111,8 @@ module Errors {
>a2 : any

declare function foo11(a2: (x: { foo: string }, y: { foo: string; bar: string }) => Base): typeof a2;
>foo11 : { (a2: (x: { foo: string; }, y: { foo: string; bar: string; }) => Base): (x: { foo: string; }, y: { foo: string; bar: string; }) => Base; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo11 : { (a2: (x: { foo: string; }, y: { foo: string; bar: string; }) => Base): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: { foo: string; }, y: { foo: string; bar: string; }) => Base
> : ^^^^ ^^^^^ ^^^^^
>x : { foo: string; }
Expand All @@ -134,8 +134,8 @@ module Errors {
>a2 : any

declare function foo12(a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): typeof a2;
>foo12 : { (a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): (x: Array<Base>, y: Array<Derived2>) => Array<Derived>; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo12 : { (a2: (x: Array<Base>, y: Array<Derived2>) => Array<Derived>): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: Array<Base>, y: Array<Derived2>) => Array<Derived>
> : ^^^^ ^^^^^ ^^^^^
>x : Base[]
Expand All @@ -151,8 +151,8 @@ module Errors {
>a2 : any

declare function foo15(a2: (x: { a: string; b: number }) => number): typeof a2;
>foo15 : { (a2: (x: { a: string; b: number; }) => number): (x: { a: string; b: number; }) => number; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo15 : { (a2: (x: { a: string; b: number; }) => number): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : (x: { a: string; b: number; }) => number
> : ^^^^ ^^^^^
>x : { a: string; b: number; }
Expand All @@ -170,8 +170,8 @@ module Errors {
>a2 : any

declare function foo16(a2: {
>foo16 : { (a2: { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }): { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^
>foo16 : { (a2: { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : { (x: { (a: number): number; (a?: number): number; }): number[]; (x: { (a: boolean): boolean; (a?: boolean): boolean; }): boolean[]; }
> : ^^^^^^ ^^^ ^^^^^^ ^^^ ^^^

Expand Down Expand Up @@ -212,8 +212,8 @@ module Errors {
>a2 : any

declare function foo17(a2: {
>foo17 : { (a2: { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }): { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }; (a2: any): any; }
> : ^^^^^^^ ^^^^^^^^^ ^^^ ^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^
>foo17 : { (a2: { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : { (x: { <T extends Derived>(a: T): T; <T extends Base>(a: T): T; }): any[]; (x: { <T extends Derived2>(a: T): T; <T extends Base>(a: T): T; }): any[]; }
> : ^^^^^^ ^^^ ^^^^^^ ^^^ ^^^

Expand Down Expand Up @@ -748,8 +748,8 @@ module WithGenericSignaturesInBaseType {
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

declare function foo2(a2: <T>(x: T) => T[]): typeof a2;
>foo2 : { (a2: <T>(x: T) => T[]): <T>(x: T) => T[]; (a2: any): any; }
> : ^^^^^^^ ^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (a2: <T>(x: T) => T[]): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : <T>(x: T) => T[]
> : ^ ^^^^^ ^^^^^
>x : T
Expand Down Expand Up @@ -783,8 +783,8 @@ module WithGenericSignaturesInBaseType {
> : ^^^^^^^^^^^^^^^^^^^^^

declare function foo3(a2: <T>(x: T) => string[]): typeof a2;
>foo3 : { (a2: <T>(x: T) => string[]): <T>(x: T) => string[]; (a2: any): any; }
> : ^^^^^^^ ^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo3 : { (a2: <T>(x: T) => string[]): typeof a2; (a2: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>a2 : <T>(x: T) => string[]
> : ^ ^^^^^ ^^^^^
>x : T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module ConstructSignature {
> : ^^^^^^^^^^^^^^^^^^^^^^^^^

declare function foo1(cb: new (x: number) => void): typeof cb;
>foo1 : { (cb: new (x: number) => void): new (x: number) => void; (cb: any): any; }
> : ^^^^^^^ ^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo1 : { (cb: new (x: number) => void): typeof cb; (cb: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>cb : new (x: number) => void
> : ^^^^^^^^ ^^^^^
>x : number
Expand Down Expand Up @@ -53,8 +53,8 @@ module ConstructSignature {
> : ^^^^^^^^^^^^^^^^^^^^^^^

declare function foo2(cb: new (x: number, y: number) => void): typeof cb;
>foo2 : { (cb: new (x: number, y: number) => void): new (x: number, y: number) => void; (cb: any): any; }
> : ^^^^^^^ ^^^^^^^^^^^ ^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^
>foo2 : { (cb: new (x: number, y: number) => void): typeof cb; (cb: any): any; }
> : ^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^
>cb : new (x: number, y: number) => void
> : ^^^^^^^^ ^^^^^ ^^^^^
>x : number
Expand Down
Loading

0 comments on commit f2bd592

Please sign in to comment.