8000 Keep parentheses for all numeric literals with property access expres… · sweshgit/TypeScript@c3323c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3323c9

Browse files
author
vilicvane
committed
Keep parentheses for all numeric literals with property access expression
1 parent 8d45711 commit c3323c9

9 files changed

+75
-86
lines changed

src/compiler/emitter.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -897,17 +897,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
897897
write(text);
898898
}
899899
}
900-
901-
function getEmittingNumericLiteralText(node: LiteralExpression): string {
902-
let text = getLiteralText(node);
903-
904-
if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) {
905-
return node.text;
906-
}
907-
else {
908-
return text;
909-
}
910-
}
911900

912901
function getLiteralText(node: LiteralExpression) {
913902
// Any template literal or string literal with an extended escape
@@ -2364,7 +2353,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
23642353
operand.kind !== SyntaxKind.NewExpression &&
23652354
!(operand.kind === SyntaxKind.CallExpression && node.parent.kind === SyntaxKind.NewExpression) &&
23662355
!(operand.kind === SyntaxKind.FunctionExpression && node.parent.kind === SyntaxKind.CallExpression) &&
2367-
!(operand.kind === SyntaxKind.NumericLiteral && node.parent.kind === SyntaxKind.PropertyAccessExpression && !/^0[box]|[e.]/i.test(getEmittingNumericLiteralText(<LiteralExpression>operand)))) {
2356+
!(operand.kind === SyntaxKind.NumericLiteral && node.parent.kind === SyntaxKind.PropertyAccessExpression)) {
23682357
emit(operand);
23692358
return;
23702359
}

tests/baselines/reference/castExpressionParentheses.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ declare var a;
88
(<any>"string");
99
(<any>23.0);
1010
(<any>1);
11-
(<any>1.).foo;
12-
(<any>1.0).foo;
13-
(<any>12e+34).foo;
14-
(<any>0xff).foo;
11+
(<any>1.);
12+
(<any>1.0);
13+
(<any>12e+34);
14+
(<any>0xff);
1515
(<any>/regexp/g);
1616
(<any>false);
1717
(<any>true);
@@ -28,8 +28,12 @@ declare var a;
2828
declare var A;
2929

3030
// should keep the parentheses in emit
31-
(<any>1).foo;
32-
(<any>(1.0)).foo;
31+
(<any>1).foo;
32+
(<any>1.).foo;
33+
(<any>1.0).foo;
34+
(<any>12e+34).foo;
35+
(<any>0xff).foo;
36+
(<any>(1.0));
3337
(<any>new A).foo;
3438
(<any>typeof A).x;
3539
(<any>-A).x;
@@ -54,10 +58,10 @@ new (<any>A());
5458
"string";
5559
23.0;
5660
1;
57-
1..foo;
58-
1.0.foo;
59-
12e+34.foo;
60-
0xff.foo;
61+
1.;
62+
1.0;
63+
12e+34;
64+
0xff;
6165
/regexp/g;
6266
false;
6367
true;
@@ -72,7 +76,11 @@ a.b["0"];
7276
a().x;
7377
// should keep the parentheses in emit
7478
(1).foo;
79+
(1.).foo;
7580
(1.0).foo;
81+
(12e+34).foo;
82+
(0xff).foo;
83+
(1.0);
7684
(new A).foo;
7785
(typeof A).x;
7886
(-A).x;

tests/baselines/reference/castExpressionParentheses.symbols

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ declare var a;
1111
(<any>"string");
1212
(<any>23.0);
1313
(<any>1);
14-
(<any>1.).foo;
15-
(<any>1.0).foo;
16-
(<any>12e+34).foo;
17-
(<any>0xff).foo;
14+
(<any>1.);
15+
(<any>1.0);
16+
(<any>12e+34);
17+
(<any>0xff);
1818
(<any>/regexp/g);
1919
(<any>false);
2020
(<any>true);
@@ -41,8 +41,12 @@ declare var A;
4141
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
4242

4343
// should keep the parentheses in emit
44-
(<any>1).foo;
45-
(<any>(1.0)).foo;
44+
(<any>1).foo;
45+
(<any>1.).foo;
46+
(<any>1.0).foo;
47+
(<any>12e+34).foo;
48+
(<any>0xff).foo;
49+
(<any>(1.0));
4650
(<any>new A).foo;
4751
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
4852

@@ -56,10 +60,10 @@ new (<any>A());
5660
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
5761

5862
(<Tany>()=> {})();
59-
>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 35, 2))
63+
>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 39, 2))
6064

6165
(<any>function foo() { })();
62-
>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 36, 6))
66+
>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 40, 6))
6367

6468
(<any><number><any>-A).x;
6569
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))

tests/baselines/reference/castExpressionParentheses.types

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,25 @@ declare var a;
3333
><any>1 : any
3434
>1 : number
3535

36-
(<any>1.).foo;
37-
>(<any>1.).foo : any
36+
(<any>1.);
3837
>(<any>1.) : any
3938
><any>1. : any
4039
>1. : number
41-
>foo : any
4240

43-
(<any>1.0).foo;
44-
>(<any>1.0).foo : any
41+
(<any>1.0);
4542
>(<any>1.0) : any
4643
><any>1.0 : any
4744
>1.0 : number
48-
>foo : any
4945

50-
(<any>12e+34).foo;
51-
>(<any>12e+34).foo : any
46+
(<any>12e+34);
5247
>(<any>12e+34) : any
5348
><any>12e+34 : any
5449
>12e+34 : number
55-
>foo : any
5650

57-
(<any>0xff).foo;
58-
>(<any>0xff).foo : any
51+
(<any>0xff);
5952
>(<any>0xff) : any
6053
><any>0xff : any
6154
>0xff : number
62-
>foo : any
6355

6456
(<any>/regexp/g);
6557
>(<any>/regexp/g) : any
@@ -137,20 +129,46 @@ declare var A;
137129
>A : any
138130

139131
// should keep the parentheses in emit
140-
(<any>1).foo;
132+
(<any>1).foo;
141133
>(<any>1).foo : any
142134
>(<any>1) : any
143135
><any>1 : any
144136
>1 : number
145137
>foo : any
146138

147-
(<any>(1.0)).foo;
148-
>(<any>(1.0)).foo : any
139+
(<any>1.).foo;
140+
>(<any>1.).foo : any
141+
>(<any>1.) : any
142+
><any>1. : any
143+
>1. : number
144+
>foo : any
145+
146+
(<any>1.0).foo;
147+
>(<any>1.0).foo : any
148+
>(<any>1.0) : any
149+
><any>1.0 : any
150+
>1.0 : number
151+
>foo : any
152+
153+
(<any>12e+34).foo;
154+
>(<any>12e+34).foo : any
155+
>(<any>12e+34) : any
156+
><any>12e+34 : any
157+
>12e+34 : number
158+
>foo : any
159+
160+
(<any>0xff).foo;
161+
>(<any>0xff).foo : any
162+
>(<any>0xff) : any
163+
><any>0xff : any
164+
>0xff : number
165+
>foo : any
166+
167+
(<any>(1.0));
149168
>(<any>(1.0)) : any
150169
><any>(1.0) : any
151170
>(1.0) : number
152171
>1.0 : number
153-
>foo : any
154172

155173
(<any>new A).foo;
156174
>(<any>new A).foo : any

tests/baselines/reference/castExpressionParentheses_ES6.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/baselines/reference/castExpressionParentheses_ES6.symbols

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/baselines/reference/castExpressionParentheses_ES6.types

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/cases/compiler/castExpressionParentheses.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ declare var a;
77
(<any>"string");
88
(<any>23.0);
99
(<any>1);
10-
(<any>1.).foo;
11-
(<any>1.0).foo;
12-
(<any>12e+34).foo;
13-
(<any>0xff).foo;
10+
(<any>1.);
11+
(<any>1.0);
12+
(<any>12e+34);
13+
(<any>0xff);
1414
(<any>/regexp/g);
1515
(<any>false);
1616
(<any>true);
@@ -27,8 +27,12 @@ declare var a;
2727
declare var A;
2828

2929
// should keep the parentheses in emit
30-
(<any>1).foo;
31-
(<any>(1.0)).foo;
30+
(<any>1).foo;
31+
(<any>1.).foo;
32+
(<any>1.0).foo;
33+
(<any>12e+34).foo;
34+
(<any>0xff).foo;
35+
(<any>(1.0));
3236
(<any>new A).foo;
3337
(<any>typeof A).x;
3438
(<any>-A).x;

tests/cases/compiler/castExpressionParentheses_ES6.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0