Skip to content

Commit

Permalink
object-literal-key-quotes: No need to quote a float if its .toString(…
Browse files Browse the repository at this point in the history
…) is the same. (palantir#2144)
  • Loading branch information
andy-hanson authored and nchen63 committed Jan 31, 2017
1 parent 547f2fe commit 6395aea
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/rules/objectLiteralKeyQuotesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ function quotesAreInconsistent(properties: ts.ObjectLiteralElementLike[]): boole
}

function propertyNeedsQuotes(property: string): boolean {
return !(IDENTIFIER_NAME_REGEX.test(property) || NUMBER_REGEX.test(property) && Number(property).toString() === property);
return !IDENTIFIER_NAME_REGEX.test(property) && Number(property).toString() !== property;
}

// This is simplistic. See https://mothereff.in/js-properties for the gorey details.
const IDENTIFIER_NAME_REGEX = /^(?:[\$A-Z_a-z])+$/;
const NUMBER_REGEX = /^[0-9]+$/;
1 change: 1 addition & 0 deletions test/rules/object-literal-key-quotes/as-needed/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const o = {
1e4: "something",
.123: "float",
123: 'numbers do not need quotes', // failure
1.23: null,
'010': 'but this one does.',
'.123': 'as does this one',
fn() { return },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const o = {
.123: "float",
'123': 'numbers do not need quotes', // failure
~~~~~ [Unnecessarily quoted property '123' found.]
1.23: null,
'010': 'but this one does.',
'.123': 'as does this one',
fn() { return },
Expand Down

0 comments on commit 6395aea

Please sign in to comment.