You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fix various LS text insertion / quoting bugs - #65608
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.
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.
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.
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.
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.
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Same sort of deal as #64839.
It's easiest to just look at the fix commits.