Skip to content

Commit c27e0f7

Browse files
committed
feat: suppress export statements in functions
Signed-off-by: Christian Stewart <[email protected]>
1 parent d9e5fc3 commit c27e0f7

File tree

8 files changed

+46
-106
lines changed

8 files changed

+46
-106
lines changed

compiler/analysis.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type NodeInfo struct {
5656
IsBareReturn bool
5757
EnclosingFuncDecl *ast.FuncDecl
5858
EnclosingFuncLit *ast.FuncLit
59+
IsInsideFunction bool // true if this declaration is inside a function body
5960
}
6061

6162
// Analysis holds information gathered during the analysis phase of the Go code compilation.
@@ -689,6 +690,25 @@ func (v *analysisVisitor) Visit(node ast.Node) ast.Visitor {
689690
}
690691
}
691692
return v // Continue traversal
693+
694+
case *ast.DeclStmt:
695+
// Handle declarations inside functions (const, var, type declarations within function bodies)
696+
// These should not have export modifiers in TypeScript
697+
if genDecl, ok := n.Decl.(*ast.GenDecl); ok {
698+
// Check if we're inside a function (either FuncDecl or FuncLit)
699+
isInsideFunction := v.currentFuncDecl != nil || v.currentFuncLit != nil
700+
701+
if isInsideFunction {
702+
// Mark all specs in this declaration as being inside a function
703+
for _, spec := range genDecl.Specs {
704+
if v.analysis.NodeData[spec] == nil {
705+
v.analysis.NodeData[spec] = &NodeInfo{}
706+
}
707+
v.analysis.NodeData[spec].IsInsideFunction = true
708+
}
709+
}
710+
}
711+
return v // Continue traversal
692712
}
693713

694714
// For all other nodes, continue traversal

compiler/spec-value.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ func (c *GoToTSCompiler) WriteValueSpec(a *ast.ValueSpec) error {
8181
}
8282
}
8383

84-
// Start declaration - add export for Go-exported symbols
85-
if name.IsExported() {
84+
// Start declaration - add export for Go-exported symbols (but not if inside a function)
85+
isInsideFunction := false
86+
if nodeInfo := c.analysis.NodeData[a]; nodeInfo != nil {
87+
isInsideFunction = nodeInfo.IsInsideFunction
88+
}
89+
90+
if name.IsExported() && !isInsideFunction {
8691
c.tsw.WriteLiterally("export ")
8792
}
8893
c.tsw.WriteLiterally("let ")

compiler/spec.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,13 @@ func (c *GoToTSCompiler) WriteTypeSpec(a *ast.TypeSpec) error {
165165
case *ast.InterfaceType:
166166
return c.WriteInterfaceTypeSpec(a, t)
167167
default:
168-
// type alias - add export for Go-exported types
169-
if a.Name.IsExported() {
168+
// type alias - add export for Go-exported types (but not if inside a function)
169+
isInsideFunction := false
170+
if nodeInfo := c.analysis.NodeData[a]; nodeInfo != nil {
171+
isInsideFunction = nodeInfo.IsInsideFunction
172+
}
173+
174+
if a.Name.IsExported() && !isInsideFunction {
170175
c.tsw.WriteLiterally("export ")
171176
}
172177
c.tsw.WriteLiterally("type ")
@@ -182,8 +187,13 @@ func (c *GoToTSCompiler) WriteTypeSpec(a *ast.TypeSpec) error {
182187

183188
// WriteInterfaceTypeSpec writes the TypeScript type for a Go interface type.
184189
func (c *GoToTSCompiler) WriteInterfaceTypeSpec(a *ast.TypeSpec, t *ast.InterfaceType) error {
185-
// Add export for Go-exported interfaces
186-
if a.Name.IsExported() {
190+
// Add export for Go-exported interfaces (but not if inside a function)
191+
isInsideFunction := false
192+
if nodeInfo := c.analysis.NodeData[a]; nodeInfo != nil {
193+
isInsideFunction = nodeInfo.IsInsideFunction
194+
}
195+
196+
if a.Name.IsExported() && !isInsideFunction {
187197
c.tsw.WriteLiterally("export ")
188198
}
189199
c.tsw.WriteLiterally("type ")

compliance/WIP.md

Lines changed: 0 additions & 93 deletions
This file was deleted.

compliance/tests/flag_bitwise_op/actual.log

Lines changed: 0 additions & 1 deletion
This file was deleted.

compliance/tests/flag_bitwise_op/flag_bitwise_op.gs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import * as $ from "@goscript/builtin/builtin.js";
55

66
export async function main(): Promise<void> {
7-
export let O_WRONLY: number = 0x1
8-
export let O_CREATE: number = 0x40
9-
export let O_APPEND: number = 0x400
10-
export let O_TRUNC: number = 0x200
7+
let O_WRONLY: number = 0x1
8+
let O_CREATE: number = 0x40
9+
let O_APPEND: number = 0x400
10+
let O_TRUNC: number = 0x200
1111
let flag = ((1 | 64) | 1024)
1212
if ((flag & 1024) != 0) {
1313
console.log("O_APPEND is set: Expected: O_APPEND is set, Actual: O_APPEND is set")

compliance/tests/inline_function_type_cast/actual.log

Lines changed: 0 additions & 1 deletion
This file was deleted.

compliance/tests/inline_function_type_cast/inline_function_type_cast.gs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function main(): Promise<void> {
1919
console.log(castedGreeter!("Inline World"))
2020

2121
// Test with a different signature
22-
export type Adder = ((a: number, b: number) => number) | null;
22+
type Adder = ((a: number, b: number) => number) | null;
2323
let theInlineAdder = (a: number, b: number): number => {
2424
return a + b
2525
}

0 commit comments

Comments
 (0)