Skip to content

Commit bed25dd

Browse files
Merge pull request #4641 from vilic/cast-expression-parentheses-with-numeric-literal
Do not omit parentheses for numeric literal with ".property" if it can not be followed by dot operator directly.
2 parents 87890af + c3323c9 commit bed25dd

File tree

5 files changed

+122
-11
lines changed

5 files changed

+122
-11
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
23542354
operand.kind !== SyntaxKind.PostfixUnaryExpression &&
23552355
operand.kind !== SyntaxKind.NewExpression &&
23562356
!(operand.kind === SyntaxKind.CallExpression && node.parent.kind === SyntaxKind.NewExpression) &&
2357-
!(operand.kind === SyntaxKind.FunctionExpression && node.parent.kind === SyntaxKind.CallExpression)) {
2357+
!(operand.kind === SyntaxKind.FunctionExpression && node.parent.kind === SyntaxKind.CallExpression) &&
2358+
!(operand.kind === SyntaxKind.NumericLiteral && node.parent.kind === SyntaxKind.PropertyAccessExpression)) {
23582359
emit(operand);
23592360
return;
23602361
}

tests/baselines/reference/castExpressionParentheses.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ declare var a;
77
(<any>[1,3,]);
88
(<any>"string");
99
(<any>23.0);
10+
(<any>1);
11+
(<any>1.);
12+
(<any>1.0);
13+
(<any>12e+34);
14+
(<any>0xff);
1015
(<any>/regexp/g);
1116
(<any>false);
1217
(<any>true);
@@ -23,6 +28,12 @@ declare var a;
2328
declare var A;
2429

2530
// should keep the parentheses in emit
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));
2637
(<any>new A).foo;
2738
(<any>typeof A).x;
2839
(<any>-A).x;
@@ -46,6 +57,11 @@ new (<any>A());
4657
[1, 3,];
4758
"string";
4859
23.0;
60+
1;
61+
1.;
62+
1.0;
63+
12e+34;
64+
0xff;
4965
/regexp/g;
5066
false;
5167
true;
@@ -59,6 +75,12 @@ a[0];
5975
a.b["0"];
6076
a().x;
6177
// should keep the parentheses in emit
78+
(1).foo;
79+
(1.).foo;
80+
(1.0).foo;
81+
(12e+34).foo;
82+
(0xff).foo;
83+
(1.0);
6284
(new A).foo;
6385
(typeof A).x;
6486
(-A).x;

tests/baselines/reference/castExpressionParentheses.symbols

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ declare var a;
1010
(<any>[1,3,]);
1111
(<any>"string");
1212
(<any>23.0);
13+
(<any>1);
14+
(<any>1.);
15+
(<any>1.0);
16+
(<any>12e+34);
17+
(<any>0xff);
1318
(<any>/regexp/g);
1419
(<any>false);
1520
(<any>true);
@@ -33,36 +38,42 @@ declare var a;
3338
>a : Symbol(a, Decl(castExpressionParentheses.ts, 0, 11))
3439

3540
declare var A;
36-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
41+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
3742

3843
// should keep the parentheses in emit
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));
3950
(<any>new A).foo;
40-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
51+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
4152

4253
(<any>typeof A).x;
43-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
54+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
4455

4556
(<any>-A).x;
46-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
57+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
4758

4859
new (<any>A());
49-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
60+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
5061

5162
(<Tany>()=> {})();
52-
>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 28, 2))
63+
>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 39, 2))
5364

5465
(<any>function foo() { })();
55-
>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 29, 6))
66+
>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 40, 6))
5667

5768
(<any><number><any>-A).x;
58-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
69+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
5970

6071
// nested cast, should keep one pair of parenthese
6172
(<any><number>(<any>-A)).x;
62-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
73+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
6374

6475
// nested parenthesized expression, should keep one pair of parenthese
6576
(<any>(A))
66-
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
77+
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))
6778

6879

tests/baselines/reference/castExpressionParentheses.types

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,31 @@ declare var a;
2828
><any>23.0 : any
2929
>23.0 : number
3030

31+
(<any>1);
32+
>(<any>1) : any
33+
><any>1 : any
34+
>1 : number
35+
36+
(<any>1.);
37+
>(<any>1.) : any
38+
><any>1. : any
39+
>1. : number
40+
41+
(<any>1.0);
42+
>(<any>1.0) : any
43+
><any>1.0 : any
44+
>1.0 : number
45+
46+
(<any>12e+34);
47+
>(<any>12e+34) : any
48+
><any>12e+34 : any
49+
>12e+34 : number
50+
51+
(<any>0xff);
52+
>(<any>0xff) : any
53+
><any>0xff : any
54+
>0xff : number
55+
3156
(<any>/regexp/g);
3257
>(<any>/regexp/g) : any
3358
><any>/regexp/g : any
@@ -104,6 +129,47 @@ declare var A;
104129
>A : any
105130

106131
// should keep the parentheses in emit
132+
(<any>1).foo;
133+
>(<any>1).foo : any
134+
>(<any>1) : any
135+
><any>1 : any
136+
>1 : number
137+
>foo : any
138+
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));
168+
>(<any>(1.0)) : any
169+
><any>(1.0) : any
170+
>(1.0) : number
171+
>1.0 : number
172+
107173
(<any>new A).foo;
108174
>(<any>new A).foo : any
109175
>(<any>new A) : any

tests/cases/compiler/castExpressionParentheses.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ declare var a;
66
(<any>[1,3,]);
77
(<any>"string");
88
(<any>23.0);
9+
(<any>1);
10+
(<any>1.);
11+
(<any>1.0);
12+
(<any>12e+34);
13+
(<any>0xff);
914
(<any>/regexp/g);
1015
(<any>false);
1116
(<any>true);
@@ -22,6 +27,12 @@ declare var a;
2227
declare var A;
2328

2429
// should keep the parentheses in emit
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));
2536
(<any>new A).foo;
2637
(<any>typeof A).x;
2738
(<any>-A).x;

0 commit comments

Comments
 (0)