Skip to content

Commit

Permalink
Add to recommended, add skipImportCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerJDev committed Jul 10, 2023
1 parent fbdcfd8 commit d17554f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = {
'primer-react/direct-slot-children': 'error',
'primer-react/no-deprecated-colors': 'warn',
'primer-react/no-system-props': 'warn',
'primer-react/a11y-tooltip-interactive-trigger': 'error'
'primer-react/a11y-tooltip-interactive-trigger': 'error',
'primer-react/explicit-heading': 'error'
},
settings: {
github: {
Expand Down
81 changes: 41 additions & 40 deletions src/rules/explicit-heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,59 @@ const {isPrimerComponent} = require('../utils/is-primer-component')
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')

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

const isHeadingComponent = elem => getJSXOpeningElementName(elem) === 'Heading'
const isUsingAsProp = elem => {
const componentAs = getJSXOpeningElementAttribute(elem, 'as');
const componentAs = getJSXOpeningElementAttribute(elem, 'as')

if (!componentAs) return;
if (!componentAs) return

return componentAs.value;
return componentAs.value
}

const isValidAsUsage = value => validHeadings.includes(value.toLowerCase());
const isValidAsUsage = value => validHeadings.includes(value.toLowerCase())
const isInvalid = elem => {
const elemAs = isUsingAsProp(elem);
const elemAs = isUsingAsProp(elem)

if (!elemAs) return 'nonExplicitHeadingLevel';
if(!isValidAsUsage(elemAs.value)) return 'invalidAsValue';
if (!elemAs) return 'nonExplicitHeadingLevel'
if (!isValidAsUsage(elemAs.value)) return 'invalidAsValue'

return false;
return false
}

module.exports = {
meta: {
type: "problem",
schema: [
{
properties: {
skipImportCheck: {
type: 'boolean'
}
}
}
],
messages: {
nonExplicitHeadingLevel: "Heading must have an explicit heading level applied through the `as` prop.",
invalidAsValue: "Usage of `as` must only be used for heading elements (h1-h6)."
meta: {
schema: [
{
properties: {
skipImportCheck: {
type: 'boolean'
}
},
create: function(context) {
return {
JSXOpeningElement(jsxNode) {
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && isHeadingComponent(jsxNode)) {
const error = isInvalid(jsxNode);

if (error) {
context.report({
node: jsxNode,
messageId: error
})
}
}
}
};
}
}
],
messages: {
nonExplicitHeadingLevel: 'Heading must have an explicit heading level applied through the `as` prop.',
invalidAsValue: 'Usage of `as` must only be used for heading elements (h1-h6).'
}
};
},
create: function(context) {
return {
JSXOpeningElement(jsxNode) {
const skipImportCheck = context.options[0] ? context.options[0].skipImportCheck : false

if ((skipImportCheck || isPrimerComponent(jsxNode.name, context.getScope(jsxNode))) && isHeadingComponent(jsxNode)) {
const error = isInvalid(jsxNode)

if (error) {
context.report({
node: jsxNode,
messageId: error
})
}
}
}
}
}
}

0 comments on commit d17554f

Please sign in to comment.