Skip to content

Commit 7eca32d

Browse files
committed
Fix bug when using spread, or prop variable for as
1 parent c0a7ad7 commit 7eca32d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/rules/__tests__/a11y-explicit-heading.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ ruleTester.run('a11y-explicit-heading', rule, {
2222
`import {Heading} from '@primer/react';
2323
<Heading as="H3">Heading level 3</Heading>
2424
`,
25+
`import {Heading} from '@primer/react';
26+
const args = {};
27+
<Heading {...args}>A heading with spread props</Heading>
28+
`,
29+
`import {Heading} from '@primer/react';
30+
const as = 'h3';
31+
<Heading as={as}>Heading as passed prop</Heading>
32+
`,
33+
`import {Heading} from '@primer/react';
34+
const args = {};
35+
<Heading as="h2" {...args}>Heading level 2</Heading>
36+
`,
37+
2538
],
2639
invalid: [
2740
{

src/rules/a11y-explicit-heading.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ const isUsingAsProp = elem => {
1515

1616
const isValidAsUsage = value => validHeadings.includes(value.toLowerCase())
1717
const isInvalid = elem => {
18+
if (elem.attributes.length === 1 && elem.attributes[0].type === 'JSXSpreadAttribute') return;
19+
1820
const elemAs = isUsingAsProp(elem)
1921

2022
if (!elemAs) return 'nonExplicitHeadingLevel'
21-
if (!isValidAsUsage(elemAs.value)) return 'invalidAsValue'
23+
if (elemAs?.value && !isValidAsUsage(elemAs.value)) return 'invalidAsValue'
2224

2325
return false
2426
}
@@ -57,4 +59,4 @@ module.exports = {
5759
}
5860
}
5961
}
60-
}
62+
}

0 commit comments

Comments
 (0)