Skip to content

Commit d17554f

Browse files
committed
Add to recommended, add skipImportCheck
1 parent fbdcfd8 commit d17554f

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/configs/recommended.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
'primer-react/direct-slot-children': 'error',
1212
'primer-react/no-deprecated-colors': 'warn',
1313
'primer-react/no-system-props': 'warn',
14-
'primer-react/a11y-tooltip-interactive-trigger': 'error'
14+
'primer-react/a11y-tooltip-interactive-trigger': 'error',
15+
'primer-react/explicit-heading': 'error'
1516
},
1617
settings: {
1718
github: {

src/rules/explicit-heading.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,59 @@ const {isPrimerComponent} = require('../utils/is-primer-component')
22
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
33
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
44

5-
const validHeadings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
5+
const validHeadings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
66

77
const isHeadingComponent = elem => getJSXOpeningElementName(elem) === 'Heading'
88
const isUsingAsProp = elem => {
9-
const componentAs = getJSXOpeningElementAttribute(elem, 'as');
9+
const componentAs = getJSXOpeningElementAttribute(elem, 'as')
1010

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

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

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

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

23-
return false;
23+
return false
2424
}
2525

2626
module.exports = {
27-
meta: {
28-
type: "problem",
29-
schema: [
30-
{
31-
properties: {
32-
skipImportCheck: {
33-
type: 'boolean'
34-
}
35-
}
36-
}
37-
],
38-
messages: {
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)."
27+
meta: {
28+
schema: [
29+
{
30+
properties: {
31+
skipImportCheck: {
32+
type: 'boolean'
4133
}
42-
},
43-
create: function(context) {
44-
return {
45-
JSXOpeningElement(jsxNode) {
46-
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && isHeadingComponent(jsxNode)) {
47-
const error = isInvalid(jsxNode);
48-
49-
if (error) {
50-
context.report({
51-
node: jsxNode,
52-
messageId: error
53-
})
54-
}
55-
}
56-
}
57-
};
34+
}
35+
}
36+
],
37+
messages: {
38+
nonExplicitHeadingLevel: 'Heading must have an explicit heading level applied through the `as` prop.',
39+
invalidAsValue: 'Usage of `as` must only be used for heading elements (h1-h6).'
5840
}
59-
};
41+
},
42+
create: function(context) {
43+
return {
44+
JSXOpeningElement(jsxNode) {
45+
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false
46+
47+
if ((skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) && isHeadingComponent(jsxNode)) {
48+
const error = isInvalid(jsxNode)
49+
50+
if (error) {
51+
context.report({
52+
node: jsxNode,
53+
messageId: error
54+
})
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)