eslint-plugin-primer-react 4.0.0-rc.2ea2bf7 → 4.0.0-rc.5015672

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-primer-react",
3
- "version": "4.0.0-rc.2ea2bf7",
3
+ "version": "4.0.0-rc.5015672",
4
4
  "description": "ESLint rules for Primer React",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -96,7 +96,7 @@ ruleTester.run('non-interactive-tooltip-trigger', rule, {
96
96
  </Tooltip>`,
97
97
  errors: [
98
98
  {
99
- messageId: 'anchorTagWithoutHref'
99
+ messageId: 'nonInteractiveLink'
100
100
  }
101
101
  ]
102
102
  },
@@ -108,7 +108,7 @@ ruleTester.run('non-interactive-tooltip-trigger', rule, {
108
108
  </Tooltip>`,
109
109
  errors: [
110
110
  {
111
- messageId: 'anchorTagWithoutHref'
111
+ messageId: 'nonInteractiveLink'
112
112
  }
113
113
  ]
114
114
  },
@@ -120,7 +120,7 @@ ruleTester.run('non-interactive-tooltip-trigger', rule, {
120
120
  </Tooltip>`,
121
121
  errors: [
122
122
  {
123
- messageId: 'hiddenInput'
123
+ messageId: 'nonInteractiveInput'
124
124
  }
125
125
  ]
126
126
  },
@@ -132,7 +132,43 @@ ruleTester.run('non-interactive-tooltip-trigger', rule, {
132
132
  </Tooltip>`,
133
133
  errors: [
134
134
  {
135
- messageId: 'hiddenInput'
135
+ messageId: 'nonInteractiveInput'
136
+ }
137
+ ]
138
+ },
139
+ {
140
+ code: `
141
+ import {Tooltip, Button} from '@primer/react';
142
+ <Tooltip aria-label="Supplementary text" direction="e">
143
+ <Button disabled>Save</Button>
144
+ </Tooltip>`,
145
+ errors: [
146
+ {
147
+ messageId: 'nonInteractiveTrigger'
148
+ }
149
+ ]
150
+ },
151
+ {
152
+ code: `
153
+ import {Tooltip, Button} from '@primer/react';
154
+ <Tooltip aria-label="Supplementary text" direction="e">
155
+ <IconButton disabled>Save</IconButton>
156
+ </Tooltip>`,
157
+ errors: [
158
+ {
159
+ messageId: 'nonInteractiveTrigger'
160
+ }
161
+ ]
162
+ },
163
+ {
164
+ code: `
165
+ import {Tooltip, Button} from '@primer/react';
166
+ <Tooltip aria-label="Supplementary text" direction="e">
167
+ <input disabled>Save</input>
168
+ </Tooltip>`,
169
+ errors: [
170
+ {
171
+ messageId: 'nonInteractiveInput'
136
172
  }
137
173
  ]
138
174
  },
@@ -159,7 +195,7 @@ ruleTester.run('non-interactive-tooltip-trigger', rule, {
159
195
  </Tooltip>`,
160
196
  errors: [
161
197
  {
162
- messageId: 'anchorTagWithoutHref'
198
+ messageId: 'nonInteractiveLink'
163
199
  }
164
200
  ]
165
201
  }
@@ -4,16 +4,21 @@ const {getJSXOpeningElementAttribute} = require('../utils/get-jsx-opening-elemen
4
4
 
5
5
  const isInteractive = child => {
6
6
  const childName = getJSXOpeningElementName(child.openingElement)
7
- return ['button', 'summary', 'select', 'textarea', 'a', 'input', 'link', 'iconbutton', 'textinput'].includes(
8
- childName.toLowerCase()
7
+ return (
8
+ ['button', 'summary', 'select', 'textarea', 'a', 'input', 'link', 'iconbutton', 'textinput'].includes(
9
+ childName.toLowerCase()
10
+ ) && !hasDisabledAttr(child)
9
11
  )
10
12
  }
11
13
 
14
+ const hasDisabledAttr = child => {
15
+ const hasDisabledAttr = getJSXOpeningElementAttribute(child.openingElement, 'disabled')
16
+ return hasDisabledAttr
17
+ }
18
+
12
19
  const isAnchorTag = el => {
13
- return (
14
- getJSXOpeningElementName(el.openingElement) === 'a' ||
15
- getJSXOpeningElementName(el.openingElement).toLowerCase() === 'link'
16
- )
20
+ const openingEl = getJSXOpeningElementName(el.openingElement)
21
+ return openingEl === 'a' || openingEl.toLowerCase() === 'link'
17
22
  }
18
23
 
19
24
  const isInteractiveAnchor = child => {
@@ -25,17 +30,15 @@ const isInteractiveAnchor = child => {
25
30
  }
26
31
 
27
32
  const isInputTag = el => {
28
- return (
29
- getJSXOpeningElementName(el.openingElement) === 'input' ||
30
- getJSXOpeningElementName(el.openingElement).toLowerCase() === 'textinput'
31
- )
33
+ const openingEl = getJSXOpeningElementName(el.openingElement)
34
+ return openingEl === 'input' || openingEl.toLowerCase() === 'textinput'
32
35
  }
33
36
 
34
37
  const isInteractiveInput = child => {
35
38
  const hasHiddenType =
36
39
  getJSXOpeningElementAttribute(child.openingElement, 'type') &&
37
40
  getJSXOpeningElementAttribute(child.openingElement, 'type').value.value === 'hidden'
38
- return !hasHiddenType
41
+ return !hasHiddenType && !hasDisabledAttr(child)
39
42
  }
40
43
 
41
44
  const isOtherThanAnchorOrInput = el => {
@@ -57,12 +60,12 @@ const getAllChildren = node => {
57
60
 
58
61
  const checks = [
59
62
  {
60
- id: 'anchorTagWithoutHref',
63
+ id: 'nonInteractiveLink',
61
64
  filter: jsxElement => isAnchorTag(jsxElement),
62
65
  check: isInteractiveAnchor
63
66
  },
64
67
  {
65
- id: 'hiddenInput',
68
+ id: 'nonInteractiveInput',
66
69
  filter: jsxElement => isInputTag(jsxElement),
67
70
  check: isInteractiveInput
68
71
  },
@@ -76,16 +79,11 @@ const checks = [
76
79
  const checkTriggerElement = jsxNode => {
77
80
  const elements = [...getAllChildren(jsxNode)]
78
81
  const hasInteractiveElement = elements.find(element => {
79
- if (
80
- getJSXOpeningElementName(element.openingElement) === 'a' ||
81
- getJSXOpeningElementName(element.openingElement) === 'Link'
82
- ) {
82
+ const openingEl = getJSXOpeningElementName(element.openingElement)
83
+ if (openingEl === 'a' || openingEl === 'Link') {
83
84
  return isInteractiveAnchor(element)
84
85
  }
85
- if (
86
- getJSXOpeningElementName(element.openingElement) === 'input' ||
87
- getJSXOpeningElementName(element.openingElement) === 'TextInput'
88
- ) {
86
+ if (openingEl === 'input' || openingEl === 'TextInput') {
89
87
  return isInteractiveInput(element)
90
88
  } else {
91
89
  return isInteractive(element)
@@ -110,10 +108,10 @@ const checkTriggerElement = jsxNode => {
110
108
  }
111
109
  // check the specificity of the errors. If there are multiple errors, only return the most specific one.
112
110
  if (errors.size > 1) {
113
- if (errors.has('anchorTagWithoutHref')) {
111
+ if (errors.has('nonInteractiveLink')) {
114
112
  errors.delete('nonInteractiveTrigger')
115
113
  }
116
- if (errors.has('hiddenInput')) {
114
+ if (errors.has('nonInteractiveInput')) {
117
115
  errors.delete('nonInteractiveTrigger')
118
116
  }
119
117
  }
@@ -135,11 +133,11 @@ module.exports = {
135
133
  ],
136
134
  messages: {
137
135
  nonInteractiveTrigger:
138
- 'The `Tooltip` component expects a single React element that contains interactive content. Consider using a `<button>` or equivalent interactive element instead.',
139
- anchorTagWithoutHref:
136
+ 'Tooltips should only be applied to interactive elements that are not disabled. Consider using a `<button>` or equivalent interactive element instead.',
137
+ nonInteractiveLink:
140
138
  'Anchor tags without an href attribute are not interactive, therefore they cannot be used as a trigger for a tooltip. Please add an href attribute or use an alternative interactive element instead',
141
- hiddenInput:
142
- 'Hidden inputs are not interactive and cannot be used as a trigger for a tooltip. Please use an alternate input type or use a different interactive element instead',
139
+ nonInteractiveInput:
140
+ 'Hidden or disabled inputs are not interactive and cannot be used as a trigger for a tooltip. Please use an alternate input type or use a different interactive element instead',
143
141
  singleChild: 'The `Tooltip` component expects a single React element as a child.'
144
142
  }
145
143
  },