Description
Right now the style guide recommends:
3.8 Only quote properties that are invalid identifiers.
Why? In general we consider it subjectively easier to read. It improves syntax highlighting, and is also more easily optimized by many JS engines.
// bad const bad = { 'foo': 3, 'bar': 4, 'data-blah': 5, }; // good const good = { foo: 3, bar: 4, 'data-blah': 5, };
But this is too strict for common sense situations where it's nicer to leave all properties quoted. For example, if you are dealing with an object whose properties always abide by slug-casing
, then it's less confusing to actually quote all of them, instead of alternating between quoted any unquoted.
A live example of this actually already exists in this very repository 😄 when defining the ESLint rules in Javascript: https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb-base/rules/best-practices.js#L14
... // treat var statements as if they were block scoped 'block-scoped-var': 2, // specify the maximum cyclomatic complexity allowed in a program 'complexity': [0, 11], // require return statements to either always or never specify values 'consistent-return': 2, ...
Going by the current style guide, complexity
should not be quoted, but that would actually make the file more less symmetrical and thus a bit more tedious to maintain.
Instead, I think it should mention that maintaining a consistent quoting scheme is recommended when the casing of the properties is clearly defined, and doesn't lend itself to being unquoted.