Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25336,6 +25336,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
3 changes: 3 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
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
17 changes: 14 additions & 3 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,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 @@ -996,18 +999,26 @@ 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) => {
if (!isImportType) {
return typeChecker.isValidPropertyAccess(<PropertyAccessExpression>(node.parent), symbol.name);
}
else {
return !(node as ImportTypeNode).isTypeOf && !!(symbol.flags & SymbolFlags.Value);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont you still need typeChecker.isValidPropertyAccess especially checkPropertyAccessibility needs to be done to avoid not showing non exported stuff?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-exported symbols aren't returned by getExportsOfModule - except for class statics! I'll add a test.

}
};
const isValidTypeAccess = (symbol: Symbol) => symbolCanBeReferencedAtTypeLocation(symbol);
const isValidAccess = allowTypeOrValue ?
// Any kind is allowed when dotting off namespace in internal import equals declaration
Expand Down
40 changes: 40 additions & 0 deletions tests/cases/fourslash/importTypeMemberCompletions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/// <reference path="fourslash.ts" />

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

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

// @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*/

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"]);