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

Add prefer-object-define-properties rule #1843

Closed
wants to merge 25 commits into from
Closed

Add prefer-object-define-properties rule #1843

wants to merge 25 commits into from

Conversation

Slowlife01
Copy link

Added prefer-object-define-properties rule, resolves #1729

Might need more test cases and better description
And unsure if fixer can be improved (formatting the output etc)

@Slowlife01
Copy link
Author

Slowlife01 commented Jun 20, 2022

Should I check if they are in the same scope?
Updated

@Slowlife01
Copy link
Author

I tried to update the snapshot but it doesn't seems to work

@fisker
Copy link
Collaborator

fisker commented Jun 21, 2022

Should I check if they are in the same scope?

Shouldn't they next to each other?

@Slowlife01
Copy link
Author

Slowlife01 commented Jun 21, 2022

Should I check if they are in the same scope?

Shouldn't they next to each other?

Oh right.
How should I check this? Just the ranges? Or if there is something in between them? (although I don't know if that's hard)

@fisker
Copy link
Collaborator

fisker commented Jun 27, 2022

This is how I did in other rule

function getFirstExpression(node, sourceCode) {

@Slowlife01
Copy link
Author

Putting this on draft for now

@Slowlife01 Slowlife01 marked this pull request as draft June 29, 2022 07:49
@Slowlife01 Slowlife01 requested a review from fisker July 3, 2022 11:34
@Slowlife01 Slowlife01 marked this pull request as ready for review July 15, 2022 02:34
return (
node
&& node.type === 'CallExpression'
&& node.callee.type === 'MemberExpression'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& node.callee.type === 'MemberExpression'
&& node.callee.type === 'MemberExpression'
&& !node.computed

Object[defineProperty](...)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test for it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Slowlife01 Slowlife01 requested a review from fisker September 16, 2022 02:08
@Slowlife01 Slowlife01 requested a review from fisker September 16, 2022 05:06
@fisker
Copy link
Collaborator

fisker commented Sep 17, 2022

@Slowlife01

I made some changes. I hope it's easier to understand the code.

Mainly changes:

  1. Instead of grouping calls. It only checks two sibling calls.
  2. Relax the node type check, not only Identifier/Literal for property
  3. Fix the fix logic for some untested node types. (eg, use ...descriptors)

This not done yet.

  1. Need more test for the stricter isObjectDefinePropertyOrObjectDefineProperties
  2. The "secondCallExpression" removal is not correct, it's tricky for ASI problem.

Maybe other isssues, I'll review again later.

return descriptors.properties.map(property => sourceCode.getText(property)).join(',\n');
}

return `...(${sourceCode.getText(descriptors)})`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need add tests, and should do isParenthesized check first.


const MESSAGE_ID = 'prefer-object-define-properties';
const messages = {
[MESSAGE_ID]: 'Prefer `{{replacement}}` over multiple `{{value}}`.',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message may cause confusing, since the "secondCallExpression" can be Object.defineProperties, we need improve it.

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*
<!-- /RULE_NOTICE -->

When defining more than one properties, [Object.defineProperties](https://mdn.io/Object.defineProperties) should be preferred.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When defining more than one properties, [Object.defineProperties](https://mdn.io/Object.defineProperties) should be preferred.
When defining more than one property, [Object.defineProperties](https://mdn.io/Object.defineProperties) should be preferred.

@Slowlife01
Copy link
Author

I won't be able to finish this now

@Slowlife01 Slowlife01 closed this Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule proposal: prefer-object-define-properties
3 participants