+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): noDuplicateClassMembers #4137

Merged
merged 23 commits into from
Feb 3, 2023

Conversation

nissy-dev
Copy link
Contributor

@nissy-dev nissy-dev commented Jan 4, 2023

Summary

Fix #3963

Test Plan

I made the same test cases as in ESLint. In this PR, this rule doesn't support computed and numerical properties.

I refer to the following comments.

// This particular simple computed property case with just a string literal would be easy to catch,
// but we don't want to open Pandora's static analysis box so we have to draw a line somewhere
({ a: 1, ["a"]: 1 });

Documentation

  • The PR requires documentation
  • I will create a new PR to update the documentation

@nissy-dev nissy-dev requested review from leops, ematipico, xunilrj and a team as code owners January 4, 2023 13:59
@netlify
Copy link

netlify bot commented Jan 4, 2023

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit aa6e194
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/63dd20bd5f196c0008c87ce0
😎 Deploy Preview https://deploy-preview-4137--docs-rometools.netlify.app/playground
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@Conaclos
Copy link
Contributor

Conaclos commented Jan 4, 2023

It could be nice to have an action for removing the overwritten member (Similarly to noDuplicateObjectKeys). You are free to take inspiration from my closed PR.

@nissy-dev
Copy link
Contributor Author

nissy-dev commented Jan 5, 2023

It could be nice to have an action for removing the overwritten member (Similarly to noDuplicateObjectKeys). You are free to take inspiration from my #4130.

Thank for your suggestion. I think the action for removing the overwritten member is not needed. This is because it is not enough to delete the last overwritten member about duplicated members . In some cases, users want to delete the member defined firstly. This is not up to us.

ESLint and other rules(like noDuplicateJsxProps) also doesn't have the action.

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

You might want to look at https://github.com/rome/tools/pull/4130/files , maybe some logic can be joined

JsMethodModifierList(JsMethodModifierList),
}

fn get_static_type(modifier_list: AnyModifierList) -> StaticType {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can avoid the enum AnyModifierList by accepting a JsSyntaxList instead. From a typed list - for example, JsPropertyModifierList - you can retrieve a generic list using syntax_list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored get_static_type to is_static_member which accepts JsSyntaxList.

let static_type = member_def.static_type();
let member_type = member_def.member_type();
defined_members
.entry((member_name, static_type, access_type))
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the point of saving static_type, access_type and member_name if they are all computed from member_def? I think it's better to save member_def at this point and implement Eq, PartialEq

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I try to implement PartialEq and Eq for AnyClassMemberDefinition, but I couldn't because declare_node_union macro have already implemented PartialEq and Eq.

#[derive(Clone, PartialEq, Eq, Hash)]

How do I resolve this error?

error[E0119]: conflicting implementations of trait `std::cmp::PartialEq` for type `analyzers::nursery::no_duplicate_class_members::AnyClassMemberDefinition`
   --> crates/rome_js_analyze/src/analyzers/nursery/no_duplicate_class_members.rs:144:1
    |
144 | / declare_node_union! {
145 | |     pub(crate) AnyClassMemberDefinition = JsGetterClassMember | JsMethodClassMember | JsPropertyClassMember | JsSetterClassMember
146 | | }
    | |_^ conflicting implementation for `analyzers::nursery::no_duplicate_class_members::AnyClassMemberDefinition`
...
174 |   impl PartialEq for AnyClassMemberDefinition {
    |   ------------------------------------------- first implementation here
    |
    = note: this error originates in the derive macro `PartialEq` which comes from the expansion of the macro `declare_node_union` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0119`.
error: could not compile `rome_js_analyze` due to previous error

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh sorry, then there's no need to manually implement it :)

Copy link
Contributor Author

@nissy-dev nissy-dev Jan 7, 2023

Choose a reason for hiding this comment

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

What's the point of saving static_type, access_type and member_name if they are all computed from member_def?

These values are necessary for an unique key about judging whether the class member has already defined or not. I seem AnyClassMemberDefinition is not able to used as a key. If you think AnyClassMemberDefinition could be used as a key of HashMap, can you show more details?

@nissy-dev nissy-dev force-pushed the impl-no-dupe-class-members branch from a365b19 to 1bcf95a Compare January 6, 2023 15:30
@nissy-dev
Copy link
Contributor Author

Thank you for your reviews! I refactored this PR based on your comments. I would appreciate if you could review again.

@nissy-dev
Copy link
Contributor Author

nissy-dev commented Jan 7, 2023

I updated the PR for #4143 and this PR is ready for review.

I see #4143 (comment) and updated again. This PR is ready for review.

@nissy-dev nissy-dev force-pushed the impl-no-dupe-class-members branch from 987d96f to 732ec84 Compare January 7, 2023 12:09
@github-actions
Copy link

This PR is stale because it has been open 14 days with no activity.

Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Once rebased, we can merge it!

@github-actions github-actions bot removed the S-Stale label Feb 3, 2023
@nissy-dev
Copy link
Contributor Author

I resolved conflicts. This PR is ready to merge 🚀

@ematipico ematipico merged commit c3fb1a8 into rome:main Feb 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

noDuplicateClassMembers, no-dupe-class-members
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载