Skip to content

Commit c67bb5e

Browse files
authored
Elide parens around assertions during emit (microsoft#441)
1 parent 1e36258 commit c67bb5e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

internal/transformers/typeeraser.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
199199
// !!! Use PartiallyEmittedExpression to preserve comments
200200
return tx.visitor.VisitNode(node.AsSatisfiesExpression().Expression)
201201

202+
case ast.KindParenthesizedExpression:
203+
n := node.AsParenthesizedExpression()
204+
expression := ast.SkipOuterExpressions(n.Expression, ^(ast.OEKTypeAssertions | ast.OEKExpressionsWithTypeArguments))
205+
if ast.IsAssertionExpression(expression) || ast.IsSatisfiesExpression(expression) {
206+
// !!! Use PartiallyEmittedExpression to preserve comments
207+
return tx.visitor.VisitNode(n.Expression)
208+
}
209+
return tx.visitor.VisitEachChild(node)
210+
202211
case ast.KindJsxSelfClosingElement:
203212
n := node.AsJsxSelfClosingElement()
204213
return tx.factory.UpdateJsxSelfClosingElement(n, tx.visitor.VisitNode(n.TagName), nil, tx.visitor.VisitNode(n.Attributes))

internal/transformers/typeeraser_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ func TestTypeEraser(t *testing.T) {
5858
{title: "NewExpression2", input: "new f<T>", output: "new f;"},
5959
{title: "TaggedTemplateExpression", input: "f<T>``", output: "f ``;"},
6060
{title: "NonNullExpression", input: "x!", output: "x;"},
61-
{title: "TypeAssertionExpression", input: "<T>x", output: "x;"},
62-
{title: "AsExpression", input: "x as T", output: "x;"},
63-
{title: "SatisfiesExpression", input: "x satisfies T", output: "x;"},
61+
{title: "TypeAssertionExpression#1", input: "<T>x", output: "x;"},
62+
{title: "TypeAssertionExpression#2", input: "(<T>x).c", output: "x.c;"},
63+
{title: "AsExpression#1", input: "x as T", output: "x;"},
64+
{title: "AsExpression#2", input: "(x as T).c", output: "x.c;"},
65+
{title: "SatisfiesExpression#1", input: "x satisfies T", output: "x;"},
66+
{title: "SatisfiesExpression#2", input: "(x satisfies T).c", output: "x.c;"},
6467
{title: "JsxSelfClosingElement", input: "<x<T> />", output: "<x />;", jsx: true},
6568
{title: "JsxOpeningElement", input: "<x<T>></x>", output: "<x></x>;", jsx: true},
6669
{title: "ImportEqualsDeclaration#1", input: "import x = require(\"m\");", output: "import x = require(\"m\");"},

0 commit comments

Comments
 (0)