Repository navigation
Stop building invalid type syntax for hover and code fixes - #65502
Jake Bailey (jakebailey) wants to merge 12 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Repeated generic-symbol hovers retain display declarations in the cached builder, and ordinary qualified generic types lose module/class classification in hover text.
2 open issues
What changed in this PR
This PR separates hover-only display notation from the native compiler’s syntax trees, keeping nodes valid for language-service code fixes and API consumers.
Changes:
- Adds display metadata for the printer instead of embedding noncanonical syntax in type nodes.
- Uses temporary builders for display-oriented type formatting and constructs valid qualified names for
typeofannotations. - Tightens type-node classification and adds checker, code-fix, and API regression tests.
| File | Description |
|---|---|
tsc/internal/printer/printer.go |
Prints display substitutions and signature type arguments. |
tsc/internal/printer/factory.go |
Converts access expressions into valid entity names. |
tsc/internal/printer/emitcontext.go |
Stores and copies display-only metadata. |
tsc/internal/ls/codeactions_fixmissingtypeannotation.go |
Builds canonical names for typeof annotations. |
tsc/internal/fourslash/tests/codeFixMissingTypeAnnotationOnExports34-object-spread_test.go |
Tests annotations for qualified object spreads. |
tsc/internal/checker/printer.go |
Selects display-formatting builders and preserves valid API signatures. |
tsc/internal/checker/nodebuilderimpl.go |
Separates display notation from canonical type nodes. |
tsc/internal/checker/nodebuilder.go |
Adds operation-local builders for display-oriented type formatting. |
tsc/internal/checker/checker_test.go |
Tests display-only generic parameter notation. |
tsc/internal/ast/utilities.go |
Excludes instantiation expressions from type-node classification. |
tsc/internal/ast/precedence.go |
Removes precedence handling for noncanonical type syntax. |
packages/typescript/test/sync/api.test.ts |
Adds synchronous API syntax-validity tests. |
packages/typescript/test/async/api.test.ts |
Adds asynchronous API syntax-validity tests. |
packages/typescript/src/ast/is.ts |
Aligns JavaScript type-node classification with the compiler. |
Review effort: Balanced
977dabf to
70f413c
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved findings identify accumulating retained emit nodes during repeated generic-symbol hovers and lost symbol mappings in cloned display subtrees, which degrade classified hover output.
1 open issue
2 resolved since last review
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because unpublished findings identify a critical closure-narrowing regression, retained hover metadata, and omitted generic type arguments in hover text.
No open issues 🎉
1 resolved since last review
Review effort: Max
There was a problem hiding this comment.
🔵 Needs a closer look
The shared AST change can bind class-heritage references in parameter initializers to function-body variables instead of outer bindings, and this ES2015–ES2019 behavior lacks test coverage.
No open issues 🎉
2 resolved since last review
Review effort: Max
f4530b4 to
e96944f
Compare
e96944f to
5a36216
Compare
The node builder and the missing-type-annotation code fix could build type nodes containing syntax invalid in those positions: expressions in place of types, property-access expressions as typeof names, type parameter declarations in type argument lists, and type arguments in type parameter lists. These trees reach API consumers and every caller expecting valid syntax. To let them through, the printer accepted property accesses as entity names, and ExpressionWithTypeArguments was classified as a type node. Most of this came from hover display notation outside valid type syntax: f<string>.C for a class local to a generic function, outer type parameters with their constraints and defaults in names such as C<out T extends string = "a">.prototype, and instantiated arguments in place of a signature's type parameters, as in <string>(value: string). Build valid syntax, and record the hover notation as emit-context metadata for the printer to substitute. An instantiated signature declares its remaining type parameters, but those declarations are not printed, so they do not count toward the truncation length. Hover formatting uses a builder local to the operation, so the checker's cached builder does not retain its display metadata.
Classified hover colors each name by looking up the symbol recorded for its identifier. Display notation such as foo<void>.X.m<number, boolean>.(Anonymous function)<Date>.Y<string> is assembled from copies of earlier display expressions, and those copies had no recorded symbols, so most of the names printed as plain text. Copy the symbols along with every identifier in a copied display expression.
Constrained and defaulted outer type parameters, as in D<out T extends string = "a">.prototype, appear only in hover text, so check that text directly. Drop the earlier API and checker assertions on tree shape; the quick info baselines cover the same cases.
Display expressions are synthesized and only printed, so they can share nodes with each other and with the type tree. Sharing keeps each identifier's recorded symbol for classified hover without copying. Instantiated signatures used for display print their arguments in place of the type parameters, so build the arguments alone for them. Hover is the only consumer; the checker's public entry points clear the hover-only flags. The printer prints a display type parameter through its type reference, so type argument lists need no separate hook.
isTypeNode rejects ExpressionWithTypeArguments, so inlay hints no longer need to exclude it separately. Inline typeParametersToTypeParameterDeclarations into symbolToTypeParameterDeclarations, its only caller.
The checker reuses one node builder for type, symbol and signature formatting. Display entries recorded on its emit context stayed for the checker's lifetime, so repeated hovers kept adding to it. Store display entries in their own table and clear it when the outermost operation releases the builder. Skip caching serialized types with display notation, because a cached copy would outlive those entries. Every formatting path can then share the builder.
Qualified names such as f<string>.C and D<out T extends string = "a">.prototype need type arguments on an earlier identifier. The display was built as a substitute expression for the whole name, which took several helpers and dropped a reference's own type arguments, so hover lost the <string> in (Anonymous function)<Date>.Y<string>. Record those type arguments with the identifier they follow, outside the syntax tree, in the table holding a signature's instantiated arguments, and print them after that identifier. Qualified names and type references keep their ordinary shape, and the expression-building helpers are gone.
Type argument lists were printed through the type parameter printer so display type arguments could include declarations, and type lists were annotated as type parameter lists. Print display type arguments through their own path, so emitTypeParameterDeclarationNode and emitTypeArguments each accept only their own node kinds, and annotate the lookup helpers' lists with their element kinds.
Contextual typing from an ancestor type node asks whether a node is written in a type position. A class's extends element such as Base<T> is an ExpressionWithTypeArguments: Base is a runtime expression, and its type arguments are written in a type position. isTypeNode no longer covers this node, so this caller checks for it directly.
A class's extends expression runs, so an assignment inside it must end narrowing for closures created earlier. Assignment tracking used to skip it as part of a type.
The node builder writes a generic container's type parameters only the first time it appears in an operation. A later occurrence therefore serializes without them, and caching that copy meant other calls could print N.C.D where they should print N.C<T>.D. Display type arguments are cleared after each operation anyway, so skip the cache for every operation with display flags, and drop the per-result tracking.
5a36216 to
e2f70d8
Compare


This gets rid of all the places in the LS that we break the AST rules and smuggle in data for nicer printing.
Now, like in Strada, the extra type arguments that hover shows live on the emit context, outside the tree. They're printed after an identifier in an entity name or property access (
f<string>.C,D<out T extends string = "a">.prototype), or in place of a signature's type parameters (<string>(value: string)).To keep the checker's shared node builder from leaking memory, that data is cleared after each top-level call, and the serialized type cache is skipped for calls that can record it.
One hover changes: a reference after an instantiated container keeps its own type arguments, so
(Anonymous function)<Date>.Yis now(Anonymous function)<Date>.Y<string>, matching Strada.isTypeNode(in Go and the public API) no longer acceptsExpressionWithTypeArguments, since nothing builds it as a type anymore. As a side effect, an assignment inside aclass extendsexpression now ends narrowing for closures created earlier; that expression runs, so this is more sound, and there's a test for it.