Repository navigation
Fix more LS escaping related bugs - #65665
Jake Bailey (jakebailey) wants to merge 53 commits into
Conversation
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.
There was a problem hiding this comment.
🟡 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
Filter non-identifier exports when building the module index · New Detect intrinsic JSX names instead of checking uppercase runes · New Validate unsafe names in nested destructuring parameter paths · New Use JSON-compatible escaping for config path edits · New Switch quote delimiters when renaming paths with conflicting quotes · New Expand string-named re-exports into correctly quoted alias clauses · New Handle JSX string attributes separately during reference renames · New Quote renamed numeric property names in declarations · New Escape comment terminators in generated JSDoc import specifiers · New
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
There was a problem hiding this comment.
🟡 Changes recommended
Code fixes and serialization can emit unresolved or incorrectly bound symbols, while JSX renames can skip valid occurrences in content-mapped files.
4 open issues
9 resolved since last review
Escape comment terminators in generated JSDoc import specifiers Quote renamed numeric property names in declarations Handle JSX string attributes separately during reference renames Expand string-named re-exports into correctly quoted alias clauses Switch quote delimiters when renaming paths with conflicting quotes Use JSON-compatible escaping for config path edits Validate unsafe names in nested destructuring parameter paths Detect intrinsic JSX names instead of checking uppercase runes Filter non-identifier exports when building the module index
Review effort: Max
97c34a4 to
6c2539f
Compare
6c2539f to
e2f8630
Compare
There was a problem hiding this comment.
🟡 Changes recommended
External unique-symbol references can crash declaration emit, and missing-member fixes can generate invalid code with unresolved types, missing value imports, or duplicate bindings.
6 open issues
Declaration emit crashes on indexed static symbol references · New Computed-name imports do not avoid local name collisions · New Qualified symbol references add an unnecessary named import · New Type-only symbol imports are not promoted for computed names · New Export-list unique symbols are not imported for computed members · New Renaming an empty JSX attribute string removes its quotes · New
4 resolved since last review
Review effort: Max
e2f8630 to
fcf2269
Compare
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.
03914e9 to
94c1b57
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved bugs can produce invalid JSX when renaming empty attribute strings, break namespace accesses when exports are renamed to non-identifiers, and emit incomplete types for computed unique-symbol members.
3 open issues
6 resolved since last review
Preserve numeric property syntax when renaming numeric keys Preserve numeric literal types in generated mapped members Export-list unique symbols are not imported for computed members Type-only symbol imports are not promoted for computed names Qualified symbol references add an unnecessary named import Computed-name imports do not avoid local name collisions
Review effort: Max
|
Opened #65744 |


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