Skip to content

Bring back SourceFile.EndOfFileToken #1257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions internal/transformers/declarations/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type DeclarationTransformer struct {
needsScopeFixMarker bool
resultHasScopeMarker bool
enclosingDeclaration *ast.Node
generatedDefaultExportIdentifier *ast.Node
resultHasExternalModuleIndicator bool
suppressNewDiagnosticContexts bool
lateStatementReplacementMap map[ast.NodeId]*ast.Node
Expand Down Expand Up @@ -912,7 +913,7 @@ func (tx *DeclarationTransformer) visitDeclarationStatements(input *ast.Node) *a
return input
}
// expression is non-identifier, create _default typed variable to reference
newId := tx.Factory().NewUniqueNameEx("_default", printer.AutoGenerateOptions{Flags: printer.GeneratedIdentifierFlagsOptimistic})
newId := tx.getGeneratedDefaultExportIdentifier()
tx.state.getSymbolAccessibilityDiagnostic = func(_ printer.SymbolAccessibilityResult) *SymbolAccessibilityDiagnostic {
return &SymbolAccessibilityDiagnostic{
diagnosticMessage: diagnostics.Default_export_of_the_module_has_or_is_using_private_name_0,
Expand Down Expand Up @@ -1114,10 +1115,14 @@ func (tx *DeclarationTransformer) transformTopLevelDeclaration(input *ast.Node)

func (tx *DeclarationTransformer) transformTypeAliasDeclaration(input *ast.TypeAliasDeclaration) *ast.Node {
tx.needsDeclare = false
name := input.Name()
if ast.IsSourceFile(input.Parent) && ast.IsIdentifier(name) && name.Text() == "default" {
name = tx.getGeneratedDefaultExportIdentifier()
}
return tx.Factory().UpdateTypeAliasDeclaration(
input,
tx.ensureModifiers(input.AsNode()),
input.Name(),
name,
tx.Visitor().VisitNodes(input.TypeParameters),
tx.Visitor().Visit(input.Type),
)
Expand Down Expand Up @@ -1767,3 +1772,12 @@ func (tx *DeclarationTransformer) transformJSDocOptionalType(input *ast.JSDocOpt
tx.EmitContext().SetOriginal(replacement, input.AsNode())
return replacement
}

func (tx *DeclarationTransformer) getGeneratedDefaultExportIdentifier() *ast.Node {
name := tx.generatedDefaultExportIdentifier
if name == nil {
name = tx.Factory().NewUniqueNameEx("_default", printer.AutoGenerateOptions{Flags: printer.GeneratedIdentifierFlagsOptimistic})
tx.generatedDefaultExportIdentifier = name
}
return name
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,84 +108,8 @@ export default Bar;
//// [index5.d.ts]
declare const _default: number;
export default _default;
export type default = string | number;
export type _default = string | number;
//// [index6.d.ts]
// merge type alias and function (OK)
export default function func(): void;
export type default = string | number;


//// [DtsFileErrors]


out/index5.d.ts(3,1): error TS1128: Declaration or statement expected.
out/index5.d.ts(3,8): error TS2304: Cannot find name 'type'.
out/index5.d.ts(3,13): error TS2457: Type alias name cannot be 'default'.
out/index5.d.ts(3,21): error TS1128: Declaration or statement expected.
out/index5.d.ts(3,23): error TS2693: 'string' only refers to a type, but is being used as a value here.
out/index5.d.ts(3,32): error TS2693: 'number' only refers to a type, but is being used as a value here.
out/index6.d.ts(3,1): error TS1128: Declaration or statement expected.
out/index6.d.ts(3,8): error TS2304: Cannot find name 'type'.
out/index6.d.ts(3,13): error TS2457: Type alias name cannot be 'default'.
out/index6.d.ts(3,21): error TS1128: Declaration or statement expected.
out/index6.d.ts(3,23): error TS2693: 'string' only refers to a type, but is being used as a value here.
out/index6.d.ts(3,32): error TS2693: 'number' only refers to a type, but is being used as a value here.


==== out/index1.d.ts (0 errors) ====
declare const _default: number;
export default _default;

==== out/index2.d.ts (0 errors) ====
export default function foo(): typeof foo;
export declare const x: typeof foo;
export { foo as bar };

==== out/index3.d.ts (0 errors) ====
export default class Foo {
a: Foo;
}
export declare const X: typeof Foo;
export { Foo as Bar };

==== out/index4.d.ts (0 errors) ====
import Fab from "./index3";
declare class Bar extends Fab {
x: Bar;
}
export default Bar;

==== out/index5.d.ts (6 errors) ====
declare const _default: number;
export default _default;
export type default = string | number;
~~~~~~
!!! error TS1128: Declaration or statement expected.
~~~~
!!! error TS2304: Cannot find name 'type'.
~~~~~~~
!!! error TS2457: Type alias name cannot be 'default'.
~
!!! error TS1128: Declaration or statement expected.
~~~~~~
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
~~~~~~
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.

==== out/index6.d.ts (6 errors) ====
// merge type alias and function (OK)
export default function func(): void;
export type default = string | number;
~~~~~~
!!! error TS1128: Declaration or statement expected.
~~~~
!!! error TS2304: Cannot find name 'type'.
~~~~~~~
!!! error TS2457: Type alias name cannot be 'default'.
~
!!! error TS1128: Declaration or statement expected.
~~~~~~
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
~~~~~~
!!! error TS2693: 'number' only refers to a type, but is being used as a value here.

export type _default = string | number;
Original file line number Diff line number Diff line change
Expand Up @@ -74,87 +74,11 @@
-declare const _default: 12;
+declare const _default: number;
export default _default;
+export type default = string | number;
+export type _default = string | number;
//// [index6.d.ts]
-declare function func(): void;
-type func = string | number;
-export default func;
+// merge type alias and function (OK)
+export default function func(): void;
+export type default = string | number;
+
+
+//// [DtsFileErrors]
+
+
+out/index5.d.ts(3,1): error TS1128: Declaration or statement expected.
+out/index5.d.ts(3,8): error TS2304: Cannot find name 'type'.
+out/index5.d.ts(3,13): error TS2457: Type alias name cannot be 'default'.
+out/index5.d.ts(3,21): error TS1128: Declaration or statement expected.
+out/index5.d.ts(3,23): error TS2693: 'string' only refers to a type, but is being used as a value here.
+out/index5.d.ts(3,32): error TS2693: 'number' only refers to a type, but is being used as a value here.
+out/index6.d.ts(3,1): error TS1128: Declaration or statement expected.
+out/index6.d.ts(3,8): error TS2304: Cannot find name 'type'.
+out/index6.d.ts(3,13): error TS2457: Type alias name cannot be 'default'.
+out/index6.d.ts(3,21): error TS1128: Declaration or statement expected.
+out/index6.d.ts(3,23): error TS2693: 'string' only refers to a type, but is being used as a value here.
+out/index6.d.ts(3,32): error TS2693: 'number' only refers to a type, but is being used as a value here.
+
+
+==== out/index1.d.ts (0 errors) ====
+ declare const _default: number;
+ export default _default;
+
+==== out/index2.d.ts (0 errors) ====
+ export default function foo(): typeof foo;
+ export declare const x: typeof foo;
+ export { foo as bar };
+
+==== out/index3.d.ts (0 errors) ====
+ export default class Foo {
+ a: Foo;
+ }
+ export declare const X: typeof Foo;
+ export { Foo as Bar };
+
+==== out/index4.d.ts (0 errors) ====
+ import Fab from "./index3";
+ declare class Bar extends Fab {
+ x: Bar;
+ }
+ export default Bar;
+
+==== out/index5.d.ts (6 errors) ====
+ declare const _default: number;
+ export default _default;
+ export type default = string | number;
+ ~~~~~~
+!!! error TS1128: Declaration or statement expected.
+ ~~~~
+!!! error TS2304: Cannot find name 'type'.
+ ~~~~~~~
+!!! error TS2457: Type alias name cannot be 'default'.
+ ~
+!!! error TS1128: Declaration or statement expected.
+ ~~~~~~
+!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
+ ~~~~~~
+!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
+
+==== out/index6.d.ts (6 errors) ====
+ // merge type alias and function (OK)
+ export default function func(): void;
+ export type default = string | number;
+ ~~~~~~
+!!! error TS1128: Declaration or statement expected.
+ ~~~~
+!!! error TS2304: Cannot find name 'type'.
+ ~~~~~~~
+!!! error TS2457: Type alias name cannot be 'default'.
+ ~
+!!! error TS1128: Declaration or statement expected.
+ ~~~~~~
+!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
+ ~~~~~~
+!!! error TS2693: 'number' only refers to a type, but is being used as a value here.
+
+export type _default = string | number;
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ declare class Cls {
static y: string;
}
export default Cls;
export type default = string | number;
export type _default = string | number;
//// [index2.d.ts]
// merge type alias and class (error message improvement needed, see #32368)
export default class C {
}
export type default = string | number;
export type _default = string | number;
//// [index3.d.ts]
// merge type alias and variable (behavior is borked, see #32366)
declare const x = 12;
export { x as default };
export type default = string | number;
export type _default = string | number;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, but this is weird, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is, the comment above this says the behavior is borked - and the type alias declaration here is the same in Strada:
https://github.com/microsoft/TypeScript/blob/78c16795cdee70b9d9f0f248b6dbb6ba50994a59/tests/baselines/reference/jsDeclarationsDefaultsErr.js#L81-L84

Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@
+ static y: string;
}
+export default Cls;
+export type default = string | number;
+export type _default = string | number;
//// [index2.d.ts]
+// merge type alias and class (error message improvement needed, see #32368)
export default class C {
}
+export type default = string | number;
+export type _default = string | number;
//// [index3.d.ts]
-export type _default = string | number;
+// merge type alias and variable (behavior is borked, see #32366)
+declare const x = 12;
export { x as default };
-declare const x: 12;
+export type default = string | number;
+export type _default = string | number;