Skip to content

Commit fbdcfd8

Browse files
committed
Update wording, add to index.js
1 parent e1ea0b9 commit fbdcfd8

File tree

3 files changed

+20
-31
lines changed

3 files changed

+20
-31
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module.exports = {
33
'direct-slot-children': require('./rules/direct-slot-children'),
44
'no-deprecated-colors': require('./rules/no-deprecated-colors'),
55
'no-system-props': require('./rules/no-system-props'),
6-
'a11y-tooltip-interactive-trigger': require('./rules/a11y-tooltip-interactive-trigger')
6+
'a11y-tooltip-interactive-trigger': require('./rules/a11y-tooltip-interactive-trigger'),
7+
'explicit-heading': require('./rules/explicit-heading')
78
},
89
configs: {
910
recommended: require('./configs/recommended')

src/rules/__tests__/implicit-heading.test.js renamed to src/rules/__tests__/explicit-heading.test.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,17 @@ ruleTester.run('explicit-heading', rule, {
2525
],
2626
invalid: [
2727
{
28-
code: `
29-
import {Heading} from '@primer/react';
30-
31-
<Heading>Heading without "as"</Heading>
32-
`,
33-
errors: [
34-
{
35-
messageId: 'nonExplicitHeadingLevel'
36-
}
37-
]
28+
code:
29+
`import {Heading} from '@primer/react';
30+
<Heading>Heading without "as"</Heading>`,
31+
errors: [{ messageId: 'nonExplicitHeadingLevel' }]
3832
},
3933
{
40-
code: `
41-
import {Heading} from '@primer/react';
42-
34+
code:
35+
`import {Heading} from '@primer/react';
4336
<Heading as="span">Heading component used as "span"</Heading>
4437
`,
45-
errors: [
46-
{
47-
messageId: 'invalidAsValue'
48-
}
49-
]
38+
errors: [{ messageId: 'invalidAsValue' }]
5039
},
5140
]
5241
})

src/rules/explicit-heading.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-elemen
44

55
const validHeadings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
66

7-
const isHeading = elem => getJSXOpeningElementName(elem) === 'Heading'
8-
const isUsingAs = elem => {
9-
const asUsage = getJSXOpeningElementAttribute(elem, 'as');
7+
const isHeadingComponent = elem => getJSXOpeningElementName(elem) === 'Heading'
8+
const isUsingAsProp = elem => {
9+
const componentAs = getJSXOpeningElementAttribute(elem, 'as');
1010

11-
if (!asUsage) return;
11+
if (!componentAs) return;
1212

13-
return asUsage.value;
13+
return componentAs.value;
1414
}
1515

16-
const isValidAs = value => validHeadings.includes(value.toLowerCase());
16+
const isValidAsUsage = value => validHeadings.includes(value.toLowerCase());
1717
const isInvalid = elem => {
18-
const elemAs = isUsingAs(elem);
18+
const elemAs = isUsingAsProp(elem);
1919

2020
if (!elemAs) return 'nonExplicitHeadingLevel';
21-
if(!isValidAs(elemAs.value)) return 'invalidAsValue';
21+
if(!isValidAsUsage(elemAs.value)) return 'invalidAsValue';
2222

2323
return false;
2424
}
@@ -36,15 +36,14 @@ module.exports = {
3636
}
3737
],
3838
messages: {
39-
nonExplicitHeadingLevel: "Heading must have an explicit heading level applied through `as` prop.",
40-
invalidAsValue: "Usage of `as` must only be used for headings (h1-h6)."
39+
nonExplicitHeadingLevel: "Heading must have an explicit heading level applied through the `as` prop.",
40+
invalidAsValue: "Usage of `as` must only be used for heading elements (h1-h6)."
4141
}
4242
},
4343
create: function(context) {
4444
return {
45-
// callback functions
4645
JSXOpeningElement(jsxNode) {
47-
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && isHeading(jsxNode)) {
46+
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && isHeadingComponent(jsxNode)) {
4847
const error = isInvalid(jsxNode);
4948

5049
if (error) {

0 commit comments

Comments
 (0)