Coder Social home page Coder Social logo

eslint-plugin-jsx-a11y's Introduction

build status npm version license Coverage Status Total npm downloads

Get professional support for eslint-plugin-jsx-a11y on Tidelift

eslint-plugin-jsx-a11y

Static AST checker for accessibility rules on JSX elements.

Read this in other languages.

Mexican Spanish๐Ÿ‡ฒ๐Ÿ‡ฝ

Why?

This plugin does aย static evaluation of the JSX to spot accessibility issues in React apps. Because it only catches errors in static code, use it in combination with @axe-core/react to test the accessibility of the rendered DOM. Consider theseย toolsย just as one step of a larger a11y testing process andย always test your apps with assistive technology.

Installation

If you are installing this plugin via eslint-config-airbnb, please follow these instructions.

You'll first need to install ESLint:

# npm
npm install eslint --save-dev

# yarn
yarn add eslint --dev

Next, install eslint-plugin-jsx-a11y:

# npm
npm install eslint-plugin-jsx-a11y --save-dev

# yarn
yarn add eslint-plugin-jsx-a11y --dev

Note: If you installed ESLint globally (using the -g flag in npm, or the global prefix in yarn) then you must also install eslint-plugin-jsx-a11y globally.

Usage

Add jsx-a11y to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["jsx-a11y"]
}

Then configure the rules you want to use under the rules section.

{
  "rules": {
    "jsx-a11y/rule-name": 2
  }
}

You can also enable all the recommended or strict rules at once. Add plugin:jsx-a11y/recommended or plugin:jsx-a11y/strict in extends:

{
  "extends": ["plugin:jsx-a11y/recommended"]
}

Configurations

As you are extending our configuration, you can omit "plugins": ["jsx-a11y"] from your .eslintrc configuration file.

{
  "settings": {
    "jsx-a11y": {
      "polymorphicPropName": "as",
      "components": {
        "CityInput": "input",
        "CustomButton": "button",
        "MyButton": "button",
        "RoundButton": "button"
      }
    }
  }
}

Component Mapping

To enable your custom components to be checked as DOM elements, you can set global settings in your configuration file by mapping each custom component name to a DOM element type.

Polymorphic Components

You can optionally use the polymorphicPropName setting to define the prop your code uses to create polymorphic components. This setting will be used determine the element type in rules that require semantic context.

For example, if you set the polymorphicPropName setting to as then this element:

<Box as="h3">Configurations </Box>

will be evaluated as an h3. If no polymorphicPropName is set, then the component will be evaluated as Box.

โš ๏ธ Polymorphic components can make code harder to maintain; please use this feature with caution.

Supported Rules

๐Ÿ’ผ Configurations enabled in.
๐Ÿšซ Configurations disabled in.
โ˜‘๏ธ Set in the recommended configuration.
๐Ÿ”’ Set in the strict configuration.
โŒ Deprecated.

Nameย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย  Description ๐Ÿ’ผ ๐Ÿšซ โŒ
accessible-emoji Enforce emojis are wrapped in <span> and provide screenreader access. โŒ
alt-text Enforce all elements that require alternative text have meaningful information to relay back to end user. โ˜‘๏ธ ๐Ÿ”’
anchor-ambiguous-text Enforce <a> text to not exactly match "click here", "here", "link", or "a link". โ˜‘๏ธ
anchor-has-content Enforce all anchors to contain accessible content. โ˜‘๏ธ ๐Ÿ”’
anchor-is-valid Enforce all anchors are valid, navigable elements. โ˜‘๏ธ ๐Ÿ”’
aria-activedescendant-has-tabindex Enforce elements with aria-activedescendant are tabbable. โ˜‘๏ธ ๐Ÿ”’
aria-props Enforce all aria-* props are valid. โ˜‘๏ธ ๐Ÿ”’
aria-proptypes Enforce ARIA state and property values are valid. โ˜‘๏ธ ๐Ÿ”’
aria-role Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role. โ˜‘๏ธ ๐Ÿ”’
aria-unsupported-elements Enforce that elements that do not support ARIA roles, states, and properties do not have those attributes. โ˜‘๏ธ ๐Ÿ”’
autocomplete-valid Enforce that autocomplete attributes are used correctly. โ˜‘๏ธ ๐Ÿ”’
click-events-have-key-events Enforce a clickable non-interactive element has at least one keyboard event listener. โ˜‘๏ธ ๐Ÿ”’
control-has-associated-label Enforce that a control (an interactive element) has a text label. โ˜‘๏ธ ๐Ÿ”’
heading-has-content Enforce heading (h1, h2, etc) elements contain accessible content. โ˜‘๏ธ ๐Ÿ”’
html-has-lang Enforce <html> element has lang prop. โ˜‘๏ธ ๐Ÿ”’
iframe-has-title Enforce iframe elements have a title attribute. โ˜‘๏ธ ๐Ÿ”’
img-redundant-alt Enforce <img> alt prop does not contain the word "image", "picture", or "photo". โ˜‘๏ธ ๐Ÿ”’
interactive-supports-focus Enforce that elements with interactive handlers like onClick must be focusable. โ˜‘๏ธ ๐Ÿ”’
label-has-associated-control Enforce that a label tag has a text label and an associated control. โ˜‘๏ธ ๐Ÿ”’
label-has-for Enforce that <label> elements have the htmlFor prop. โ˜‘๏ธ ๐Ÿ”’ โŒ
lang Enforce lang attribute has a valid value.
media-has-caption Enforces that <audio> and <video> elements must have a <track> for captions. โ˜‘๏ธ ๐Ÿ”’
mouse-events-have-key-events Enforce that onMouseOver/onMouseOut are accompanied by onFocus/onBlur for keyboard-only users. โ˜‘๏ธ ๐Ÿ”’
no-access-key Enforce that the accessKey prop is not used on any element to avoid complications with keyboard commands used by a screenreader. โ˜‘๏ธ ๐Ÿ”’
no-aria-hidden-on-focusable Disallow aria-hidden="true" from being set on focusable elements.
no-autofocus Enforce autoFocus prop is not used. โ˜‘๏ธ ๐Ÿ”’
no-distracting-elements Enforce distracting elements are not used. โ˜‘๏ธ ๐Ÿ”’
no-interactive-element-to-noninteractive-role Interactive elements should not be assigned non-interactive roles. โ˜‘๏ธ ๐Ÿ”’
no-noninteractive-element-interactions Non-interactive elements should not be assigned mouse or keyboard event listeners. โ˜‘๏ธ ๐Ÿ”’
no-noninteractive-element-to-interactive-role Non-interactive elements should not be assigned interactive roles. โ˜‘๏ธ ๐Ÿ”’
no-noninteractive-tabindex tabIndex should only be declared on interactive elements. โ˜‘๏ธ ๐Ÿ”’
no-onchange Enforce usage of onBlur over onChange on select menus for accessibility. โŒ
no-redundant-roles Enforce explicit role property is not the same as implicit/default role property on element. โ˜‘๏ธ ๐Ÿ”’
no-static-element-interactions Enforce that non-interactive, visible elements (such as <div>) that have click handlers use the role attribute. โ˜‘๏ธ ๐Ÿ”’
prefer-tag-over-role Enforces using semantic DOM elements over the ARIA role property.
role-has-required-aria-props Enforce that elements with ARIA roles must have all required attributes for that role. โ˜‘๏ธ ๐Ÿ”’
role-supports-aria-props Enforce that elements with explicit or implicit roles defined contain only aria-* properties supported by that role. โ˜‘๏ธ ๐Ÿ”’
scope Enforce scope prop is only used on <th> elements. โ˜‘๏ธ ๐Ÿ”’
tabindex-no-positive Enforce tabIndex value is not greater than zero. โ˜‘๏ธ ๐Ÿ”’

The following rules have extra options when in recommended mode:

no-interactive-element-to-noninteractive-role

'jsx-a11y/no-interactive-element-to-noninteractive-role': [
  'error',
  {
    tr: ['none', 'presentation'],
  },
]

no-noninteractive-element-interactions

'jsx-a11y/no-noninteractive-element-interactions': [
  'error',
  {
    handlers: [
      'onClick',
      'onMouseDown',
      'onMouseUp',
      'onKeyPress',
      'onKeyDown',
      'onKeyUp',
    ],
  },
]

no-noninteractive-element-to-interactive-role

'jsx-a11y/no-noninteractive-element-to-interactive-role': [
  'error',
  {
    ul: [
      'listbox',
      'menu',
      'menubar',
      'radiogroup',
      'tablist',
      'tree',
      'treegrid',
    ],
    ol: [
      'listbox',
      'menu',
      'menubar',
      'radiogroup',
      'tablist',
      'tree',
      'treegrid',
    ],
    li: ['menuitem', 'option', 'row', 'tab', 'treeitem'],
    table: ['grid'],
    td: ['gridcell'],
  },
]

no-noninteractive-tabindex

'jsx-a11y/no-noninteractive-tabindex': [
  'error',
  {
    tags: [],
    roles: ['tabpanel'],
  },
]

no-static-element-interactions

'jsx-a11y/no-noninteractive-element-interactions': [
  'error',
  {
    handlers: [
      'onClick',
      'onMouseDown',
      'onMouseUp',
      'onKeyPress',
      'onKeyDown',
      'onKeyUp',
    ],
  },
]

Creating a new rule

If you are developing new rules for this project, you can use the create-rule script to scaffold the new files.

./scripts/create-rule.js my-new-rule

Some background on WAI-ARIA, the AX Tree and Browsers

Accessibility API

An operating system will provide an accessibility API that maps application state and content onto input/output controllers such as a screen reader, braille device, keyboard, etc.

These APIs were developed as computer interfaces shifted from buffers (which are text-based and inherently quite accessible) to graphical user interfaces (GUIs). The first attempts to make GUIs accessible involved raster image parsing to recognize characters, words, etc. This information was stored in a parallel buffer and made accessible to assistive technology (AT) devices.

As GUIs became more complex, the raster parsing approach became untenable. Accessibility APIs were developed to replace them. Check out NSAccessibility (AXAPI) for an example. See Core Accessibility API Mappings 1.1 for more details.

Browsers

Browsers support an Accessibility API on a per operating system basis. For instance, Firefox implements the MSAA accessibility API on Windows, but does not implement the AXAPI on OSX.

The Accessibility (AX) Tree & DOM

From the W3 Core Accessibility API Mappings 1.1

The accessibility tree and the DOM tree are parallel structures. Roughly speaking the accessibility tree is a subset of the DOM tree. It includes the user interface objects of the user agent and the objects of the document. Accessible objects are created in the accessibility tree for every DOM element that should be exposed to assistive technology, either because it may fire an accessibility event or because it has a property, relationship or feature which needs to be exposed. Generally, if something can be trimmed out it will be, for reasons of performance and simplicity. For example, a <span> with just a style change and no semantics may not get its own accessible object, but the style change will be exposed by other means.

Browser vendors are beginning to expose the AX Tree through inspection tools. Chrome has an experiment available to enable their inspection tool.

You can also see a text-based version of the AX Tree in Chrome in the stable release version.

Viewing the AX Tree in Chrome

  1. Navigate to chrome://accessibility/ in Chrome.
  2. Toggle the accessibility off link for any tab that you want to inspect.
  3. A link labeled show accessibility tree will appear; click this link.
  4. Balk at the wall of text that gets displayed, but then regain your conviction.
  5. Use the browser's find command to locate strings and values in the wall of text.

Pulling it all together

A browser constructs an AX Tree as a subset of the DOM. ARIA heavily informs the properties of this AX Tree. This AX Tree is exposed to the system level Accessibility API which mediates assistive technology agents.

We model ARIA in the aria-query project. We model AXObjects (that comprise the AX Tree) in the axobject-query project. The goal of the WAI-ARIA specification is to be a complete declarative interface to the AXObject model. The in-draft 1.2 version is moving towards this goal. But until then, we must consider the semantics constructs afforded by ARIA as well as those afforded by the AXObject model (AXAPI) in order to determine how HTML can be used to express user interface affordances to assistive technology users.

License

eslint-plugin-jsx-a11y is licensed under the MIT License.

eslint-plugin-jsx-a11y's People

Contributors

almerosteyn avatar anthony0030 avatar badtant avatar beefancohen avatar bmish avatar denis-sokolov avatar eps1lon avatar greenkeeper[bot] avatar greenkeeperio-bot avatar hallzac2 avatar hozefaj avatar imtsuki avatar jessebeach avatar joshuakgoldberg avatar kendallgassner avatar khiga8 avatar lencioni avatar leos avatar ljharb avatar markreay avatar mattxwang avatar michaeldeboey avatar othree avatar qw-in avatar stevemao avatar tanmoyopenroot avatar uncommon-type avatar v2dha avatar vdh avatar wilcofiers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-plugin-jsx-a11y's Issues

Exception after update to 1.4.0

Since updating from 1.3.0 to 1.4.0, I'm getting the following stack trace when I run eslint:

> eslint '**/*.js' --ignore-path .eslintignore

Cannot read property 'name' of undefined
TypeError: Cannot read property 'name' of undefined
    at /Users/sdavenpo/code/my-proj/node_modules/eslint-plugin-jsx-a11y/lib/rules/role-supports-aria-props.js:59:54
    at Array.forEach (native)
    at EventEmitter.JSXOpeningElement (/Users/sdavenpo/code/my-proj/node_modules/eslint-plugin-jsx-a11y/lib/rules/role-supports-aria-props.js:58:23)
    at emitOne (events.js:101:20)
    at EventEmitter.emit (events.js:188:7)
    at NodeEventGenerator.enterNode (/Users/sdavenpo/code/my-proj/node_modules/eslint/lib/util/node-event-generator.js:40:22)
    at CodePathAnalyzer.enterNode (/Users/sdavenpo/code/my-proj/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:607:23)
    at CommentEventGenerator.enterNode (/Users/sdavenpo/code/my-proj/node_modules/eslint/lib/util/comment-event-generator.js:97:23)
    at Controller.traverser.traverse.enter (/Users/sdavenpo/code/my-proj/node_modules/eslint/lib/eslint.js:886:36)
    at Controller.__execute (/Users/sdavenpo/code/my-proj/node_modules/eslint/node_modules/estraverse/estraverse.js:397:31)

Any ideas? FWIW I'm running nodejs 6.2.1.

Rule to disallow non-native DOM element interactions

For example, onClick should only be valid on <a>, <button>, <input>, <textarea>, <select>โ€ฆ i'm sure I'm forgetting some, but there should not be an onClick on a <div>, for example.

The same should apply to keyboard events.

img-uses-alt does not allow empty string or possible empty string

This rule considers the following JSX bad:

function Foo() {
  return <img alt={foo || ''} />;
}

this is also considered bad:

function Foo() {
  return <img alt="" />;
}

but this is okay:

function Foo() {
  return <img alt={foo} />;
}

However, I think that all three should be okay. Empty strings can be used appropriately for alt text on images that are decorative.

Add a CHANGELOG

It would be really helpful to have a CHANGELOG.md file to track what's changed between versions.

JSON missing from lib directory in 1.0.0

I'm working on upgrading something to use 1.0.0, and am running into this error:

Error: Failed to load plugin jsx-a11y: Cannot find module '../util/attributes/ARIA'

I believe this is because the build step doesn't bring the JSON files over to the lib directory from src.

label-has-for rule should pass if containing nesting content

I am getting hit by the "label-has-for" rule in my code. It appears that every label is required to have an "htmlFor" that matches an ID for an element on the page, but I believe that this should be valid:

<label>
  <input />
</label>

According to this: https://www.w3.org/TR/html401/interact/forms.html#edef-LABEL

Referring to the for attribute: "When present, the value of this attribute must be the same as the value of the id attribute of some other control in the same document. When absent, the label being defined is associated with the element's contents."

The main hiccup I can think of is if you have multiple elements inside a label.

Clickable table row

I'm trying to create a clickable table row (search result with navigation), but I'm running into the no-static-element-interactions rule.

`<tr tabIndex="0" role="link" onClick={ this.onClick }>
(...)

`

With tabIndex and role set, isn' this a 'valid' accessible element? Or, do you have any alternatives, other than to not use a table?

`no-onchange` rule getting triggered on `<input />` and `<textarea />`

The explanation for using onBlur instead of or in addition to onChange only makes references to <select> elements in its explanation, but gets blanket-applied to all inputs, including regular text input and text areas. Is this intentional? If so, can we talk a little bit about why onChange is problematic in this scenario?

`jsx-a11y/aria-proptypes` errors: "The prop value with an expression type of JSXElement could not be resolved"

When I enable the jsx-a11y/aria-proptypes rule, I get the following error:

The prop value with an expression type of JSXElement could not be resolved.
  Please file issue to get this fixed immediately.
Error: The prop value with an expression type of JSXElement could not be resolved.
  Please file issue to get this fixed immediately.
    at Object.extractLiteral [as JSXExpressionContainer] ($PWD/node_modules/eslint-plugin-jsx-a11y/node_modules/jsx-ast-utils/lib/values/expressions/index.js:162:11)
    at getLiteralValue ($PWD/node_modules/eslint-plugin-jsx-a11y/node_modules/jsx-ast-utils/lib/values/index.js:65:35)
    at extractValue ($PWD/node_modules/eslint-plugin-jsx-a11y/node_modules/jsx-ast-utils/lib/getPropValue.js:24:12)
    at getLiteralPropValue ($PWD/node_modules/eslint-plugin-jsx-a11y/node_modules/jsx-ast-utils/lib/getPropValue.js:56:10)
    at EventEmitter.JSXAttribute ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/rules/aria-proptypes.js:71:56)
    at emitOne (events.js:82:20)
    at EventEmitter.emit (events.js:169:7)
    at NodeEventGenerator.enterNode ($PWD/node_modules/eslint/lib/util/node-event-generator.js:40:22)
    at CodePathAnalyzer.enterNode ($PWD/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:607:23)
    at CommentEventGenerator.enterNode ($PWD/node_modules/eslint/lib/util/comment-event-generator.js:97:23)
    at Controller.traverser.traverse.enter ($PWD/node_modules/eslint/lib/eslint.js:905:36)

I have no idea how to find out on which file it's erroring out, but it seems like it might be a bug in jsx-ast-utils.

Review: simplify and clarify rule names

Right now we have:

  • img-uses-alt: Enforce that img jsx elements use the alt attribute.
  • onclick-uses-role: Enforce that non-interactive, visible elements (such as div) that have click handlers use the role attribute.
  • mouse-events-map-to-key-events: Enforce that onMouseOver/onMouseOut are accompanied by onFocus/onBlur for strictly keyboard users.
  • use-onblur-not-onchange: Enforce that onBlur is used instead of onChange.
  • no-access-key: Enforce that the accessKey prop is not used on any element to avoid complications with keyboard commands used by a screenreader.
  • label-uses-for: Enforce that label elements have the htmlFor attribute
  • redundant-alt: Enforce img alt attribute does not contain the word image, picture, or photo.
  • no-hash-href: Enforce an anchor element's href prop value is not just #.
  • no-invalid-aria: Enforce all aria-* properties are valid.
  • valid-aria-role: Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role.
  • valid-aria-proptype: Enforce ARIA state and property values are valid.
  • role-requires-aria: Enforce that elements with ARIA roles must have all required attributes for that role.
  • no-unsupported-elements-use-aria: Enforce that elements that do not support ARIA roles, states and properties do not have those attributes.
  • avoid-positive-tabindex: Enforce tabIndex value is not greater than zero.

Some of these names are verbose and unclear. Feel free to suggest more consistently clear and simple rule names based on descriptions.

Whatever results from this issue will be used for v1.0.0 release fairly soon.

onclick-uses-role rule incorrectly reports violations non-DOM components

By default I think the onlick-uses-role should only report violations on DOM elements, like <div>, <span>, etc. Instead, it reports violations on non-DOM JSXElements.

Code
<TestComponent onClick={doFoo} />

Expected
No violations.

Actual
jsx-a11y/onclick-uses-role: Visible, non-interactive elements with click handlers must have role attribute.

Test Case
{ code: '<TestComponent onClick={doFoo} />', parserOptions },

jsx-a11y/label-has-for: `<label>` can also be a containing element

the jsx-a11y/label-has-for rule requires htmlFor=, but the <label> tag can also contain a form element:

<label><input type="checkbox">Enabled</label>

Often this ends up being a more useful way to use label.

It would be nice if this rule would allow you to leave out htmlFor if it contained a form element.

See the label spec: https://html.spec.whatwg.org/multipage/forms.html#the-label-element

If the for attribute is not specified, but the label element has a labelable element descendant, then the first such descendant in tree order is the label element's labeled control.

Rule proposal: AX_ARIA_12

I think that this would make a really good linter rule. https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_aria_12

This element does not support ARIA roles, states and properties

Some HTML elements should not be given ARIA attributes. This is often because they are not visible, for example meta, html, script, style.

Bad:

<!-- Bad: the meta element should not be given any ARIA attributes -->
<meta charset="UTF-8" aria-hidden="false">

Good:

<!-- Good: the meta element should not be given any ARIA attributes -->
<meta charset="UTF-8">

Rule for inputs without an associated label

Is there a rule for making sure every input has an associated label? I've switched on the rule "label-has-for" but it only checks whether the label has a for attribute. Lets say I just have an input and no label to it, that should produce an error.

<input type="text" id="id1" />

VS

<label for="id1">Some text</label>
<input type="text" id="id1" />

Also when a label is empty, there should be an error.

Better error handling/messaging

  • Show source of error (line # and filename)
  • Fail gracefully with unknown expression types when trying to extract its value
  • Smart suggestions for misspelled aria-* properties and role

tabIndex cannot be dynamic

It is seems really unfortunate that you can't pass a tabIndex in as a parameter to an element, like:

<a onClick={ something() } tabIndex={ dynamicIndex } />

This triggers the "onclick-has-focus" rule. Do you have any thoughts on that?

aria-multiselectable should be supported in role=tablist

Using the latest version of this plugin (2.2.0), I received the error when running eslint:

The attribute aria-multiselectable is not supported by the role tablist

I checked in the ARIA spec and it looks like aria-multiselectable should be allowed in a tablist role.

I think this is simply a matter of adding ARIA-MULTISELECTABLE to the TABLIST entry in https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/src/util/attributes/role.json#L2473-L2497

(If that's the case, happy to submit a PR.)

role-has-required-aria-props being applied to react components

We are trying to build a React component that takes in aria-props and passes them down to a child <input /> element. Thus, we have properties on our component like ariaAutocomplete, ariaExpanded, ariaOwns, etc.

However, we can't add a role prop because when we try to assign role="combobox" to the React component to be passed down, we end up triggering this rule because we are using ariaExpanded and not aria-expanded at this level.

Would it be possible to limit this rule to DOM level elements?

Unknown expression type `ThisExpression`

on v1.0.1:

TYPES[expression.type] is not a function
TypeError: TYPES[expression.type] is not a function
    at extract ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/values/expressions/index.js:102:32)
    at Object.extractValueFromMemberExpression [as MemberExpression] ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/values/expressions/MemberExpression.js:23:30)
    at extract ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/values/expressions/index.js:102:32)
    at Object.extractValueFromMemberExpression [as MemberExpression] ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/values/expressions/MemberExpression.js:23:30)
    at Object.extract [as JSXExpressionContainer] ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/values/expressions/index.js:102:32)
    at getValue ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/values/index.js:52:27)
    at extractValue ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/getAttributeValue.js:24:12)
    at getAttributeValue ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/util/getAttributeValue.js:41:10)
    at EventEmitter.JSXOpeningElement ($PWD/node_modules/eslint-plugin-jsx-a11y/lib/rules/img-has-alt.js:56:54)
    at emitOne (events.js:82:20)
    at EventEmitter.emit (events.js:169:7)

I added a console.log to line 101 in index.js and got:

Node {
  type: 'ThisExpression',
  start: 1419,
  end: 1423,
  loc: 
   SourceLocation {
     start: Position { line: 54, column: 17 },
     end: Position { line: 54, column: 21 } },
  range: [ 1419, 1423 ] }

Perhaps the comment "This will map correctly for *all* possible expression types." above the function is inaccurate, and it should be explicitly handling unknown expression types?

Extract AST JSX utils to separate repo/npm module

Just wanted to start a discussion and see if anyone thought there was value in maybe creating a jsx-ast-utils npm module so people can build other cool linting rules surrounding JSX. We can export functions like getAttribute, hasAttribute, getAttributeValue, and getLiteralAttributeValue.

I think there's value in keeping it separately maintained but would definitely be more overhead for fixing bugs here and defining a more robust and well defined API for those functions. Thoughts?

Be able to specify component and props for rules like href-no-hash

We have a component that takes a couple of href props, like hrefLeft and hrefRight. These end up being used further down in the chain as href attributes, so it would be nice to be able to specify this in the configuration for this rule so it can be checked.

There may be other rules that could use a similar style of configuration.

cc @wyattdanger

Rule proposal: AX_ARIA_03

I think that this would make a really good linter rule. https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules#ax_aria_03

Elements with ARIA roles must have all required attributes for that role

Good:

<!-- Good: the checkbox role requires the aria-checked state -->
<span role="checkbox" aria-checked="false" aria-labelledby="foo" tabindex="0"></span>

Bad:

<!-- Bad: the checkbox role requires the aria-checked state -->
<span role="checkbox" aria-labelledby="foo" tabindex="0"></span>

The WAI-ARIA spec has more information on "Required States and Properties"

anchor-has-content false positive for object spread

A stripped-down version of the component that anchor-has-content has an issue with:

const Button = ({ className, type, link, title, children }) => {
  const commonProps = { className, title, children };
  if (link) {
    return <Link {...commonProps} to={link} />;
  }
  return <button {...commonProps} type={type} />;
};

"Cannot read property 'type' of null" in valid-aria-role.js

v0.5.0

with the following JSX

function Foo() {
  return <Bar baz />;
}

I get the following error:

Cannot read property 'type' of null
TypeError: Cannot read property 'type' of null
    at EventEmitter.JSXAttribute (/path/to/project/node_modules/eslint-plugin-jsx-a11y/lib/rules/valid-aria-role.js:27:43)

However it works if I use this instead:

function Foo() {
  return <Bar baz={false} />;
}

Adding `||` to alt tag causes a false positive

Currently getting a false positive with this block of code:

<img
    alt={briefHeadline || "Player thumbnail"}
    src={`${posterImage}`}
    style={styles.img}
/>

Even though this passes:

<img
    alt={briefHeadline}
    src={`${posterImage}`}
    style={styles.img}
/>

This unfortunately means there's a chance briefHeadline could be null/undefined preventing an alt tag from being rendered. Any ideas of what could be wrong?

valid-aria-role does not allow lists of roles

This rule considers the following JSX bad:

function Foo() {
  return <div role="tabpanel row" />;
}

However, according to role mapping:

WAI-ARIA allows multiple roles to be specified as an ordered set of space-separated valid role tokens. The additional roles are fallback roles similar to the concept of specifying multiple fonts in case the first choice font type is not supported.

Adding options to image-redundant-alt

Hi,

I'm trying to do a pull request which add options to image-redundant-alt so that it can cater for any words the user wants. How do I add words to be included in the tests so that I can see that it's working when I run them? I tried to add it in src/index.js but when I log context.options it's empty.

Thanks

isInteractiveElement() test for higher level JSX components produces false negatives when components are similarly named to DOM elements

The test to skip testing higher level JSX components in isInteractiveElement seems to be too broad as it uses .toUpperCase() on the tagName before comparing with the map of DOM elements. This produces false negatives when a custom component is named similarly to a DOM element, but with capitalizations (eg. Button vs. button).

To my knowledge, removing the .toUpperCase() should fix this, but I'm not sure if there are non-React use cases of JSX where, for example, Button might mean the same as button, or if there were any other factors that required relaxing the case-sensitivity.

Rule proposal: autocomplete

I'm not entirely sure what this rule could do, or even if it is a11y related, but it crossed my mind and I figured I'd open an issue to discuss it.

HTML has an autocomplete attribute that can be used to provide powerful form autofilling features. Here's the spec: https://html.spec.whatwg.org/multipage/forms.html#autofill

It would be useful to have a rule that would at least validate the attribute values, but perhaps there is more that can be done?

Check for text in links

Both react-a11y and react-axe warns for links without any text. Maybe something to write a rule for here also.

from axe:
link-name
Ensures links have discernible text
wcag2a, wcag111, wcag412, section508, section508.22.a

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.