Skip to content

Commit 0b66c90

Browse files
committed
[refactor] - Refactor hasAttribute to be more semantically correct to getAttribute (#38)
Returns undefined if it is not found… still a falsy value.
1 parent da3be73 commit 0b66c90

23 files changed

+90
-89
lines changed

src/rules/aria-unsupported-elements.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import DOM from '../util/attributes/DOM';
1212
import ARIA from '../util/attributes/ARIA';
13-
import hasAttribute from '../util/hasAttribute';
13+
import getAttribute from '../util/getAttribute';
1414
import getNodeType from '../util/getNodeType';
1515

1616
const errorMessage = 'This element does not support ARIA roles, states and properties.';
@@ -27,9 +27,9 @@ module.exports = context => ({
2727
}
2828

2929
const invalidAttributes = Object.keys(ARIA).concat('ROLE');
30-
const hasAria = hasAttribute(node.attributes, ...invalidAttributes);
30+
const hasInvalidAttribute = getAttribute(node.attributes, ...invalidAttributes) !== undefined;
3131

32-
if (hasAria) {
32+
if (hasInvalidAttribute) {
3333
context.report({
3434
node,
3535
message: errorMessage

src/rules/href-no-hash.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import hasAttribute from '../util/hasAttribute';
11+
import getAttribute from '../util/getAttribute';
1212
import getAttributeValue from '../util/getAttributeValue';
1313
import getNodeType from '../util/getNodeType';
1414

@@ -24,7 +24,7 @@ module.exports = context => ({
2424
return;
2525
}
2626

27-
const href = hasAttribute(node.attributes, 'href');
27+
const href = getAttribute(node.attributes, 'href');
2828
const value = getAttributeValue(href);
2929

3030
if (href && value === '#') {

src/rules/img-has-alt.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import hasAttribute from '../util/hasAttribute';
11+
import getAttribute from '../util/getAttribute';
1212
import getAttributeValue from '../util/getAttributeValue';
1313
import getNodeType from '../util/getNodeType';
1414

@@ -22,18 +22,18 @@ module.exports = context => ({
2222
return;
2323
}
2424

25-
const hasRoleProp = hasAttribute(node.attributes, 'role');
26-
const roleValue = getAttributeValue(hasRoleProp);
27-
const isPresentation = hasRoleProp && roleValue.toLowerCase() === 'presentation';
25+
const roleProp = getAttribute(node.attributes, 'role');
26+
const roleValue = getAttributeValue(roleProp);
27+
const isPresentation = roleProp && roleValue.toLowerCase() === 'presentation';
2828

2929
if (isPresentation) {
3030
return;
3131
}
3232

33-
const hasAltProp = hasAttribute(node.attributes, 'alt');
33+
const altProp = getAttribute(node.attributes, 'alt');
3434

3535
// Missing alt prop error.
36-
if (!hasAltProp) {
36+
if (altProp === undefined) {
3737
context.report({
3838
node,
3939
message: `${nodeType} elements must have an alt prop or use role="presentation".`
@@ -42,8 +42,8 @@ module.exports = context => ({
4242
}
4343

4444
// Check if alt prop is undefined.
45-
const altValue = getAttributeValue(hasAltProp);
46-
const isNullValued = hasAltProp.value === null; // <img alt />
45+
const altValue = getAttributeValue(altProp);
46+
const isNullValued = altProp.value === null; // <img alt />
4747

4848
if ((altValue && !isNullValued) || altValue === '') {
4949
return;

src/rules/img-redundant-alt.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import hasAttribute from '../util/hasAttribute';
11+
import getAttribute from '../util/getAttribute';
1212
import getAttributeValue from '../util/getAttributeValue';
1313
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
1414
import getNodeType from '../util/getNodeType';
@@ -34,9 +34,9 @@ module.exports = context => ({
3434
return;
3535
}
3636

37-
const altProp = hasAttribute(node.attributes, 'alt');
37+
const altProp = getAttribute(node.attributes, 'alt');
3838
// Return if alt prop is not present.
39-
if (altProp === false) {
39+
if (altProp === undefined) {
4040
return;
4141
}
4242

src/rules/label-has-for.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import hasAttribute from '../util/hasAttribute';
11+
import getAttribute from '../util/getAttribute';
1212
import getAttributeValue from '../util/getAttributeValue';
1313
import getNodeType from '../util/getNodeType';
1414

@@ -25,7 +25,7 @@ module.exports = context => ({
2525
return;
2626
}
2727

28-
const htmlForAttr = hasAttribute(node.attributes, 'htmlFor');
28+
const htmlForAttr = getAttribute(node.attributes, 'htmlFor');
2929
const htmlForValue = getAttributeValue(htmlForAttr);
3030
const isInvalid = htmlForAttr === false || !htmlForValue;
3131

src/rules/mouse-events-have-key-events.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// Rule Definition
1010
// ----------------------------------------------------------------------------
1111

12-
import hasAttribute from '../util/hasAttribute';
12+
import getAttribute from '../util/getAttribute';
1313
import getAttributeValue from '../util/getAttributeValue';
1414

1515
const mouseOverErrorMessage = 'onMouseOver must be accompanied by onFocus for accessibility.';
@@ -20,11 +20,11 @@ module.exports = context => ({
2020
const attributes = node.attributes;
2121

2222
// Check onmouseover / onfocus pairing.
23-
const hasOnMouseOver = hasAttribute(attributes, 'onMouseOver');
24-
const onMouseOverValue = getAttributeValue(hasOnMouseOver);
23+
const onMouseOver = getAttribute(attributes, 'onMouseOver');
24+
const onMouseOverValue = getAttributeValue(onMouseOver);
2525

26-
if (Boolean(hasOnMouseOver) === true && (onMouseOverValue !== null || onMouseOverValue !== undefined)) {
27-
const hasOnFocus = hasAttribute(attributes, 'onFocus');
26+
if (onMouseOver && (onMouseOverValue !== null || onMouseOverValue !== undefined)) {
27+
const hasOnFocus = getAttribute(attributes, 'onFocus');
2828
const onFocusValue = getAttributeValue(hasOnFocus);
2929

3030
if (hasOnFocus === false || onFocusValue === null || onFocusValue === undefined) {
@@ -36,10 +36,10 @@ module.exports = context => ({
3636
}
3737

3838
// Checkout onmouseout / onblur pairing
39-
const hasOnMouseOut = hasAttribute(attributes, 'onMouseOut');
40-
const onMouseOutValue = getAttributeValue(hasOnMouseOut);
41-
if (Boolean(hasOnMouseOut) === true && (onMouseOutValue !== null || onMouseOutValue !== undefined)) {
42-
const hasOnBlur = hasAttribute(attributes, 'onBlur');
39+
const onMouseOut = getAttribute(attributes, 'onMouseOut');
40+
const onMouseOutValue = getAttributeValue(onMouseOut);
41+
if (onMouseOut && (onMouseOutValue !== null || onMouseOutValue !== undefined)) {
42+
const hasOnBlur = getAttribute(attributes, 'onBlur');
4343
const onBlurValue = getAttributeValue(hasOnBlur);
4444

4545
if (hasOnBlur === false || onBlurValue === null || onBlurValue === undefined) {

src/rules/no-access-key.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import hasAttribute from '../util/hasAttribute';
11+
import getAttribute from '../util/getAttribute';
1212
import getAttributeValue from '../util/getAttributeValue';
1313

1414
const errorMessage = 'No access key attribute allowed. Inconsistencies ' +
@@ -17,10 +17,10 @@ const errorMessage = 'No access key attribute allowed. Inconsistencies ' +
1717

1818
module.exports = context => ({
1919
JSXOpeningElement: node => {
20-
const hasAccessKey = hasAttribute(node.attributes, 'accesskey');
21-
const accessKeyValue = getAttributeValue(hasAccessKey);
20+
const accessKey = getAttribute(node.attributes, 'accesskey');
21+
const accessKeyValue = getAttributeValue(accessKey);
2222

23-
if (Boolean(hasAccessKey) === true && Boolean(accessKeyValue) === true) {
23+
if (accessKey && accessKeyValue) {
2424
context.report({
2525
node,
2626
message: errorMessage

src/rules/no-onchange.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import hasAttribute from '../util/hasAttribute';
11+
import getAttribute from '../util/getAttribute';
1212

1313
const errorMessage = 'onBlur must be used instead of onchange, ' +
1414
'unless absolutely necessary and it causes no negative consequences ' +
1515
'for keyboard only or screen reader users.';
1616

1717
module.exports = context => ({
1818
JSXOpeningElement: node => {
19-
const hasOnChange = hasAttribute(node.attributes, 'onChange');
20-
const hasOnBlur = hasAttribute(node.attributes, 'onBlur');
19+
const onChange = getAttribute(node.attributes, 'onChange');
20+
const hasOnBlur = getAttribute(node.attributes, 'onBlur') !== undefined;
2121

22-
if (Boolean(hasOnChange) === true && hasOnBlur === false) {
22+
if (onChange && !hasOnBlur) {
2323
context.report({
2424
node,
2525
message: errorMessage

src/rules/onclick-has-focus.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
88
import isInteractiveElement from '../util/isInteractiveElement';
9-
import hasAttribute from '../util/hasAttribute';
9+
import getAttribute from '../util/getAttribute';
1010
import getNodeType from '../util/getNodeType';
1111
import getAttributeValue from '../util/getAttributeValue';
1212

@@ -21,7 +21,7 @@ const errorMessage = 'Elements with onClick handlers must be focusable. ' +
2121
module.exports = context => ({
2222
JSXOpeningElement: node => {
2323
const { attributes } = node;
24-
if (hasAttribute(attributes, 'onClick') === false) {
24+
if (getAttribute(attributes, 'onClick') === undefined) {
2525
return;
2626
}
2727

@@ -31,7 +31,7 @@ module.exports = context => ({
3131
return;
3232
} else if (isInteractiveElement(type, attributes)) {
3333
return;
34-
} else if (getAttributeValue(hasAttribute(attributes, 'tabIndex'))) {
34+
} else if (getAttributeValue(getAttribute(attributes, 'tabIndex'))) {
3535
return;
3636
}
3737

src/rules/onclick-has-role.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
99
import isInteractiveElement from '../util/isInteractiveElement';
10-
import hasAttribute from '../util/hasAttribute';
10+
import getAttribute from '../util/getAttribute';
1111
import getAttributeValue from '../util/getAttributeValue';
1212
import getNodeType from '../util/getNodeType';
1313

@@ -21,7 +21,7 @@ const errorMessage = 'Visible, non-interactive elements with click handlers must
2121
module.exports = context => ({
2222
JSXOpeningElement: node => {
2323
const attributes = node.attributes;
24-
if (hasAttribute(attributes, 'onclick') === false) {
24+
if (getAttribute(attributes, 'onclick') === undefined) {
2525
return;
2626
}
2727

@@ -31,7 +31,7 @@ module.exports = context => ({
3131
return;
3232
} else if (isInteractiveElement(type, attributes)) {
3333
return;
34-
} else if (getAttributeValue(hasAttribute(attributes, 'role'))) {
34+
} else if (getAttributeValue(getAttribute(attributes, 'role'))) {
3535
return;
3636
}
3737

0 commit comments

Comments
 (0)