Skip to content

Fix more LS escaping related bugs - #65665

Open
Jake Bailey (jakebailey) wants to merge 53 commits into
microsoft:mainfrom
jakebailey:fix-ls-edit-escaping
Open

Jake Bailey (jakebailey) wants to merge 53 commits into
microsoft:mainfrom
jakebailey:fix-ls-edit-escaping

Conversation

@jakebailey

Copy link
Copy Markdown
Member

More stuff similar to the last PR but in other areas.

These show that renaming a file or directory writes the updated
module specifier into import strings, tsconfig strings and reference
directives without escaping it, so a file name containing a quote
produces broken code.
Renaming a file or directory rewrites import strings and tsconfig
paths using the unescaped specifier text. A file name containing a
quote, such as one shipped in a dependency's declaration files, then
closes the string and turns the rest of the name into code in the
importing file.

Escape the new text for the quote used by each string literal.
Reference directive paths have no escape syntax, so leave a directive
unchanged when its new path contains the directive's quote or a line
terminator.
Auto-import and file rename write module specifiers into JSDoc import
types. These tests record the current output for a path containing */,
which ends the comment early.
Module specifiers written into JSDoc import types sit inside a block
comment. A specifier containing */ ends the comment early, so the rest
of the specifier and the remainder of the JSDoc become code.
JSDoc @PARAM completions copy initializers, inferred types and
destructured property names into the comment. This test records the
current completions for text containing */.
JSDoc @PARAM completions copy initializers, inferred types and
destructured property names into the comment. Text containing */ or a
line break ends the comment or the tag early, so such text is left out
and the tag falls back to a plainer form.
These tests record member, object literal method, exhaustive switch and
import statement completions for names containing $ and backslashes,
with and without client snippet support.
Clients without snippet support insert completion text verbatim. Member
and import statement completions escaped $ for snippets in that mode
too, so names such as $returnPromise were inserted with a stray
backslash.
This test records the closing tag snippet for a JSX tag name written
with a Unicode escape.
Snippet syntax treats a backslash as an escape character, so a literal
backslash must be written as \\. Snippet text containing a backslash,
such as a string-named member or a JSX tag name with a Unicode escape,
lost the backslash when the client expanded the snippet.

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 escaping and name-handling defects can produce invalid code or JSON, break JSDoc comments, and alter or leave stale renamed references.

9 open issues
What changed in this PR

This PR hardens escaping in TypeScript’s language service so generated edits preserve unusual names and string contents across renames, completions, imports, and code fixes. It also updates compiler alias selection for non-identifier export names.

Changes:

  • Adds context-aware escaping for strings, snippets, JSDoc text, and file-rename edits.
  • Improves handling of quoted names, mapped properties, destructuring, and Unicode JSX auto-imports.
  • Adds regression tests and updates rename baselines to use an identifier placeholder.
File Description
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitNonIdentifierExportName.ts Tests quoted exports and identifier alias fallback.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​renameInCommonFile.baseline Updates logged rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​renameAncestorProjectRefMangement.baseline Updates project-reference rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithSourceMapsNotSolutionEditEnd.baseline Updates rename placeholder after end edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithSourceMapsNotSolutionEdit.baseline Updates rename placeholder after edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithSourceMapsNotSolution.baseline Updates source-map rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithSourceMapsEditEnd.baseline Updates rename placeholder after end edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithSourceMapsEdit.baseline Updates rename placeholder after edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithSourceMaps.baseline Updates source-map rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithProjectReferencesEditEnd.baseline Updates project-reference rename placeholder after end edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithProjectReferencesEdit.baseline Updates project-reference rename placeholder after edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithProjectReferences.baseline Updates project-reference rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithDisableSourceOfProjectReferenceRedirectEditEnd.baseline Updates redirect-disabled rename placeholder after end edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithDisableSourceOfProjectReferenceRedirectEdit.baseline Updates redirect-disabled rename placeholder after edits.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameWithDisableSourceOfProjectReferenceRedirect.baseline Updates redirect-disabled rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameTargetDoesNotExist.baseline Updates missing-target rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRenameStartingAtDefinition.baseline Updates definition-origin rename placeholder.
tsc/​testdata/​baselines/​reference/​fourslash/​state/​declarationMapsRename.baseline Updates declaration-map rename placeholder.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitNonIdentifierExportName.types Records inferred alias types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitNonIdentifierExportName.symbols Records quoted export symbols.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitNonIdentifierExportName.js Records JavaScript and declaration output.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitNonIdentifierExportName.errors.txt Records the unnameable-export diagnostic.
tsc/​internal/​ls/​utilities.go Escapes line separators in generated quoted strings.
tsc/​internal/​ls/​rename.go Adds string escaping and non-identifier alias handling.
tsc/​internal/​ls/​organizeimports.go Quotes import-attribute grouping keys to avoid collisions.
tsc/​internal/​ls/​lsutil/​utilities.go Adds comment-safe string escaping.
tsc/​internal/​ls/​file_rename.go Escapes replacement paths and guards reference directives.
tsc/​internal/​ls/​completions.go Separates snippet escaping and guards generated JSDoc text.
tsc/​internal/​ls/​codeactions_missingmemberfixer.go Quotes non-identifier mapped member names.
tsc/​internal/​ls/​codeactions_fixmissingtypeannotation.go Uses element access for non-identifier binding properties.
tsc/​internal/​ls/​autoimport/​view.go Decodes Unicode initials for JSX completion filtering.
tsc/​internal/​ls/​autoimport/​fix.go Escapes JSDoc imports and preserves quoted import names.
tsc/​internal/​fourslash/​tests/​renameEscaping_test.go Adds literal and non-identifier rename cases.
tsc/​internal/​fourslash/​tests/​organizeImportsAttributes4_test.go Tests quoted import-attribute values.
tsc/​internal/​fourslash/​tests/​importStatementCompletions_noSnippet_test.go Tests plain-text imports containing dollar signs.
tsc/​internal/​fourslash/​tests/​getEditsForFileRename_escapedSpecifier_test.go Tests escaped paths and JSDoc file-renaming edits.
tsc/​internal/​fourslash/​tests/​completionsSnippetEscaping_test.go Tests snippet and plain-text completion escaping.
tsc/​internal/​fourslash/​tests/​completionsOverridingMethod22_test.go Updates plain-text dollar-sign completion output.
tsc/​internal/​fourslash/​tests/​completionsImport_jsxOpeningTagImportDefault_test.go Adds a non-ASCII JSX default-import case.
tsc/​internal/​fourslash/​tests/​completionFilterText3_test.go Tests line-separator property completion escaping.
tsc/​internal/​fourslash/​tests/​codeFixPromoteTypeOnlyOrderingCrash_test.go Tests promotion of quoted type-only imports.
tsc/​internal/​fourslash/​tests/​codeFixMissingTypeAnnotationOnExports14_test.go Tests extraction of non-identifier binding properties.
tsc/​internal/​fourslash/​tests/​codeFixClassImplementInterfaceMappedType2_test.go Tests quoted mapped member generation.
tsc/​internal/​fourslash/​tests/​basicJSDocCompletions_test.go Tests comment terminators in parameter completions.
tsc/​internal/​fourslash/​tests/​autoImportQuotedModuleSpecifier_test.go Tests comment-safe JSDoc auto-import paths.
tsc/​internal/​fourslash/​tests/​autoCloseTag_test.go Tests escaped JSX closing-tag output.
tsc/​internal/​fourslash/​fourslash.go Uses an identifier placeholder for rename baselines.
tsc/​internal/​checker/​symbolaccessibility.go Filters non-identifier aliases for import-type qualifiers.

Review effort: Max

Comment thread tsc/internal/ls/autoimport/view.go Resolved
Comment thread tsc/internal/ls/completions.go Resolved
Comment thread tsc/internal/ls/file_rename.go Resolved
Comment thread tsc/internal/ls/file_rename.go Resolved
Comment thread tsc/internal/ls/rename.go Resolved
Comment thread tsc/internal/ls/rename.go Resolved
Comment thread tsc/internal/ls/rename.go Resolved
Comment thread tsc/internal/ls/rename.go Resolved
Comment thread tsc/internal/ls/rename.go Resolved

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.

Comment thread tsc/internal/checker/nodebuilderimpl.go Resolved
Comment thread tsc/internal/ls/rename.go Resolved
Config files are JSON, and JavaScript string escapes such as \v or
\x00 are invalid there. Renaming a file to a name with such a character
wrote a tsconfig.json that no longer parses.
A reference directive path has no escape syntax, so a path containing
the directive's own quote is skipped during file rename edits. The
other quote character could still delimit such a path.
A file renamed to a name with a quote left every reference directive
delimited by that quote pointing at the old path. When the path does
not contain the other quote character, it can still be written by
switching the directive to that quote.
JSX treats a tag name as intrinsic only when it starts with an ASCII
lowercase letter or contains a hyphen. Auto-import instead requires an
uppercase first character, so a component named with a non-ASCII
lowercase letter is never offered.
Auto-import should skip or capitalize an export name for a JSX tag
only when JSX would read that name as an intrinsic element. Checking
for an uppercase first character also rejected names starting with a
non-ASCII lowercase letter, which JSX treats as components.
A property name containing */ in a nested destructuring pattern is
still copied into the generated @PARAM tags, where it ends the comment.
Property names in nested destructuring patterns are copied into the
generated @PARAM tags the same way as top-level ones, so a name
containing */ or a line break ends the comment or the tag early.
A module that re-exports a type only under a string literal name can
still be chosen as the module an import type refers to, which prints
the string name as the import type qualifier.
Import type qualifiers must be identifiers, so a module exporting a
type only under a string literal name cannot be used to name it. The
container index used to find re-exporting modules included such
modules, and one could be chosen over a module with a usable name.
Renaming a numeric property such as 0 in { 0: x } wrote the new name
unquoted, so a name that is not an identifier produced invalid code.
Element accesses of the property were already quoted. A new name that
is itself a non-negative number stays unquoted so the property keeps
its numeric key.
Renaming the export behind export { "a b" } from "m" with prefix and
suffix text enabled writes the alias clause inside the string literal,
which changes the exported name and drops the original one.
In export { "a b" } from "m", the rename edit covers only the
contents of the string literal. Adding an alias clause there put the
whole clause inside one literal, so each name has to be written as its
own literal within that range.
JSX attribute strings have no backslash escapes, so the escapes added
when renaming a string literal reference inside one change its value,
and an escaped quote ends the attribute early.
JSX attribute strings are taken literally, so a renamed string literal
reference inside one has to be written as raw text. When the attribute's
quote cannot contain the new name, the other quote or an expression
container replaces the whole initializer; otherwise the edit stays
inside the quotes, which keeps it applicable in content-mapped files
where only the value lies in a verbatim span.
Renaming an export to a name that is not an identifier quotes the name
in import specifiers, and specifiers in a JSDoc @import tag are inside a
comment that a */ in the name ends.
A name quoted in a JSDoc @import specifier during rename is written
inside a comment, so a */ in the name has to be escaped there the same
way as in other string literals renamed within JSDoc.
A property name containing "*/" completed inside a JSDoc type closes
the comment early, leaving the rest of the name as source code.
A string completion inside a JSDoc comment is written into the comment
text, so an unescaped "*/" in the completed name ends the comment and
turns the rest of the name into source code.
A qualified type name such as `dep.x` can only reference an export with
an identifier name, so exports like `export { A as "a b" }` cannot name
a type through a namespace import. Choosing one emits a declaration file
that does not parse instead of falling back to another export or
reporting that the type cannot be named. Values stay reachable through
element access, as in `ns["a b"]`, so type queries for them are written
as indexed access types, as in `(typeof ns)["a b"]`. Static members with
such names are written the same way, including through import types, as
in `typeof import("./dep").C["a b"]`.
A namespace export name may be a string literal, so a rename to a name
that is not an identifier has to quote it; written bare, the export
statement no longer parses.
The name in `export * as ns` is a module export name that may be a
string literal, so renaming it to a non-identifier quotes the name in
the same way as an export specifier.
A property synthesized by intersecting mapped types has no declaration
to copy its name from, so its name was written as an identifier even
when it was not one, which crashed the printer.
Clients match the text typed so far against the filter text of an
import statement completion. Snippet escapes in that filter text, such
as the backslash before a `$` in an export name, keep `import $f` from
matching an export named `$foo`.
Clients compare the filter text against the import statement typed so
far, which contains neither tab stops nor snippet escapes. Building the
filter text from the plain statement keeps names and module specifiers
containing `$` or `\` matchable.

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.

Comment thread tsc/internal/checker/nodebuilderimpl.go Resolved
@jakebailey

Copy link
Copy Markdown
Member Author

Opened #65744

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

2 participants