question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add autofix for object-shorthand 'consistent' option

See original GitHub issue

(edit by @not-an-aardvark: See this comment for the updated enhancement proposal.)

Tell us about your environment

  • ESLint Version: 3.10.1
  • Node Version: 6.7.0
  • npm Version: 3.10.3

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

{
  "parserOptions": {
    "ecmaVersion": 6,
  },
  "rules": {
    "object-shorthand": ["error", "consistent"],
  }
}

What did you do? Please include the actual source code causing the issue.

let a = 1;
let b = {
  a,
  hello: 'world',
};

What did you expect to happen? I expected eslint --fix to change the a line to a: a,.

What actually happened? Please include the actual, raw output from ESLint. 2:9 error Unexpected mix of shorthand and non-shorthand properties object-shorthand

When I change the object-shorthand rule from consistent to never, the autofix works. The autofix also fails for consistent-as-needed.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
not-an-aardvarkcommented, Nov 16, 2016

What rule do you want to change?

object-shorthand

Does this change cause the rule to produce more or fewer warnings?

The same amount

How will the change be implemented? (New option, new default behavior, etc.)?

This will change the default behavior for autofixing.

Please provide some example code that this change will affect:

/* eslint object-shorthand: [error, "consistent"] */

({ foo: foo, bar });
/* eslint object-shorthand: [error, "consistent-as-needed"] */

({ foo: foo, bar });

What does the rule currently do for this code?

It reports an error, but does not autofix it.

What will the rule do after it’s changed?

It will autofix the cases to:

/* eslint object-shorthand: [error, "consistent"] */

({ foo: foo, bar: bar });
/* eslint object-shorthand: [error, "consistent-as-needed"] */

({ foo, bar });

In other words, the consistent option will update the object to use longform for all properties. The consistent-as-needed option will use shorthand for all properties if possible, otherwise it will use longform for all properties.

I think this is in line with what the user would expect. The only potential surprise would be with the consistent option using longform for all properties. However, if people want to prefer shorthand for all properties in cases like this, they can simply use consistent-as-needed. (Note that the quote-props fixer already functions like this.)

0reactions
not-an-aardvarkcommented, Aug 14, 2017

Thanks for your interest in improving ESLint. Unfortunately, it looks like this issue didn’t get enough support from the team and so I’m closing it. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach consensus after a long time tend to never do it, and as such, we close those issues. This doesn’t mean the idea isn’t interesting, just that it’s not something the team can commit to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

object-shorthand - ESLint - Pluggable JavaScript Linter
"consistent-as-needed" ensures that either all shorthand or all long-form will be used in an object literal, but ensures all shorthand whenever possible.
Read more >
ESLint v2.10.0 released - ESLint中文文档
ESLint v2.10.0 released. We just pushed ESLint v2.10.0, which is a minor release upgrade of ESLint. This release adds some new features and...
Read more >
tslint: benefit of "object-literal-shorthand" - Stack Overflow
I guess the rule allows 1. to make you aware of this shorthand syntax 2. to promote consistency in naming by encouraging you...
Read more >
tslint-eslint-rules - npm Package Health Analysis | Snyk
In your tslint.json file, insert the rules as described below. ... Not applicable, disallow duplicate keys when creating object literals (recommended).
Read more >
eslint-plugin-vue - Awesome JS
#2045 Fixed vue/attribute-hyphenation rule to disable autofix for attributes ... #1982 Added consistent option to vue/padding-line-between-tags rule.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found