diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index c93eef5b065..5fe5027978c 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -2578,7 +2578,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge operand = (operand).expression; } - // We have an expression of the form: (SubExpr) + // We have an expression of the form: (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.: @@ -2592,6 +2592,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)) { diff --git a/tests/baselines/reference/asOpEmitParens.js b/tests/baselines/reference/asOpEmitParens.js new file mode 100644 index 00000000000..dee8d263eae --- /dev/null +++ b/tests/baselines/reference/asOpEmitParens.js @@ -0,0 +1,19 @@ +//// [asOpEmitParens.ts] +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); + + +//// [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()); diff --git a/tests/baselines/reference/asOpEmitParens.symbols b/tests/baselines/reference/asOpEmitParens.symbols new file mode 100644 index 00000000000..210d9f3ea0d --- /dev/null +++ b/tests/baselines/reference/asOpEmitParens.symbols @@ -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)) + diff --git a/tests/baselines/reference/asOpEmitParens.types b/tests/baselines/reference/asOpEmitParens.types new file mode 100644 index 00000000000..b87d7f4d91f --- /dev/null +++ b/tests/baselines/reference/asOpEmitParens.types @@ -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 + diff --git a/tests/baselines/reference/umdGlobalMerge.errors.txt b/tests/baselines/reference/umdGlobalMerge.errors.txt deleted file mode 100644 index eb4a116ef09..00000000000 --- a/tests/baselines/reference/umdGlobalMerge.errors.txt +++ /dev/null @@ -1,20 +0,0 @@ -tests/cases/compiler/b.d.ts(2,20): error TS2305: Module '"tests/cases/compiler/a".ns' has no exported member 'IFoo'. - - -==== tests/cases/compiler/a.d.ts (0 errors) ==== - export = ns; - - export as namespace ns; - - declare namespace ns { - export var x: number; - export interface IFoo { } - } - -==== tests/cases/compiler/b.d.ts (1 errors) ==== - declare namespace ns.something { - export var p: ns.IFoo; - ~~~~ -!!! error TS2305: Module '"tests/cases/compiler/a".ns' has no exported member 'IFoo'. - } - \ No newline at end of file diff --git a/tests/cases/compiler/umdGlobalMerge.ts b/tests/cases/compiler/umdGlobalMerge.ts deleted file mode 100644 index 1f42e2fda71..00000000000 --- a/tests/cases/compiler/umdGlobalMerge.ts +++ /dev/null @@ -1,14 +0,0 @@ -// @filename: a.d.ts -export = ns; - -export as namespace ns; - -declare namespace ns { - export var x: number; - export interface IFoo { } -} - -// @filename: b.d.ts -declare namespace ns.something { - export var p: ns.IFoo; -} diff --git a/tests/cases/conformance/expressions/asOperator/asOpEmitParens.ts b/tests/cases/conformance/expressions/asOperator/asOpEmitParens.ts new file mode 100644 index 00000000000..7a97a74168a --- /dev/null +++ b/tests/cases/conformance/expressions/asOperator/asOpEmitParens.ts @@ -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);