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

[New] create ignorePrivate option for no-multi-comp rule #3842

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

vwillyams
Copy link

@vwillyams vwillyams commented Oct 18, 2024

Adds a new option to no-multi-comp which ignores components that are not exported.

Currently WIP:

  • Thorough testing, find edge cases. npm test wasn't working locally so I'm hoping to rely on CI for this.
  • Comparisons between exports and React components probably needs more work. Maybe there's a pre-existing lib function for this. Maybe this functionality should be moved into lib.

@vwillyams vwillyams changed the title [New] create exportOnly rule for no-multi-comp [New] create ignorePrivate option for no-multi-comp rule Oct 18, 2024
@@ -69,6 +69,55 @@ class HelloJohn extends React.Component {
module.exports = HelloJohn;
```

### `ignorePrivate`
Copy link
Member

Choose a reason for hiding this comment

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

"private" might not be the best name here. maybe "ignoreNonExported"?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I originally had this labeled as exportOnly but wanted to stick closer to the ignoreStateless convention already established.

Copy link
Author

Choose a reason for hiding this comment

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

ignoreNonExported feels a little clunky to me but I can't think of anything better. just one of those hard problems in software.

Copy link
Member

Choose a reason for hiding this comment

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

yeah totally :-) "ignoreInternal"?

Copy link
Author

Choose a reason for hiding this comment

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

Sounds good to me, switched over.

docs/rules/no-multi-comp.md Outdated Show resolved Hide resolved
const ignorePrivate = configuration.ignorePrivate || false;

const exportedComponents = new Set(); // Track exported components
const validIdentifiers = new Set(['ArrowFunctionExpression', 'Identifier', 'FunctionExpression']);
Copy link
Member

Choose a reason for hiding this comment

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

i believe in react 19, async functions can be components as well.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah good point, I had not thought about RSCs.

Copy link
Author

Choose a reason for hiding this comment

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

Added a little coverage for this - I'm not an RSC dev by day though so may have made some oversights.

lib/rules/no-multi-comp.js Outdated Show resolved Hide resolved
lib/rules/no-multi-comp.js Outdated Show resolved Hide resolved
Comment on lines 136 to 144
if (ignorePrivate) {
rule.ExportNamedDeclaration = (node) => {
exportedComponents.add(getExportedComponentName(node));
};

rule.ExportDefaultDeclaration = (node) => {
exportedComponents.add(getExportedComponentName(node));
};
}
Copy link
Member

Choose a reason for hiding this comment

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

instead of mutating maybe we could use Object.assign here?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah sure, sounds good. I'm not clear on why but I'm not super experienced in the eslint lifecycle and I can imagine that mutation may introduce issues somehow.

Copy link
Member

Choose a reason for hiding this comment

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

it probably doesn't matter much at all, but mutation generally risks slowdowns later, as opposed to creating objects all at once.

Copy link
Author

Choose a reason for hiding this comment

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

Switched over.

@vwillyams
Copy link
Author

FYI I will hopefully find time to have this ready for merge in the next 2-3 days. I'm going to get this working with RSCs and I also want to look at making the tests programmatically generated so that we can iterate over every possible permutation without having to write them all out individually (and risk missing something). In practice there's likely to be diminishing returns on that kind of testing but I assume that individual test cases are very cheap here and I don't really like the maintenance burden of the test cases I've added in this PR.

lib/rules/no-multi-comp.js Outdated Show resolved Hide resolved
@vwillyams
Copy link
Author

FYI I'm currently iterating on the test coverage and making sure this works in all cases. I didn't entirely expect the amount of complexity that would go into this feature! But it's been a fun learning process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants