Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dc73483
Parse type-only import specifiers
andrewbranch Sep 16, 2021
ad27364
Add type-only export specifiers
andrewbranch Sep 16, 2021
0ba00b9
Update transform and emit
andrewbranch Sep 16, 2021
eb63b02
Update checking
andrewbranch Sep 16, 2021
9b31bd1
Fix elision when combined with importsNotUsedAsValues=preserve
andrewbranch Sep 16, 2021
02d4ff9
Accept baselines
andrewbranch Sep 17, 2021
ac9b14b
Add test
andrewbranch Sep 20, 2021
577868d
WIP auto imports updates
andrewbranch Sep 20, 2021
c41f8ee
First auto-imports test working
andrewbranch Sep 20, 2021
d34544c
More auto-import tests
andrewbranch Sep 20, 2021
76da8c7
Fix auto imports of type-only exports
andrewbranch Sep 21, 2021
487f585
Add test for promoting type-only import
andrewbranch Sep 21, 2021
b6ea3ac
Sort import/export specifiers by type-onlyness
andrewbranch Sep 21, 2021
9a1765e
Update completions for `import { type |`
andrewbranch Sep 22, 2021
9e1878d
Update other completions tests
andrewbranch Sep 22, 2021
f7248e1
Respect organize imports sorting when promoting type-only to regular …
andrewbranch Sep 22, 2021
06063f2
Fix comment mistakes
andrewbranch Sep 24, 2021
4ba367b
Update src/services/codefixes/importFixes.ts
andrewbranch Sep 24, 2021
75a2909
Rearrange some order of assignments in parser
andrewbranch Sep 24, 2021
b55acde
Split huge if statement
andrewbranch Sep 24, 2021
414f38b
Remove redundant check
andrewbranch Sep 24, 2021
3768d0a
Merge branch 'main' into feature/type-only-specifiers
andrewbranch Sep 24, 2021
39e72d8
Update new transformer
andrewbranch Sep 24, 2021
9033c72
Fix import statement completions
andrewbranch Sep 24, 2021
adcb331
Fix type keyword completions good grief
andrewbranch Sep 27, 2021
d64c923
Fix last tests
andrewbranch Sep 27, 2021
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
Prev Previous commit
Next Next commit
Update completions for import { type |
  • Loading branch information
andrewbranch committed Sep 22, 2021
commit 9a1765e86418f0218f33e5aaa81b71c24abc4853
38 changes: 34 additions & 4 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ namespace ts.Completions {
FunctionLikeBodyKeywords, // Keywords at function like body
TypeAssertionKeywords,
TypeKeywords,
TypeKeyword, // Literally just `type`
Last = TypeKeywords
}

Expand Down Expand Up @@ -1776,6 +1777,9 @@ namespace ts.Completions {
function tryGetImportCompletionSymbols(): GlobalsSearch {
if (!importCompletionNode) return GlobalsSearch.Continue;
isNewIdentifierLocation = true;
if (isTypeKeywordTokenOrIdentifier(contextToken)) {
keywordFilters = KeywordCompletionFilters.TypeKeyword;
}
collectAutoImports();
return GlobalsSearch.Success;
}
Expand Down Expand Up @@ -2252,11 +2256,20 @@ namespace ts.Completions {
* Relevant symbols are stored in the captured 'symbols' variable.
*/
function tryGetImportOrExportClauseCompletionSymbols(): GlobalsSearch {
// `import { |` or `import { a as 0, | }`
const namedImportsOrExports = contextToken && (contextToken.kind === SyntaxKind.OpenBraceToken || contextToken.kind === SyntaxKind.CommaToken)
? tryCast(contextToken.parent, isNamedImportsOrExports) : undefined;
if (!contextToken) return GlobalsSearch.Continue;

// `import { |` or `import { a as 0, | }` or `import { type | }`
const namedImportsOrExports =
contextToken.kind === SyntaxKind.OpenBraceToken || contextToken.kind === SyntaxKind.CommaToken ? tryCast(contextToken.parent, isNamedImportsOrExports) :
isTypeKeywordTokenOrIdentifier(contextToken) ? tryCast(contextToken.parent.parent, isNamedImportsOrExports) : undefined;

if (!namedImportsOrExports) return GlobalsSearch.Continue;

// We can at least offer `type` at `import { |`
if (!isTypeKeywordTokenOrIdentifier(contextToken)) {
keywordFilters = KeywordCompletionFilters.TypeKeyword;
}

// try to show exported member for imported/re-exported module
const { moduleSpecifier } = namedImportsOrExports.kind === SyntaxKind.NamedImports ? namedImportsOrExports.parent.parent : namedImportsOrExports.parent;
if (!moduleSpecifier) return namedImportsOrExports.kind === SyntaxKind.NamedImports ? GlobalsSearch.Fail : GlobalsSearch.Continue;
Expand All @@ -2267,7 +2280,12 @@ namespace ts.Completions {
isNewIdentifierLocation = false;
const exports = typeChecker.getExportsAndPropertiesOfModule(moduleSpecifierSymbol);
const existing = new Set((namedImportsOrExports.elements as NodeArray<ImportOrExportSpecifier>).filter(n => !isCurrentlyEditingNode(n)).map(n => (n.propertyName || n.name).escapedText));
symbols = concatenate(symbols, exports.filter(e => e.escapedName !== InternalSymbolName.Default && !existing.has(e.escapedName)));
const uniques = exports.filter(e => e.escapedName !== InternalSymbolName.Default && !existing.has(e.escapedName));
symbols = concatenate(symbols, uniques);
if (!uniques.length) {
// If there's nothing else to import, don't offer `type` either
keywordFilters = KeywordCompletionFilters.None;
}
return GlobalsSearch.Success;
}

Expand Down Expand Up @@ -2557,6 +2575,16 @@ namespace ts.Completions {
case SyntaxKind.SetKeyword:
return !isFromObjectTypeDeclaration(contextToken);

case SyntaxKind.Identifier:
if (containingNodeKind === SyntaxKind.ImportSpecifier &&
contextToken === (parent as ImportSpecifier).name &&
(contextToken as Identifier).text === "type"
) {
// import { type | }
return false;
}
break;

case SyntaxKind.ClassKeyword:
case SyntaxKind.EnumKeyword:
case SyntaxKind.InterfaceKeyword:
Expand Down Expand Up @@ -2974,6 +3002,8 @@ namespace ts.Completions {
return isTypeKeyword(kind) || kind === SyntaxKind.ConstKeyword;
case KeywordCompletionFilters.TypeKeywords:
return isTypeKeyword(kind);
case KeywordCompletionFilters.TypeKeyword:
return kind === SyntaxKind.TypeKeyword;
default:
return Debug.assertNever(keywordFilter);
}
Expand Down
4 changes: 4 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,10 @@ namespace ts {
return node.kind === SyntaxKind.TypeKeyword;
}

export function isTypeKeywordTokenOrIdentifier(node: Node) {
return isTypeKeywordToken(node) || isIdentifier(node) && node.text === "type";
}

/** True if the symbol is for an external module, as opposed to a namespace. */
export function isExternalModuleSymbol(moduleSymbol: Symbol): boolean {
return !!(moduleSymbol.flags & SymbolFlags.Module) && moduleSymbol.name.charCodeAt(0) === CharacterCodes.doubleQuote;
Expand Down
11 changes: 8 additions & 3 deletions tests/cases/fourslash/completionListInImportClause01.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference path='fourslash.ts'/>

// @ModuleResolution: classic

// @Filename: m1.ts
Expand All @@ -12,11 +13,15 @@
////import {foo,/*4*/ from "m1"
////import {bar as /*5*/, /*6*/ from "m1"
////import {foo, bar, baz as b,/*7*/} from "m1"
////import { type /*8*/ } from "m1";

const type = { name: "type", sortText: completion.SortText.GlobalsOrKeywords };

verify.completions(
{ marker: ["1", "2", "3"], exact: ["bar", "baz", "foo"] },
{ marker: "4", exact: ["bar", "baz"] },
{ marker: ["1", "2", "3"], exact: ["bar", "baz", "foo", type] },
{ marker: "4", exact: ["bar", "baz", type] },
{ marker: "5", exact: undefined, isNewIdentifierLocation: true },
{ marker: "6", exact: ["baz", "foo"] },
{ marker: "6", exact: ["baz", "foo", type] },
{ marker: "7", exact: undefined },
{ marker: "8", exact: ["bar", "baz", "foo"] }, // No 'type'
);