Skip to content

Commit 120af86

Browse files
author
Andy
authored
Fix bug: replacmentSpan for completion shouldn't include property name that doesn't start with completion name (microsoft#21587)
1 parent 1784e51 commit 120af86

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/services/completions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ namespace ts.Completions {
185185
else if (needsConvertPropertyAccess) {
186186
// TODO: GH#20619 Use configured quote style
187187
insertText = `["${name}"]`;
188-
replacementSpan = createTextSpanFromBounds(findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!.getStart(sourceFile), propertyAccessToConvert!.name.end);
188+
const dot = findChildOfKind(propertyAccessToConvert!, SyntaxKind.DotToken, sourceFile)!;
189+
// If the text after the '.' starts with this name, write over it. Else, add new text.
190+
const end = startsWith(name, propertyAccessToConvert!.name.text) ? propertyAccessToConvert!.name.end : dot.end;
191+
replacementSpan = createTextSpanFromBounds(dot.getStart(sourceFile), end);
189192
}
190193

191194
if (isJsxInitializer) {
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/// <reference path='fourslash.ts' />
22

33
////declare const x: { "foo ": "space in the name", };
4-
////x[|.fo/**/|];
4+
////x[|.fo/*0*/|];
5+
////x[|./*1*/|]
6+
////unrelatedIdentifier;
57

6-
const replacementSpan = test.ranges()[0];
7-
verify.completionsAt("", [{ name: "foo ", insertText: '["foo "]', replacementSpan }], { includeInsertTextCompletions: true });
8+
const [r0, r1] = test.ranges();
9+
verify.completionsAt("0", [{ name: "foo ", insertText: '["foo "]', replacementSpan: r0 }], { includeInsertTextCompletions: true });
10+
verify.completionsAt("1", [{ name: "foo ", insertText: '["foo "]', replacementSpan: r1 }], { includeInsertTextCompletions: true });

0 commit comments

Comments
 (0)