Skip to content

Add assert keyword in completions #47644

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 2 commits into from
Jan 28, 2022
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
44 changes: 42 additions & 2 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ namespace ts.Completions {

switch (completionData.kind) {
case CompletionDataKind.Data:
const response = completionInfoFromData(sourceFile, host, program, compilerOptions, log, completionData, preferences, formatContext);
const response = completionInfoFromData(sourceFile, host, program, compilerOptions, log, completionData, preferences, formatContext, position);
if (response?.isIncomplete) {
incompleteCompletionsCache?.set(response);
}
Expand Down Expand Up @@ -441,6 +441,7 @@ namespace ts.Completions {
completionData: CompletionData,
preferences: UserPreferences,
formatContext: formatting.FormatContext | undefined,
position: number
): CompletionInfo | undefined {
const {
symbols,
Expand Down Expand Up @@ -501,7 +502,7 @@ namespace ts.Completions {
isJsxIdentifierExpected,
isRightOfOpenTag,
);
getJSCompletionEntries(sourceFile, location.pos, uniqueNames, getEmitScriptTarget(compilerOptions), entries); // TODO: GH#18217
getJSCompletionEntries(sourceFile, location.pos, uniqueNames, getEmitScriptTarget(compilerOptions), entries);
}
else {
if (!isNewIdentifierLocation && (!symbols || symbols.length === 0) && keywordFilters === KeywordCompletionFilters.None) {
Expand Down Expand Up @@ -545,6 +546,13 @@ namespace ts.Completions {
}
}

const entryNames = new Set(entries.map(e => e.name));
for (const keywordEntry of getContextualKeywords(contextToken, position)) {
if (!entryNames.has(keywordEntry.name)) {
insertSorted(entries, keywordEntry, compareCompletionEntries, /*allowDuplicates*/ true);
}
}

for (const literal of literals) {
insertSorted(entries, createCompletionEntryForLiteral(sourceFile, preferences, literal), compareCompletionEntries, /*allowDuplicates*/ true);
}
Expand Down Expand Up @@ -3578,6 +3586,38 @@ namespace ts.Completions {
return isIdentifier(node) ? node.originalKeywordKind || SyntaxKind.Unknown : node.kind;
}

function getContextualKeywords(
contextToken: Node | undefined,
position: number,
): readonly CompletionEntry[] {
const entries = [];
/**
* An `AssertClause` can come after an import declaration:
* import * from "foo" |
* import "foo" |
* or after a re-export declaration that has a module specifier:
* export { foo } from "foo" |
* Source: https://tc39.es/proposal-import-assertions/
*/
if (contextToken) {
const file = contextToken.getSourceFile();
const parent = contextToken.parent;
const tokenLine = file.getLineAndCharacterOfPosition(contextToken.end).line;
const currentLine = file.getLineAndCharacterOfPosition(position).line;
if ((isImportDeclaration(parent) || isExportDeclaration(parent) && parent.moduleSpecifier)
&& contextToken === parent.moduleSpecifier
&& tokenLine === currentLine) {
entries.push({
name: tokenToString(SyntaxKind.AssertKeyword)!,
kind: ScriptElementKind.keyword,
kindModifiers: ScriptElementKindModifier.none,
sortText: SortText.GlobalsOrKeywords,
});
}
}
return entries;
}

/** Get the corresponding JSDocTag node if the position is in a jsDoc comment */
function getJsDocTagAtPosition(node: Node, position: number): JSDocTag | undefined {
return findAncestor(node, n =>
Expand Down
54 changes: 54 additions & 0 deletions tests/cases/fourslash/completionsAssertKeyword.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/// <reference path="fourslash.ts" />

// @allowJs: true
// @Filename: a.ts
//// const f = {
//// a: 1
////};
//// import * as thing from "thing" /*0*/
//// export { foo } from "foo" /*1*/
//// import "foo" as /*2*/
//// import "foo" a/*3*/
//// import * as that from "that"
//// /*4*/
//// import * /*5*/ as those from "those"

// @Filename: b.js
//// import * as thing from "thing" /*js*/;

const assertEntry = {
name: "assert",
kind: "keyword",
sortText: completion.SortText.GlobalsOrKeywords,
};

verify.completions(
{
marker: "0",
includes: [assertEntry],
},
{
marker: "1",
includes: [assertEntry],
},
{
marker: "2",
excludes: ["assert"],
},
{
marker: "3",
includes: [assertEntry],
},
{
marker: "4",
excludes: ["assert"],
},
{
marker: "5",
excludes: ["assert"],
},
{
marker: "js",
includes: [assertEntry],
},
);