From 6395aeaee1d8431797193254a724c84b939b99c4 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Mon, 30 Jan 2017 18:03:35 -0800 Subject: [PATCH] object-literal-key-quotes: No need to quote a float if its .toString() is the same. (#2144) --- src/rules/objectLiteralKeyQuotesRule.ts | 3 +-- test/rules/object-literal-key-quotes/as-needed/test.ts.fix | 1 + test/rules/object-literal-key-quotes/as-needed/test.ts.lint | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rules/objectLiteralKeyQuotesRule.ts b/src/rules/objectLiteralKeyQuotesRule.ts index c13491834a8..9be27c2a570 100644 --- a/src/rules/objectLiteralKeyQuotesRule.ts +++ b/src/rules/objectLiteralKeyQuotesRule.ts @@ -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]+$/; diff --git a/test/rules/object-literal-key-quotes/as-needed/test.ts.fix b/test/rules/object-literal-key-quotes/as-needed/test.ts.fix index 97192e27cff..9dd05ac5201 100644 --- a/test/rules/object-literal-key-quotes/as-needed/test.ts.fix +++ b/test/rules/object-literal-key-quotes/as-needed/test.ts.fix @@ -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 }, diff --git a/test/rules/object-literal-key-quotes/as-needed/test.ts.lint b/test/rules/object-literal-key-quotes/as-needed/test.ts.lint index f0171420b1b..4a7facdccfe 100644 --- a/test/rules/object-literal-key-quotes/as-needed/test.ts.lint +++ b/test/rules/object-literal-key-quotes/as-needed/test.ts.lint @@ -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 },