From c3374d1fb913222041295cfbea04d6d0dba06110 Mon Sep 17 00:00:00 2001 From: Ethan Cohen Date: Fri, 10 Jun 2016 17:57:13 -0400 Subject: [PATCH] [new] - Integrate with latest jsx-ast-utils propName API more robust name extraction --- src/rules/aria-props.js | 5 +++-- src/rules/aria-proptypes.js | 6 +++--- src/rules/aria-role.js | 6 ++++-- src/rules/aria-unsupported-elements.js | 9 ++++++--- src/rules/role-has-required-aria-props.js | 6 ++++-- src/rules/role-supports-aria-props.js | 9 ++++++--- src/rules/tabindex-no-positive.js | 6 +++--- 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/rules/aria-props.js b/src/rules/aria-props.js index 08354aed5..58a2b17ee 100644 --- a/src/rules/aria-props.js +++ b/src/rules/aria-props.js @@ -9,6 +9,7 @@ import ariaAttributes from '../util/attributes/ARIA'; import getSuggestion from '../util/getSuggestion'; +import { propName } from 'jsx-ast-utils'; const errorMessage = name => { const dictionary = Object.keys(ariaAttributes).map(aria => aria.toLowerCase()); @@ -24,8 +25,8 @@ const errorMessage = name => { module.exports = context => ({ JSXAttribute: attribute => { - const name = attribute.name.name; - const normalizedName = name.toUpperCase(); + const name = propName(attribute); + const normalizedName = name ? name.toUpperCase() : ''; // `aria` needs to be prefix of property. if (normalizedName.indexOf('ARIA-') !== 0) { diff --git a/src/rules/aria-proptypes.js b/src/rules/aria-proptypes.js index 6865c0575..05389ce8f 100644 --- a/src/rules/aria-proptypes.js +++ b/src/rules/aria-proptypes.js @@ -8,7 +8,7 @@ // ---------------------------------------------------------------------------- import ariaAttributes from '../util/attributes/ARIA'; -import { getLiteralPropValue } from 'jsx-ast-utils'; +import { getLiteralPropValue, propName } from 'jsx-ast-utils'; const errorMessage = (name, type, permittedValues) => { switch (type) { @@ -52,8 +52,8 @@ const validityCheck = (value, expectedType, permittedValues) => { module.exports = context => ({ JSXAttribute: attribute => { - const name = attribute.name.name; - const normalizedName = name.toUpperCase(); + const name = propName(attribute); + const normalizedName = name ? name.toUpperCase() : ''; // Not a valid aria-* state or property. if (normalizedName.indexOf('ARIA-') !== 0 || ariaAttributes[normalizedName] === undefined) { diff --git a/src/rules/aria-role.js b/src/rules/aria-role.js index cffbecfd9..ad56ff49a 100644 --- a/src/rules/aria-role.js +++ b/src/rules/aria-role.js @@ -8,13 +8,15 @@ // ---------------------------------------------------------------------------- import roles from '../util/attributes/role'; -import { getLiteralPropValue } from 'jsx-ast-utils'; +import { getLiteralPropValue, propName } from 'jsx-ast-utils'; const errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.'; module.exports = context => ({ JSXAttribute: attribute => { - const normalizedName = attribute.name.name.toUpperCase(); + const name = propName(attribute); + const normalizedName = name ? name.toUpperCase() : ''; + if (normalizedName !== 'ROLE') { return; } diff --git a/src/rules/aria-unsupported-elements.js b/src/rules/aria-unsupported-elements.js index d16fa72f5..69e532ba2 100644 --- a/src/rules/aria-unsupported-elements.js +++ b/src/rules/aria-unsupported-elements.js @@ -10,7 +10,7 @@ import DOM from '../util/attributes/DOM'; import ARIA from '../util/attributes/ARIA'; -import { elementType } from 'jsx-ast-utils'; +import { elementType, propName } from 'jsx-ast-utils'; const errorMessage = invalidProp => `This element does not support ARIA roles, states and properties. \ @@ -34,10 +34,13 @@ module.exports = context => ({ return; } - if (invalidAttributes.indexOf(prop.name.name.toUpperCase()) > -1) { + const name = propName(prop); + const normalizedName = name ? name.toUpperCase() : ''; + + if (invalidAttributes.indexOf(normalizedName) > -1) { context.report({ node, - message: errorMessage(prop.name.name), + message: errorMessage(name), }); } }); diff --git a/src/rules/role-has-required-aria-props.js b/src/rules/role-has-required-aria-props.js index 234048946..71afde737 100644 --- a/src/rules/role-has-required-aria-props.js +++ b/src/rules/role-has-required-aria-props.js @@ -9,7 +9,7 @@ // ---------------------------------------------------------------------------- import validRoleTypes from '../util/attributes/role'; -import { getProp, getLiteralPropValue } from 'jsx-ast-utils'; +import { getProp, getLiteralPropValue, propName } from 'jsx-ast-utils'; const errorMessage = (role, requiredProps) => @@ -18,7 +18,9 @@ const errorMessage = (role, requiredProps) => module.exports = context => ({ JSXAttribute: attribute => { - const normalizedName = attribute.name.name.toUpperCase(); + const name = propName(attribute); + const normalizedName = name ? name.toUpperCase() : ''; + if (normalizedName !== 'ROLE') { return; } diff --git a/src/rules/role-supports-aria-props.js b/src/rules/role-supports-aria-props.js index 58ba8a81c..6e60e96bf 100644 --- a/src/rules/role-supports-aria-props.js +++ b/src/rules/role-supports-aria-props.js @@ -11,7 +11,7 @@ import ROLES from '../util/attributes/role'; import ARIA from '../util/attributes/ARIA'; import getImplicitRole from '../util/getImplicitRole'; -import { getProp, getLiteralPropValue, elementType } from 'jsx-ast-utils'; +import { getProp, getLiteralPropValue, elementType, propName } from 'jsx-ast-utils'; const errorMessage = (attr, role, tag, isImplicit) => { if (isImplicit) { @@ -47,10 +47,13 @@ module.exports = context => ({ return; } - if (invalidAriaPropsForRole.indexOf(prop.name.name.toUpperCase()) > -1) { + const name = propName(prop); + const normalizedName = name ? name.toUpperCase() : ''; + + if (invalidAriaPropsForRole.indexOf(normalizedName) > -1) { context.report({ node, - message: errorMessage(prop.name.name, roleValue, type, isImplicit), + message: errorMessage(name, roleValue, type, isImplicit), }); } }); diff --git a/src/rules/tabindex-no-positive.js b/src/rules/tabindex-no-positive.js index 158c36bb1..474aa6998 100644 --- a/src/rules/tabindex-no-positive.js +++ b/src/rules/tabindex-no-positive.js @@ -7,14 +7,14 @@ // Rule Definition // ---------------------------------------------------------------------------- -import { getLiteralPropValue } from 'jsx-ast-utils'; +import { getLiteralPropValue, propName } from 'jsx-ast-utils'; const errorMessage = 'Avoid positive integer values for tabIndex.'; module.exports = context => ({ JSXAttribute: attribute => { - const name = attribute.name.name; - const normalizedName = name.toUpperCase(); + const name = propName(attribute); + const normalizedName = name ? name.toUpperCase() : ''; // Check if tabIndex is the attribute if (normalizedName !== 'TABINDEX') {