Skip to content

Conversation

@iisaduan
Copy link
Member

@iisaduan iisaduan commented Jun 3, 2024

fixes #58438

The above issue is a regression bug that happened because of #57839, which was included with 5.5 beta. This PR updates the replacementRange to be more inline with 5.4 behavior, namely completions on unterminated strings will not remove anything to the right of the cursor. Closed strings will replace everything within the quotes.

Examples:

type Test = `abcde';
const test: Test = 'a|; //comment
const test2: Test = 'a|bc // comment
const test3: Test = 'a|bc' //comment

becomes

type Test = `abcde';
const test: Test = 'abcde; //comment
const test2: Test = 'abcdebc // comment
const test:3 Test = 'abcde'; //comment

after completions are triggered at all three locations.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jun 3, 2024
@iisaduan iisaduan changed the title fix58438: do not delete comments following unterminated string completion fix58438: do not delete comments/code following unterminated string completion Jun 3, 2024
Co-authored-by: Daniel Rosenwasser <[email protected]>
Comment on lines 2338 to 2343
export function createTextSpanFromStringLiteralLikeContent(node: StringLiteralLike, position?: number) {
let replacementEnd = node.getEnd() - 1;
if (node.isUnterminated) {
// we return no replacement range only if unterminated string is empty
if (node.getStart() === replacementEnd) return undefined;
replacementEnd = node.getEnd();
replacementEnd = position !== undefined && position <= node.getEnd() ? position : node.getEnd();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are only two callers of createTextSpanFromStringLiteralLikeContent, and neither of them omit the position argument, so just make it required (and document what position is supposed to mean)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do you have any tests for when position is strictly greater than the end of the node? I don't think I'm seeing any.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that was old code, thanks for catching it! (I tried to make position optional, but it ended up being needed in more places)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be possible in a completion for position > node end but I will add that check incase createTextSpanFromStringLiteralLikeContent will be used in a non-completion context in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I can think of is possibly an unterminated string literal on the previous line

let s: "string" = "s
/*$*/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completions won't be triggered on new lines -- so

type LongString = `asdf
string
line`

let s: LongString = `asdf
/**/
  ^ no suggestions

let r: LongString = `asdf/**/
                          ^ completes to `asdf\nstring\nline`

@DanielRosenwasser
Copy link
Member

@typescript-bot cherry-pick to release-5.5

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jun 3, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
cherry-pick to release-5.5 ✅ Started ✅ Results

@typescript-bot
Copy link
Collaborator

Hey, @DanielRosenwasser! I've created #58762 for you.

DanielRosenwasser pushed a commit that referenced this pull request Jun 3, 2024
@iisaduan iisaduan merged commit 506f3e2 into microsoft:main Jun 4, 2024
@iisaduan iisaduan deleted the string-completion-no-delete-comment branch June 4, 2024 20:10
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Selecting suggestion of string completion deletes everything up to end of line, if no ending quote is present

4 participants