Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebeach committed Dec 26, 2020
2 parents d85ce54 + 7d5511d commit ad474a0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 14 deletions.
6 changes: 6 additions & 0 deletions __tests__/src/rules/label-has-associated-control-test.js
Expand Up @@ -36,6 +36,9 @@ const htmlForValid = [
{ code: '<CustomLabel htmlFor="js_id" label="A label" />', options: [{ labelAttributes: ['label'], labelComponents: ['CustomLabel'] }] },
// Custom label attributes.
{ code: '<label htmlFor="js_id" label="A label" />', options: [{ labelAttributes: ['label'] }] },
// Glob support for controlComponents option.
{ code: '<CustomLabel htmlFor="js_id" aria-label="A label" />', options: [{ controlComponents: ['Custom*'] }] },
{ code: '<CustomLabel htmlFor="js_id" aria-label="A label" />', options: [{ controlComponents: ['*Label'] }] },
];
const nestingValid = [
{ code: '<label>A label<input /></label>' },
Expand All @@ -57,6 +60,9 @@ const nestingValid = [
{ code: '<label><span>A label<CustomInput /></span></label>', options: [{ controlComponents: ['CustomInput'] }] },
{ code: '<CustomLabel><span>A label<CustomInput /></span></CustomLabel>', options: [{ controlComponents: ['CustomInput'], labelComponents: ['CustomLabel'] }] },
{ code: '<CustomLabel><span label="A label"><CustomInput /></span></CustomLabel>', options: [{ controlComponents: ['CustomInput'], labelComponents: ['CustomLabel'], labelAttributes: ['label'] }] },
// Glob support for controlComponents option.
{ code: '<label><span>A label<CustomInput /></span></label>', options: [{ controlComponents: ['Custom*'] }] },
{ code: '<label><span>A label<CustomInput /></span></label>', options: [{ controlComponents: ['*Input'] }] },
];

const bothValid = [
Expand Down
48 changes: 48 additions & 0 deletions __tests__/src/util/mayContainChildComponent-test.js
Expand Up @@ -104,4 +104,52 @@ describe('mayContainChildComponent', () => {
});
});
});

describe('Glob name matching', () => {
describe('component name contains question mark ? - match any single character', () => {
it('should return true', () => {
expect(mayContainChildComponent(
JSXElementMock('div', [], [
JSXElementMock('FancyComponent'),
]),
'Fanc?Co??onent',
)).toBe(true);
});
it('should return false', () => {
expect(mayContainChildComponent(
JSXElementMock('div', [], [
JSXElementMock('FancyComponent'),
]),
'FancyComponent?',
)).toBe(false);
});
});

describe('component name contains asterisk * - match zero or more characters', () => {
it('should return true', () => {
expect(mayContainChildComponent(
JSXElementMock('div', [], [
JSXElementMock('FancyComponent'),
]),
'Fancy*',
)).toBe(true);
});
it('should return true', () => {
expect(mayContainChildComponent(
JSXElementMock('div', [], [
JSXElementMock('FancyComponent'),
]),
'*Component',
)).toBe(true);
});
it('should return true', () => {
expect(mayContainChildComponent(
JSXElementMock('div', [], [
JSXElementMock('FancyComponent'),
]),
'Fancy*C*t',
)).toBe(true);
});
});
});
});
2 changes: 1 addition & 1 deletion docs/rules/label-has-associated-control.md
Expand Up @@ -101,7 +101,7 @@ This rule takes one optional object argument of type object:

`labelComponents` is a list of custom React Component names that should be checked for an associated control.
`labelAttributes` is a list of attributes to check on the label component and its children for a label. Use this if you have a custom component that uses a string passed on a prop to render an HTML `label`, for example.
`controlComponents` is a list of custom React Components names that will output an input element.
`controlComponents` is a list of custom React Components names that will output an input element. [Glob format](https://linuxhint.com/bash_globbing_tutorial/) is also supported for specifying names (e.g., `Label*` matches `LabelComponent` but not `CustomLabel`, `????Label` matches `LinkLabel` but not `CustomLabel`).
`assert` asserts that the label has htmlFor, a nested label, both or either. Available options: `'htmlFor', 'nesting', 'both', 'either'`.
`depth` (default 2, max 25) is an integer that determines how deep within a `JSXElement` label the rule should look for text content or an element with a label to determine if the `label` element will have an accessible label.

Expand Down
23 changes: 12 additions & 11 deletions package.json
Expand Up @@ -30,15 +30,15 @@
"jest": "jest --coverage __tests__/**/*"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/plugin-transform-flow-strip-types": "^7.10.4",
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/plugin-transform-flow-strip-types": "^7.12.10",
"babel-eslint": "^10.1.0",
"babel-jest": "^24.9.0",
"babel-preset-airbnb": "^5.0.0",
"coveralls": "^3.1.0",
"eslint": "^3 || ^4 || ^5 || ^6 || ^7",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-flowtype": "^5.2.0",
"eslint-plugin-import": "^2.22.1",
"estraverse": "^5.2.0",
Expand All @@ -48,7 +48,7 @@
"jest": "^24.9.0",
"jscodeshift": "^0.7.0",
"minimist": "^1.2.5",
"object.assign": "^4.1.1",
"object.assign": "^4.1.2",
"rimraf": "^3.0.2",
"safe-publish-latest": "^1.1.4",
"to-ast": "^1.0.0"
Expand All @@ -58,17 +58,18 @@
},
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.11.2",
"@babel/runtime": "^7.12.5",
"aria-query": "^4.2.2",
"array-includes": "^3.1.1",
"array-includes": "^3.1.2",
"ast-types-flow": "^0.0.7",
"axe-core": "^4.0.2",
"axe-core": "^4.1.1",
"axobject-query": "^2.2.0",
"damerau-levenshtein": "^1.0.6",
"emoji-regex": "^9.0.0",
"emoji-regex": "^9.2.0",
"has": "^1.0.3",
"jsx-ast-utils": "^3.1.0",
"language-tags": "^1.0.5"
"jsx-ast-utils": "^3.2.0",
"language-tags": "^1.0.5",
"minimatch": "^3.0.4"
},
"peerDependencies": {
"eslint": "^3 || ^4 || ^5 || ^6 || ^7"
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -264,7 +264,7 @@ module.exports = {
],
},
],
'jsx-a11y/label-has-for': 'error',
'jsx-a11y/label-has-for': 'off',
'jsx-a11y/label-has-associated-control': 'error',
'jsx-a11y/media-has-caption': 'error',
'jsx-a11y/mouse-events-have-key-events': 'error',
Expand Down
3 changes: 2 additions & 1 deletion src/util/mayContainChildComponent.js
Expand Up @@ -9,6 +9,7 @@

import { elementType } from 'jsx-ast-utils';
import type { Node } from 'ast-types-flow';
import minimatch from 'minimatch';

export default function mayContainChildComponent(
root: Node,
Expand Down Expand Up @@ -37,7 +38,7 @@ export default function mayContainChildComponent(
if (
childNode.type === 'JSXElement'
&& childNode.openingElement
&& elementType(childNode.openingElement) === componentName
&& minimatch(elementType(childNode.openingElement), componentName)
) {
return true;
}
Expand Down

0 comments on commit ad474a0

Please sign in to comment.