Skip to content

Commit ef765e5

Browse files
committed
Filter out would-be-duplicate names from JSX attribute completion
1 parent f465d99 commit ef765e5

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/services/services.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,20 +3062,17 @@ namespace ts {
30623062
}
30633063
else if(jsxContainer) {
30643064
let attrsType: Type;
3065-
if (jsxContainer.kind === SyntaxKind.JsxSelfClosingElement) {
3066-
// Cursor is inside a JSX self-closing element
3067-
attrsType = typeChecker.getJsxElementAttributesType(<JsxSelfClosingElement>jsxContainer);
3068-
}
3069-
else if(jsxContainer.kind === SyntaxKind.JsxOpeningElement) {
3070-
// Cursor is inside a JSX element
3071-
attrsType = typeChecker.getJsxElementAttributesType(<JsxOpeningElement>jsxContainer);
3072-
}
3065+
if ((jsxContainer.kind === SyntaxKind.JsxSelfClosingElement) || (jsxContainer.kind === SyntaxKind.JsxOpeningElement)) {
3066+
// Cursor is inside a JSX self-closing element or opening element
3067+
attrsType = typeChecker.getJsxElementAttributesType(<JsxOpeningLikeElement>jsxContainer);
3068+
3069+
if (attrsType) {
3070+
symbols = filterJsxAttributes((<JsxOpeningLikeElement>jsxContainer).attributes, typeChecker.getPropertiesOfType(attrsType));
3071+
isMemberCompletion = true;
3072+
isNewIdentifierLocation = false;
3073+
return true;
3074+
}
30733075

3074-
if (attrsType) {
3075-
symbols = typeChecker.getPropertiesOfType(attrsType);
3076-
isMemberCompletion = true;
3077-
isNewIdentifierLocation = false;
3078-
return true;
30793076
}
30803077
}
30813078

@@ -3476,6 +3473,22 @@ namespace ts {
34763473
}
34773474
}
34783475

3476+
function filterJsxAttributes(attributes: NodeArray<JsxAttribute|JsxSpreadAttribute>, symbols: Symbol[]): Symbol[] {
3477+
let seenNames: Map<boolean> = {};
3478+
for(let attr of attributes) {
3479+
if(attr.kind === SyntaxKind.JsxAttribute) {
3480+
seenNames[(<JsxAttribute>attr).name.text] = true;
3481+
}
3482+
}
3483+
let result: Symbol[] = [];
3484+
for(let sym of symbols) {
3485+
if(!seenNames[sym.name]) {
3486+
result.push(sym);
3487+
}
3488+
}
3489+
return result;
3490+
}
3491+
34793492
function getCompletionsAtPosition(fileName: string, position: number): CompletionInfo {
34803493
synchronizeHostData();
34813494

tests/cases/fourslash/tsxCompletion3.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111

1212
goTo.marker();
1313
verify.completionListContains('two');
14+
verify.not.completionListContains('one');

0 commit comments

Comments
 (0)