Skip to content

Do not omit parentheses for numeric literal with ".property" if it can not be followed by dot operator directly. #4641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 @@ -2352,7 +2352,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)) {
emit(operand);
return;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/castExpressionParentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ declare var a;
(<any>[1,3,]);
(<any>"string");
(<any>23.0);
(<any>1);
(<any>1.);
(<any>1.0);
(<any>12e+34);
(<any>0xff);
(<any>/regexp/g);
(<any>false);
(<any>true);
Expand All @@ -23,6 +28,12 @@ declare var a;
declare var A;

// should keep the parentheses in emit
(<any>1).foo;
(<any>1.).foo;
(<any>1.0).foo;
(<any>12e+34).foo;
(<any>0xff).foo;
(<any>(1.0));
(<any>new A).foo;
(<any>typeof A).x;
(<any>-A).x;
Expand All @@ -46,6 +57,11 @@ new (<any>A());
[1, 3,];
"string";
23.0;
1;
1.;
1.0;
12e+34;
0xff;
/regexp/g;
false;
true;
Expand All @@ -59,6 +75,12 @@ a[0];
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;
Expand Down
31 changes: 21 additions & 10 deletions tests/baselines/reference/castExpressionParentheses.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ declare var a;
(<any>[1,3,]);
(<any>"string");
(<any>23.0);
(<any>1);
(<any>1.);
(<any>1.0);
(<any>12e+34);
(<any>0xff);
(<any>/regexp/g);
(<any>false);
(<any>true);
Expand All @@ -33,36 +38,42 @@ 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
(<any>1).foo;
(<any>1.).foo;
(<any>1.0).foo;
(<any>12e+34).foo;
(<any>0xff).foo;
(<any>(1.0));
(<any>new A).foo;
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))

(<any>typeof A).x;
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))

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

new (<any>A());
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))

(<Tany>()=> {})();
>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 28, 2))
>Tany : Symbol(Tany, Decl(castExpressionParentheses.ts, 39, 2))

(<any>function foo() { })();
>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 29, 6))
>foo : Symbol(foo, Decl(castExpressionParentheses.ts, 40, 6))

(<any><number><any>-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
(<any><number>(<any>-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
(<any>(A))
>A : Symbol(A, Decl(castExpressionParentheses.ts, 21, 11))
>A : Symbol(A, Decl(castExpressionParentheses.ts, 26, 11))


66 changes: 66 additions & 0 deletions tests/baselines/reference/castExpressionParentheses.types
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ declare var a;
><any>23.0 : any
>23.0 : number

(<any>1);
>(<any>1) : any
><any>1 : any
>1 : number

(<any>1.);
>(<any>1.) : any
><any>1. : any
>1. : number

(<any>1.0);
>(<any>1.0) : any
><any>1.0 : any
>1.0 : number

(<any>12e+34);
>(<any>12e+34) : any
><any>12e+34 : any
>12e+34 : number

(<any>0xff);
>(<any>0xff) : any
><any>0xff : any
>0xff : number

(<any>/regexp/g);
>(<any>/regexp/g) : any
><any>/regexp/g : any
Expand Down Expand Up @@ -104,6 +129,47 @@ declare var A;
>A : any

// should keep the parentheses in emit
(<any>1).foo;
>(<any>1).foo : any
>(<any>1) : any
><any>1 : any
>1 : number
>foo : any

(<any>1.).foo;
>(<any>1.).foo : any
>(<any>1.) : any
><any>1. : any
>1. : number
>foo : any

(<any>1.0).foo;
>(<any>1.0).foo : any
>(<any>1.0) : any
><any>1.0 : any
>1.0 : number
>foo : any

(<any>12e+34).foo;
>(<any>12e+34).foo : any
>(<any>12e+34) : any
><any>12e+34 : any
>12e+34 : number
>foo : any

(<any>0xff).foo;
>(<any>0xff).foo : any
>(<any>0xff) : any
><any>0xff : any
>0xff : number
>foo : any

(<any>(1.0));
>(<any>(1.0)) : any
><any>(1.0) : any
>(1.0) : number
>1.0 : number

(<any>new A).foo;
>(<any>new A).foo : any
>(<any>new A) : any
Expand Down
11 changes: 11 additions & 0 deletions tests/cases/compiler/castExpressionParentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ declare var a;
(<any>[1,3,]);
(<any>"string");
(<any>23.0);
(<any>1);
(<any>1.);
(<any>1.0);
(<any>12e+34);
(<any>0xff);
(<any>/regexp/g);
(<any>false);
(<any>true);
Expand All @@ -22,6 +27,12 @@ declare var a;
declare var A;

// should keep the parentheses in emit
(<any>1).foo;
(<any>1.).foo;
(<any>1.0).foo;
(<any>12e+34).foo;
(<any>0xff).foo;
(<any>(1.0));
(<any>new A).foo;
(<any>typeof A).x;
(<any>-A).x;
Expand Down