From 0ebff99d82e6ed2a51338069dc73d6e134e6736b Mon Sep 17 00:00:00 2001 From: Kaique da Silva Date: Thu, 26 Nov 2026 00:13:46 -0300 Subject: [PATCH 1/2] test: add declaration emit test for identifier-valued expando members 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 Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7 --- .../declarationEmitExpandoIdentifierValue.js | 350 ++++++++++++++++++ ...larationEmitExpandoIdentifierValue.symbols | 285 ++++++++++++++ ...eclarationEmitExpandoIdentifierValue.types | 327 ++++++++++++++++ .../declarationEmitExpandoIdentifierValue.ts | 95 +++++ 4 files changed, 1057 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.types create mode 100644 tsc/testdata/tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js new file mode 100644 index 0000000000000..0760fad4b89c3 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js @@ -0,0 +1,350 @@ +//// [tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts] //// + +//// [dep.ts] +export const imported = 1; +export let importedLet = "s"; + +//// [lib.ts] +import { imported, importedLet } from "./dep"; + +// Not an alias: the identifier means something else, or has another type, where the namespace is declared + +export function loop() {} +for (const k of ["a", "b"]) { + loop.last = k; +} + +const shadowed = 1; +export function shadow() {} +{ + const shadowed = "s"; + shadow.v = shadowed; +} + +declare const wide: string | number; +export function narrowed() {} +if (typeof wide === "string") { + narrowed.s = wide; +} + +const VERSION = "1.2.3"; +export function literal() {} +literal.version = VERSION; + +const initial: number = 1; +export function reassigned() {} +reassigned.n = initial; +reassigned.n = 5; + +export function fromImport() {} +fromImport.literal = imported; + +// Still an alias + +export function keepsImport() {} +keepsImport.value = importedLet; + +let mutable = 1; +export function keepsLet() {} +keepsLet.m = mutable; + +class C { + x = 1; +} +export function keepsClass() {} +keepsClass.C = C; + +function helper() { + return 1; +} +export function keepsFunction() {} +keepsFunction.helper = helper; + +export const arrowHost = () => {}; +const arrowValue: string = "x"; +arrowHost.v = arrowValue; + +export namespace N { + const k: number = 1; + export function f() {} + f.k = k; + { + const k = "s"; + f.inner = k; + } +} + +//// [use.ts] +import { loop, shadow, narrowed, literal, reassigned, fromImport, keepsImport, keepsLet, keepsClass, keepsFunction, arrowHost, N } from "./lib"; +const s1: string = loop.last; +const s2: string = shadow.v; +const s3: string = narrowed.s; +literal.version = "2.0.0"; +reassigned.n = 6; +fromImport.literal = 2; +const s4: string = keepsImport.value; +const n1: number = keepsLet.m; +const n0: number = new keepsClass.C().x; +const n2: number = keepsFunction.helper(); +const s5: string = arrowHost.v; +const n3: number = N.f.k; +const s6: string = N.f.inner; + + +//// [dep.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.importedLet = exports.imported = void 0; +exports.imported = 1; +exports.importedLet = "s"; +//// [lib.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.N = exports.arrowHost = void 0; +exports.loop = loop; +exports.shadow = shadow; +exports.narrowed = narrowed; +exports.literal = literal; +exports.reassigned = reassigned; +exports.fromImport = fromImport; +exports.keepsImport = keepsImport; +exports.keepsLet = keepsLet; +exports.keepsClass = keepsClass; +exports.keepsFunction = keepsFunction; +const dep_1 = require("./dep"); +// Not an alias: the identifier means something else, or has another type, where the namespace is declared +function loop() { } +for (const k of ["a", "b"]) { + loop.last = k; +} +const shadowed = 1; +function shadow() { } +{ + const shadowed = "s"; + shadow.v = shadowed; +} +function narrowed() { } +if (typeof wide === "string") { + narrowed.s = wide; +} +const VERSION = "1.2.3"; +function literal() { } +literal.version = VERSION; +const initial = 1; +function reassigned() { } +reassigned.n = initial; +reassigned.n = 5; +function fromImport() { } +fromImport.literal = dep_1.imported; +// Still an alias +function keepsImport() { } +keepsImport.value = dep_1.importedLet; +let mutable = 1; +function keepsLet() { } +keepsLet.m = mutable; +class C { + x = 1; +} +function keepsClass() { } +keepsClass.C = C; +function helper() { + return 1; +} +function keepsFunction() { } +keepsFunction.helper = helper; +const arrowHost = () => { }; +exports.arrowHost = arrowHost; +const arrowValue = "x"; +exports.arrowHost.v = arrowValue; +var N; +(function (N) { + const k = 1; + function f() { } + N.f = f; + f.k = k; + { + const k = "s"; + f.inner = k; + } +})(N || (exports.N = N = {})); +//// [use.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const lib_1 = require("./lib"); +const s1 = lib_1.loop.last; +const s2 = lib_1.shadow.v; +const s3 = lib_1.narrowed.s; +lib_1.literal.version = "2.0.0"; +lib_1.reassigned.n = 6; +lib_1.fromImport.literal = 2; +const s4 = lib_1.keepsImport.value; +const n1 = lib_1.keepsLet.m; +const n0 = new lib_1.keepsClass.C().x; +const n2 = lib_1.keepsFunction.helper(); +const s5 = lib_1.arrowHost.v; +const n3 = lib_1.N.f.k; +const s6 = lib_1.N.f.inner; + + +//// [dep.d.ts] +export declare const imported = 1; +export declare let importedLet: string; +//// [lib.d.ts] +import { imported, importedLet } from "./dep"; +export declare function loop(): void; +export declare namespace loop { + export { k as last }; +} +declare const shadowed = 1; +export declare function shadow(): void; +export declare namespace shadow { + export { shadowed as v }; +} +declare const wide: string | number; +export declare function narrowed(): void; +export declare namespace narrowed { + export { wide as s }; +} +declare const VERSION = "1.2.3"; +export declare function literal(): void; +export declare namespace literal { + export { VERSION as version }; +} +declare const initial: number; +export declare function reassigned(): void; +export declare namespace reassigned { + export { initial as n }; + export var n: number; +} +export declare function fromImport(): void; +export declare namespace fromImport { + export { imported as literal }; +} +export declare function keepsImport(): void; +export declare namespace keepsImport { + export { importedLet as value }; +} +declare let mutable: number; +export declare function keepsLet(): void; +export declare namespace keepsLet { + export { mutable as m }; +} +declare class C { + x: number; +} +export declare function keepsClass(): void; +export declare namespace keepsClass { + export { C }; +} +declare function helper(): number; +export declare function keepsFunction(): void; +export declare namespace keepsFunction { + export { helper }; +} +export declare function arrowHost(): void; +export declare namespace arrowHost { + export { arrowValue as v }; +} +declare const arrowValue: string; +export declare namespace N { + function f(): void; + namespace f { + export { k }; + export { k as inner }; + } +} +export {}; +//// [use.d.ts] +export {}; + + +//// [DtsFileErrors] + + +out/lib.d.ts(4,14): error TS2304: Cannot find name 'k'. +out/lib.d.ts(24,14): error TS2484: Export declaration conflicts with exported declaration of 'n'. +out/lib.d.ts(60,18): error TS2304: Cannot find name 'k'. +out/lib.d.ts(61,18): error TS2304: Cannot find name 'k'. + + +==== out/dep.d.ts (0 errors) ==== + export declare const imported = 1; + export declare let importedLet: string; + +==== out/lib.d.ts (4 errors) ==== + import { imported, importedLet } from "./dep"; + export declare function loop(): void; + export declare namespace loop { + export { k as last }; + ~ +!!! error TS2304: Cannot find name 'k'. + } + declare const shadowed = 1; + export declare function shadow(): void; + export declare namespace shadow { + export { shadowed as v }; + } + declare const wide: string | number; + export declare function narrowed(): void; + export declare namespace narrowed { + export { wide as s }; + } + declare const VERSION = "1.2.3"; + export declare function literal(): void; + export declare namespace literal { + export { VERSION as version }; + } + declare const initial: number; + export declare function reassigned(): void; + export declare namespace reassigned { + export { initial as n }; + ~~~~~~~~~~~~ +!!! error TS2484: Export declaration conflicts with exported declaration of 'n'. + export var n: number; + } + export declare function fromImport(): void; + export declare namespace fromImport { + export { imported as literal }; + } + export declare function keepsImport(): void; + export declare namespace keepsImport { + export { importedLet as value }; + } + declare let mutable: number; + export declare function keepsLet(): void; + export declare namespace keepsLet { + export { mutable as m }; + } + declare class C { + x: number; + } + export declare function keepsClass(): void; + export declare namespace keepsClass { + export { C }; + } + declare function helper(): number; + export declare function keepsFunction(): void; + export declare namespace keepsFunction { + export { helper }; + } + export declare function arrowHost(): void; + export declare namespace arrowHost { + export { arrowValue as v }; + } + declare const arrowValue: string; + export declare namespace N { + function f(): void; + namespace f { + export { k }; + ~ +!!! error TS2304: Cannot find name 'k'. + export { k as inner }; + ~ +!!! error TS2304: Cannot find name 'k'. + } + } + export {}; + +==== out/use.d.ts (0 errors) ==== + export {}; + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.symbols new file mode 100644 index 0000000000000..0e8976acd5301 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.symbols @@ -0,0 +1,285 @@ +//// [tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts] //// + +=== dep.ts === +export const imported = 1; +>imported : Symbol(imported, Decl(dep.ts, 0, 12)) + +export let importedLet = "s"; +>importedLet : Symbol(importedLet, Decl(dep.ts, 1, 10)) + +=== lib.ts === +import { imported, importedLet } from "./dep"; +>imported : Symbol(imported, Decl(lib.ts, 0, 8)) +>importedLet : Symbol(importedLet, Decl(lib.ts, 0, 18)) + +// Not an alias: the identifier means something else, or has another type, where the namespace is declared + +export function loop() {} +>loop : Symbol(loop, Decl(lib.ts, 0, 46)) + +for (const k of ["a", "b"]) { +>k : Symbol(k, Decl(lib.ts, 5, 10)) + + loop.last = k; +>loop.last : Symbol(loop.last, Decl(lib.ts, 5, 29)) +>loop : Symbol(loop, Decl(lib.ts, 0, 46)) +>last : Symbol(loop.last, Decl(lib.ts, 5, 29)) +>k : Symbol(k, Decl(lib.ts, 5, 10)) +} + +const shadowed = 1; +>shadowed : Symbol(shadowed, Decl(lib.ts, 9, 5)) + +export function shadow() {} +>shadow : Symbol(shadow, Decl(lib.ts, 9, 19)) +{ + const shadowed = "s"; +>shadowed : Symbol(shadowed, Decl(lib.ts, 12, 9)) + + shadow.v = shadowed; +>shadow.v : Symbol(shadow.v, Decl(lib.ts, 12, 25)) +>shadow : Symbol(shadow, Decl(lib.ts, 9, 19)) +>v : Symbol(shadow.v, Decl(lib.ts, 12, 25)) +>shadowed : Symbol(shadowed, Decl(lib.ts, 12, 9)) +} + +declare const wide: string | number; +>wide : Symbol(wide, Decl(lib.ts, 16, 13)) + +export function narrowed() {} +>narrowed : Symbol(narrowed, Decl(lib.ts, 16, 36)) + +if (typeof wide === "string") { +>wide : Symbol(wide, Decl(lib.ts, 16, 13)) + + narrowed.s = wide; +>narrowed.s : Symbol(narrowed.s, Decl(lib.ts, 18, 31)) +>narrowed : Symbol(narrowed, Decl(lib.ts, 16, 36)) +>s : Symbol(narrowed.s, Decl(lib.ts, 18, 31)) +>wide : Symbol(wide, Decl(lib.ts, 16, 13)) +} + +const VERSION = "1.2.3"; +>VERSION : Symbol(VERSION, Decl(lib.ts, 22, 5)) + +export function literal() {} +>literal : Symbol(literal, Decl(lib.ts, 22, 24)) + +literal.version = VERSION; +>literal.version : Symbol(literal.version, Decl(lib.ts, 23, 28)) +>literal : Symbol(literal, Decl(lib.ts, 22, 24)) +>version : Symbol(literal.version, Decl(lib.ts, 23, 28)) +>VERSION : Symbol(VERSION, Decl(lib.ts, 22, 5)) + +const initial: number = 1; +>initial : Symbol(initial, Decl(lib.ts, 26, 5)) + +export function reassigned() {} +>reassigned : Symbol(reassigned, Decl(lib.ts, 26, 26)) + +reassigned.n = initial; +>reassigned.n : Symbol(reassigned.n, Decl(lib.ts, 27, 31), Decl(lib.ts, 28, 23)) +>reassigned : Symbol(reassigned, Decl(lib.ts, 26, 26)) +>n : Symbol(reassigned.n, Decl(lib.ts, 27, 31), Decl(lib.ts, 28, 23)) +>initial : Symbol(initial, Decl(lib.ts, 26, 5)) + +reassigned.n = 5; +>reassigned.n : Symbol(reassigned.n, Decl(lib.ts, 27, 31), Decl(lib.ts, 28, 23)) +>reassigned : Symbol(reassigned, Decl(lib.ts, 26, 26)) +>n : Symbol(reassigned.n, Decl(lib.ts, 27, 31), Decl(lib.ts, 28, 23)) + +export function fromImport() {} +>fromImport : Symbol(fromImport, Decl(lib.ts, 29, 17)) + +fromImport.literal = imported; +>fromImport.literal : Symbol(fromImport.literal, Decl(lib.ts, 31, 31)) +>fromImport : Symbol(fromImport, Decl(lib.ts, 29, 17)) +>literal : Symbol(fromImport.literal, Decl(lib.ts, 31, 31)) +>imported : Symbol(imported, Decl(lib.ts, 0, 8)) + +// Still an alias + +export function keepsImport() {} +>keepsImport : Symbol(keepsImport, Decl(lib.ts, 32, 30)) + +keepsImport.value = importedLet; +>keepsImport.value : Symbol(keepsImport.value, Decl(lib.ts, 36, 32)) +>keepsImport : Symbol(keepsImport, Decl(lib.ts, 32, 30)) +>value : Symbol(keepsImport.value, Decl(lib.ts, 36, 32)) +>importedLet : Symbol(importedLet, Decl(lib.ts, 0, 18)) + +let mutable = 1; +>mutable : Symbol(mutable, Decl(lib.ts, 39, 3)) + +export function keepsLet() {} +>keepsLet : Symbol(keepsLet, Decl(lib.ts, 39, 16)) + +keepsLet.m = mutable; +>keepsLet.m : Symbol(keepsLet.m, Decl(lib.ts, 40, 29)) +>keepsLet : Symbol(keepsLet, Decl(lib.ts, 39, 16)) +>m : Symbol(keepsLet.m, Decl(lib.ts, 40, 29)) +>mutable : Symbol(mutable, Decl(lib.ts, 39, 3)) + +class C { +>C : Symbol(C, Decl(lib.ts, 41, 21)) + + x = 1; +>x : Symbol(C.x, Decl(lib.ts, 43, 9)) +} +export function keepsClass() {} +>keepsClass : Symbol(keepsClass, Decl(lib.ts, 45, 1)) + +keepsClass.C = C; +>keepsClass.C : Symbol(keepsClass.C, Decl(lib.ts, 46, 31)) +>keepsClass : Symbol(keepsClass, Decl(lib.ts, 45, 1)) +>C : Symbol(keepsClass.C, Decl(lib.ts, 46, 31)) +>C : Symbol(C, Decl(lib.ts, 41, 21)) + +function helper() { +>helper : Symbol(helper, Decl(lib.ts, 47, 17)) + + return 1; +} +export function keepsFunction() {} +>keepsFunction : Symbol(keepsFunction, Decl(lib.ts, 51, 1)) + +keepsFunction.helper = helper; +>keepsFunction.helper : Symbol(keepsFunction.helper, Decl(lib.ts, 52, 34)) +>keepsFunction : Symbol(keepsFunction, Decl(lib.ts, 51, 1)) +>helper : Symbol(keepsFunction.helper, Decl(lib.ts, 52, 34)) +>helper : Symbol(helper, Decl(lib.ts, 47, 17)) + +export const arrowHost = () => {}; +>arrowHost : Symbol(arrowHost, Decl(lib.ts, 55, 12)) + +const arrowValue: string = "x"; +>arrowValue : Symbol(arrowValue, Decl(lib.ts, 56, 5)) + +arrowHost.v = arrowValue; +>arrowHost.v : Symbol(arrowHost.v, Decl(lib.ts, 56, 31)) +>arrowHost : Symbol(arrowHost, Decl(lib.ts, 55, 12)) +>v : Symbol(arrowHost.v, Decl(lib.ts, 56, 31)) +>arrowValue : Symbol(arrowValue, Decl(lib.ts, 56, 5)) + +export namespace N { +>N : Symbol(N, Decl(lib.ts, 57, 25)) + + const k: number = 1; +>k : Symbol(k, Decl(lib.ts, 60, 9)) + + export function f() {} +>f : Symbol(f, Decl(lib.ts, 60, 24)) + + f.k = k; +>f.k : Symbol(f.k, Decl(lib.ts, 61, 26)) +>f : Symbol(f, Decl(lib.ts, 60, 24)) +>k : Symbol(f.k, Decl(lib.ts, 61, 26)) +>k : Symbol(k, Decl(lib.ts, 60, 9)) + { + const k = "s"; +>k : Symbol(k, Decl(lib.ts, 64, 13)) + + f.inner = k; +>f.inner : Symbol(f.inner, Decl(lib.ts, 64, 22)) +>f : Symbol(f, Decl(lib.ts, 60, 24)) +>inner : Symbol(f.inner, Decl(lib.ts, 64, 22)) +>k : Symbol(k, Decl(lib.ts, 64, 13)) + } +} + +=== use.ts === +import { loop, shadow, narrowed, literal, reassigned, fromImport, keepsImport, keepsLet, keepsClass, keepsFunction, arrowHost, N } from "./lib"; +>loop : Symbol(loop, Decl(use.ts, 0, 8)) +>shadow : Symbol(shadow, Decl(use.ts, 0, 14)) +>narrowed : Symbol(narrowed, Decl(use.ts, 0, 22)) +>literal : Symbol(literal, Decl(use.ts, 0, 32)) +>reassigned : Symbol(reassigned, Decl(use.ts, 0, 41)) +>fromImport : Symbol(fromImport, Decl(use.ts, 0, 53)) +>keepsImport : Symbol(keepsImport, Decl(use.ts, 0, 65)) +>keepsLet : Symbol(keepsLet, Decl(use.ts, 0, 78)) +>keepsClass : Symbol(keepsClass, Decl(use.ts, 0, 88)) +>keepsFunction : Symbol(keepsFunction, Decl(use.ts, 0, 100)) +>arrowHost : Symbol(arrowHost, Decl(use.ts, 0, 115)) +>N : Symbol(N, Decl(use.ts, 0, 126)) + +const s1: string = loop.last; +>s1 : Symbol(s1, Decl(use.ts, 1, 5)) +>loop.last : Symbol(loop.last, Decl(lib.ts, 5, 29)) +>loop : Symbol(loop, Decl(use.ts, 0, 8)) +>last : Symbol(loop.last, Decl(lib.ts, 5, 29)) + +const s2: string = shadow.v; +>s2 : Symbol(s2, Decl(use.ts, 2, 5)) +>shadow.v : Symbol(shadow.v, Decl(lib.ts, 12, 25)) +>shadow : Symbol(shadow, Decl(use.ts, 0, 14)) +>v : Symbol(shadow.v, Decl(lib.ts, 12, 25)) + +const s3: string = narrowed.s; +>s3 : Symbol(s3, Decl(use.ts, 3, 5)) +>narrowed.s : Symbol(narrowed.s, Decl(lib.ts, 18, 31)) +>narrowed : Symbol(narrowed, Decl(use.ts, 0, 22)) +>s : Symbol(narrowed.s, Decl(lib.ts, 18, 31)) + +literal.version = "2.0.0"; +>literal.version : Symbol(literal.version, Decl(lib.ts, 23, 28)) +>literal : Symbol(literal, Decl(use.ts, 0, 32)) +>version : Symbol(literal.version, Decl(lib.ts, 23, 28)) + +reassigned.n = 6; +>reassigned.n : Symbol(reassigned.n, Decl(lib.ts, 27, 31), Decl(lib.ts, 28, 23)) +>reassigned : Symbol(reassigned, Decl(use.ts, 0, 41)) +>n : Symbol(reassigned.n, Decl(lib.ts, 27, 31), Decl(lib.ts, 28, 23)) + +fromImport.literal = 2; +>fromImport.literal : Symbol(fromImport.literal, Decl(lib.ts, 31, 31)) +>fromImport : Symbol(fromImport, Decl(use.ts, 0, 53)) +>literal : Symbol(fromImport.literal, Decl(lib.ts, 31, 31)) + +const s4: string = keepsImport.value; +>s4 : Symbol(s4, Decl(use.ts, 7, 5)) +>keepsImport.value : Symbol(keepsImport.value, Decl(lib.ts, 36, 32)) +>keepsImport : Symbol(keepsImport, Decl(use.ts, 0, 65)) +>value : Symbol(keepsImport.value, Decl(lib.ts, 36, 32)) + +const n1: number = keepsLet.m; +>n1 : Symbol(n1, Decl(use.ts, 8, 5)) +>keepsLet.m : Symbol(keepsLet.m, Decl(lib.ts, 40, 29)) +>keepsLet : Symbol(keepsLet, Decl(use.ts, 0, 78)) +>m : Symbol(keepsLet.m, Decl(lib.ts, 40, 29)) + +const n0: number = new keepsClass.C().x; +>n0 : Symbol(n0, Decl(use.ts, 9, 5)) +>new keepsClass.C().x : Symbol(C.x, Decl(lib.ts, 43, 9)) +>keepsClass.C : Symbol(keepsClass.C, Decl(lib.ts, 46, 31)) +>keepsClass : Symbol(keepsClass, Decl(use.ts, 0, 88)) +>C : Symbol(keepsClass.C, Decl(lib.ts, 46, 31)) +>x : Symbol(C.x, Decl(lib.ts, 43, 9)) + +const n2: number = keepsFunction.helper(); +>n2 : Symbol(n2, Decl(use.ts, 10, 5)) +>keepsFunction.helper : Symbol(keepsFunction.helper, Decl(lib.ts, 52, 34)) +>keepsFunction : Symbol(keepsFunction, Decl(use.ts, 0, 100)) +>helper : Symbol(keepsFunction.helper, Decl(lib.ts, 52, 34)) + +const s5: string = arrowHost.v; +>s5 : Symbol(s5, Decl(use.ts, 11, 5)) +>arrowHost.v : Symbol(arrowHost.v, Decl(lib.ts, 56, 31)) +>arrowHost : Symbol(arrowHost, Decl(use.ts, 0, 115)) +>v : Symbol(arrowHost.v, Decl(lib.ts, 56, 31)) + +const n3: number = N.f.k; +>n3 : Symbol(n3, Decl(use.ts, 12, 5)) +>N.f.k : Symbol(N.f.k, Decl(lib.ts, 61, 26)) +>N.f : Symbol(N.f, Decl(lib.ts, 60, 24)) +>N : Symbol(N, Decl(use.ts, 0, 126)) +>f : Symbol(N.f, Decl(lib.ts, 60, 24)) +>k : Symbol(N.f.k, Decl(lib.ts, 61, 26)) + +const s6: string = N.f.inner; +>s6 : Symbol(s6, Decl(use.ts, 13, 5)) +>N.f.inner : Symbol(N.f.inner, Decl(lib.ts, 64, 22)) +>N.f : Symbol(N.f, Decl(lib.ts, 60, 24)) +>N : Symbol(N, Decl(use.ts, 0, 126)) +>f : Symbol(N.f, Decl(lib.ts, 60, 24)) +>inner : Symbol(N.f.inner, Decl(lib.ts, 64, 22)) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.types b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.types new file mode 100644 index 0000000000000..e7f315046cefb --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.types @@ -0,0 +1,327 @@ +//// [tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts] //// + +=== dep.ts === +export const imported = 1; +>imported : 1 +>1 : 1 + +export let importedLet = "s"; +>importedLet : string +>"s" : "s" + +=== lib.ts === +import { imported, importedLet } from "./dep"; +>imported : 1 +>importedLet : string + +// Not an alias: the identifier means something else, or has another type, where the namespace is declared + +export function loop() {} +>loop : { (): void; last: string; } + +for (const k of ["a", "b"]) { +>k : string +>["a", "b"] : string[] +>"a" : "a" +>"b" : "b" + + loop.last = k; +>loop.last = k : string +>loop.last : string +>loop : { (): void; last: string; } +>last : string +>k : string +} + +const shadowed = 1; +>shadowed : 1 +>1 : 1 + +export function shadow() {} +>shadow : { (): void; v: string; } +{ + const shadowed = "s"; +>shadowed : "s" +>"s" : "s" + + shadow.v = shadowed; +>shadow.v = shadowed : "s" +>shadow.v : string +>shadow : { (): void; v: string; } +>v : string +>shadowed : "s" +} + +declare const wide: string | number; +>wide : string | number + +export function narrowed() {} +>narrowed : { (): void; s: string; } + +if (typeof wide === "string") { +>typeof wide === "string" : boolean +>typeof wide : "bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined" +>wide : string | number +>"string" : "string" + + narrowed.s = wide; +>narrowed.s = wide : string +>narrowed.s : string +>narrowed : { (): void; s: string; } +>s : string +>wide : string +} + +const VERSION = "1.2.3"; +>VERSION : "1.2.3" +>"1.2.3" : "1.2.3" + +export function literal() {} +>literal : { (): void; version: string; } + +literal.version = VERSION; +>literal.version = VERSION : "1.2.3" +>literal.version : string +>literal : { (): void; version: string; } +>version : string +>VERSION : "1.2.3" + +const initial: number = 1; +>initial : number +>1 : 1 + +export function reassigned() {} +>reassigned : { (): void; n: number; } + +reassigned.n = initial; +>reassigned.n = initial : number +>reassigned.n : number +>reassigned : { (): void; n: number; } +>n : number +>initial : number + +reassigned.n = 5; +>reassigned.n = 5 : 5 +>reassigned.n : number +>reassigned : { (): void; n: number; } +>n : number +>5 : 5 + +export function fromImport() {} +>fromImport : { (): void; literal: number; } + +fromImport.literal = imported; +>fromImport.literal = imported : 1 +>fromImport.literal : number +>fromImport : { (): void; literal: number; } +>literal : number +>imported : 1 + +// Still an alias + +export function keepsImport() {} +>keepsImport : { (): void; value: string; } + +keepsImport.value = importedLet; +>keepsImport.value = importedLet : string +>keepsImport.value : string +>keepsImport : { (): void; value: string; } +>value : string +>importedLet : string + +let mutable = 1; +>mutable : number +>1 : 1 + +export function keepsLet() {} +>keepsLet : { (): void; m: number; } + +keepsLet.m = mutable; +>keepsLet.m = mutable : number +>keepsLet.m : number +>keepsLet : { (): void; m: number; } +>m : number +>mutable : number + +class C { +>C : C + + x = 1; +>x : number +>1 : 1 +} +export function keepsClass() {} +>keepsClass : { (): void; C: typeof C; } + +keepsClass.C = C; +>keepsClass.C = C : typeof C +>keepsClass.C : typeof C +>keepsClass : { (): void; C: typeof C; } +>C : typeof C +>C : typeof C + +function helper() { +>helper : () => number + + return 1; +>1 : 1 +} +export function keepsFunction() {} +>keepsFunction : { (): void; helper: () => number; } + +keepsFunction.helper = helper; +>keepsFunction.helper = helper : () => number +>keepsFunction.helper : () => number +>keepsFunction : { (): void; helper: () => number; } +>helper : () => number +>helper : () => number + +export const arrowHost = () => {}; +>arrowHost : { (): void; v: string; } +>() => {} : { (): void; v: string; } + +const arrowValue: string = "x"; +>arrowValue : string +>"x" : "x" + +arrowHost.v = arrowValue; +>arrowHost.v = arrowValue : string +>arrowHost.v : string +>arrowHost : { (): void; v: string; } +>v : string +>arrowValue : string + +export namespace N { +>N : typeof N + + const k: number = 1; +>k : number +>1 : 1 + + export function f() {} +>f : { (): void; k: number; inner: string; } + + f.k = k; +>f.k = k : number +>f.k : number +>f : { (): void; k: number; inner: string; } +>k : number +>k : number + { + const k = "s"; +>k : "s" +>"s" : "s" + + f.inner = k; +>f.inner = k : "s" +>f.inner : string +>f : { (): void; k: number; inner: string; } +>inner : string +>k : "s" + } +} + +=== use.ts === +import { loop, shadow, narrowed, literal, reassigned, fromImport, keepsImport, keepsLet, keepsClass, keepsFunction, arrowHost, N } from "./lib"; +>loop : { (): void; last: string; } +>shadow : { (): void; v: string; } +>narrowed : { (): void; s: string; } +>literal : { (): void; version: string; } +>reassigned : { (): void; n: number; } +>fromImport : { (): void; literal: number; } +>keepsImport : { (): void; value: string; } +>keepsLet : { (): void; m: number; } +>keepsClass : { (): void; C: typeof C; } +>keepsFunction : { (): void; helper: () => number; } +>arrowHost : { (): void; v: string; } +>N : typeof N + +const s1: string = loop.last; +>s1 : string +>loop.last : string +>loop : { (): void; last: string; } +>last : string + +const s2: string = shadow.v; +>s2 : string +>shadow.v : string +>shadow : { (): void; v: string; } +>v : string + +const s3: string = narrowed.s; +>s3 : string +>narrowed.s : string +>narrowed : { (): void; s: string; } +>s : string + +literal.version = "2.0.0"; +>literal.version = "2.0.0" : "2.0.0" +>literal.version : string +>literal : { (): void; version: string; } +>version : string +>"2.0.0" : "2.0.0" + +reassigned.n = 6; +>reassigned.n = 6 : 6 +>reassigned.n : number +>reassigned : { (): void; n: number; } +>n : number +>6 : 6 + +fromImport.literal = 2; +>fromImport.literal = 2 : 2 +>fromImport.literal : number +>fromImport : { (): void; literal: number; } +>literal : number +>2 : 2 + +const s4: string = keepsImport.value; +>s4 : string +>keepsImport.value : string +>keepsImport : { (): void; value: string; } +>value : string + +const n1: number = keepsLet.m; +>n1 : number +>keepsLet.m : number +>keepsLet : { (): void; m: number; } +>m : number + +const n0: number = new keepsClass.C().x; +>n0 : number +>new keepsClass.C().x : number +>new keepsClass.C() : C +>keepsClass.C : typeof C +>keepsClass : { (): void; C: typeof C; } +>C : typeof C +>x : number + +const n2: number = keepsFunction.helper(); +>n2 : number +>keepsFunction.helper() : number +>keepsFunction.helper : () => number +>keepsFunction : { (): void; helper: () => number; } +>helper : () => number + +const s5: string = arrowHost.v; +>s5 : string +>arrowHost.v : string +>arrowHost : { (): void; v: string; } +>v : string + +const n3: number = N.f.k; +>n3 : number +>N.f.k : number +>N.f : { (): void; k: number; inner: string; } +>N : typeof N +>f : { (): void; k: number; inner: string; } +>k : number + +const s6: string = N.f.inner; +>s6 : string +>N.f.inner : string +>N.f : { (): void; k: number; inner: string; } +>N : typeof N +>f : { (): void; k: number; inner: string; } +>inner : string + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts b/tsc/testdata/tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts new file mode 100644 index 0000000000000..4f73e3cbc90c2 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitExpandoIdentifierValue.ts @@ -0,0 +1,95 @@ +// @declaration: true +// @strict: true +// @target: esnext +// @module: nodenext +// @outDir: out + +// @filename: dep.ts +export const imported = 1; +export let importedLet = "s"; + +// @filename: lib.ts +import { imported, importedLet } from "./dep"; + +// Not an alias: the identifier means something else, or has another type, where the namespace is declared + +export function loop() {} +for (const k of ["a", "b"]) { + loop.last = k; +} + +const shadowed = 1; +export function shadow() {} +{ + const shadowed = "s"; + shadow.v = shadowed; +} + +declare const wide: string | number; +export function narrowed() {} +if (typeof wide === "string") { + narrowed.s = wide; +} + +const VERSION = "1.2.3"; +export function literal() {} +literal.version = VERSION; + +const initial: number = 1; +export function reassigned() {} +reassigned.n = initial; +reassigned.n = 5; + +export function fromImport() {} +fromImport.literal = imported; + +// Still an alias + +export function keepsImport() {} +keepsImport.value = importedLet; + +let mutable = 1; +export function keepsLet() {} +keepsLet.m = mutable; + +class C { + x = 1; +} +export function keepsClass() {} +keepsClass.C = C; + +function helper() { + return 1; +} +export function keepsFunction() {} +keepsFunction.helper = helper; + +export const arrowHost = () => {}; +const arrowValue: string = "x"; +arrowHost.v = arrowValue; + +export namespace N { + const k: number = 1; + export function f() {} + f.k = k; + { + const k = "s"; + f.inner = k; + } +} + +// @filename: use.ts +import { loop, shadow, narrowed, literal, reassigned, fromImport, keepsImport, keepsLet, keepsClass, keepsFunction, arrowHost, N } from "./lib"; +const s1: string = loop.last; +const s2: string = shadow.v; +const s3: string = narrowed.s; +literal.version = "2.0.0"; +reassigned.n = 6; +fromImport.literal = 2; +const s4: string = keepsImport.value; +const n1: number = keepsLet.m; +const n0: number = new keepsClass.C().x; +const n2: number = keepsFunction.helper(); +const s5: string = arrowHost.v; +const n3: number = N.f.k; +const s6: string = N.f.inner; From 018f2f187f7849bc9dd2ed04be7c2a0491031750 Mon Sep 17 00:00:00 2001 From: Kaique da Silva Date: Thu, 26 Nov 2026 08:59:51 -0300 Subject: [PATCH 2/2] fix: alias an identifier-valued expando member only when the alias means 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 #65615 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7 --- tsc/internal/checker/emitresolver.go | 27 ++++ tsc/internal/printer/emitresolver.go | 1 + .../transformers/declarations/transform.go | 8 +- .../declarationEmitExpandoIdentifierValue.js | 121 ++---------------- 4 files changed, 49 insertions(+), 108 deletions(-) diff --git a/tsc/internal/checker/emitresolver.go b/tsc/internal/checker/emitresolver.go index e4fad5c3f5b1f..49832dc4f479c 100644 --- a/tsc/internal/checker/emitresolver.go +++ b/tsc/internal/checker/emitresolver.go @@ -367,6 +367,33 @@ func (r *EmitResolver) IsExpandoFunctionDeclaration(node *ast.Node) bool { return r.IsExpandoFunctionDeclarationUnsafe(node) } +// CanDeclareExpandoAssignmentAsAlias reports whether an expando assignment `host.name = id` can be declared +// as `export { id as name }` in a namespace declared at location. That holds when the assignment is the only +// declaration of the member, `id` resolves to the same symbol at location, and the declared type of that +// symbol is the type of the member (it isn't narrowed, widened from a literal, or reassigned). +func (r *EmitResolver) CanDeclareExpandoAssignmentAsAlias(node *ast.Node, location *ast.Node) bool { + if !ast.IsParseTreeNode(node) || !ast.IsBinaryExpression(node) || !ast.IsIdentifier(node.AsBinaryExpression().Right) { + return false + } + r.checkerMu.Lock() + defer r.checkerMu.Unlock() + c := r.checker + member := node.Symbol() + if member == nil || len(member.Declarations()) != 1 { + return false + } + right := node.AsBinaryExpression().Right + target := c.getResolvedSymbol(right) + if target == c.unknownSymbol { + return false + } + if c.resolveName(location, right.Text(), ast.SymbolFlagsValue|ast.SymbolFlagsExportValue, nil /*nameNotFoundMessage*/, false /*isUse*/, false /*excludeGlobals*/) != target { + return false + } + // An exported declaration resolves to its local symbol, which only carries ExportValue + return c.isTypeIdenticalTo(c.getTypeOfSymbol(member), c.getTypeOfSymbol(c.getExportSymbolOfValueSymbolIfExported(target))) +} + func (r *EmitResolver) isSymbolAccessible(symbol *ast.Symbol, enclosingDeclaration *ast.Node, meaning ast.SymbolFlags, shouldComputeAliasToMarkVisible bool) printer.SymbolAccessibilityResult { return r.checker.IsSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasToMarkVisible) } diff --git a/tsc/internal/printer/emitresolver.go b/tsc/internal/printer/emitresolver.go index d86a0ca378ecc..d4e39956898a6 100644 --- a/tsc/internal/printer/emitresolver.go +++ b/tsc/internal/printer/emitresolver.go @@ -100,6 +100,7 @@ type EmitResolver interface { IsEntityNameVisible(entityName *ast.Node, enclosingDeclaration *ast.Node) SymbolAccessibilityResult // previously SymbolVisibilityResult in strada - ErrorModuleName never set IsExpandoFunctionDeclaration(node *ast.Node) bool IsExpandoFunctionDeclarationUnsafe(node *ast.Node) bool + CanDeclareExpandoAssignmentAsAlias(node *ast.Node, location *ast.Node) bool IsLiteralConstDeclaration(node *ast.Node) bool RequiresAddingImplicitUndefined(node *ast.Node, symbol *ast.Symbol, enclosingDeclaration *ast.Node) bool IsDeclarationVisible(node *ast.Node) bool diff --git a/tsc/internal/transformers/declarations/transform.go b/tsc/internal/transformers/declarations/transform.go index 9538225ed249f..27af4b1f8ce95 100644 --- a/tsc/internal/transformers/declarations/transform.go +++ b/tsc/internal/transformers/declarations/transform.go @@ -2908,12 +2908,18 @@ func (tx *DeclarationTransformer) transformExpandoAssignment(node *ast.BinaryExp preexistingExpandoHasExport := core.Some(tx.expandoMembers[hostId], ast.IsExportDeclaration) - if ast.IsIdentifier(node.Right) { + // The namespace is declared next to the host, so the alias must mean the same thing there + hostContainer := core.IfElse(ast.IsVariableDeclaration(declaration), declaration.Parent.Parent, declaration).Parent + if ast.IsIdentifier(node.Right) && tx.resolver.CanDeclareExpandoAssignmentAsAlias(node.AsNode(), hostContainer) { if !preexistingExpandoHasExport { tx.addExportModifierToExpandoMembers(hostId) } // alias-like, emit an `export {name}` or `export {name as alias}` + // Expando members are collected from the source file, so make the aliased name visible from the host's scope + oldEnclosing := tx.enclosingDeclaration + tx.enclosingDeclaration = hostContainer result := tx.transformBinaryExpressionToExportDeclaration(node.AsNode(), exportName) + tx.enclosingDeclaration = oldEnclosing tx.expandoMembers[hostId] = append(tx.expandoMembers[hostId], result) return } diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js index 0760fad4b89c3..0831da47dda90 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitExpandoIdentifierValue.js @@ -190,35 +190,32 @@ const s6 = lib_1.N.f.inner; export declare const imported = 1; export declare let importedLet: string; //// [lib.d.ts] -import { imported, importedLet } from "./dep"; +import { importedLet } from "./dep"; export declare function loop(): void; export declare namespace loop { - export { k as last }; + var last: string; } -declare const shadowed = 1; export declare function shadow(): void; export declare namespace shadow { - export { shadowed as v }; + var v: string; } -declare const wide: string | number; export declare function narrowed(): void; export declare namespace narrowed { - export { wide as s }; + var s: string; } -declare const VERSION = "1.2.3"; export declare function literal(): void; export declare namespace literal { - export { VERSION as version }; + var version: string; } -declare const initial: number; export declare function reassigned(): void; export declare namespace reassigned { - export { initial as n }; - export var n: number; + var n: number; + var n: number; } export declare function fromImport(): void; export declare namespace fromImport { - export { imported as literal }; + var _a: number; + export { _a as literal }; } export declare function keepsImport(): void; export declare namespace keepsImport { @@ -247,104 +244,14 @@ export declare namespace arrowHost { } declare const arrowValue: string; export declare namespace N { - function f(): void; - namespace f { + const k: number; + export function f(): void; + export namespace f { export { k }; - export { k as inner }; + export var inner: string; } + export {}; } export {}; //// [use.d.ts] export {}; - - -//// [DtsFileErrors] - - -out/lib.d.ts(4,14): error TS2304: Cannot find name 'k'. -out/lib.d.ts(24,14): error TS2484: Export declaration conflicts with exported declaration of 'n'. -out/lib.d.ts(60,18): error TS2304: Cannot find name 'k'. -out/lib.d.ts(61,18): error TS2304: Cannot find name 'k'. - - -==== out/dep.d.ts (0 errors) ==== - export declare const imported = 1; - export declare let importedLet: string; - -==== out/lib.d.ts (4 errors) ==== - import { imported, importedLet } from "./dep"; - export declare function loop(): void; - export declare namespace loop { - export { k as last }; - ~ -!!! error TS2304: Cannot find name 'k'. - } - declare const shadowed = 1; - export declare function shadow(): void; - export declare namespace shadow { - export { shadowed as v }; - } - declare const wide: string | number; - export declare function narrowed(): void; - export declare namespace narrowed { - export { wide as s }; - } - declare const VERSION = "1.2.3"; - export declare function literal(): void; - export declare namespace literal { - export { VERSION as version }; - } - declare const initial: number; - export declare function reassigned(): void; - export declare namespace reassigned { - export { initial as n }; - ~~~~~~~~~~~~ -!!! error TS2484: Export declaration conflicts with exported declaration of 'n'. - export var n: number; - } - export declare function fromImport(): void; - export declare namespace fromImport { - export { imported as literal }; - } - export declare function keepsImport(): void; - export declare namespace keepsImport { - export { importedLet as value }; - } - declare let mutable: number; - export declare function keepsLet(): void; - export declare namespace keepsLet { - export { mutable as m }; - } - declare class C { - x: number; - } - export declare function keepsClass(): void; - export declare namespace keepsClass { - export { C }; - } - declare function helper(): number; - export declare function keepsFunction(): void; - export declare namespace keepsFunction { - export { helper }; - } - export declare function arrowHost(): void; - export declare namespace arrowHost { - export { arrowValue as v }; - } - declare const arrowValue: string; - export declare namespace N { - function f(): void; - namespace f { - export { k }; - ~ -!!! error TS2304: Cannot find name 'k'. - export { k as inner }; - ~ -!!! error TS2304: Cannot find name 'k'. - } - } - export {}; - -==== out/use.d.ts (0 errors) ==== - export {}; - \ No newline at end of file