Skip to content

Commit

Permalink
PR suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vwillyams committed Oct 23, 2024
1 parent 6655950 commit b420cb5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion docs/rules/no-multi-comp.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class HelloJohn extends React.Component {
module.exports = HelloJohn;
```

### `ignorePrivate`
### `ignoreInternal`

When `true` the rule will ignore components which are not exported, which allows you to define components that are consumed only within the same file.
This ensures there is only one entry point for a React component without limiting the structural content of the file.
Expand Down
27 changes: 14 additions & 13 deletions lib/rules/no-multi-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
default: false,
type: 'boolean',
},
ignorePrivate: {
ignoreInternal: {
default: false,
type: 'boolean',
},
Expand All @@ -51,7 +51,7 @@ module.exports = {
create: Components.detect((context, components, utils) => {
const configuration = context.options[0] || {};
const ignoreStateless = configuration.ignoreStateless || false;
const ignorePrivate = configuration.ignorePrivate || false;
const ignoreInternal = configuration.ignoreInternal || false;

const exportedComponents = new Set(); // Track exported components
const validIdentifiers = new Set(['ArrowFunctionExpression', 'Identifier', 'FunctionExpression']);
Expand Down Expand Up @@ -109,7 +109,7 @@ module.exports = {
* @returns {boolean} True if the component is exported or exportOnly is false
*/
function isPrivate(component) {
return ignorePrivate && exportedComponents.has(findComponentIdentifierFromComponent(component));
return ignoreInternal && exportedComponents.has(findComponentIdentifierFromComponent(component));
}

const rule = {
Expand All @@ -123,23 +123,24 @@ module.exports = {
.slice(1)
.forEach((component) => {
report(context,
ignorePrivate ? messages.onlyOneExportedComponent : messages.onlyOneComponent,
ignorePrivate ? 'onlyOneExportedComponent' : 'onlyOneComponent',
ignoreInternal ? messages.onlyOneExportedComponent : messages.onlyOneComponent,
ignoreInternal ? 'onlyOneExportedComponent' : 'onlyOneComponent',
{
node: component.node,
});
});
},
};

if (ignorePrivate) {
rule.ExportNamedDeclaration = (node) => {
exportedComponents.add(getExportedComponentName(node));
};

rule.ExportDefaultDeclaration = (node) => {
exportedComponents.add(getExportedComponentName(node));
};
if (ignoreInternal) {
Object.assign(rule, {
ExportNamedDeclaration(node) {
exportedComponents.add(getExportedComponentName(node));
},
ExportDefaultDeclaration(node) {
exportedComponents.add(getExportedComponentName(node));
},
});
}

return rule;
Expand Down
70 changes: 35 additions & 35 deletions tests/lib/rules/no-multi-comp.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,54 +270,54 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
export const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
module.exports = { ComponentOne };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
function ComponentOne() { return <></> };
const ComponentTwo = () => <></>;
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
function ComponentOne() { return <></> };
function ComponentTwo() { return <></> };
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
import React, {Component} from "react";
export class ComponentOne extends Component() { render() { return <></>; }};
function ComponentTwo() { return <></> };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
Expand All @@ -326,46 +326,46 @@ ruleTester.run('no-multi-comp', rule, {
function ComponentTwo() { return <></> };
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
export { ComponentOne };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
export function ComponentOne() { return <></>; }
function ComponentTwo() { return <></>; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
module.exports = ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
const ComponentOne = () => <></>;
const ComponentTwo = () => <></>;
export default function() { return <ComponentOne />; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
function ComponentOne() { return <></>; }
const ComponentTwo = () => <></>;
export { ComponentOne as default };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
Expand All @@ -377,7 +377,7 @@ ruleTester.run('no-multi-comp', rule, {
render() { return <></>; }
}
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
Expand All @@ -390,7 +390,7 @@ ruleTester.run('no-multi-comp', rule, {
}
export { ComponentOne };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
Expand All @@ -399,15 +399,15 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
{
code: `
import React from "react";
export default function Component(props) { return <div>{props.children}</div>; }
function ComponentTwo(props) { return <div>{props.children}</div>; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
},
]),

Expand Down Expand Up @@ -761,7 +761,7 @@ ruleTester.run('no-multi-comp', rule, {
export const ComponentOne = () => <></>;
export const ComponentTwo = () => <></>;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -770,7 +770,7 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
module.exports = { ComponentOne, ComponentTwo };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -779,7 +779,7 @@ ruleTester.run('no-multi-comp', rule, {
export const ComponentTwo = () => <></>;
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -788,7 +788,7 @@ ruleTester.run('no-multi-comp', rule, {
export const ComponentTwo = () => <></>;
export default ComponentTwo;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -797,7 +797,7 @@ ruleTester.run('no-multi-comp', rule, {
export function ComponentTwo() { return <></> };
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -806,7 +806,7 @@ ruleTester.run('no-multi-comp', rule, {
export class ComponentOne extends Component() { render() { return <></>; }};
export function ComponentTwo() { return <></> };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -816,7 +816,7 @@ ruleTester.run('no-multi-comp', rule, {
export function ComponentTwo() { return <></> };
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -826,7 +826,7 @@ ruleTester.run('no-multi-comp', rule, {
function ComponentTwo() { return <></> };
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -835,15 +835,15 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
export { ComponentOne };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
code: `
export function ComponentOne() { return <></>; }
function ComponentTwo() { return <></>; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -852,7 +852,7 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
module.exports = ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -861,7 +861,7 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
export default function() { return <ComponentOne />; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -870,7 +870,7 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
export { ComponentOne as default };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -883,7 +883,7 @@ ruleTester.run('no-multi-comp', rule, {
render() { return <></>; }
}
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -897,7 +897,7 @@ ruleTester.run('no-multi-comp', rule, {
}
export { ComponentOne };
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -907,7 +907,7 @@ ruleTester.run('no-multi-comp', rule, {
const ComponentTwo = () => <></>;
export default ComponentOne;
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -916,7 +916,7 @@ ruleTester.run('no-multi-comp', rule, {
export default function Component(props) { return <div>{props.children}</div>; }
export function ComponentTwo(props) { return <div>{props.children}</div>; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
{
Expand All @@ -925,7 +925,7 @@ ruleTester.run('no-multi-comp', rule, {
export function componentOne(props) { return <div>{props.children}</div>; }
export function ComponentOne(props) { return <div>{props.children}</div>; }
`,
options: [{ ignorePrivate: true }],
options: [{ ignoreInternal: true }],
errors: [{ messageId: 'onlyOneExportedComponent' }],
},
]),
Expand Down

0 comments on commit b420cb5

Please sign in to comment.