Skip to content

Commit 10a472a

Browse files
authored
[dep] - Integrate jsx-ast-utils (#56)
Extracted the main utility functions to package `jsx-ast-utils` [jsx-ast-utils](https://github.com/evcohen/jsx-ast-utils) Discussed in #54
1 parent b884b19 commit 10a472a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+95
-691
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"license": "MIT",
4242
"dependencies": {
4343
"damerau-levenshtein": "^1.0.0",
44+
"jsx-ast-utils": "^1.0.0",
4445
"object-assign": "^4.0.1"
4546
}
4647
}

src/rules/aria-proptypes.js

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

1111
import ariaAttributes from '../util/attributes/ARIA';
12-
import { getLiteralAttributeValue } from '../util/getAttributeValue';
12+
import { getLiteralPropValue } from 'jsx-ast-utils';
1313

1414
const errorMessage = (name, type, permittedValues) => {
1515
switch (type) {
@@ -59,7 +59,7 @@ module.exports = context => ({
5959
return;
6060
}
6161

62-
const value = getLiteralAttributeValue(attribute);
62+
const value = getLiteralPropValue(attribute);
6363

6464
// We only want to check literal prop values, so just pass if it's null.
6565
if (value === null) {

src/rules/aria-role.js

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

1111
import roles from '../util/attributes/role';
12-
import { getLiteralAttributeValue } from '../util/getAttributeValue';
12+
import { getLiteralPropValue } from 'jsx-ast-utils';
1313

1414
const errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';
1515

@@ -20,7 +20,7 @@ module.exports = context => ({
2020
return;
2121
}
2222

23-
const value = getLiteralAttributeValue(attribute);
23+
const value = getLiteralPropValue(attribute);
2424

2525
// If value is undefined, then the role attribute will be dropped in the DOM.
2626
// If value is null, then getLiteralAttributeValue is telling us that the value isn't in the form of a literal.

src/rules/aria-unsupported-elements.js

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

1111
import DOM from '../util/attributes/DOM';
1212
import ARIA from '../util/attributes/ARIA';
13-
import getAttribute from '../util/getAttribute';
14-
import getNodeType from '../util/getNodeType';
13+
import { hasAnyProp, elementType } from 'jsx-ast-utils';
1514

1615
const errorMessage = 'This element does not support ARIA roles, states and properties.';
1716

1817
module.exports = context => ({
1918
JSXOpeningElement: node => {
20-
const nodeType = getNodeType(node);
19+
const nodeType = elementType(node);
2120
const nodeAttrs = DOM[nodeType];
2221
const isReservedNodeType = nodeAttrs && nodeAttrs.reserved || false;
2322

@@ -27,7 +26,7 @@ module.exports = context => ({
2726
}
2827

2928
const invalidAttributes = Object.keys(ARIA).concat('ROLE');
30-
const hasInvalidAttribute = getAttribute(node.attributes, ...invalidAttributes) !== undefined;
29+
const hasInvalidAttribute = hasAnyProp(node.attributes, invalidAttributes);
3130

3231
if (hasInvalidAttribute) {
3332
context.report({

src/rules/href-no-hash.js

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

11-
import getAttribute from '../util/getAttribute';
12-
import getAttributeValue from '../util/getAttributeValue';
13-
import getNodeType from '../util/getNodeType';
11+
import { getProp, getPropValue, elementType } from 'jsx-ast-utils';
1412

1513
const errorMessage = 'Links must not point to "#". Use a more descriptive href or use a button instead.';
1614

1715
module.exports = context => ({
1816
JSXOpeningElement: node => {
1917
const typeCheck = [ 'a' ].concat(context.options[0]);
20-
const nodeType = getNodeType(node);
18+
const nodeType = elementType(node);
2119

2220
// Only check 'a' elements and custom types.
2321
if (typeCheck.indexOf(nodeType) === -1) {
2422
return;
2523
}
2624

27-
const href = getAttribute(node.attributes, 'href');
28-
const value = getAttributeValue(href);
25+
const href = getProp(node.attributes, 'href');
26+
const value = getPropValue(href);
2927

3028
if (href && value === '#') {
3129
context.report({

src/rules/img-has-alt.js

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

11-
import getAttribute from '../util/getAttribute';
12-
import getAttributeValue from '../util/getAttributeValue';
13-
import getNodeType from '../util/getNodeType';
11+
import { getProp, getPropValue, elementType } from 'jsx-ast-utils';
1412

1513
module.exports = context => ({
1614
JSXOpeningElement: node => {
1715
const typeCheck = [ 'img' ].concat(context.options[0]);
18-
const nodeType = getNodeType(node);
16+
const nodeType = elementType(node);
1917

2018
// Only check 'img' elements and custom types.
2119
if (typeCheck.indexOf(nodeType) === -1) {
2220
return;
2321
}
2422

25-
const roleProp = getAttribute(node.attributes, 'role');
26-
const roleValue = getAttributeValue(roleProp);
23+
const roleProp = getProp(node.attributes, 'role');
24+
const roleValue = getPropValue(roleProp);
2725
const isPresentation = roleProp && typeof roleValue === 'string' && roleValue.toLowerCase() === 'presentation';
2826

2927
if (isPresentation) {
3028
return;
3129
}
3230

33-
const altProp = getAttribute(node.attributes, 'alt');
31+
const altProp = getProp(node.attributes, 'alt');
3432

3533
// Missing alt prop error.
3634
if (altProp === undefined) {
@@ -42,7 +40,7 @@ module.exports = context => ({
4240
}
4341

4442
// Check if alt prop is undefined.
45-
const altValue = getAttributeValue(altProp);
43+
const altValue = getPropValue(altProp);
4644
const isNullValued = altProp.value === null; // <img alt />
4745

4846
if ((altValue && !isNullValued) || altValue === '') {

src/rules/img-redundant-alt.js

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

11-
import getAttribute from '../util/getAttribute';
12-
import getAttributeValue from '../util/getAttributeValue';
11+
import { getProp, getPropValue, elementType } from 'jsx-ast-utils';
1312
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
14-
import getNodeType from '../util/getNodeType';
1513

1614
const REDUNDANT_WORDS = [
1715
'image',
@@ -29,12 +27,12 @@ const validTypes = [
2927

3028
module.exports = context => ({
3129
JSXOpeningElement: node => {
32-
const type = getNodeType(node);
30+
const type = elementType(node);
3331
if (type !== 'img') {
3432
return;
3533
}
3634

37-
const altProp = getAttribute(node.attributes, 'alt');
35+
const altProp = getProp(node.attributes, 'alt');
3836
// Return if alt prop is not present.
3937
if (altProp === undefined) {
4038
return;
@@ -49,7 +47,7 @@ module.exports = context => ({
4947
return;
5048
}
5149

52-
const value = getAttributeValue(altProp);
50+
const value = getPropValue(altProp);
5351
const isVisible = isHiddenFromScreenReader(type, node.attributes) === false;
5452

5553
if (Boolean(value) && typeof value === 'string' && isVisible) {

src/rules/label-has-for.js

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

11-
import getAttribute from '../util/getAttribute';
12-
import getAttributeValue from '../util/getAttributeValue';
13-
import getNodeType from '../util/getNodeType';
11+
import { getProp, getPropValue, elementType } from 'jsx-ast-utils';
1412

1513
const errorMessage = 'Form controls using a label to identify them must be ' +
1614
'programmatically associated with the control using htmlFor';
1715

1816
module.exports = context => ({
1917
JSXOpeningElement: node => {
2018
const typeCheck = [ 'label' ].concat(context.options[0]);
21-
const nodeType = getNodeType(node);
19+
const nodeType = elementType(node);
2220

2321
// Only check 'label' elements and custom types.
2422
if (typeCheck.indexOf(nodeType) === -1) {
2523
return;
2624
}
2725

28-
const htmlForAttr = getAttribute(node.attributes, 'htmlFor');
29-
const htmlForValue = getAttributeValue(htmlForAttr);
26+
const htmlForAttr = getProp(node.attributes, 'htmlFor');
27+
const htmlForValue = getPropValue(htmlForAttr);
3028
const isInvalid = htmlForAttr === false || !htmlForValue;
3129

3230
if (isInvalid) {

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

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

12-
import getAttribute from '../util/getAttribute';
13-
import getAttributeValue from '../util/getAttributeValue';
12+
import { getProp, getPropValue } from 'jsx-ast-utils';
13+
1414

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

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

2626
if (onMouseOver && (onMouseOverValue !== null || onMouseOverValue !== undefined)) {
27-
const hasOnFocus = getAttribute(attributes, 'onFocus');
28-
const onFocusValue = getAttributeValue(hasOnFocus);
27+
const hasOnFocus = getProp(attributes, 'onFocus');
28+
const onFocusValue = getPropValue(hasOnFocus);
2929

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

3838
// Checkout onmouseout / onblur pairing
39-
const onMouseOut = getAttribute(attributes, 'onMouseOut');
40-
const onMouseOutValue = getAttributeValue(onMouseOut);
39+
const onMouseOut = getProp(attributes, 'onMouseOut');
40+
const onMouseOutValue = getPropValue(onMouseOut);
4141
if (onMouseOut && (onMouseOutValue !== null || onMouseOutValue !== undefined)) {
42-
const hasOnBlur = getAttribute(attributes, 'onBlur');
43-
const onBlurValue = getAttributeValue(hasOnBlur);
42+
const hasOnBlur = getProp(attributes, 'onBlur');
43+
const onBlurValue = getPropValue(hasOnBlur);
4444

4545
if (hasOnBlur === false || onBlurValue === null || onBlurValue === undefined) {
4646
context.report({

src/rules/no-access-key.js

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

11-
import getAttribute from '../util/getAttribute';
12-
import getAttributeValue from '../util/getAttributeValue';
11+
import { getProp, getPropValue } from 'jsx-ast-utils';
1312

1413
const errorMessage = 'No access key attribute allowed. Inconsistencies ' +
1514
'between keyboard shortcuts and keyboard comments used by screenreader ' +
1615
'and keyboard only users create a11y complications.';
1716

1817
module.exports = context => ({
1918
JSXOpeningElement: node => {
20-
const accessKey = getAttribute(node.attributes, 'accesskey');
21-
const accessKeyValue = getAttributeValue(accessKey);
19+
const accessKey = getProp(node.attributes, 'accesskey');
20+
const accessKeyValue = getPropValue(accessKey);
2221

2322
if (accessKey && accessKeyValue) {
2423
context.report({

0 commit comments

Comments
 (0)