Skip to content

Commit 3b1b5e2

Browse files
committed
Add string literal completion for equality expression
1 parent cf20850 commit 3b1b5e2

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/services/completions.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ namespace ts.Completions {
173173
// var y = require("/*completion position*/");
174174
return getStringLiteralCompletionEntriesFromModuleNames(<StringLiteral>node, compilerOptions, host, typeChecker);
175175
}
176+
else if (isEqualityExpression(node.parent)) {
177+
// Get all known external module names or complete a path to a module
178+
// i.e. x === '/*completion position'
179+
return getStringLiteralCompletionEntriesFromBinaryExpression(<StringLiteral>node, node.parent, typeChecker);
180+
}
176181
else {
177182
const argumentInfo = SignatureHelp.getImmediatelyContainingArgumentInfo(node, position, sourceFile);
178183
if (argumentInfo) {
@@ -240,6 +245,18 @@ namespace ts.Completions {
240245
return undefined;
241246
}
242247

248+
function getStringLiteralCompletionEntriesFromBinaryExpression(node: StringLiteral, parent: BinaryExpression, typeChecker: TypeChecker): CompletionInfo | undefined {
249+
const type = typeChecker.getTypeAtLocation(parent.left === node ? parent.right : parent.left);
250+
if (type) {
251+
const entries: CompletionEntry[] = [];
252+
addStringLiteralCompletionsFromType(type, entries, typeChecker);
253+
if (entries.length) {
254+
return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries };
255+
}
256+
}
257+
return undefined;
258+
}
259+
243260
function addStringLiteralCompletionsFromType(type: Type, result: Push<CompletionEntry>, typeChecker: TypeChecker): void {
244261
if (type && type.flags & TypeFlags.TypeParameter) {
245262
type = typeChecker.getApparentType(type);
@@ -1756,4 +1773,11 @@ namespace ts.Completions {
17561773
catch (e) {}
17571774
return undefined;
17581775
}
1776+
1777+
function isEqualityExpression(node: Node): node is BinaryExpression {
1778+
return isBinaryExpression(node) && (node.operatorToken.kind == SyntaxKind.EqualsEqualsToken ||
1779+
node.operatorToken.kind === SyntaxKind.ExclamationEqualsToken ||
1780+
node.operatorToken.kind === SyntaxKind.EqualsEqualsEqualsToken ||
1781+
node.operatorToken.kind === SyntaxKind.ExclamationEqualsEqualsToken);
1782+
}
17591783
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////type As = 'arf' | 'abacus' | 'abaddon';
4+
////let a: As;
5+
////if ('/**/' != a
6+
7+
goTo.marker();
8+
verify.completionListContains("arf");
9+
verify.completionListContains("abacus");
10+
verify.completionListContains("abaddon");
11+
verify.completionListCount(3);
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////type As = 'arf' | 'abacus' | 'abaddon';
4+
////let a: As;
5+
////if (a === '/**/
6+
7+
goTo.marker();
8+
verify.completionListContains("arf");
9+
verify.completionListContains("abacus");
10+
verify.completionListContains("abaddon");
11+
verify.completionListCount(3);
12+

0 commit comments

Comments
 (0)