Skip to content

Suggestion: remove need to "only" quote properties that are invalid identifiers #885

Closed
@ianstormtaylor

Description

@ianstormtaylor

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions