Skip to content

Fix various LS text insertion / quoting bugs - #65608

Open
Jake Bailey (jakebailey) wants to merge 24 commits into
microsoft:mainfrom
jakebailey:escape-string-completions
Open

Jake Bailey (jakebailey) wants to merge 24 commits into
microsoft:mainfrom
jakebailey:escape-string-completions

Conversation

@jakebailey

Copy link
Copy Markdown
Member

Same sort of deal as #64839.

It's easiest to just look at the fix commits.

The language service needs the same check the checker uses to decide
whether a property name can be written as a numeric literal. It only
depends on number conversion, so it belongs with jsnum.
Property names completed inside a string are inserted without escaping
for the surrounding quote, so a name containing that quote or ${ ends
the string or starts a substitution. String literal type completions in
single-quoted strings are escaped for double quotes.
A property name inserted into a string literal unescaped can end the
string early or start a template substitution, turning type metadata
from a dependency into code in the user's file. Escape names for the
quote of the string being completed, and read the quote style from the
token flags so single-quoted strings are no longer escaped as if they
were double-quoted.
File and directory names completed in a module specifier are inserted
without escaping, so a name containing the specifier's quote character
ends the string early.
A file or directory name containing the specifier's quote character
otherwise ends the string early instead of naming the selected file.
Any property name starting with a digit is inserted unquoted in an
element access, so "1+sideEffect()" becomes an expression and "01"
accesses property 1.
Only names that are the canonical text of a number can be written as a
numeric literal in an element access. Inserting other names that start
with a digit produced an arbitrary expression, or accessed a different
property, as with "01" accessing property 1.
Completions inside a quoted name in an import or export specifier list
look up the declaration instead of its module specifier, so they find
nothing. Outside quotes, a non-identifier export name starting with a
digit is inserted unquoted.
Completions inside a quoted specifier name resolved the declaration
node as if it were the module specifier and so found no exports. Names
inserted outside quotes must be quoted whenever they are not
identifiers, since a specifier name can never be a numeric literal.
JSX attribute strings have no escape sequences, so completions escaped
as JavaScript strings insert stray backslashes into the value, and a
value containing the attribute's quote cannot be written at all.
JSX attribute strings have no escape sequences, and character
references in them are decoded only during emit, so the checker and the
emitted code would see different values. Insert values literally when
the attribute string can hold them, and otherwise replace the string
with an expression container.
Literal completions directly after the equals sign of a JSX attribute
are inserted as JavaScript literals, which are not valid JSX attribute
values when they are numbers or contain escape sequences.
Literals completed directly after the equals sign of a JSX attribute
must be valid attribute values. Write strings with whichever quote can
hold them literally, falling back to an expression container, and wrap
other literals in an expression container.
After an identifier following the equals sign of a JSX attribute,
completions do not know they are completing the attribute's value, so
they insert bare names that are not valid JSX and leave the identifier
in place.
After an identifier following the equals sign, completions did not know
they were completing an attribute value, so they inserted bare names and
literals that are not valid JSX, and did not replace the identifier.
The replacement range for a module path completion is measured in the
string's value, which is shorter than its source text when an earlier
segment contains an escape sequence, so the edit lands on the wrong
characters.
When an earlier segment of a module specifier contains an escape
sequence, its value is shorter than its source text, so a range measured
in the value lands on the wrong characters.
Triple-slash directives cannot escape their quote character, so
offering a file name containing it inserts a reference to a different
path.
Triple-slash directives cannot escape their quote character, so file
names containing it cannot be referenced and must not be offered.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved issues corrupt lone-surrogate values in JSX completions and cause quadratic rescanning when calculating module-specifier replacement ranges.

2 open issues
What changed in this PR

This PR extends the earlier module-specifier escaping work to language-service completions, aiming to insert valid text in string literals, JSX attributes, imports, and property accesses.

Changes:

  • Makes completion text and replacement ranges account for quoting, escapes, and JSX syntax.
  • Shares numeric-name validation between the checker and language service.
  • Adds regression coverage and re-enables a JSX completion test.
File Description
tsc/​internal/​ls/​string_completions.go Handles string and path escaping, JSX values, and escaped replacement ranges.
tsc/​internal/​ls/​completions.go Corrects JSX initializer insertion and property/import name quoting.
tsc/​internal/​jsnum/​string.go Exposes shared numeric-literal-name validation.
tsc/​internal/​fourslash/​tests/​stringLiteralCompletionsInJsxAttributeInitializer_test.go Adds JSX escaping and initializer completion tests.
tsc/​internal/​fourslash/​tests/​completionsJsxAttributeInitializer2_test.go Re-enables the existing JSX initializer test.
tsc/​internal/​fourslash/​tests/​completionListInvalidMemberNames_escapeQuote_test.go Tests escaped strings, digit-prefixed members, and quoted imports/exports.
tsc/​internal/​fourslash/​tests/​completionForStringLiteralRelativeImport4_test.go Tests path escaping, replacement ranges, and reference-directive quotes.
tsc/​internal/​checker/​utilities.go Removes the relocated numeric-name helper.
tsc/​internal/​checker/​relater.go Uses shared numeric-name validation in tuple comparisons.
tsc/​internal/​checker/​nodebuilderimpl.go Uses the shared helper when constructing names.
tsc/​internal/​checker/​checker.go Updates numeric-name checks to use the shared helper.

Review effort: Balanced

Comment thread tsc/internal/ls/string_completions.go Resolved
Comment thread tsc/internal/ls/string_completions.go Resolved
JSX attribute strings cannot escape a lone surrogate, and one inserted
as raw text cannot survive LSP serialization, so the completion inserts
replacement characters instead of the selected value.
A lone surrogate cannot be written in a JSX attribute string and cannot
survive LSP serialization as raw text, so use an expression container
with an escaped string literal for such values.
Rescanning every source prefix of a module specifier made computing
the replacement range quadratic in the specifier's length. Walk its
escape sequences once with the scanner's own decoding instead,
including the CR and CRLF to LF normalization of template literals.
Completions quote string values with a JSON encoder that replaces a
lone surrogate with replacement characters, where JSON.stringify would
write an escape sequence, so both the label and the inserted literal
lose the value.
JSON and LSP cannot carry a lone surrogate as raw text, so write one as
an escape sequence the way JSON.stringify does, both in quoted completion
text and in the labels of JSX attribute string completions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

A cursor immediately after an escaped separator receives no explicit zero-length completion range, which can cause suggestions to be filtered out or overwrite the escape digits when accepted.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve escaped separator when completing at end of raw text

tsc/​internal/​ls/​string_completions.go:761

When the cursor is immediately after an escaped separator, such as import "./a\x2f|", offset equals len(rawText). Returning nil omits the completion's text edit, so the client falls back to the source word x2f rather than an empty fragment: the suggestion can be filtered out or replace the escape digits when accepted. Return an explicit zero-length range at the mapped source offset in this case, so the separator is preserved.

2 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

2 participants