Skip to content

Commit ac82a7d

Browse files
authored
Merge pull request #431 from pdhoopr/fix-heading-has-content-components
Fix heading-has-content so it works for custom components
2 parents 82dc999 + 7f13a92 commit ac82a7d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 19 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,11 +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+
// CUSTOM ELEMENT TESTS FOR COMPONENTS OPTION
47+
{ code: '<Heading>Foo</Heading>', options: components },
48+
{ code: '<Title>Foo</Title>', options: components },
49+
{ code: '<Heading><Bar /></Heading>', options: components },
50+
{ code: '<Heading>{foo}</Heading>', options: components },
51+
{ code: '<Heading>{foo.bar}</Heading>', options: components },
52+
{ code: '<Heading dangerouslySetInnerHTML={{ __html: "foo" }} />', options: components },
53+
{ code: '<Heading children={children} />', options: components },
4154
{ code: '<h1 aria-hidden />' },
4255
].map(parserOptionsMapper),
4356
invalid: [
57+
// DEFAULT ELEMENT TESTS
4458
{ code: '<h1 />', errors: [expectedError] },
4559
{ code: '<h1><Bar aria-hidden /></h1>', errors: [expectedError] },
4660
{ 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 },
4766
].map(parserOptionsMapper),
4867
});

src/rules/heading-has-content.js

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

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

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

0 commit comments

Comments
 (0)