Skip to content

Commit

Permalink
Update wording, add to index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerJDev committed Jul 10, 2023
1 parent e1ea0b9 commit fbdcfd8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = {
'direct-slot-children': require('./rules/direct-slot-children'),
'no-deprecated-colors': require('./rules/no-deprecated-colors'),
'no-system-props': require('./rules/no-system-props'),
'a11y-tooltip-interactive-trigger': require('./rules/a11y-tooltip-interactive-trigger')
'a11y-tooltip-interactive-trigger': require('./rules/a11y-tooltip-interactive-trigger'),
'explicit-heading': require('./rules/explicit-heading')
},
configs: {
recommended: require('./configs/recommended')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,17 @@ ruleTester.run('explicit-heading', rule, {
],
invalid: [
{
code: `
import {Heading} from '@primer/react';
<Heading>Heading without "as"</Heading>
`,
errors: [
{
messageId: 'nonExplicitHeadingLevel'
}
]
code:
`import {Heading} from '@primer/react';
<Heading>Heading without "as"</Heading>`,
errors: [{ messageId: 'nonExplicitHeadingLevel' }]
},
{
code: `
import {Heading} from '@primer/react';
code:
`import {Heading} from '@primer/react';
<Heading as="span">Heading component used as "span"</Heading>
`,
errors: [
{
messageId: 'invalidAsValue'
}
]
errors: [{ messageId: 'invalidAsValue' }]
},
]
})
23 changes: 11 additions & 12 deletions src/rules/explicit-heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-elemen

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

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

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

return asUsage.value;
return componentAs.value;
}

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

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

return false;
}
Expand All @@ -36,15 +36,14 @@ module.exports = {
}
],
messages: {
nonExplicitHeadingLevel: "Heading must have an explicit heading level applied through `as` prop.",
invalidAsValue: "Usage of `as` must only be used for headings (h1-h6)."
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 {
// callback functions
JSXOpeningElement(jsxNode) {
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && isHeading(jsxNode)) {
if (isPrimerComponent(jsxNode.name, context.getScope(jsxNode)) && isHeadingComponent(jsxNode)) {
const error = isInvalid(jsxNode);

if (error) {
Expand Down

0 comments on commit fbdcfd8

Please sign in to comment.