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

Conversation

vilicvane
Copy link

Fix issue #4603.

Added a function getEmittingNumericLiteralText as a helper of determine whether we want to keep the parentheses, as the emitted numeric literal maybe different under different target.

E.g., 0o123 emits 83 (which needs the parentheses) under ES3/5, but emits 0o123 under ES6.

@msftclas
Copy link

msftclas commented Sep 4, 2015

Hi @vilic, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!
We will now validate the agreement and then real humans will evaluate your PR.

TTYL, MSBOT;


if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) {
return node.text;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else on the next line

@DanielRosenwasser
Copy link
Member

Personally, I'd rather we just always emit the parens. Thoughts @mhegazy?

@mhegazy
Copy link
Contributor

mhegazy commented Sep 4, 2015

We try to emit clean code that you would write by hand. and i do not think you will always parenthesize your numeric literals.

@@ -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(<LiteralExpression>operand)))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't cover when your numeric literal consists of escape sequences.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice there could be escape sequences in a numeric literal. http://www.ecma-international.org/ecma-262/6.0/#sec-literals-numeric-literals

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser So what do you mean by "numeric literal consists of escape sequences"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can literally write var x = \u0032\u002e\u0030 instead of var x = 2.0 and both are valid and ECMAScript code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I stand corrected - the escapes can't be used in numeric literals. From 10.1 - Source Text:

In string literals, regular expression literals, template literals and identifiers, any Unicode code point may also be expressed using Unicode escape sequences that explicitly express a code point’s numeric value. Within a comment, such an escape sequence is effectively ignored as part of the comment.

But as you mentioned, it doesn't cover numeric literals.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser
Copy link
Member

@mhegazy I'd rather we just didn't special case this. 2.0.toString() doesn't seem more canonical to me than (2).toString()

@mhegazy
Copy link
Contributor

mhegazy commented Sep 10, 2015

so @DanielRosenwasser what is the proposal, just check if the expression is numeric literal, and the parent is property access expression? this sound fine by me. @vilic, any concerns?

@DanielRosenwasser
Copy link
Member

@mhegazy that sounds fine, or maybe even just don't strip parens from a numeric literal in a type assertion because it's likely going to have a property access for whatever reason anyway. Either is fine with me.

@vilicvane
Copy link
Author

@mhegazy @DanielRosenwasser I am cool with both. But I prefer keeping property access as a condition though other situations might be rare.

@DanielRosenwasser
Copy link
Member

👍

DanielRosenwasser added a commit that referenced this pull request Sep 10, 2015
…numeric-literal

Do not omit parentheses for numeric literal with ".property" if it can not be followed by dot operator directly.
@DanielRosenwasser DanielRosenwasser merged commit bed25dd into microsoft:master Sep 10, 2015
@DanielRosenwasser
Copy link
Member

Thanks @vilic!

@vilicvane vilicvane deleted the cast-expression-parentheses-with-numeric-literal branch September 10, 2015 19:16
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants