eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: `prefer-object-define-properties`

Open Pyrolistical opened this issue 2 years ago • 6 comments

Description

I didn't know Object.defineProperties existed. Would have been nice for a rule to teach me as I wrote consecutive Object.defineProperty.

Fail

Object.defineProperty(matcher, 'logger', {
  value: () => {},
  writable: true,
})
Object.defineProperty(matcher, 'builder', {
  value: (value) => value.map(({ build }) => build()),
  writable: true,
})

Pass

Object.defineProperties(matcher, {
  logger: {
    value: () => {},
    writable: true,
  },
  builder: {
    value: (value) => value.map(({ build }) => build()),
    writable: true,
  }
})

Pyrolistical avatar Feb 11 '22 04:02 Pyrolistical

If there are many descriptors, normally it's a loop, if there are not many descriptors, I don't think they make much difference. But I'm fine adding this rule, just don't think it have much value.

fisker avatar Feb 11 '22 04:02 fisker

If we decide to add this rule, we should also check Reflect.defineProperty

fisker avatar Feb 11 '22 04:02 fisker

Another reason to reject this rule, it would interact poorly with https://github.com/microsoft/TypeScript/issues/41424

Pyrolistical avatar Feb 11 '22 06:02 Pyrolistical

Another reason to reject this rule, it would interact poorly with https://github.com/microsoft/TypeScript/issues/41424

This plugin targets JavaScript. TypeScript weaknesses is not a concern. TypeScript users can just disable this rule.

sindresorhus avatar Mar 01 '22 13:03 sindresorhus

I think if this is going to be useful, it needs an auto-fix. Manually converting to Object.defineProperties would not be worth the effort.

sindresorhus avatar Mar 01 '22 13:03 sindresorhus

Accepted

sindresorhus avatar Mar 01 '22 13:03 sindresorhus