From 856f2d895c4f18f1b7c89f4248622788e8c85fd3 Mon Sep 17 00:00:00 2001 From: vilicvane Date: Fri, 4 Sep 2015 21:47:28 +0800 Subject: [PATCH 1/4] Fix issue #4603 --- src/compiler/emitter.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index eeaab6d2123..8a04be99caa 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -897,6 +897,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write(text); } } + + function getEmittingNumericLiteralText(node: LiteralExpression): string { + let text = getLiteralText(node); + + if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) { + return node.text; + } else { + return text; + } + } function getLiteralText(node: LiteralExpression) { // Any template literal or string literal with an extended escape @@ -2352,7 +2362,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi operand.kind !== SyntaxKind.PostfixUnaryExpression && operand.kind !== SyntaxKind.NewExpression && !(operand.kind === SyntaxKind.CallExpression && node.parent.kind === SyntaxKind.NewExpression) && - !(operand.kind === SyntaxKind.FunctionExpression && node.parent.kind === SyntaxKind.CallExpression)) { + !(operand.kind === SyntaxKind.FunctionExpression && node.parent.kind === SyntaxKind.CallExpression) && + !(operand.kind === SyntaxKind.NumericLiteral && node.parent.kind === SyntaxKind.PropertyAccessExpression && !/^0[box]|[e.]/i.test(getEmittingNumericLiteralText(operand)))) { emit(operand); return; } From 32d8a22990612e3616068960042803d63b5b3150 Mon Sep 17 00:00:00 2001 From: vilicvane Date: Fri, 4 Sep 2015 22:15:10 +0800 Subject: [PATCH 2/4] Add tests for issue #4603 --- .../reference/castExpressionParentheses.js | 14 ++++++ .../castExpressionParentheses.symbols | 27 +++++++---- .../reference/castExpressionParentheses.types | 48 +++++++++++++++++++ .../castExpressionParentheses_ES6.js | 11 +++++ .../castExpressionParentheses_ES6.symbols | 7 +++ .../castExpressionParentheses_ES6.types | 11 +++++ .../compiler/castExpressionParentheses.ts | 7 +++ .../compiler/castExpressionParentheses_ES6.ts | 5 ++ 8 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 tests/baselines/reference/castExpressionParentheses_ES6.js create mode 100644 tests/baselines/reference/castExpressionParentheses_ES6.symbols create mode 100644 tests/baselines/reference/castExpressionParentheses_ES6.types create mode 100644 tests/cases/compiler/castExpressionParentheses_ES6.ts diff --git a/tests/baselines/reference/castExpressionParentheses.js b/tests/baselines/reference/castExpressionParentheses.js index ec18d6e373e..22316df7f43 100644 --- a/tests/baselines/reference/castExpressionParentheses.js +++ b/tests/baselines/reference/castExpressionParentheses.js @@ -7,6 +7,11 @@ declare var a; ([1,3,]); ("string"); (23.0); +(1); +(1.).foo; +(1.0).foo; +(12e+34).foo; +(0xff).foo; (/regexp/g); (false); (true); @@ -23,6 +28,8 @@ declare var a; declare var A; // should keep the parentheses in emit +(1).foo; +((1.0)).foo; (new A).foo; (typeof A).x; (-A).x; @@ -46,6 +53,11 @@ new (A()); [1, 3,]; "string"; 23.0; +1; +1..foo; +1.0.foo; +12e+34.foo; +0xff.foo; /regexp/g; false; true; @@ -59,6 +71,8 @@ a[0]; a.b["0"]; a().x; // should keep the parentheses in emit +(1).foo; +(1.0).foo; (new A).foo; (typeof A).x; (-A).x; diff --git a/tests/baselines/reference/castExpressionParentheses.symbols b/tests/baselines/reference/castExpressionParentheses.symbols index 7bc8169e40f..a7f51f9f2cb 100644 --- a/tests/baselines/reference/castExpressionParentheses.symbols +++ b/tests/baselines/reference/castExpressionParentheses.symbols @@ -10,6 +10,11 @@ declare var a; ([1,3,]); ("string"); (23.0); +(1); +(1.).foo; +(1.0).foo; +(12e+34).foo; +(0xff).foo; (/regexp/g); (false); (true); @@ -33,36 +38,38 @@ declare var a; >a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11)) declare var A; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) // should keep the parentheses in emit +(1).foo; +((1.0)).foo; (new A).foo; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) (typeof A).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) (-A).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) new (A()); ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) (()=> {})(); ->Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 28, 2)) +>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 35, 2)) (function foo() { })(); ->foo : Symbol(foo, Decl(castExpressionParentheses.ts, 29, 6)) +>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 36, 6)) (-A).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) // nested cast, should keep one pair of parenthese ((-A)).x; ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) // nested parenthesized expression, should keep one pair of parenthese ((A)) ->A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11)) +>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) diff --git a/tests/baselines/reference/castExpressionParentheses.types b/tests/baselines/reference/castExpressionParentheses.types index ab563a4e8b7..1d203958555 100644 --- a/tests/baselines/reference/castExpressionParentheses.types +++ b/tests/baselines/reference/castExpressionParentheses.types @@ -28,6 +28,39 @@ declare var a; >23.0 : any >23.0 : number +(1); +>(1) : any +>1 : any +>1 : number + +(1.).foo; +>(1.).foo : any +>(1.) : any +>1. : any +>1. : number +>foo : any + +(1.0).foo; +>(1.0).foo : any +>(1.0) : any +>1.0 : any +>1.0 : number +>foo : any + +(12e+34).foo; +>(12e+34).foo : any +>(12e+34) : any +>12e+34 : any +>12e+34 : number +>foo : any + +(0xff).foo; +>(0xff).foo : any +>(0xff) : any +>0xff : any +>0xff : number +>foo : any + (/regexp/g); >(/regexp/g) : any >/regexp/g : any @@ -104,6 +137,21 @@ declare var A; >A : any // should keep the parentheses in emit +(1).foo; +>(1).foo : any +>(1) : any +>1 : any +>1 : number +>foo : any + +((1.0)).foo; +>((1.0)).foo : any +>((1.0)) : any +>(1.0) : any +>(1.0) : number +>1.0 : number +>foo : any + (new A).foo; >(new A).foo : any >(new A) : any diff --git a/tests/baselines/reference/castExpressionParentheses_ES6.js b/tests/baselines/reference/castExpressionParentheses_ES6.js new file mode 100644 index 00000000000..6ffbe7c2885 --- /dev/null +++ b/tests/baselines/reference/castExpressionParentheses_ES6.js @@ -0,0 +1,11 @@ +//// [castExpressionParentheses_ES6.ts] + +// parentheses should be omitted +// numeric literal +(0o123).foo; + + +//// [castExpressionParentheses_ES6.js] +// parentheses should be omitted +// numeric literal +0o123.foo; diff --git a/tests/baselines/reference/castExpressionParentheses_ES6.symbols b/tests/baselines/reference/castExpressionParentheses_ES6.symbols new file mode 100644 index 00000000000..c1b31043cb1 --- /dev/null +++ b/tests/baselines/reference/castExpressionParentheses_ES6.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/castExpressionParentheses_ES6.ts === + +No type information for this code.// parentheses should be omitted +No type information for this code.// numeric literal +No type information for this code.(0o123).foo; +No type information for this code. +No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/castExpressionParentheses_ES6.types b/tests/baselines/reference/castExpressionParentheses_ES6.types new file mode 100644 index 00000000000..41f968a834f --- /dev/null +++ b/tests/baselines/reference/castExpressionParentheses_ES6.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/castExpressionParentheses_ES6.ts === + +// parentheses should be omitted +// numeric literal +(0o123).foo; +>(0o123).foo : any +>(0o123) : any +>0o123 : any +>0o123 : number +>foo : any + diff --git a/tests/cases/compiler/castExpressionParentheses.ts b/tests/cases/compiler/castExpressionParentheses.ts index 93b26ebaf4e..e6ed4d82785 100644 --- a/tests/cases/compiler/castExpressionParentheses.ts +++ b/tests/cases/compiler/castExpressionParentheses.ts @@ -6,6 +6,11 @@ declare var a; ([1,3,]); ("string"); (23.0); +(1); +(1.).foo; +(1.0).foo; +(12e+34).foo; +(0xff).foo; (/regexp/g); (false); (true); @@ -22,6 +27,8 @@ declare var a; declare var A; // should keep the parentheses in emit +(1).foo; +((1.0)).foo; (new A).foo; (typeof A).x; (-A).x; diff --git a/tests/cases/compiler/castExpressionParentheses_ES6.ts b/tests/cases/compiler/castExpressionParentheses_ES6.ts new file mode 100644 index 00000000000..03d6d8ae5fa --- /dev/null +++ b/tests/cases/compiler/castExpressionParentheses_ES6.ts @@ -0,0 +1,5 @@ +// @target: es6 + +// parentheses should be omitted +// numeric literal +(0o123).foo; From 8d457113957da422b5cc88e5149bc0936bc8cf1f Mon Sep 17 00:00:00 2001 From: vilicvane Date: Sat, 5 Sep 2015 06:43:37 +0800 Subject: [PATCH 3/4] Else on the next line --- src/compiler/emitter.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 8a04be99caa..8fcd13c3ada 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -903,7 +903,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) { return node.text; - } else { + } + else { return text; } } From c3323c9a6939d1ba754db50829886472a468b519 Mon Sep 17 00:00:00 2001 From: vilicvane Date: Fri, 11 Sep 2015 03:06:13 +0800 Subject: [PATCH 4/4] Keep parentheses for all numeric literals with property access expression --- src/compiler/emitter.ts | 13 +---- .../reference/castExpressionParentheses.js | 28 +++++++---- .../castExpressionParentheses.symbols | 20 +++++--- .../reference/castExpressionParentheses.types | 50 +++++++++++++------ .../castExpressionParentheses_ES6.js | 11 ---- .../castExpressionParentheses_ES6.symbols | 7 --- .../castExpressionParentheses_ES6.types | 11 ---- .../compiler/castExpressionParentheses.ts | 16 +++--- .../compiler/castExpressionParentheses_ES6.ts | 5 -- 9 files changed, 75 insertions(+), 86 deletions(-) delete mode 100644 tests/baselines/reference/castExpressionParentheses_ES6.js delete mode 100644 tests/baselines/reference/castExpressionParentheses_ES6.symbols delete mode 100644 tests/baselines/reference/castExpressionParentheses_ES6.types delete mode 100644 tests/cases/compiler/castExpressionParentheses_ES6.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 8fcd13c3ada..85607fd73d3 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -897,17 +897,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi write(text); } } - - function getEmittingNumericLiteralText(node: LiteralExpression): string { - let text = getLiteralText(node); - - if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) { - return node.text; - } - else { - return text; - } - } function getLiteralText(node: LiteralExpression) { // Any template literal or string literal with an extended escape @@ -2364,7 +2353,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi operand.kind !== SyntaxKind.NewExpression && !(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 && !/^0[box]|[e.]/i.test(getEmittingNumericLiteralText(operand)))) { + !(operand.kind === SyntaxKind.NumericLiteral && node.parent.kind === SyntaxKind.PropertyAccessExpression)) { emit(operand); return; } diff --git a/tests/baselines/reference/castExpressionParentheses.js b/tests/baselines/reference/castExpressionParentheses.js index 22316df7f43..b4754d0ad41 100644 --- a/tests/baselines/reference/castExpressionParentheses.js +++ b/tests/baselines/reference/castExpressionParentheses.js @@ -8,10 +8,10 @@ declare var a; ("string"); (23.0); (1); -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; +(1.); +(1.0); +(12e+34); +(0xff); (/regexp/g); (false); (true); @@ -28,8 +28,12 @@ declare var a; declare var A; // should keep the parentheses in emit -(1).foo; -((1.0)).foo; +(1).foo; +(1.).foo; +(1.0).foo; +(12e+34).foo; +(0xff).foo; +((1.0)); (new A).foo; (typeof A).x; (-A).x; @@ -54,10 +58,10 @@ new (A()); "string"; 23.0; 1; -1..foo; -1.0.foo; -12e+34.foo; -0xff.foo; +1.; +1.0; +12e+34; +0xff; /regexp/g; false; true; @@ -72,7 +76,11 @@ a.b["0"]; a().x; // should keep the parentheses in emit (1).foo; +(1.).foo; (1.0).foo; +(12e+34).foo; +(0xff).foo; +(1.0); (new A).foo; (typeof A).x; (-A).x; diff --git a/tests/baselines/reference/castExpressionParentheses.symbols b/tests/baselines/reference/castExpressionParentheses.symbols index a7f51f9f2cb..ef31ee0f226 100644 --- a/tests/baselines/reference/castExpressionParentheses.symbols +++ b/tests/baselines/reference/castExpressionParentheses.symbols @@ -11,10 +11,10 @@ declare var a; ("string"); (23.0); (1); -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; +(1.); +(1.0); +(12e+34); +(0xff); (/regexp/g); (false); (true); @@ -41,8 +41,12 @@ declare var A; >A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) // should keep the parentheses in emit -(1).foo; -((1.0)).foo; +(1).foo; +(1.).foo; +(1.0).foo; +(12e+34).foo; +(0xff).foo; +((1.0)); (new A).foo; >A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) @@ -56,10 +60,10 @@ new (A()); >A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) (()=> {})(); ->Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 35, 2)) +>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 39, 2)) (function foo() { })(); ->foo : Symbol(foo, Decl(castExpressionParentheses.ts, 36, 6)) +>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 40, 6)) (-A).x; >A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11)) diff --git a/tests/baselines/reference/castExpressionParentheses.types b/tests/baselines/reference/castExpressionParentheses.types index 1d203958555..cdfbf4f7db2 100644 --- a/tests/baselines/reference/castExpressionParentheses.types +++ b/tests/baselines/reference/castExpressionParentheses.types @@ -33,33 +33,25 @@ declare var a; >1 : any >1 : number -(1.).foo; ->(1.).foo : any +(1.); >(1.) : any >1. : any >1. : number ->foo : any -(1.0).foo; ->(1.0).foo : any +(1.0); >(1.0) : any >1.0 : any >1.0 : number ->foo : any -(12e+34).foo; ->(12e+34).foo : any +(12e+34); >(12e+34) : any >12e+34 : any >12e+34 : number ->foo : any -(0xff).foo; ->(0xff).foo : any +(0xff); >(0xff) : any >0xff : any >0xff : number ->foo : any (/regexp/g); >(/regexp/g) : any @@ -137,20 +129,46 @@ declare var A; >A : any // should keep the parentheses in emit -(1).foo; +(1).foo; >(1).foo : any >(1) : any >1 : any >1 : number >foo : any -((1.0)).foo; ->((1.0)).foo : any +(1.).foo; +>(1.).foo : any +>(1.) : any +>1. : any +>1. : number +>foo : any + +(1.0).foo; +>(1.0).foo : any +>(1.0) : any +>1.0 : any +>1.0 : number +>foo : any + +(12e+34).foo; +>(12e+34).foo : any +>(12e+34) : any +>12e+34 : any +>12e+34 : number +>foo : any + +(0xff).foo; +>(0xff).foo : any +>(0xff) : any +>0xff : any +>0xff : number +>foo : any + +((1.0)); >((1.0)) : any >(1.0) : any >(1.0) : number >1.0 : number ->foo : any (new A).foo; >(new A).foo : any diff --git a/tests/baselines/reference/castExpressionParentheses_ES6.js b/tests/baselines/reference/castExpressionParentheses_ES6.js deleted file mode 100644 index 6ffbe7c2885..00000000000 --- a/tests/baselines/reference/castExpressionParentheses_ES6.js +++ /dev/null @@ -1,11 +0,0 @@ -//// [castExpressionParentheses_ES6.ts] - -// parentheses should be omitted -// numeric literal -(0o123).foo; - - -//// [castExpressionParentheses_ES6.js] -// parentheses should be omitted -// numeric literal -0o123.foo; diff --git a/tests/baselines/reference/castExpressionParentheses_ES6.symbols b/tests/baselines/reference/castExpressionParentheses_ES6.symbols deleted file mode 100644 index c1b31043cb1..00000000000 --- a/tests/baselines/reference/castExpressionParentheses_ES6.symbols +++ /dev/null @@ -1,7 +0,0 @@ -=== tests/cases/compiler/castExpressionParentheses_ES6.ts === - -No type information for this code.// parentheses should be omitted -No type information for this code.// numeric literal -No type information for this code.(0o123).foo; -No type information for this code. -No type information for this code. \ No newline at end of file diff --git a/tests/baselines/reference/castExpressionParentheses_ES6.types b/tests/baselines/reference/castExpressionParentheses_ES6.types deleted file mode 100644 index 41f968a834f..00000000000 --- a/tests/baselines/reference/castExpressionParentheses_ES6.types +++ /dev/null @@ -1,11 +0,0 @@ -=== tests/cases/compiler/castExpressionParentheses_ES6.ts === - -// parentheses should be omitted -// numeric literal -(0o123).foo; ->(0o123).foo : any ->(0o123) : any ->0o123 : any ->0o123 : number ->foo : any - diff --git a/tests/cases/compiler/castExpressionParentheses.ts b/tests/cases/compiler/castExpressionParentheses.ts index e6ed4d82785..06908f39936 100644 --- a/tests/cases/compiler/castExpressionParentheses.ts +++ b/tests/cases/compiler/castExpressionParentheses.ts @@ -7,10 +7,10 @@ declare var a; ("string"); (23.0); (1); -(1.).foo; -(1.0).foo; -(12e+34).foo; -(0xff).foo; +(1.); +(1.0); +(12e+34); +(0xff); (/regexp/g); (false); (true); @@ -27,8 +27,12 @@ declare var a; declare var A; // should keep the parentheses in emit -(1).foo; -((1.0)).foo; +(1).foo; +(1.).foo; +(1.0).foo; +(12e+34).foo; +(0xff).foo; +((1.0)); (new A).foo; (typeof A).x; (-A).x; diff --git a/tests/cases/compiler/castExpressionParentheses_ES6.ts b/tests/cases/compiler/castExpressionParentheses_ES6.ts deleted file mode 100644 index 03d6d8ae5fa..00000000000 --- a/tests/cases/compiler/castExpressionParentheses_ES6.ts +++ /dev/null @@ -1,5 +0,0 @@ -// @target: es6 - -// parentheses should be omitted -// numeric literal -(0o123).foo;