Skip to content

Conversation

a-tarasyuk
Copy link
Contributor

Fixes #1632

@a-tarasyuk a-tarasyuk force-pushed the fix/1632 branch 2 times, most recently from 798d12b to c5a62a9 Compare August 27, 2025 13:15
@a-tarasyuk a-tarasyuk marked this pull request as draft August 27, 2025 13:15
@a-tarasyuk a-tarasyuk force-pushed the fix/1632 branch 2 times, most recently from 441cca9 to ded3fa0 Compare August 27, 2025 18:42
@a-tarasyuk a-tarasyuk marked this pull request as ready for review August 27, 2025 19:59
@a-tarasyuk a-tarasyuk requested a review from jakebailey August 27, 2025 21:10
Comment on lines 13 to 15
if (1)
else if (2)
else
Copy link
Member

Choose a reason for hiding this comment

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

I commented on the diff (I usually only look at those), but this is the problematic transform I was pointing out.

I suspect this comes down to the emit for if/else/similar needing to ensure that bodies are still printed out if missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jakebailey I’m currently investigating this case

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 seems EmbeddedStatement doesn’t treat NotEmittedStatement as an empty statement the way Strada does.

https://github.com/microsoft/TypeScript/blob/7956c00166df552a1d6da6c2f996b558e5dd94ff/src/compiler/factory/nodeFactory.ts#L3928-L3929

https://github.com/microsoft/TypeScript/blob/7956c00166df552a1d6da6c2f996b558e5dd94ff/src/compiler/factory/nodeFactory.ts#L7163-L7167

As a result, when updating a statement (for example, an if), a NotEmittedStatement is returned and the ; is skipped.

if node.Kind == ast.KindEmptyStatement {
p.emitEmptyStatement(node.AsEmptyStatement(), true /*isEmbeddedStatement*/)
} else {
p.emitStatement(node)
}

Copy link
Member

Choose a reason for hiding this comment

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

So, does that mean the fix should be elsewhere?

@@ -961,3 +962,14 @@ func (c *EmitContext) GetSyntheticTrailingComments(node *ast.Node) []Synthesized
}
return nil
}

func (c *EmitContext) VisitEmbeddedStatement(node *ast.Statement, visitor *ast.NodeVisitor) *ast.Statement {
updated := visitor.VisitEmbeddedStatement(node)
Copy link
Member

Choose a reason for hiding this comment

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

I'm sort of surprised by this, given I thought the default was to do this already...

Copy link
Contributor Author

@a-tarasyuk a-tarasyuk Aug 28, 2025

Choose a reason for hiding this comment

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

It looks like it doesn’t by default

func (v *NodeVisitor) VisitEmbeddedStatement(node *Statement) *Statement {
if node == nil || v.Visit == nil {
return node
}
if v.Visit != nil {
return v.liftToBlock(v.Visit(node))
}
return node
}

func (v *NodeVisitor) visitEmbeddedStatement(node *Node) *Node {
if v.Hooks.VisitEmbeddedStatement != nil {
return v.Hooks.VisitEmbeddedStatement(node, v)
}
if v.Hooks.VisitNode != nil {
return v.liftToBlock(v.Hooks.VisitNode(node, v))
}
return v.VisitEmbeddedStatement(node)
}

I’m not convinced the visitor is the right place for this - WDYT? Also, the change can be implemented via a transformer-level hook or in the emitter.

@a-tarasyuk a-tarasyuk requested a review from jakebailey August 29, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Const enums shouldn't be emitted after inlining
3 participants