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] forbid-component-props: add allowedForPatterns and disallowedForPatterns options #3805

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
add more complex test
  • Loading branch information
Efimenko committed Aug 22, 2024
commit 5019c92b249c06c69f6c64812a3373ce1c9e3a2b
30 changes: 20 additions & 10 deletions lib/rules/forbid-component-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,31 @@ module.exports = {
return false;
}

function checkIsTagForbidden() {
if (options.allowPatternList.length > 0) {
return options.allowPatternList.every(
(pattern) => !minimatch(tagName, pattern)
);
function checkIsTagForbiddenByAllowPatternList() {
if (options.allowPatternList.length === 0) {
return true;
}

// disallowList should have a least one item (schema configuration)
return options.disallowList.length > 0
? options.disallowList.indexOf(tagName) !== -1
: options.allowList.indexOf(tagName) === -1;
return options.allowPatternList.every(
(pattern) => !minimatch(tagName, pattern)
);
}

function checkIsTagForbiddenByAllowList() {
return options.allowList.indexOf(tagName) === -1;
}

function checkIsTagForbiddenByAllowOptions() {
return checkIsTagForbiddenByAllowPatternList() && checkIsTagForbiddenByAllowList();
}

// disallowList should have a least one item (schema configuration)
const isTagForbidden = options.disallowList.length > 0
? options.disallowList.indexOf(tagName) !== -1
: checkIsTagForbiddenByAllowOptions();

// if the tagName is undefined (`<this.something>`), we assume it's a forbidden element
return typeof tagName === 'undefined' || checkIsTagForbidden();
return typeof tagName === 'undefined' || isTagForbidden;
}

return {
Expand Down
26 changes: 5 additions & 21 deletions tests/lib/rules/forbid-component-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,15 @@ ruleTester.run('forbid-component-props', rule, {
},
],
},
{
code: `
const rootElement = (
<Root>
<SomeIcon className="size-lg" />
<AnotherIcon className="size-lg" />
</Root>
);
`,
options: [
{
forbid: [
{
propName: 'className',
allowedForPatterns: ['*Icon'],
},
],
},
],
},
{
code: `
const rootElement = (
<Root>
<SomeIcon className="size-lg" />
<AnotherIcon className="size-lg" />
<SomeSvg className="size-lg" />
<UICard className="size-lg" />
<UIButton className="size-lg" />
</Root>
);
`,
Expand All @@ -285,7 +267,7 @@ ruleTester.run('forbid-component-props', rule, {
forbid: [
{
propName: 'className',
allowedForPatterns: ['*Icon', '*Svg'],
allowedForPatterns: ['*Icon', '*Svg', 'UI*'],
},
],
},
Expand All @@ -300,6 +282,7 @@ ruleTester.run('forbid-component-props', rule, {
<SomeSvg className="size-lg" />
<UICard className="size-lg" />
<UIButton className="size-lg" />
<ButtonLegacy className="size-lg" />
</Root>
);
`,
Expand All @@ -308,6 +291,7 @@ ruleTester.run('forbid-component-props', rule, {
forbid: [
{
propName: 'className',
allowedFor: ['ButtonLegacy'],
allowedForPatterns: ['*Icon', '*Svg', 'UI*'],
},
],
Expand Down