From d91c088b4ac1d4bc5f0649ba5b101020b8743dc4 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 15 Aug 2019 15:05:23 -0700 Subject: [PATCH] Walk up parenthesized type nodes when looking for the type alias hosting a node --- src/compiler/checker.ts | 6 +- .../destructuringInFunctionType.types | 2 +- .../reference/keyofIntersection.types | 2 +- ...enthesisDoesNotBlockAliasSymbolCreation.js | 49 ++++++++++++++++ ...sisDoesNotBlockAliasSymbolCreation.symbols | 56 +++++++++++++++++++ ...hesisDoesNotBlockAliasSymbolCreation.types | 45 +++++++++++++++ ...enthesisDoesNotBlockAliasSymbolCreation.ts | 18 ++++++ 7 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.js create mode 100644 tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.symbols create mode 100644 tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.types create mode 100644 tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1dc46fe95681c..7af2539c74aa3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10868,7 +10868,11 @@ namespace ts { } function getAliasSymbolForTypeNode(node: TypeNode) { - return isTypeAlias(node.parent) ? getSymbolOfNode(node.parent) : undefined; + let host = node.parent; + while (isParenthesizedTypeNode(host)) { + host = host.parent; + } + return isTypeAlias(host) ? getSymbolOfNode(host) : undefined; } function getTypeArgumentsForAliasSymbol(symbol: Symbol | undefined) { diff --git a/tests/baselines/reference/destructuringInFunctionType.types b/tests/baselines/reference/destructuringInFunctionType.types index 2ef48fcbea23a..c6c0f439ac515 100644 --- a/tests/baselines/reference/destructuringInFunctionType.types +++ b/tests/baselines/reference/destructuringInFunctionType.types @@ -18,7 +18,7 @@ type F1 = ([a, b, c]) => void; >c : any type T2 = ({ a }); ->T2 : { a: any; } +>T2 : T2 >a : any type F2 = ({ a }) => void; diff --git a/tests/baselines/reference/keyofIntersection.types b/tests/baselines/reference/keyofIntersection.types index b1c0fe8988c17..ddef2cd4b1a16 100644 --- a/tests/baselines/reference/keyofIntersection.types +++ b/tests/baselines/reference/keyofIntersection.types @@ -46,7 +46,7 @@ type Result3 = Example3<'x' | 'y'>; // "x" | "y" >Result3 : "x" | "y" type Example4 = (Record & Record); ->Example4 : Record & Record +>Example4 : Example4 type Result4 = keyof Example4<'x', 'y'>; // "x" | "y" >Result4 : "x" | "y" diff --git a/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.js b/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.js new file mode 100644 index 0000000000000..6a779f7cdf9a5 --- /dev/null +++ b/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.js @@ -0,0 +1,49 @@ +//// [parenthesisDoesNotBlockAliasSymbolCreation.ts] +export type InvalidKeys = { [P in K]? : never }; +export type InvalidKeys2 = ( + { [P in K]? : never } +); + +export type A = ( + T & InvalidKeys<"a"> +); +export type A2 = ( + T & InvalidKeys2<"a"> +); + +export const a = null as A<{ x : number }>; +export const a2 = null as A2<{ x : number }>; +export const a3 = null as { x : number } & InvalidKeys<"a">; +export const a4 = null as { x : number } & InvalidKeys2<"a">; + + +//// [parenthesisDoesNotBlockAliasSymbolCreation.js] +"use strict"; +exports.__esModule = true; +exports.a = null; +exports.a2 = null; +exports.a3 = null; +exports.a4 = null; + + +//// [parenthesisDoesNotBlockAliasSymbolCreation.d.ts] +export declare type InvalidKeys = { + [P in K]?: never; +}; +export declare type InvalidKeys2 = ({ + [P in K]?: never; +}); +export declare type A = (T & InvalidKeys<"a">); +export declare type A2 = (T & InvalidKeys2<"a">); +export declare const a: A<{ + x: number; +}>; +export declare const a2: A2<{ + x: number; +}>; +export declare const a3: { + x: number; +} & InvalidKeys<"a">; +export declare const a4: { + x: number; +} & InvalidKeys2<"a">; diff --git a/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.symbols b/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.symbols new file mode 100644 index 0000000000000..83c9bf4deed22 --- /dev/null +++ b/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.symbols @@ -0,0 +1,56 @@ +=== tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts === +export type InvalidKeys = { [P in K]? : never }; +>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0)) +>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 24)) +>P : Symbol(P, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 61)) +>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 24)) + +export type InvalidKeys2 = ( +>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80)) +>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 1, 25)) + + { [P in K]? : never } +>P : Symbol(P, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 2, 7)) +>K : Symbol(K, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 1, 25)) + +); + +export type A = ( +>A : Symbol(A, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 3, 2)) +>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 5, 14)) + + T & InvalidKeys<"a"> +>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 5, 14)) +>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0)) + +); +export type A2 = ( +>A2 : Symbol(A2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 7, 2)) +>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 8, 15)) + + T & InvalidKeys2<"a"> +>T : Symbol(T, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 8, 15)) +>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80)) + +); + +export const a = null as A<{ x : number }>; +>a : Symbol(a, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 12, 12)) +>A : Symbol(A, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 3, 2)) +>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 12, 28)) + +export const a2 = null as A2<{ x : number }>; +>a2 : Symbol(a2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 13, 12)) +>A2 : Symbol(A2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 7, 2)) +>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 13, 30)) + +export const a3 = null as { x : number } & InvalidKeys<"a">; +>a3 : Symbol(a3, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 14, 12)) +>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 14, 27)) +>InvalidKeys : Symbol(InvalidKeys, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 0)) + +export const a4 = null as { x : number } & InvalidKeys2<"a">; +>a4 : Symbol(a4, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 15, 12)) +>x : Symbol(x, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 15, 27)) +>InvalidKeys2 : Symbol(InvalidKeys2, Decl(parenthesisDoesNotBlockAliasSymbolCreation.ts, 0, 80)) + diff --git a/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.types b/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.types new file mode 100644 index 0000000000000..5bf53abed1cdc --- /dev/null +++ b/tests/baselines/reference/parenthesisDoesNotBlockAliasSymbolCreation.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts === +export type InvalidKeys = { [P in K]? : never }; +>InvalidKeys : InvalidKeys + +export type InvalidKeys2 = ( +>InvalidKeys2 : InvalidKeys2 + + { [P in K]? : never } +); + +export type A = ( +>A : A + + T & InvalidKeys<"a"> +); +export type A2 = ( +>A2 : A2 + + T & InvalidKeys2<"a"> +); + +export const a = null as A<{ x : number }>; +>a : A<{ x: number; }> +>null as A<{ x : number }> : A<{ x: number; }> +>null : null +>x : number + +export const a2 = null as A2<{ x : number }>; +>a2 : A2<{ x: number; }> +>null as A2<{ x : number }> : A2<{ x: number; }> +>null : null +>x : number + +export const a3 = null as { x : number } & InvalidKeys<"a">; +>a3 : { x: number; } & InvalidKeys<"a"> +>null as { x : number } & InvalidKeys<"a"> : { x: number; } & InvalidKeys<"a"> +>null : null +>x : number + +export const a4 = null as { x : number } & InvalidKeys2<"a">; +>a4 : { x: number; } & InvalidKeys2<"a"> +>null as { x : number } & InvalidKeys2<"a"> : { x: number; } & InvalidKeys2<"a"> +>null : null +>x : number + diff --git a/tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts b/tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts new file mode 100644 index 0000000000000..0f8e993bbd6f6 --- /dev/null +++ b/tests/cases/compiler/parenthesisDoesNotBlockAliasSymbolCreation.ts @@ -0,0 +1,18 @@ +// @declaration: true + +export type InvalidKeys = { [P in K]? : never }; +export type InvalidKeys2 = ( + { [P in K]? : never } +); + +export type A = ( + T & InvalidKeys<"a"> +); +export type A2 = ( + T & InvalidKeys2<"a"> +); + +export const a = null as A<{ x : number }>; +export const a2 = null as A2<{ x : number }>; +export const a3 = null as { x : number } & InvalidKeys<"a">; +export const a4 = null as { x : number } & InvalidKeys2<"a">;