Skip to content

Commit 5019c92

Browse files
committed
add more complex test
1 parent 88e787b commit 5019c92

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

lib/rules/forbid-component-props.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,31 @@ module.exports = {
151151
return false;
152152
}
153153

154-
function checkIsTagForbidden() {
155-
if (options.allowPatternList.length > 0) {
156-
return options.allowPatternList.every(
157-
(pattern) => !minimatch(tagName, pattern)
158-
);
154+
function checkIsTagForbiddenByAllowPatternList() {
155+
if (options.allowPatternList.length === 0) {
156+
return true;
159157
}
160158

161-
// disallowList should have a least one item (schema configuration)
162-
return options.disallowList.length > 0
163-
? options.disallowList.indexOf(tagName) !== -1
164-
: options.allowList.indexOf(tagName) === -1;
159+
return options.allowPatternList.every(
160+
(pattern) => !minimatch(tagName, pattern)
161+
);
165162
}
166163

164+
function checkIsTagForbiddenByAllowList() {
165+
return options.allowList.indexOf(tagName) === -1;
166+
}
167+
168+
function checkIsTagForbiddenByAllowOptions() {
169+
return checkIsTagForbiddenByAllowPatternList() && checkIsTagForbiddenByAllowList();
170+
}
171+
172+
// disallowList should have a least one item (schema configuration)
173+
const isTagForbidden = options.disallowList.length > 0
174+
? options.disallowList.indexOf(tagName) !== -1
175+
: checkIsTagForbiddenByAllowOptions();
176+
167177
// if the tagName is undefined (`<this.something>`), we assume it's a forbidden element
168-
return typeof tagName === 'undefined' || checkIsTagForbidden();
178+
return typeof tagName === 'undefined' || isTagForbidden;
169179
}
170180

171181
return {

tests/lib/rules/forbid-component-props.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -250,33 +250,15 @@ ruleTester.run('forbid-component-props', rule, {
250250
},
251251
],
252252
},
253-
{
254-
code: `
255-
const rootElement = (
256-
<Root>
257-
<SomeIcon className="size-lg" />
258-
<AnotherIcon className="size-lg" />
259-
</Root>
260-
);
261-
`,
262-
options: [
263-
{
264-
forbid: [
265-
{
266-
propName: 'className',
267-
allowedForPatterns: ['*Icon'],
268-
},
269-
],
270-
},
271-
],
272-
},
273253
{
274254
code: `
275255
const rootElement = (
276256
<Root>
277257
<SomeIcon className="size-lg" />
278258
<AnotherIcon className="size-lg" />
279259
<SomeSvg className="size-lg" />
260+
<UICard className="size-lg" />
261+
<UIButton className="size-lg" />
280262
</Root>
281263
);
282264
`,
@@ -285,7 +267,7 @@ ruleTester.run('forbid-component-props', rule, {
285267
forbid: [
286268
{
287269
propName: 'className',
288-
allowedForPatterns: ['*Icon', '*Svg'],
270+
allowedForPatterns: ['*Icon', '*Svg', 'UI*'],
289271
},
290272
],
291273
},
@@ -300,6 +282,7 @@ ruleTester.run('forbid-component-props', rule, {
300282
<SomeSvg className="size-lg" />
301283
<UICard className="size-lg" />
302284
<UIButton className="size-lg" />
285+
<ButtonLegacy className="size-lg" />
303286
</Root>
304287
);
305288
`,
@@ -308,6 +291,7 @@ ruleTester.run('forbid-component-props', rule, {
308291
forbid: [
309292
{
310293
propName: 'className',
294+
allowedFor: ['ButtonLegacy'],
311295
allowedForPatterns: ['*Icon', '*Svg', 'UI*'],
312296
},
313297
],

0 commit comments

Comments
 (0)