Skip to content

Add explicit-heading rule #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-toys-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-primer-react': major
---

Add `a11y-explicit-heading` rule
37 changes: 37 additions & 0 deletions docs/rules/explicit-heading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## Require explicit heading level on `<Heading>` component

The `Heading` component does not require you to use `as` to specify the heading level, as it will default to an `h2` if one isn't specified. This may lead to inaccessible usage if the default is out of order in the existing heading hierarchy.

## Rule Details

This rule enforces using `as` on the `<Heading>` component to specify a heading level (`h1`-`h6`). In addition, it enforces `as` usage to only be used for headings.

👎 Examples of **incorrect** code for this rule

```jsx
import {Heading} from '@primer/react'

<Heading>Heading without explicit heading level</Heading>
```

`as` must only be for headings (`h1`-`h6`)

```jsx
import {Heading} from '@primer/react'

<Heading as="span">Heading component used as "span"</Heading>
```

👍 Examples of **correct** code for this rule:

```jsx
import {Heading} from '@primer/react';

<Heading as="h2">Heading level 2</Heading>
```

## Options

- `skipImportCheck` (default: `false`)

By default, the `a11y-explicit-heading` rule will only check for `<Heading>` components imported directly from `@primer/react`. You can disable this behavior by setting `skipImportCheck` to `true`.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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/a11y-explicit-heading': 'error'
},
settings: {
github: {
Expand Down
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'),
'a11y-explicit-heading': require('./rules/a11y-explicit-heading')
},
configs: {
recommended: require('./configs/recommended')
Expand Down
41 changes: 41 additions & 0 deletions src/rules/__tests__/a11y-explicit-heading.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const rule = require('../a11y-explicit-heading')
const {RuleTester} = require('eslint')

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
}
})

ruleTester.run('a11y-explicit-heading', rule, {
valid: [
`import {Heading} from '@primer/react';
<Heading as="h1">Heading level 1</Heading>
`,
`import {Heading} from '@primer/react';
<Heading as="h2">Heading level 2</Heading>
`,
`import {Heading} from '@primer/react';
<Heading as="H3">Heading level 3</Heading>
`,
],
invalid: [
{
code:
`import {Heading} from '@primer/react';
<Heading>Heading without "as"</Heading>`,
errors: [{ messageId: 'nonExplicitHeadingLevel' }]
},
{
code:
`import {Heading} from '@primer/react';
<Heading as="span">Heading component used as "span"</Heading>
`,
errors: [{ messageId: 'invalidAsValue' }]
},
]
})
60 changes: 60 additions & 0 deletions src/rules/a11y-explicit-heading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 isHeadingComponent = elem => getJSXOpeningElementName(elem) === 'Heading'
const isUsingAsProp = elem => {
const componentAs = getJSXOpeningElementAttribute(elem, 'as')

if (!componentAs) return

return componentAs.value
}

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

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

return false
}

module.exports = {
meta: {
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).'
}
},
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
})
}
}
}
}
}
}