Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2553,7 +2553,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
operand = (<TypeAssertion | NonNullExpression>operand).expression;
}

// We have an expression of the form: (<Type>SubExpr)
// We have an expression of the form: (<Type>SubExpr) or (SubExpr as Type)
// Emitting this as (SubExpr) is really not desirable. We would like to emit the subexpr as is.
// Omitting the parentheses, however, could cause change in the semantics of the generated
// code if the casted expression has a lower precedence than the rest of the expression, e.g.:
Expand All @@ -2567,6 +2567,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
operand.kind !== SyntaxKind.DeleteExpression &&
operand.kind !== SyntaxKind.PostfixUnaryExpression &&
operand.kind !== SyntaxKind.NewExpression &&
!(operand.kind === SyntaxKind.BinaryExpression && node.expression.kind === SyntaxKind.AsExpression) &&
!(operand.kind === SyntaxKind.CallExpression && node.parent.kind === SyntaxKind.NewExpression) &&
!(operand.kind === SyntaxKind.FunctionExpression && node.parent.kind === SyntaxKind.CallExpression) &&
!(operand.kind === SyntaxKind.NumericLiteral && node.parent.kind === SyntaxKind.PropertyAccessExpression)) {
Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/asOpEmitParens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [asOpEmitParens.ts]
declare var x;
// Must emit as (x + 1) * 3
(x + 1 as number) * 3;

@weswigham weswigham Jul 15, 2016

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The type assertion is still in the output.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're looking at the wrong part of the baseline (this is the original .ts). Its corresponding emitted line is 15

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, derp

On Fri, Jul 15, 2016, 5:54 PM Ryan Cavanaugh notifications@github.com
wrote:

In tests/baselines/reference/asOpEmitParens.js
#9767 (comment):

@@ -0,0 +1,19 @@
+//// [asOpEmitParens.ts]
+declare var x;
+// Must emit as (x + 1) * 3
+(x + 1 as number) * 3;

You're looking at the wrong part of the baseline (this is the original
.ts). Its corresponding emitted line is 15


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Microsoft/TypeScript/pull/9767/files/40ca4a06b55d9493a7ed45bd92543a0a14a7ac3a#r71057592,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACzAMnkNN2tSPIQlLgSHEVrLTCD7tddLks5qWCvCgaJpZM4JN2j9
.


// Should still emit as x.y
(x as any).y;

// Emit as new (x())
new (x() as any);


//// [asOpEmitParens.js]
// Must emit as (x + 1) * 3
(x + 1) * 3;
// Should still emit as x.y
x.y;
// Emit as new (x())
new (x());
16 changes: 16 additions & 0 deletions tests/baselines/reference/asOpEmitParens.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/expressions/asOperator/asOpEmitParens.ts ===
declare var x;
>x : Symbol(x, Decl(asOpEmitParens.ts, 0, 11))

// Must emit as (x + 1) * 3
(x + 1 as number) * 3;
>x : Symbol(x, Decl(asOpEmitParens.ts, 0, 11))

// Should still emit as x.y
(x as any).y;
>x : Symbol(x, Decl(asOpEmitParens.ts, 0, 11))

// Emit as new (x())
new (x() as any);
>x : Symbol(x, Decl(asOpEmitParens.ts, 0, 11))

30 changes: 30 additions & 0 deletions tests/baselines/reference/asOpEmitParens.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/conformance/expressions/asOperator/asOpEmitParens.ts ===
declare var x;
>x : any

// Must emit as (x + 1) * 3
(x + 1 as number) * 3;
>(x + 1 as number) * 3 : number
>(x + 1 as number) : number
>x + 1 as number : number
>x + 1 : any
>x : any
>1 : number
>3 : number

// Should still emit as x.y
(x as any).y;
>(x as any).y : any
>(x as any) : any
>x as any : any
>x : any
>y : any

// Emit as new (x())
new (x() as any);
>new (x() as any) : any
>(x() as any) : any
>x() as any : any
>x() : any
>x : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare var x;
// Must emit as (x + 1) * 3
(x + 1 as number) * 3;

// Should still emit as x.y
(x as any).y;

// Emit as new (x())
new (x() as any);