eslint-plugin-primer-react 4.0.0-rc.4ff148f → 4.0.0-rc.8a65ea9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/docs/rules/explicit-heading.md +37 -0
- package/package-lock.json +2 -2
- package/package.json +1 -1
- package/src/configs/recommended.js +2 -1
- package/src/index.js +2 -1
- package/src/rules/__tests__/a11y-explicit-heading.test.js +41 -0
- package/src/rules/a11y-explicit-heading.js +60 -0
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
- [#51](https://github.com/primer/eslint-plugin-primer-react/pull/51) [`a65aa32`](https://github.com/primer/eslint-plugin-primer-react/commit/a65aa32c612c7fe952ec47bb3d926cf8adae9fab) Thanks [@broccolinisoup](https://github.com/broccolinisoup)! - Add `a11y-tooltip-interactive-trigger`
|
|
8
8
|
|
|
9
|
+
* [#66](https://github.com/primer/eslint-plugin-primer-react/pull/66) [`d1df609`](https://github.com/primer/eslint-plugin-primer-react/commit/d1df609b260505ee9dea1740bc96a3187355d727) Thanks [@TylerJDev](https://github.com/TylerJDev)! - Add `a11y-explicit-heading` rule
|
|
10
|
+
|
|
9
11
|
## 3.0.0
|
|
10
12
|
|
|
11
13
|
### Major Changes
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
## Require explicit heading level on `<Heading>` component
|
|
2
|
+
|
|
3
|
+
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.
|
|
4
|
+
|
|
5
|
+
## Rule Details
|
|
6
|
+
|
|
7
|
+
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.
|
|
8
|
+
|
|
9
|
+
👎 Examples of **incorrect** code for this rule
|
|
10
|
+
|
|
11
|
+
```jsx
|
|
12
|
+
import {Heading} from '@primer/react'
|
|
13
|
+
|
|
14
|
+
<Heading>Heading without explicit heading level</Heading>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`as` must only be for headings (`h1`-`h6`)
|
|
18
|
+
|
|
19
|
+
```jsx
|
|
20
|
+
import {Heading} from '@primer/react'
|
|
21
|
+
|
|
22
|
+
<Heading as="span">Heading component used as "span"</Heading>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
👍 Examples of **correct** code for this rule:
|
|
26
|
+
|
|
27
|
+
```jsx
|
|
28
|
+
import {Heading} from '@primer/react';
|
|
29
|
+
|
|
30
|
+
<Heading as="h2">Heading level 2</Heading>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Options
|
|
34
|
+
|
|
35
|
+
- `skipImportCheck` (default: `false`)
|
|
36
|
+
|
|
37
|
+
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`.
|
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-primer-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "eslint-plugin-primer-react",
|
|
9
|
-
"version": "
|
|
9
|
+
"version": "3.0.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@styled-system/props": "^5.1.5",
|
package/package.json
CHANGED
|
@@ -11,7 +11,8 @@ module.exports = {
|
|
|
11
11
|
'primer-react/direct-slot-children': 'error',
|
|
12
12
|
'primer-react/no-deprecated-colors': 'warn',
|
|
13
13
|
'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/a11y-explicit-heading': 'error'
|
|
15
16
|
},
|
|
16
17
|
settings: {
|
|
17
18
|
github: {
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,8 @@ module.exports = {
|
|
|
3
3
|
'direct-slot-children': require('./rules/direct-slot-children'),
|
|
4
4
|
'no-deprecated-colors': require('./rules/no-deprecated-colors'),
|
|
5
5
|
'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
|
+
'a11y-explicit-heading': require('./rules/a11y-explicit-heading')
|
|
7
8
|
},
|
|
8
9
|
configs: {
|
|
9
10
|
recommended: require('./configs/recommended')
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const rule = require('../a11y-explicit-heading')
|
|
2
|
+
const {RuleTester} = require('eslint')
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {
|
|
6
|
+
ecmaVersion: 'latest',
|
|
7
|
+
sourceType: 'module',
|
|
8
|
+
ecmaFeatures: {
|
|
9
|
+
jsx: true
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
ruleTester.run('a11y-explicit-heading', rule, {
|
|
15
|
+
valid: [
|
|
16
|
+
`import {Heading} from '@primer/react';
|
|
17
|
+
<Heading as="h1">Heading level 1</Heading>
|
|
18
|
+
`,
|
|
19
|
+
`import {Heading} from '@primer/react';
|
|
20
|
+
<Heading as="h2">Heading level 2</Heading>
|
|
21
|
+
`,
|
|
22
|
+
`import {Heading} from '@primer/react';
|
|
23
|
+
<Heading as="H3">Heading level 3</Heading>
|
|
24
|
+
`,
|
|
25
|
+
],
|
|
26
|
+
invalid: [
|
|
27
|
+
{
|
|
28
|
+
code:
|
|
29
|
+
`import {Heading} from '@primer/react';
|
|
30
|
+
<Heading>Heading without "as"</Heading>`,
|
|
31
|
+
errors: [{ messageId: 'nonExplicitHeadingLevel' }]
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
code:
|
|
35
|
+
`import {Heading} from '@primer/react';
|
|
36
|
+
<Heading as="span">Heading component used as "span"</Heading>
|
|
37
|
+
`,
|
|
38
|
+
errors: [{ messageId: 'invalidAsValue' }]
|
|
39
|
+
},
|
|
40
|
+
]
|
|
41
|
+
})
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const {isPrimerComponent} = require('../utils/is-primer-component')
|
|
2
|
+
const {getJSXOpeningElementName} = require('../utils/get-jsx-opening-element-name')
|
|
3
|
+
const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-element-attribute')
|
|
4
|
+
|
|
5
|
+
const validHeadings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
|
|
6
|
+
|
|
7
|
+
const isHeadingComponent = elem => getJSXOpeningElementName(elem) === 'Heading'
|
|
8
|
+
const isUsingAsProp = elem => {
|
|
9
|
+
const componentAs = getJSXOpeningElementAttribute(elem, 'as')
|
|
10
|
+
|
|
11
|
+
if (!componentAs) return
|
|
12
|
+
|
|
13
|
+
return componentAs.value
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const isValidAsUsage = value => validHeadings.includes(value.toLowerCase())
|
|
17
|
+
const isInvalid = elem => {
|
|
18
|
+
const elemAs = isUsingAsProp(elem)
|
|
19
|
+
|
|
20
|
+
if (!elemAs) return 'nonExplicitHeadingLevel'
|
|
21
|
+
if (!isValidAsUsage(elemAs.value)) return 'invalidAsValue'
|
|
22
|
+
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = {
|
|
27
|
+
meta: {
|
|
28
|
+
schema: [
|
|
29
|
+
{
|
|
30
|
+
properties: {
|
|
31
|
+
skipImportCheck: {
|
|
32
|
+
type: 'boolean'
|
|
33
|
+
}
|
|
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).'
|
|
40
|
+
}
|
|
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
|
+
}
|