From 578013b65cbc78930279388a810fbb0d12b67c73 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sun, 5 May 2019 18:59:45 +0200 Subject: [PATCH 1/7] modified the service file --- src/services/services.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 42d2278bcba93..ab5138fb489ae 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1467,33 +1467,49 @@ namespace ts { } const typeChecker = program.getTypeChecker(); - const symbol = getSymbolAtLocationForQuickInfo(node, typeChecker); + const nodeForQuickInfo = getNodeForQuickInfo(node, typeChecker); + const symbol = getSymbolAtLocationForQuickInfo(nodeForQuickInfo, typeChecker); if (!symbol || typeChecker.isUnknownSymbol(symbol)) { - const type = shouldGetType(sourceFile, node, position) ? typeChecker.getTypeAtLocation(node) : undefined; + const type = shouldGetType(sourceFile, nodeForQuickInfo, position) ? typeChecker.getTypeAtLocation(nodeForQuickInfo) : undefined; return type && { kind: ScriptElementKind.unknown, kindModifiers: ScriptElementKindModifier.none, - textSpan: createTextSpanFromNode(node, sourceFile), - displayParts: typeChecker.runWithCancellationToken(cancellationToken, typeChecker => typeToDisplayParts(typeChecker, type, getContainerNode(node))), + textSpan: createTextSpanFromNode(nodeForQuickInfo, sourceFile), + displayParts: typeChecker.runWithCancellationToken(cancellationToken, typeChecker => typeToDisplayParts(typeChecker, type, getContainerNode(nodeForQuickInfo))), documentation: type.symbol ? type.symbol.getDocumentationComment(typeChecker) : undefined, tags: type.symbol ? type.symbol.getJsDocTags() : undefined }; } const { symbolKind, displayParts, documentation, tags } = typeChecker.runWithCancellationToken(cancellationToken, typeChecker => - SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, getContainerNode(node), node) + SymbolDisplay.getSymbolDisplayPartsDocumentationAndSymbolKind(typeChecker, symbol, sourceFile, getContainerNode(nodeForQuickInfo), nodeForQuickInfo) ); return { kind: symbolKind, kindModifiers: SymbolDisplay.getSymbolModifiers(symbol), - textSpan: createTextSpanFromNode(node, sourceFile), + textSpan: createTextSpanFromNode(nodeForQuickInfo, sourceFile), displayParts, documentation, tags, }; } + function getNodeForQuickInfo(node: Node, typeChecker: TypeChecker): Node { + const firstParentNode = node.parent.getFirstToken(); + const firstNodeSyntaxKind = firstParentNode ? firstParentNode.kind : undefined; + + if (node.kind === SyntaxKind.NewKeyword || firstNodeSyntaxKind === SyntaxKind.NewKeyword) { + for (const singleNode of node.parent.getChildren()) { + const symbol = getSymbolAtLocationForQuickInfo(singleNode, typeChecker); + if (symbol) { + return singleNode; + } + } + } + return node; + } + function shouldGetType(sourceFile: SourceFile, node: Node, position: number): boolean { switch (node.kind) { case SyntaxKind.Identifier: From 9959ce449e6ef9dcb047572f7e670811f5447206 Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sun, 5 May 2019 19:00:01 +0200 Subject: [PATCH 2/7] added test --- .../fourslash/quickInfoOnNewKeyword01.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/cases/fourslash/quickInfoOnNewKeyword01.ts diff --git a/tests/cases/fourslash/quickInfoOnNewKeyword01.ts b/tests/cases/fourslash/quickInfoOnNewKeyword01.ts new file mode 100644 index 0000000000000..21ccb8a65024d --- /dev/null +++ b/tests/cases/fourslash/quickInfoOnNewKeyword01.ts @@ -0,0 +1,21 @@ +/// + +////class Cat { +//// /** +//// * NOTE: this constructor is private! Please use the factory function +//// */ +//// private constructor() { } +//// +//// static makeCat() { new Cat(); } +////} +//// +////ne/*1*/w Ca/*2*/t(/*3*/); + +verify.quickInfoAt('1', 'constructor Cat(): Cat', +'NOTE: this constructor is private! Please use the factory function'); + +verify.quickInfoAt('2', 'constructor Cat(): Cat', +'NOTE: this constructor is private! Please use the factory function'); + +verify.quickInfoAt('3', 'constructor Cat(): Cat', +'NOTE: this constructor is private! Please use the factory function'); From ca749b107cbd4b467904c51d3b6dc78d30a43d6b Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Sun, 5 May 2019 21:58:31 +0200 Subject: [PATCH 3/7] updated the baseline files --- tests/baselines/reference/jsDocTags.baseline | 70 +++++ ...oDisplayPartsTypeParameterInClass.baseline | 84 +++++- ...playPartsTypeParameterInInterface.baseline | 260 +++++++++++++++++- 3 files changed, 402 insertions(+), 12 deletions(-) diff --git a/tests/baselines/reference/jsDocTags.baseline b/tests/baselines/reference/jsDocTags.baseline index f316a9c42459d..2d89c183ab344 100644 --- a/tests/baselines/reference/jsDocTags.baseline +++ b/tests/baselines/reference/jsDocTags.baseline @@ -79,6 +79,76 @@ "marker": { "fileName": "/tests/cases/fourslash/jsDocTags.ts", "position": 981 + }, + "quickInfo": { + "kind": "constructor", + "kindModifiers": "", + "textSpan": { + "start": 977, + "length": 3 + }, + "displayParts": [ + { + "text": "constructor", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "Foo", + "kind": "className" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "value", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "number", + "kind": "keyword" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "Foo", + "kind": "className" + } + ], + "documentation": [ + { + "text": "This is the constructor.", + "kind": "text" + } + ], + "tags": [ + { + "name": "myjsdoctag", + "text": "this is a comment" + } + ] } }, { diff --git a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline index 48bd280a1b505..68e48ff9b68e4 100644 --- a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline +++ b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline @@ -2991,15 +2991,15 @@ "position": 337 }, "quickInfo": { - "kind": "var", + "kind": "constructor", "kindModifiers": "", "textSpan": { - "start": 337, - "length": 9 + "start": 334, + "length": 2 }, "displayParts": [ { - "text": "var", + "text": "constructor", "kind": "keyword" }, { @@ -3007,8 +3007,40 @@ "kind": "space" }, { - "text": "cInstance", - "kind": "localName" + "text": "c2", + "kind": "className" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "c", + "kind": "className" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "a", + "kind": "parameterName" }, { "text": ":", @@ -3030,6 +3062,46 @@ "text": "string", "kind": "keyword" }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "c2", + "kind": "className" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "c", + "kind": "className" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, { "text": ">", "kind": "punctuation" diff --git a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline index 715811f9918a1..6830394801c48 100644 --- a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline +++ b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline @@ -5568,8 +5568,8 @@ "kind": "var", "kindModifiers": "", "textSpan": { - "start": 415, - "length": 4 + "start": 409, + "length": 5 }, "displayParts": [ { @@ -5581,7 +5581,7 @@ "kind": "space" }, { - "text": "iVal", + "text": "iVal1", "kind": "localName" }, { @@ -5592,6 +5592,130 @@ "text": " ", "kind": "space" }, + { + "text": "I1", + "kind": "interfaceName" + }, + { + "text": "\n", + "kind": "lineBreak" + }, + { + "text": "new", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "I", + "kind": "interfaceName" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "a", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "I", + "kind": "interfaceName" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ",", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "b", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "I", + "kind": "interfaceName" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "=>", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, { "text": "I", "kind": "interfaceName" @@ -5621,8 +5745,8 @@ "kind": "var", "kindModifiers": "", "textSpan": { - "start": 421, - "length": 4 + "start": 409, + "length": 5 }, "displayParts": [ { @@ -5634,7 +5758,7 @@ "kind": "space" }, { - "text": "iVal", + "text": "iVal1", "kind": "localName" }, { @@ -5645,6 +5769,130 @@ "text": " ", "kind": "space" }, + { + "text": "I1", + "kind": "interfaceName" + }, + { + "text": "\n", + "kind": "lineBreak" + }, + { + "text": "new", + "kind": "keyword" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "I", + "kind": "interfaceName" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": "(", + "kind": "punctuation" + }, + { + "text": "a", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "I", + "kind": "interfaceName" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ",", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "b", + "kind": "parameterName" + }, + { + "text": ":", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "I", + "kind": "interfaceName" + }, + { + "text": "<", + "kind": "punctuation" + }, + { + "text": "string", + "kind": "keyword" + }, + { + "text": ">", + "kind": "punctuation" + }, + { + "text": ")", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, + { + "text": "=>", + "kind": "punctuation" + }, + { + "text": " ", + "kind": "space" + }, { "text": "I", "kind": "interfaceName" From 8f209be149ec626f40031a5b0f904ae5ea74479d Mon Sep 17 00:00:00 2001 From: rpgeeganage Date: Mon, 6 May 2019 21:21:37 +0200 Subject: [PATCH 4/7] fixed the comments --- src/services/services.ts | 16 +- tests/baselines/reference/jsDocTags.baseline | 70 ----- ...oDisplayPartsTypeParameterInClass.baseline | 84 +----- ...playPartsTypeParameterInInterface.baseline | 260 +----------------- .../fourslash/quickInfoOnNewKeyword01.ts | 5 +- 5 files changed, 17 insertions(+), 418 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index ab5138fb489ae..628ec395437b6 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1467,7 +1467,7 @@ namespace ts { } const typeChecker = program.getTypeChecker(); - const nodeForQuickInfo = getNodeForQuickInfo(node, typeChecker); + const nodeForQuickInfo = getNodeForQuickInfo(node); const symbol = getSymbolAtLocationForQuickInfo(nodeForQuickInfo, typeChecker); if (!symbol || typeChecker.isUnknownSymbol(symbol)) { @@ -1495,17 +1495,9 @@ namespace ts { }; } - function getNodeForQuickInfo(node: Node, typeChecker: TypeChecker): Node { - const firstParentNode = node.parent.getFirstToken(); - const firstNodeSyntaxKind = firstParentNode ? firstParentNode.kind : undefined; - - if (node.kind === SyntaxKind.NewKeyword || firstNodeSyntaxKind === SyntaxKind.NewKeyword) { - for (const singleNode of node.parent.getChildren()) { - const symbol = getSymbolAtLocationForQuickInfo(singleNode, typeChecker); - if (symbol) { - return singleNode; - } - } + function getNodeForQuickInfo(node: Node): Node { + if (isNewExpression(node.parent) && node.pos === node.parent.pos) { + return node.parent.expression; } return node; } diff --git a/tests/baselines/reference/jsDocTags.baseline b/tests/baselines/reference/jsDocTags.baseline index 2d89c183ab344..f316a9c42459d 100644 --- a/tests/baselines/reference/jsDocTags.baseline +++ b/tests/baselines/reference/jsDocTags.baseline @@ -79,76 +79,6 @@ "marker": { "fileName": "/tests/cases/fourslash/jsDocTags.ts", "position": 981 - }, - "quickInfo": { - "kind": "constructor", - "kindModifiers": "", - "textSpan": { - "start": 977, - "length": 3 - }, - "displayParts": [ - { - "text": "constructor", - "kind": "keyword" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "Foo", - "kind": "className" - }, - { - "text": "(", - "kind": "punctuation" - }, - { - "text": "value", - "kind": "parameterName" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "number", - "kind": "keyword" - }, - { - "text": ")", - "kind": "punctuation" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "Foo", - "kind": "className" - } - ], - "documentation": [ - { - "text": "This is the constructor.", - "kind": "text" - } - ], - "tags": [ - { - "name": "myjsdoctag", - "text": "this is a comment" - } - ] } }, { diff --git a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline index 68e48ff9b68e4..48bd280a1b505 100644 --- a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline +++ b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInClass.baseline @@ -2991,15 +2991,15 @@ "position": 337 }, "quickInfo": { - "kind": "constructor", + "kind": "var", "kindModifiers": "", "textSpan": { - "start": 334, - "length": 2 + "start": 337, + "length": 9 }, "displayParts": [ { - "text": "constructor", + "text": "var", "kind": "keyword" }, { @@ -3007,68 +3007,8 @@ "kind": "space" }, { - "text": "c2", - "kind": "className" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "c", - "kind": "className" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": "(", - "kind": "punctuation" - }, - { - "text": "a", - "kind": "parameterName" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "c", - "kind": "className" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ")", - "kind": "punctuation" + "text": "cInstance", + "kind": "localName" }, { "text": ":", @@ -3078,14 +3018,6 @@ "text": " ", "kind": "space" }, - { - "text": "c2", - "kind": "className" - }, - { - "text": "<", - "kind": "punctuation" - }, { "text": "c", "kind": "className" @@ -3098,10 +3030,6 @@ "text": "string", "kind": "keyword" }, - { - "text": ">", - "kind": "punctuation" - }, { "text": ">", "kind": "punctuation" diff --git a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline index 6830394801c48..715811f9918a1 100644 --- a/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline +++ b/tests/baselines/reference/quickInfoDisplayPartsTypeParameterInInterface.baseline @@ -5568,8 +5568,8 @@ "kind": "var", "kindModifiers": "", "textSpan": { - "start": 409, - "length": 5 + "start": 415, + "length": 4 }, "displayParts": [ { @@ -5581,7 +5581,7 @@ "kind": "space" }, { - "text": "iVal1", + "text": "iVal", "kind": "localName" }, { @@ -5592,130 +5592,6 @@ "text": " ", "kind": "space" }, - { - "text": "I1", - "kind": "interfaceName" - }, - { - "text": "\n", - "kind": "lineBreak" - }, - { - "text": "new", - "kind": "keyword" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "I", - "kind": "interfaceName" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": "(", - "kind": "punctuation" - }, - { - "text": "a", - "kind": "parameterName" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "I", - "kind": "interfaceName" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ",", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "b", - "kind": "parameterName" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "I", - "kind": "interfaceName" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ")", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "=>", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, { "text": "I", "kind": "interfaceName" @@ -5745,8 +5621,8 @@ "kind": "var", "kindModifiers": "", "textSpan": { - "start": 409, - "length": 5 + "start": 421, + "length": 4 }, "displayParts": [ { @@ -5758,7 +5634,7 @@ "kind": "space" }, { - "text": "iVal1", + "text": "iVal", "kind": "localName" }, { @@ -5769,130 +5645,6 @@ "text": " ", "kind": "space" }, - { - "text": "I1", - "kind": "interfaceName" - }, - { - "text": "\n", - "kind": "lineBreak" - }, - { - "text": "new", - "kind": "keyword" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "I", - "kind": "interfaceName" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": "(", - "kind": "punctuation" - }, - { - "text": "a", - "kind": "parameterName" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "I", - "kind": "interfaceName" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ",", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "b", - "kind": "parameterName" - }, - { - "text": ":", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "I", - "kind": "interfaceName" - }, - { - "text": "<", - "kind": "punctuation" - }, - { - "text": "string", - "kind": "keyword" - }, - { - "text": ">", - "kind": "punctuation" - }, - { - "text": ")", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, - { - "text": "=>", - "kind": "punctuation" - }, - { - "text": " ", - "kind": "space" - }, { "text": "I", "kind": "interfaceName" diff --git a/tests/cases/fourslash/quickInfoOnNewKeyword01.ts b/tests/cases/fourslash/quickInfoOnNewKeyword01.ts index 21ccb8a65024d..f7d1e359b8d2a 100644 --- a/tests/cases/fourslash/quickInfoOnNewKeyword01.ts +++ b/tests/cases/fourslash/quickInfoOnNewKeyword01.ts @@ -9,13 +9,10 @@ //// static makeCat() { new Cat(); } ////} //// -////ne/*1*/w Ca/*2*/t(/*3*/); +////ne/*1*/w Ca/*2*/t(); verify.quickInfoAt('1', 'constructor Cat(): Cat', 'NOTE: this constructor is private! Please use the factory function'); verify.quickInfoAt('2', 'constructor Cat(): Cat', 'NOTE: this constructor is private! Please use the factory function'); - -verify.quickInfoAt('3', 'constructor Cat(): Cat', -'NOTE: this constructor is private! Please use the factory function'); From 4b5968eb6d8a64794b51d48fd67fd28e7cd34e28 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 14 May 2019 07:45:29 -0700 Subject: [PATCH 5/7] Revert change to substitution type simplification --- src/compiler/checker.ts | 7 ++++++- src/compiler/types.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9a64dce125591..e37da1fd67860 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10196,7 +10196,6 @@ namespace ts { function getSimplifiedType(type: Type, writing: boolean): Type { return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type, writing) : type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type, writing) : - type.flags & TypeFlags.Substitution ? writing ? (type).typeVariable : (type).substitute : type; } @@ -12432,6 +12431,12 @@ namespace ts { if (isFreshLiteralType(target)) { target = (target).regularType; } + if (source.flags & TypeFlags.Substitution) { + source = (source).substitute; + } + if (target.flags & TypeFlags.Substitution) { + target = (target).typeVariable; + } if (source.flags & TypeFlags.Simplifiable) { source = getSimplifiedType(source, /*writing*/ false); } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 75fb702609b70..db1a62937e662 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3967,7 +3967,7 @@ namespace ts { /* @internal */ ObjectFlagsType = Nullable | Never | Object | Union | Intersection, /* @internal */ - Simplifiable = IndexedAccess | Conditional | Substitution, + Simplifiable = IndexedAccess | Conditional, // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never Narrowable = Any | Unknown | StructuredOrInstantiable | StringLike | NumberLike | BigIntLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive, From 92a1547efff8ce62a14e160cb007f4eb653f160d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 14 May 2019 08:41:29 -0700 Subject: [PATCH 6/7] Accept new baselines --- .../reference/conditionalTypes2.errors.txt | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/baselines/reference/conditionalTypes2.errors.txt b/tests/baselines/reference/conditionalTypes2.errors.txt index 1cf5b0eca44fa..4093bf46fb0ee 100644 --- a/tests/baselines/reference/conditionalTypes2.errors.txt +++ b/tests/baselines/reference/conditionalTypes2.errors.txt @@ -10,13 +10,13 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(24,5): error TS23 Type 'keyof B' is not assignable to type 'keyof A'. Type 'string | number | symbol' is not assignable to type 'keyof A'. Type 'string' is not assignable to type 'keyof A'. - Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. - Type 'keyof B' is not assignable to type 'keyof A'. - Type 'string | number | symbol' is not assignable to type 'keyof A'. - Type 'string' is not assignable to type 'keyof A'. + Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. + Type 'keyof B' is not assignable to type '"valueOf"'. + Type 'string | number | symbol' is not assignable to type '"valueOf"'. + Type 'string' is not assignable to type '"valueOf"'. tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. Types of property 'foo' are incompatible. Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'. @@ -73,13 +73,13 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2 !!! error TS2322: Type 'keyof B' is not assignable to type 'keyof A'. !!! error TS2322: Type 'string | number | symbol' is not assignable to type 'keyof A'. !!! error TS2322: Type 'string' is not assignable to type 'keyof A'. -!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf" | keyof A'. -!!! error TS2322: Type 'keyof B' is not assignable to type 'keyof A'. -!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'keyof A'. -!!! error TS2322: Type 'string' is not assignable to type 'keyof A'. +!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'. +!!! error TS2322: Type 'keyof B' is not assignable to type '"valueOf"'. +!!! error TS2322: Type 'string | number | symbol' is not assignable to type '"valueOf"'. +!!! error TS2322: Type 'string' is not assignable to type '"valueOf"'. b = a; // Error ~ !!! error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. From 3885e3fcda213ec8c5f21849d6b8ddf8a5357fa7 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 14 May 2019 16:58:06 -0700 Subject: [PATCH 7/7] Fix error message regressed by #30916 (#31276) --- src/compiler/checker.ts | 20 +++++++++++++++---- ...gningFunctionToTupleIssuesError.errors.txt | 8 ++++++++ .../assigningFunctionToTupleIssuesError.js | 6 ++++++ ...ssigningFunctionToTupleIssuesError.symbols | 8 ++++++++ .../assigningFunctionToTupleIssuesError.types | 8 ++++++++ .../assigningFunctionToTupleIssuesError.ts | 2 ++ 6 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.js create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols create mode 100644 tests/baselines/reference/assigningFunctionToTupleIssuesError.types create mode 100644 tests/cases/compiler/assigningFunctionToTupleIssuesError.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9a64dce125591..0f18afa77410c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12290,7 +12290,7 @@ namespace ts { let depth = 0; let expandingFlags = ExpandingFlags.None; let overflow = false; - let suppressNextError = false; + let overrideNextErrorInfo: DiagnosticMessageChain | undefined; Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); @@ -12571,10 +12571,14 @@ namespace ts { } if (!result && reportErrors) { - const maybeSuppress = suppressNextError; - suppressNextError = false; + let maybeSuppress = overrideNextErrorInfo; + overrideNextErrorInfo = undefined; if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Object) { + const currentError = errorInfo; tryElaborateArrayLikeErrors(source, target, reportErrors); + if (errorInfo !== currentError) { + maybeSuppress = errorInfo; + } } if (source.flags & TypeFlags.Object && target.flags & TypeFlags.Primitive) { tryElaborateErrorsForPrimitivesAndObjects(source, target); @@ -13506,9 +13510,10 @@ namespace ts { if (unmatchedProperty) { if (reportErrors) { const props = arrayFrom(getUnmatchedProperties(source, target, requireOptionalProperties, /*matchDiscriminantProperties*/ false)); + let shouldSkipElaboration = false; if (!headMessage || (headMessage.code !== Diagnostics.Class_0_incorrectly_implements_interface_1.code && headMessage.code !== Diagnostics.Class_0_incorrectly_implements_class_1_Did_you_mean_to_extend_1_and_inherit_its_members_as_a_subclass.code)) { - suppressNextError = true; // Retain top-level error for interface implementing issues, otherwise omit it + shouldSkipElaboration = true; // Retain top-level error for interface implementing issues, otherwise omit it } if (props.length === 1) { const propName = symbolToString(unmatchedProperty); @@ -13516,6 +13521,9 @@ namespace ts { if (length(unmatchedProperty.declarations)) { associateRelatedInfo(createDiagnosticForNode(unmatchedProperty.declarations[0], Diagnostics._0_is_declared_here, propName)); } + if (shouldSkipElaboration) { + overrideNextErrorInfo = errorInfo; + } } else if (tryElaborateArrayLikeErrors(source, target, /*reportErrors*/ false)) { if (props.length > 5) { // arbitrary cutoff for too-long list form @@ -13524,7 +13532,11 @@ namespace ts { else { reportError(Diagnostics.Type_0_is_missing_the_following_properties_from_type_1_Colon_2, typeToString(source), typeToString(target), map(props, p => symbolToString(p)).join(", ")); } + if (shouldSkipElaboration) { + overrideNextErrorInfo = errorInfo; + } } + // ELSE: No array like or unmatched property error - just issue top level error (errorInfo = undefined) } return Ternary.False; } diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt b/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt new file mode 100644 index 0000000000000..ca1af66c0873c --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.errors.txt @@ -0,0 +1,8 @@ +tests/cases/compiler/assigningFunctionToTupleIssuesError.ts(2,5): error TS2322: Type '() => void' is not assignable to type '[string]'. + + +==== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts (1 errors) ==== + declare let a: () => void; + let b: [string] = a; + ~ +!!! error TS2322: Type '() => void' is not assignable to type '[string]'. \ No newline at end of file diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.js b/tests/baselines/reference/assigningFunctionToTupleIssuesError.js new file mode 100644 index 0000000000000..3de3da7b3cc41 --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.js @@ -0,0 +1,6 @@ +//// [assigningFunctionToTupleIssuesError.ts] +declare let a: () => void; +let b: [string] = a; + +//// [assigningFunctionToTupleIssuesError.js] +var b = a; diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols b/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols new file mode 100644 index 0000000000000..7a499662aeffb --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts === +declare let a: () => void; +>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11)) + +let b: [string] = a; +>b : Symbol(b, Decl(assigningFunctionToTupleIssuesError.ts, 1, 3)) +>a : Symbol(a, Decl(assigningFunctionToTupleIssuesError.ts, 0, 11)) + diff --git a/tests/baselines/reference/assigningFunctionToTupleIssuesError.types b/tests/baselines/reference/assigningFunctionToTupleIssuesError.types new file mode 100644 index 0000000000000..6c7d57f8483f2 --- /dev/null +++ b/tests/baselines/reference/assigningFunctionToTupleIssuesError.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/assigningFunctionToTupleIssuesError.ts === +declare let a: () => void; +>a : () => void + +let b: [string] = a; +>b : [string] +>a : () => void + diff --git a/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts b/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts new file mode 100644 index 0000000000000..befea50f81d94 --- /dev/null +++ b/tests/cases/compiler/assigningFunctionToTupleIssuesError.ts @@ -0,0 +1,2 @@ +declare let a: () => void; +let b: [string] = a; \ No newline at end of file