Skip to content

Commit 0f75437

Browse files
committed
Fix heading-has-content so it works for custom components
1 parent cfc3c33 commit 0f75437

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

__tests__/src/rules/heading-has-content-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ const expectedError = {
2323
type: 'JSXOpeningElement',
2424
};
2525

26+
const components = [{
27+
components: ['Heading', 'Title'],
28+
}];
29+
2630
ruleTester.run('heading-has-content', rule, {
2731
valid: [
32+
// DEFAULT ELEMENT TESTS
2833
{ code: '<div />;' },
2934
{ code: '<h1>Foo</h1>' },
3035
{ code: '<h2>Foo</h2>' },
@@ -38,10 +43,25 @@ ruleTester.run('heading-has-content', rule, {
3843
{ code: '<h1>{foo.bar}</h1>' },
3944
{ code: '<h1 dangerouslySetInnerHTML={{ __html: "foo" }} />' },
4045
{ code: '<h1 children={children} />' },
46+
47+
// CUSTOM ELEMENT TESTS FOR COMPONENTS OPTION
48+
{ code: '<Heading>Foo</Heading>', options: components },
49+
{ code: '<Title>Foo</Title>', options: components },
50+
{ code: '<Heading><Bar /></Heading>', options: components },
51+
{ code: '<Heading>{foo}</Heading>', options: components },
52+
{ code: '<Heading>{foo.bar}</Heading>', options: components },
53+
{ code: '<Heading dangerouslySetInnerHTML={{ __html: "foo" }} />', options: components },
54+
{ code: '<Heading children={children} />', options: components },
4155
].map(parserOptionsMapper),
4256
invalid: [
57+
// DEFAULT ELEMENT TESTS
4358
{ code: '<h1 />', errors: [expectedError] },
4459
{ code: '<h1><Bar aria-hidden /></h1>', errors: [expectedError] },
4560
{ code: '<h1>{undefined}</h1>', errors: [expectedError] },
61+
62+
// CUSTOM ELEMENT TESTS FOR COMPONENTS OPTION
63+
{ code: '<Heading />', errors: [expectedError], options: components },
64+
{ code: '<Heading><Bar aria-hidden /></Heading>', errors: [expectedError], options: components },
65+
{ code: '<Heading>{undefined}</Heading>', errors: [expectedError], options: components },
4666
].map(parserOptionsMapper),
4767
});

src/rules/heading-has-content.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ module.exports = {
3535

3636
create: context => ({
3737
JSXOpeningElement: (node) => {
38-
const typeCheck = headings.concat(context.options[0]);
38+
const options = context.options[0] || {};
39+
const componentOptions = options.components || [];
40+
const typeCheck = headings.concat(componentOptions);
3941
const nodeType = elementType(node);
4042

4143
// Only check 'h*' elements and custom types.

0 commit comments

Comments
 (0)