Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions tsc/internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,14 @@ func (tx *DeclarationTransformer) transformExportAssignment(input *ast.Node, ass
return exportAssignment
}

tx.state.getSymbolAccessibilityDiagnostic = func(_ printer.SymbolAccessibilityResult) *SymbolAccessibilityDiagnostic {
return &SymbolAccessibilityDiagnostic{
diagnosticMessage: diagnostics.Default_export_of_the_module_has_or_is_using_private_name_0,
errorNode: input,
}
}
tx.tracker.PushErrorFallbackNode(assignment)

// Check if the expression is a class expression - emit as a class declaration + export assignment
unwrapped := ast.SkipOuterExpressions(expression, ast.OEKExpressionTypePassthrough)
newId := tx.getNameOfExportedAssignedExpression(unwrapped, isExportEquals)
Expand All @@ -1234,6 +1242,7 @@ func (tx *DeclarationTransformer) transformExportAssignment(input *ast.Node, ass
mods = append(mods, tx.Factory().NewModifier(ast.KindDeclareKeyword))
}
classDecl := tx.transformClassExpressionToDeclaration(unwrapped, newId, tx.Factory().NewModifierList(mods))
tx.tracker.PopErrorFallbackNode()
tx.preserveJsDoc(classDecl, input)
// Reuse the same name node for the export so unique names resolve consistently
exportAssignment := tx.Factory().NewExportAssignment(nil, isExportEquals, nil, newId)
Expand All @@ -1247,6 +1256,7 @@ func (tx *DeclarationTransformer) transformExportAssignment(input *ast.Node, ass
}
fullSignatureType := assignment.Type()
funcDecl := tx.transformFunctionLikeToDeclaration(unwrapped, newId, tx.Factory().NewModifierList(mods), fullSignatureType)
tx.tracker.PopErrorFallbackNode()
tx.preserveJsDoc(funcDecl, input)
// Reuse the same name node for the export so unique names resolve consistently
exportAssignment := tx.Factory().NewExportAssignment(nil, isExportEquals, nil, newId)
Expand All @@ -1255,14 +1265,7 @@ func (tx *DeclarationTransformer) transformExportAssignment(input *ast.Node, ass
}

// expression is non-identifier, create _default typed variable to reference
tx.state.getSymbolAccessibilityDiagnostic = func(_ printer.SymbolAccessibilityResult) *SymbolAccessibilityDiagnostic {
return &SymbolAccessibilityDiagnostic{
diagnosticMessage: diagnostics.Default_export_of_the_module_has_or_is_using_private_name_0,
errorNode: input,
}
}
tx.cjsExportAssignmentName = newId
tx.tracker.PushErrorFallbackNode(assignment)
var type_, initializer *ast.Node
if ast.IsPrimitiveLiteralValue(unwrapParenthesizedExpression(expression), true) {
initializer = tx.resolver.CreateLiteralConstValue(tx.EmitContext(), tx.EmitContext().ParseNode(assignment), tx.tracker)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
repro.ts(3,1): error TS4082: Default export of the module has or is using private name 'brand'.


==== helper.ts (0 errors) ====
declare const brand: unique symbol;

class Foo {
[brand]: number = 1;
}

export function makeFoo() {
return new Foo();
}

==== repro.ts (1 errors) ====
import { makeFoo } from './helper';

export default () => makeFoo();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4082: Default export of the module has or is using private name 'brand'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/declarationEmitDefaultExportArrowFunctionPrivateName.ts] ////

//// [helper.ts]
declare const brand: unique symbol;

class Foo {
[brand]: number = 1;
}

export function makeFoo() {
return new Foo();
}

//// [repro.ts]
import { makeFoo } from './helper';

export default () => makeFoo();




//// [helper.d.ts]
declare const brand: unique symbol;
declare class Foo {
[brand]: number;
}
export declare function makeFoo(): Foo;
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/declarationEmitDefaultExportArrowFunctionPrivateName.ts] ////

=== helper.ts ===
declare const brand: unique symbol;
>brand : Symbol(brand, Decl(helper.ts, 0, 13))

class Foo {
>Foo : Symbol(Foo, Decl(helper.ts, 0, 35))

[brand]: number = 1;
>[brand] : Symbol(Foo[brand], Decl(helper.ts, 2, 11))
>brand : Symbol(brand, Decl(helper.ts, 0, 13))
}

export function makeFoo() {
>makeFoo : Symbol(makeFoo, Decl(helper.ts, 4, 1))

return new Foo();
>Foo : Symbol(Foo, Decl(helper.ts, 0, 35))
}

=== repro.ts ===
import { makeFoo } from './helper';
>makeFoo : Symbol(makeFoo, Decl(repro.ts, 0, 8))

export default () => makeFoo();
>makeFoo : Symbol(makeFoo, Decl(repro.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/declarationEmitDefaultExportArrowFunctionPrivateName.ts] ////

=== helper.ts ===
declare const brand: unique symbol;
>brand : unique symbol

class Foo {
>Foo : Foo

[brand]: number = 1;
>[brand] : number
>brand : unique symbol
>1 : 1
}

export function makeFoo() {
>makeFoo : () => Foo

return new Foo();
>new Foo() : Foo
>Foo : typeof Foo
}

=== repro.ts ===
import { makeFoo } from './helper';
>makeFoo : () => Foo

export default () => makeFoo();
>() => makeFoo() : () => Foo
>makeFoo() : Foo
>makeFoo : () => Foo

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
repro.ts(3,1): error TS4082: Default export of the module has or is using private name 'brand'.


==== helper.ts (0 errors) ====
declare const brand: unique symbol;

class Foo {
[brand]: number = 1;
}

export function makeFoo() {
return new Foo();
}

==== repro.ts (1 errors) ====
import { makeFoo } from './helper';

export default (function () {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return makeFoo();
~~~~~~~~~~~~~~~~~~~
});
~~~
!!! error TS4082: Default export of the module has or is using private name 'brand'.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/declarationEmitDefaultExportFunctionExpressionPrivateName.ts] ////

//// [helper.ts]
declare const brand: unique symbol;

class Foo {
[brand]: number = 1;
}

export function makeFoo() {
return new Foo();
}

//// [repro.ts]
import { makeFoo } from './helper';

export default (function () {
return makeFoo();
});




//// [helper.d.ts]
declare const brand: unique symbol;
declare class Foo {
[brand]: number;
}
export declare function makeFoo(): Foo;
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/declarationEmitDefaultExportFunctionExpressionPrivateName.ts] ////

=== helper.ts ===
declare const brand: unique symbol;
>brand : Symbol(brand, Decl(helper.ts, 0, 13))

class Foo {
>Foo : Symbol(Foo, Decl(helper.ts, 0, 35))

[brand]: number = 1;
>[brand] : Symbol(Foo[brand], Decl(helper.ts, 2, 11))
>brand : Symbol(brand, Decl(helper.ts, 0, 13))
}

export function makeFoo() {
>makeFoo : Symbol(makeFoo, Decl(helper.ts, 4, 1))

return new Foo();
>Foo : Symbol(Foo, Decl(helper.ts, 0, 35))
}

=== repro.ts ===
import { makeFoo } from './helper';
>makeFoo : Symbol(makeFoo, Decl(repro.ts, 0, 8))

export default (function () {
return makeFoo();
>makeFoo : Symbol(makeFoo, Decl(repro.ts, 0, 8))

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// [tests/cases/compiler/declarationEmitDefaultExportFunctionExpressionPrivateName.ts] ////

=== helper.ts ===
declare const brand: unique symbol;
>brand : unique symbol

class Foo {
>Foo : Foo

[brand]: number = 1;
>[brand] : number
>brand : unique symbol
>1 : 1
}

export function makeFoo() {
>makeFoo : () => Foo

return new Foo();
>new Foo() : Foo
>Foo : typeof Foo
}

=== repro.ts ===
import { makeFoo } from './helper';
>makeFoo : () => Foo

export default (function () {
>(function () { return makeFoo();}) : () => Foo
>function () { return makeFoo();} : () => Foo

return makeFoo();
>makeFoo() : Foo
>makeFoo : () => Foo

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @declaration: true
// @emitDeclarationOnly: true
// @outDir: dist
// @strict: true

// @filename: helper.ts
declare const brand: unique symbol;

class Foo {
[brand]: number = 1;
}

export function makeFoo() {
return new Foo();
}

// @filename: repro.ts
import { makeFoo } from './helper';

export default () => makeFoo();
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @declaration: true
// @emitDeclarationOnly: true
// @outDir: dist
// @strict: true

// @filename: helper.ts
declare const brand: unique symbol;

class Foo {
[brand]: number = 1;
}

export function makeFoo() {
return new Foo();
}

// @filename: repro.ts
import { makeFoo } from './helper';

export default (function () {
return makeFoo();
});