Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object-shorthand rule's consistent-as-needed option does not work for certain inputs #7214

Closed
ahuth opened this issue Sep 22, 2016 · 3 comments
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@ahuth
Copy link

ahuth commented Sep 22, 2016

It appears that the object-shorthand rule has a bug. Specifically, the consistent-as-needed option is not functioning properly.

The option is supposed to enforce consistent use of object shorthand syntax per object literal, and to require shorthand syntax when otherwise we would have redundant key/values.

For instance, instead of

const test = {
  a: a,
  b: b
};

we should be required to use

const test = {
  a,
  b
};

The following is still allowed

const test = {
  first: 1,
  second: 2
};

However, when using a quoted string for the key, OR a number for the key, such as:

const test = {
  0: "zero",
  'quoted-string': "quotes"
};

The rule complains that we should be using shorthand. Obviously, that's not possible.

@naomiajacobs and I will submit a PR to resolve this issue.

  • ESLint Version:: 3.3.1
  • Node Version:: 6.4.0
  • npm Version:: 3.10.3

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

Default

Please show your full configuration:

module.exports = {
  extends: 'airbnb',
  rules: {
    'import/no-extraneous-dependencies': [
      'off',
      { devDependencies: true, optionalDependencies: false, peerDependencies: false },
    ],
    'import/no-unresolved': 'off',
    'func-names': 'error',
    'object-shorthand': ['error', 'consistent-as-needed'],
  },
  env: {
    browser: true,
    node: true,
    jasmine: true,
  },
  globals: {
    _: true,
    $: true,
  },
};

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

const example = {
  '-10': 'low',
  0: 'normal',
  10: 'high',
  20: 'critical',
};

What did you expect to happen?

This example is not using redudant property/key values, and should pass the consistent-as-needed option for the object-shorthand rule.

What actually happened? Please include the actual, raw output from ESLint.

error  Expected shorthand for all properties  object-shorthand
@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Sep 22, 2016
naomiajacobs pushed a commit to naomiajacobs/eslint that referenced this issue Sep 22, 2016
@ahuth
Copy link
Author

ahuth commented Sep 22, 2016

Fixed in #7215

@mysticatea mysticatea added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Sep 23, 2016
@mysticatea
Copy link
Member

Thank you for the report and the fix!

I confirmed it.

nzakas pushed a commit that referenced this issue Sep 30, 2016
)

* Fix: object-shorthand's consistent-as-needed option (issue #7214)

* Revise isRedundant and add more specs

* Trigger CLA agreement check again (empty commit)

* Fix typo
@kaicataldo
Copy link
Member

Looks like this was actually fixed by #7215. The reason it didn't close was because the commit message is missing fixes.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

4 participants