Skip to content

Enable member completions for import types #23085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace ts {
return node ? getConstantValue(node) : undefined;
},
isValidPropertyAccess: (node, propertyName) => {
node = getParseTreeNode(node, isPropertyAccessOrQualifiedName);
node = getParseTreeNode(node, isPropertyAccessOrQualifiedNameOrImportTypeNode);
return !!node && isValidPropertyAccess(node, escapeLeadingUnderscores(propertyName));
},
isValidPropertyAccessForCompletions: (node, type, property) => {
Expand Down Expand Up @@ -8550,7 +8550,7 @@ namespace ts {
return links.resolvedType = unknownType;
}
const moduleSymbol = resolveExternalModuleSymbol(innerModuleSymbol, /*dontResolveAlias*/ false);
if (node.qualifier) {
if (!nodeIsMissing(node.qualifier)) {
const nameStack: Identifier[] = getIdentifierChain(node.qualifier);
let currentNamespace = moduleSymbol;
let current: Identifier | undefined;
Expand Down Expand Up @@ -16240,11 +16240,13 @@ namespace ts {
* @param type The type of left.
* @param prop The symbol for the right hand side of the property access.
*/
function checkPropertyAccessibility(node: PropertyAccessExpression | QualifiedName | VariableLikeDeclaration, left: Expression | QualifiedName, type: Type, prop: Symbol): boolean {
function checkPropertyAccessibility(node: PropertyAccessExpression | QualifiedName | VariableLikeDeclaration | ImportTypeNode, left: Expression | QualifiedName | ImportTypeNode, type: Type, prop: Symbol): boolean {
const flags = getDeclarationModifierFlagsFromSymbol(prop);
const errorNode = node.kind === SyntaxKind.PropertyAccessExpression || node.kind === SyntaxKind.VariableDeclaration ?
node.name :
(<QualifiedName>node).right;
node.kind === SyntaxKind.ImportTypeNode ?
node :
(<QualifiedName>node).right;

if (getCheckFlags(prop) & CheckFlags.ContainsPrivate) {
// Synthetic property with private constituent property
Expand Down Expand Up @@ -16683,13 +16685,19 @@ namespace ts {
(getCheckFlags(prop) & CheckFlags.Instantiated ? getSymbolLinks(prop).target : prop).isReferenced = SymbolFlags.All;
}

function isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: __String): boolean {
const left = node.kind === SyntaxKind.PropertyAccessExpression ? node.expression : node.left;
return isValidPropertyAccessWithType(node, left, propertyName, getWidenedType(checkExpression(left)));
function isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName | ImportTypeNode, propertyName: __String): boolean {
switch (node.kind) {
case SyntaxKind.PropertyAccessExpression:
return isValidPropertyAccessWithType(node, node.expression, propertyName, getWidenedType(checkExpression(node.expression)));
case SyntaxKind.QualifiedName:
return isValidPropertyAccessWithType(node, node.left, propertyName, getWidenedType(checkExpression(node.left)));
case SyntaxKind.ImportTypeNode:
return isValidPropertyAccessWithType(node, node, propertyName, getTypeFromTypeNode(node));
}
}

function isValidPropertyAccessForCompletions(node: PropertyAccessExpression, type: Type, property: Symbol): boolean {
return isValidPropertyAccessWithType(node, node.expression, property.escapedName, type)
function isValidPropertyAccessForCompletions(node: PropertyAccessExpression | ImportTypeNode, type: Type, property: Symbol): boolean {
return isValidPropertyAccessWithType(node, node.kind === SyntaxKind.ImportTypeNode ? node : node.expression, property.escapedName, type)
&& (!(property.flags & SymbolFlags.Method) || isValidMethodAccess(property, type));
}
function isValidMethodAccess(method: Symbol, actualThisType: Type): boolean {
Expand All @@ -16711,8 +16719,8 @@ namespace ts {
}

function isValidPropertyAccessWithType(
node: PropertyAccessExpression | QualifiedName,
left: LeftHandSideExpression | QualifiedName,
node: PropertyAccessExpression | QualifiedName | ImportTypeNode,
left: LeftHandSideExpression | QualifiedName | ImportTypeNode,
propertyName: __String,
type: Type): boolean {

Expand Down Expand Up @@ -25339,6 +25347,8 @@ namespace ts {
case SyntaxKind.FunctionKeyword:
case SyntaxKind.EqualsGreaterThanToken:
return getSymbolOfNode(node.parent);
case SyntaxKind.ImportTypeNode:
return isLiteralImportTypeNode(node) ? getSymbolAtLocation(node.argument.literal) : undefined;

default:
return undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2890,9 +2890,9 @@ namespace ts {
/* @internal */ getMergedSymbol(symbol: Symbol): Symbol;

getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): string | number | undefined;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName | ImportTypeNode, propertyName: string): boolean;
/** Exclude accesses to private properties or methods with a `this` parameter that `type` doesn't satisfy. */
/* @internal */ isValidPropertyAccessForCompletions(node: PropertyAccessExpression, type: Type, property: Symbol): boolean;
/* @internal */ isValidPropertyAccessForCompletions(node: PropertyAccessExpression | ImportTypeNode, type: Type, property: Symbol): boolean;
/** Follow all aliases to get the original symbol. */
getAliasedSymbol(symbol: Symbol): Symbol;
/** Follow a *single* alias to get the immediately aliased symbol. */
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ namespace ts {
if (parent.kind === SyntaxKind.TypeQuery) {
return false;
}
if (parent.kind === SyntaxKind.ImportTypeNode) {
return !(parent as ImportTypeNode).isTypeOf;
}
// Do not recursively call isPartOfTypeNode on the parent. In the example:
//
// let a: A.B.C;
Expand Down Expand Up @@ -5728,6 +5731,14 @@ namespace ts {
return false;
}

/* @internal */
export function isPropertyAccessOrQualifiedNameOrImportTypeNode(node: Node): node is PropertyAccessExpression | QualifiedName | ImportTypeNode {
const kind = node.kind;
return kind === SyntaxKind.PropertyAccessExpression
|| kind === SyntaxKind.QualifiedName
|| kind === SyntaxKind.ImportTypeNode;
}

// Expression

export function isPropertyAccessOrQualifiedName(node: Node): node is PropertyAccessExpression | QualifiedName {
Expand Down
2 changes: 2 additions & 0 deletions src/harness/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
"../services/navigationBar.ts",
"../services/outliningElementsCollector.ts",
"../services/patternMatcher.ts",
"../services/pathCompletions.ts",
"../services/completions.ts",
"../services/services.ts",
"../services/shims.ts",
"../services/signatureHelp.ts",
Expand Down
12 changes: 8 additions & 4 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,9 @@ namespace ts.Completions {
case SyntaxKind.QualifiedName:
node = (parent as QualifiedName).left;
break;
case SyntaxKind.ImportTypeNode:
node = parent;
break;
default:
// There is nothing that precedes the dot, so this likely just a stray character
// or leading into a '...' token. Just bail out instead.
Expand Down Expand Up @@ -1012,18 +1015,19 @@ namespace ts.Completions {
completionKind = CompletionKind.PropertyAccess;

// Since this is qualified name check its a type node location
const isTypeLocation = insideJsDocTagTypeExpression || isPartOfTypeNode(node.parent);
const isImportType = isLiteralImportTypeNode(node);
const isTypeLocation = insideJsDocTagTypeExpression || (isImportType && !(node as ImportTypeNode).isTypeOf) || isPartOfTypeNode(node.parent);
const isRhsOfImportDeclaration = isInRightSideOfInternalImportEqualsDeclaration(node);
const allowTypeOrValue = isRhsOfImportDeclaration || (!isTypeLocation && isPossiblyTypeArgumentPosition(contextToken, sourceFile));
if (isEntityName(node)) {
if (isEntityName(node) || isImportType) {
let symbol = typeChecker.getSymbolAtLocation(node);
if (symbol) {
symbol = skipAlias(symbol, typeChecker);

if (symbol.flags & (SymbolFlags.Module | SymbolFlags.Enum)) {
// Extract module or enum members
const exportedSymbols = Debug.assertEachDefined(typeChecker.getExportsOfModule(symbol), "getExportsOfModule() should all be defined");
const isValidValueAccess = (symbol: Symbol) => typeChecker.isValidPropertyAccess(<PropertyAccessExpression>(node.parent), symbol.name);
const isValidValueAccess = (symbol: Symbol) => typeChecker.isValidPropertyAccess(isImportType ? <ImportTypeNode>node : <PropertyAccessExpression>(node.parent), symbol.name);
const isValidTypeAccess = (symbol: Symbol) => symbolCanBeReferencedAtTypeLocation(symbol);
const isValidAccess = allowTypeOrValue ?
// Any kind is allowed when dotting off namespace in internal import equals declaration
Expand Down Expand Up @@ -1063,7 +1067,7 @@ namespace ts.Completions {
}
else {
for (const symbol of type.getApparentProperties()) {
if (typeChecker.isValidPropertyAccessForCompletions(<PropertyAccessExpression>(node.parent), type, symbol)) {
if (typeChecker.isValidPropertyAccessForCompletions(node.kind === SyntaxKind.ImportTypeNode ? <ImportTypeNode>node : <PropertyAccessExpression>node.parent, type, symbol)) {
addPropertySymbol(symbol);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ declare namespace ts {
isArgumentsSymbol(symbol: Symbol): boolean;
isUnknownSymbol(symbol: Symbol): boolean;
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): string | number | undefined;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName | ImportTypeNode, propertyName: string): boolean;
/** Follow all aliases to get the original symbol. */
getAliasedSymbol(symbol: Symbol): Symbol;
getExportsOfModule(moduleSymbol: Symbol): Symbol[];
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ declare namespace ts {
isArgumentsSymbol(symbol: Symbol): boolean;
isUnknownSymbol(symbol: Symbol): boolean;
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): string | number | undefined;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName | ImportTypeNode, propertyName: string): boolean;
/** Follow all aliases to get the original symbol. */
getAliasedSymbol(symbol: Symbol): Symbol;
getExportsOfModule(moduleSymbol: Symbol): Symbol[];
Expand Down
51 changes: 51 additions & 0 deletions tests/cases/fourslash/importTypeMemberCompletions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// <reference path="fourslash.ts" />

// @Filename: /ns.ts
////export namespace Foo {
//// export namespace Bar {
//// export class Baz {}
//// export interface Bat {}
//// export const a: number;
//// const b: string;
//// }
////}

// @Filename: /top.ts
////export interface Bat {}
////export const a: number;

// @Filename: /equals.ts
////class Foo {
//// public static bar: string;
//// private static baz: number;
////}
////export = Foo;

// @Filename: /usage1.ts
////type A = typeof import("./ns")./*1*/
// @Filename: /usage2.ts
////type B = typeof import("./ns").Foo./*2*/
// @Filename: /usage3.ts
////type C = typeof import("./ns").Foo.Bar./*3*/
// @Filename: /usage4.ts
////type D = import("./ns")./*4*/
// @Filename: /usage5.ts
////type E = import("./ns").Foo./*5*/
// @Filename: /usage6.ts
////type F = import("./ns").Foo.Bar./*6*/
// @Filename: /usage7.ts
////type G = typeof import("./top")./*7*/
// @Filename: /usage8.ts
////type H = import("./top")./*8*/
// @Filename: /usage9.ts
////type H = typeof import("./equals")./*9*/

verify.completionsAt("1", ["Foo"]);
verify.completionsAt("2", ["Bar"]);
verify.completionsAt("3", ["Baz", "a"]);
verify.completionsAt("4", ["Foo"]);
verify.completionsAt("5", ["Bar"]);
verify.completionsAt("6", ["Baz", "Bat"]);
verify.completionsAt("7", ["a"]);
verify.completionsAt("8", ["Bat"]);
verify.completionsAt("9", ["prototype", "bar"]);