Skip to content

Commit a35c9a7

Browse files
committed
fix: sanitize more identifiers
Signed-off-by: Christian Stewart <[email protected]>
1 parent 483e650 commit a35c9a7

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

compiler/compiler.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,15 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
595595
for _, sourceFile := range sourceFiles {
596596
functions := imports[sourceFile]
597597
if len(functions) > 0 {
598+
// Apply sanitization to function names
599+
var sanitizedFunctions []string
600+
for _, fn := range functions {
601+
sanitizedFunctions = append(sanitizedFunctions, sanitizeIdentifier(fn))
602+
}
598603
// Sort functions for consistent output
599-
sort.Strings(functions)
604+
sort.Strings(sanitizedFunctions)
600605
c.codeWriter.WriteLinef("import { %s } from \"./%s.gs.js\";",
601-
strings.Join(functions, ", "), sourceFile)
606+
strings.Join(sanitizedFunctions, ", "), sourceFile)
602607
}
603608
}
604609
}
@@ -649,10 +654,15 @@ func (c *FileCompiler) Compile(ctx context.Context) error {
649654
}
650655

651656
if len(nonProtobufTypes) > 0 {
657+
// Apply sanitization to type names
658+
var sanitizedTypes []string
659+
for _, typeName := range nonProtobufTypes {
660+
sanitizedTypes = append(sanitizedTypes, sanitizeIdentifier(typeName))
661+
}
652662
// Sort types for consistent output
653-
sort.Strings(nonProtobufTypes)
663+
sort.Strings(sanitizedTypes)
654664
c.codeWriter.WriteLinef("import { %s } from \"./%s.gs.js\";",
655-
strings.Join(nonProtobufTypes, ", "), sourceFile)
665+
strings.Join(sanitizedTypes, ", "), sourceFile)
656666
}
657667
}
658668
}

compiler/spec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,9 @@ func (c *GoToTSCompiler) WriteImportSpec(a *ast.ImportSpec) {
623623
}
624624
}
625625

626+
// Apply sanitization to handle known names like "Promise" -> "PromiseType"
627+
impName = c.sanitizeIdentifier(impName)
628+
626629
// All Go package imports are mapped to the @goscript/ scope.
627630
// The TypeScript compiler will resolve these using tsconfig paths to either
628631
// handwritten versions (in .goscript-assets) or transpiled versions (in goscript).

0 commit comments

Comments
 (0)