Skip to content

Commit 008c284

Browse files
committed
refactor: cleanup code with lhsHasComplexTargets
Signed-off-by: Christian Stewart <[email protected]>
1 parent 3edee05 commit 008c284

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

compiler/stmt-assign.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,35 +127,19 @@ func (c *GoToTSCompiler) WriteStmtAssign(exp *ast.AssignStmt) error {
127127
if ident, ok := lhsExpr.(*ast.Ident); ok && ident.Name != "_" && newVars[i] {
128128
c.tsw.WriteLiterally("let ")
129129
c.WriteIdent(ident, false)
130-
131130
// Add type annotation if we have type information
132131
if i < len(resultTypes) {
133132
c.tsw.WriteLiterally(": ")
134133
c.WriteGoType(resultTypes[i].Type(), GoTypeContextGeneral)
135134
}
136-
137135
c.tsw.WriteLine("")
138136
}
139137
}
140138
}
141139
}
142140

143141
// First, collect all the selector expressions to identify variables that need to be initialized
144-
hasSelectors := false
145-
for _, lhsExpr := range lhs {
146-
if _, ok := lhsExpr.(*ast.SelectorExpr); ok {
147-
hasSelectors = true
148-
break
149-
}
150-
if _, ok := lhsExpr.(*ast.StarExpr); ok {
151-
hasSelectors = true
152-
break
153-
}
154-
if _, ok := lhsExpr.(*ast.IndexExpr); ok {
155-
hasSelectors = true
156-
break
157-
}
158-
}
142+
hasSelectors := c.lhsHasComplexTargets(lhs)
159143

160144
// If we have selector expressions, we need to ensure variables are initialized
161145
// before the destructuring assignment
@@ -541,3 +525,14 @@ func (c *GoToTSCompiler) writeLHSTarget(lhsExpr ast.Expr) error {
541525
return errors.Errorf("unhandled LHS expression in assignment: %T", lhsExpr)
542526
}
543527
}
528+
529+
// lhsHasComplexTargets returns true if any LHS expression is a selector, star (dereference), or index expression.
530+
func (c *GoToTSCompiler) lhsHasComplexTargets(lhs []ast.Expr) bool {
531+
for _, e := range lhs {
532+
switch e.(type) {
533+
case *ast.SelectorExpr, *ast.StarExpr, *ast.IndexExpr:
534+
return true
535+
}
536+
}
537+
return false
538+
}

0 commit comments

Comments
 (0)