Repository navigation
Alias an identifier-valued expando member only when the alias means the same thing - #65692
Open
Kaique da Silva (ktfth) wants to merge 2 commits into
Open
Kaique da Silva (ktfth) wants to merge 2 commits into
Kaique da Silva (ktfth) wants to merge 2 commits into
Conversation
Records the current output: `host.name = id` always becomes `export { id as name }`,
even when `id` is out of scope where the namespace is declared, names another symbol
there, or has a declared type other than the member's.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
…ans the same thing
`host.name = id` was always declared as `export { id as name }` in the host's
namespace. That is wrong when `id` is out of scope next to the host (a loop or
block binding), names another symbol there (shadowing), has a declared type other
than the member's (narrowing, a literal `const`, an imported literal), or when the
member has other assignments (TS2484 with the `var` they produce).
Alias only when the assignment is the member's only declaration, `id` resolves to the
same symbol in the host's container, and the type of its (export) symbol is identical
to the member's type. Otherwise declare a typed `var`, as 6.0 did. Aliases to classes,
enums and functions are kept, so they still carry the type meaning.
Expando members are collected from the source file, so the alias's visibility is now
checked from the host's container; a namespace-local `const` it names is emitted.
Fixes microsoft#65615
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
A critical name-collision bug allows the typed fallback to shadow retained alias targets inside namespaces, causing consumers to see incorrect member types.
1 open issue
What changed in this PR
This PR addresses #65615 by restricting declaration-emission aliases for identifier-valued expando members to cases where the identifier preserves the member’s symbol and type.
Changes:
- Adds a resolver check for declaration count, symbol identity, and type identity.
- Uses the host’s scope when emitting retained aliases; otherwise emits typed variables.
- Adds regression coverage for scope, narrowing, literals, reassignment, imports, and preserved aliases.
| File | Description |
|---|---|
tsc/internal/checker/emitresolver.go |
Implements alias eligibility checks. |
tsc/internal/printer/emitresolver.go |
Exposes the new resolver method. |
tsc/internal/transformers/declarations/transform.go |
Applies eligibility checks and host-scope alias emission. |
tsc/testdata/tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts |
Adds expando declaration-emission regression cases. |
tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js |
Records JavaScript and declaration output. |
tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.symbols |
Records expected symbol bindings. |
tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.types |
Records expected inferred types. |
Review effort: Balanced
Comment on lines
+2912
to
+2913
| hostContainer := core.IfElse(ast.IsVariableDeclaration(declaration), declaration.Parent.Parent, declaration).Parent | ||
| if ast.IsIdentifier(node.Right) && tx.resolver.CanDeclareExpandoAssignmentAsAlias(node.AsNode(), hostContainer) { |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Fixes #65615
This is an automated PR authored by Claude Code using Claude Opus 5.5 with high reasoning level.
Motivation & Guidance
Motivation:
The issue reporter (the user) asked me to open PRs for the issues they reported that now have a milestone and no PR. This is one of them.
Guidance:
The prompt was "Crie os PR's para as issues livres" ("Create the PRs for the free issues"). While I was working, the user added a reminder to fix at root-cause depth: cover the whole bug class, not only the issue's repro. In this case that meant checking every case where the identifier and the member can disagree (block-scoped, shadowed, narrowed, literal
const, reassigned, imported, inside a namespace), and checking that each emitted declaration file compiles. There were no other edits.Analysis
For
host.name = id,transformExpandoAssignmentalways emittedexport { id as name }in the host's namespace. That alias is only right when two things hold:idnames the same symbol where the namespace is declared (next to the host).Neither was checked:
.d.tsfor (const k of ["a"]) f.last = k;export { k as last }kisn't in scope: TS2304{ const shadowed = "s"; f.v = shadowed; }with a top-levelconst shadowed = 1export { shadowed as v }shadowed:numberinstead ofstringif (typeof v === "string") f.s = v;export { v as s }string | numberconst VERSION = "1.2.3"; f.version = VERSION;export { VERSION as version }"1.2.3"instead ofstringf.n = initial; f.n = 5;export { initial as n }andexport var n: numberimport { imported } from "./dep"(a literalconst),f.literal = importedexport { imported as literal }1instead ofnumberThere is a second problem for hosts inside a namespace. Expando members are collected in a pre-pass over the source file, so
transformBinaryExpressionToExportDeclarationchecked the alias's visibility from the source file. A namespace-localkdoesn't resolve there, soconst kwas never late-painted, andexport { k }failed with TS2304.6.0 never aliased: it always emitted
var name: <member type>. The alias does have one benefit worth keeping: for classes, enums and functions it also carries the type meaning (keepsClass.Cusable as a type). So this PR keeps the alias when it is valid and falls back otherwise.Fix
EmitResolver.CanDeclareExpandoAssignmentAsAlias(assignment, location). It holds only when all of the following are true:idresolves to the same symbol atlocation(the host's container);isTypeIdenticalTo(type of member, type of id's symbol). An exported declaration resolves to its local symbol, which only carriesExportValue, so the type is read fromgetExportSymbolOfValueSymbolIfExported.transformExpandoAssignmentuses it, and otherwise takes the existing typedvarpath. In the alias path,tx.enclosingDeclarationis set to the host's container while the alias is built, so the aliased declaration is painted in the right scope.Cases that still alias: an imported
let, a top-levellet, a class, a function, a typedconston an arrow host, and a namespace-localconst(which is now emitted).I looked at CommonJS
exports.x = id(isCommonJSAliasExport). It already requires a single declaration and a top-level statement, and its literal-constaliasing matches 6.0. I left it unchanged.Related: maintainer PR #65606 (#65605) touches the same function. Its new baseline has
withAlias.x = aliaswithconst alias = 0. With this change, that member becomesvar x: numberinstead ofexport { alias as x }, because the member's type isnumberand the alias would give0. Whichever PR merges second needs that baseline line updated.Tests
New test
declarationEmitExpandoIdentifierValue.ts. It covers every row of the table, plus the still-alias cases and a namespace host. The first commit records the current output, which has 4 errors in its declaration files (DtsFileErrors). With the change there are none, and every non-alias member has the type 6.0.3 gives it.No existing baseline changes. Before reading the type from the export symbol, three baselines lost a valid alias:
declarationEmitExpandoFunction(exported enum),declarationEmitDefaultExportWithStaticAssignment(exported class) andjsDeclarationsFunctions(f.self = f). With that fixed, all three matchmainagain. On currentmain(e0fce98),go test ./internal/testrunner/passes with only the new test's baselines.gofmtandgo vetare clean for the touched packages.