diff --git a/docs/rules/no-multi-comp.md b/docs/rules/no-multi-comp.md index fb61677f87..1e38c10a10 100644 --- a/docs/rules/no-multi-comp.md +++ b/docs/rules/no-multi-comp.md @@ -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. diff --git a/lib/rules/no-multi-comp.js b/lib/rules/no-multi-comp.js index 8da20e02b5..b07251d465 100644 --- a/lib/rules/no-multi-comp.js +++ b/lib/rules/no-multi-comp.js @@ -39,7 +39,7 @@ module.exports = { default: false, type: 'boolean', }, - ignorePrivate: { + ignoreInternal: { default: false, type: 'boolean', }, @@ -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']); @@ -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 = { @@ -123,8 +123,8 @@ 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, }); @@ -132,14 +132,15 @@ module.exports = { }, }; - 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; diff --git a/tests/lib/rules/no-multi-comp.js b/tests/lib/rules/no-multi-comp.js index 2a8b2611d7..b39e2a07ff 100644 --- a/tests/lib/rules/no-multi-comp.js +++ b/tests/lib/rules/no-multi-comp.js @@ -270,14 +270,14 @@ 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: ` @@ -285,7 +285,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; module.exports = { ComponentOne }; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -293,7 +293,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export default ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -301,7 +301,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export default ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -309,7 +309,7 @@ ruleTester.run('no-multi-comp', rule, { function ComponentTwo() { return <> }; export default ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -317,7 +317,7 @@ ruleTester.run('no-multi-comp', rule, { export class ComponentOne extends Component() { render() { return <>; }}; function ComponentTwo() { return <> }; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -326,7 +326,7 @@ ruleTester.run('no-multi-comp', rule, { function ComponentTwo() { return <> }; export default ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -334,14 +334,14 @@ ruleTester.run('no-multi-comp', rule, { 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: ` @@ -349,7 +349,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; module.exports = ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -357,7 +357,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export default function() { return ; } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -365,7 +365,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export { ComponentOne as default }; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -377,7 +377,7 @@ ruleTester.run('no-multi-comp', rule, { render() { return <>; } } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -390,7 +390,7 @@ ruleTester.run('no-multi-comp', rule, { } export { ComponentOne }; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -399,7 +399,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export default ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, { code: ` @@ -407,7 +407,7 @@ ruleTester.run('no-multi-comp', rule, { export default function Component(props) { return
{props.children}
; } function ComponentTwo(props) { return
{props.children}
; } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], }, ]), @@ -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' }], }, { @@ -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' }], }, { @@ -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' }], }, { @@ -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' }], }, { @@ -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' }], }, { @@ -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' }], }, { @@ -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' }], }, { @@ -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' }], }, { @@ -835,7 +835,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export { ComponentOne }; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -843,7 +843,7 @@ ruleTester.run('no-multi-comp', rule, { export function ComponentOne() { return <>; } function ComponentTwo() { return <>; } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -852,7 +852,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; module.exports = ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -861,7 +861,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export default function() { return ; } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -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' }], }, { @@ -883,7 +883,7 @@ ruleTester.run('no-multi-comp', rule, { render() { return <>; } } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -897,7 +897,7 @@ ruleTester.run('no-multi-comp', rule, { } export { ComponentOne }; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -907,7 +907,7 @@ ruleTester.run('no-multi-comp', rule, { const ComponentTwo = () => <>; export default ComponentOne; `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -916,7 +916,7 @@ ruleTester.run('no-multi-comp', rule, { export default function Component(props) { return
{props.children}
; } export function ComponentTwo(props) { return
{props.children}
; } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, { @@ -925,7 +925,7 @@ ruleTester.run('no-multi-comp', rule, { export function componentOne(props) { return
{props.children}
; } export function ComponentOne(props) { return
{props.children}
; } `, - options: [{ ignorePrivate: true }], + options: [{ ignoreInternal: true }], errors: [{ messageId: 'onlyOneExportedComponent' }], }, ]),