Skip to content

Commit

Permalink
Add warning and default to off
Browse files Browse the repository at this point in the history
  • Loading branch information
kendallgassner committed Jul 18, 2023
1 parent dd0695d commit 9d12654
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ Add `plugin:jsx-a11y/recommended` or `plugin:jsx-a11y/strict` in `extends`:
}
```

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

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.
> As you are extending our configuration, you can omit `"plugins": ["jsx-a11y"]` from your `.eslintrc` configuration file.
```json
{
"settings": {
"jsx-a11y": {
"polymorphicPropName": "as",
"components": {
"CityInput": "input",
"CustomButton": "button",
Expand All @@ -109,6 +109,24 @@ configuration file by mapping each custom component name to a DOM element type.
}
```

#### 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 Props

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 components:

`<Box as='h3'>Configurations </Box>`

will be evalutated as an `h3`. If no `polymorphicPropName` is set then the component will be evalutated as `Box`.

⚠️ Polymorphic props can make code harder to maintain please use this feature with caution.

## Supported Rules

<!-- begin auto-generated rules list -->
Expand Down
8 changes: 4 additions & 4 deletions __tests__/src/util/getElementType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('getElementType', () => {
it('should return the exact tag name for names that are in Object.prototype', () => {
expect(elementType(JSXElementMock('toString').openingElement)).toBe('toString');
});
it('should return the tag name provided by the polymorphic "as" prop', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('as', 'h1')]).openingElement)).toBe('h1');
it('should return the default tag name provided', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('as', 'h1')]).openingElement)).toBe('span');
});
});

Expand All @@ -46,8 +46,8 @@ describe('getElementType', () => {
expect(elementType(JSXElementMock('CityInput').openingElement)).toBe('CityInput');
});

it('should return the tag name provided by the polymorphic "as" prop', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('as', 'h1')]).openingElement)).toBe('h1');
it('should return the default tag name since not polymorphicPropName was provided', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('as', 'h1')]).openingElement)).toBe('span');
});
});

Expand Down
7 changes: 4 additions & 3 deletions src/util/getElementType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@

import type { JSXOpeningElement } from 'ast-types-flow';
import has from 'has';
import { elementType, getPropValue, getProp } from 'jsx-ast-utils';
import { elementType, getProp, getLiteralPropValue } from 'jsx-ast-utils';

import type { ESLintContext } from '../../flow/eslint';

const getElementType = (context: ESLintContext): ((node: JSXOpeningElement) => string) => {
const { settings } = context;
const polymorphicPropName = settings['jsx-a11y']?.polymorphicPropName ?? 'as';
const polymorphicPropName = settings['jsx-a11y']?.polymorphicPropName;
const componentMap = settings['jsx-a11y']?.components;

return (node: JSXOpeningElement): string => {
const rawType = getPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node);
const polymorphicProp = polymorphicPropName ? getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) : undefined;
const rawType = polymorphicProp ?? elementType(node);

if (!componentMap) {
return rawType;
Expand Down

0 comments on commit 9d12654

Please sign in to comment.