Skip to content

Commit

Permalink
Merge pull request #31262 from rpgeeganage/new_keyword_consistent_res…
Browse files Browse the repository at this point in the history
…olve

Quick info on 'new' keyword should be the same as that of resolved expression
  • Loading branch information
DanielRosenwasser committed May 15, 2019
2 parents 9221868 + 8f209be commit d484163
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/services/services.ts
Expand Up @@ -1467,33 +1467,41 @@ namespace ts {
}

const typeChecker = program.getTypeChecker();
const symbol = getSymbolAtLocationForQuickInfo(node, typeChecker);
const nodeForQuickInfo = getNodeForQuickInfo(node);
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): Node {
if (isNewExpression(node.parent) && node.pos === node.parent.pos) {
return node.parent.expression;
}
return node;
}

function shouldGetType(sourceFile: SourceFile, node: Node, position: number): boolean {
switch (node.kind) {
case SyntaxKind.Identifier:
Expand Down
18 changes: 18 additions & 0 deletions tests/cases/fourslash/quickInfoOnNewKeyword01.ts
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

////class Cat {
//// /**
//// * NOTE: this constructor is private! Please use the factory function
//// */
//// private constructor() { }
////
//// static makeCat() { new Cat(); }
////}
////
////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');

0 comments on commit d484163

Please sign in to comment.