Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow polymorphic linting to be restricted to specified components #984

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ For example, if you set the `polymorphicPropName` setting to `as` then this elem

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

To restrict polymorphic linting to specified components, additionally set `polymorphicAllowList` to an array of component names.

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

## Supported Rules
Expand Down
34 changes: 34 additions & 0 deletions __tests__/src/util/getElementType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,38 @@ describe('getElementType', () => {
expect(elementType(JSXElementMock('CustomButton', [JSXAttributeMock('as', 'a')]).openingElement)).toBe('button');
});
});

describe('polymorphicPropName settings and explicitly defined polymorphicAllowList in context', () => {
const elementType = getElementType({
settings: {
'jsx-a11y': {
polymorphicPropName: 'asChild',
polymorphicAllowList: [
'Box',
'Icon',
],
components: {
Box: 'div',
Icon: 'svg',
},
},
},
});

it('should not use the polymorphic prop if polymorphicAllowList is defined, but element is not part of polymorphicAllowList', () => {
expect(elementType(JSXElementMock('Spinner', [JSXAttributeMock('asChild', 'img')]).openingElement)).toBe('Spinner');
});

it('should use the polymorphic prop if it is in explicitly defined polymorphicAllowList', () => {
expect(elementType(JSXElementMock('Icon', [JSXAttributeMock('asChild', 'img')]).openingElement)).toBe('img');
});

it('should return the tag name provided by the polymorphic prop, "asChild", defined in the settings instead of the component mapping tag', () => {
expect(elementType(JSXElementMock('Box', [JSXAttributeMock('asChild', 'span')]).openingElement)).toBe('span');
});

it('should return the tag name provided by the component mapping if the polymorphic prop, "asChild", defined in the settings is not set', () => {
expect(elementType(JSXElementMock('Box', [JSXAttributeMock('as', 'a')]).openingElement)).toBe('div');
});
});
});
1 change: 1 addition & 0 deletions flow/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ESLintSettings = {
[string]: mixed,
'jsx-a11y'?: {
polymorphicPropName?: string,
polymorphicAllowList?: Array<string>,
components?: {[string]: string},
},
}
Expand Down
11 changes: 10 additions & 1 deletion src/util/getElementType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@

import type { JSXOpeningElement } from 'ast-types-flow';
import hasOwn from 'hasown';
import includes from 'array-includes';
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;
const polymorphicAllowList = settings['jsx-a11y']?.polymorphicAllowList;

const componentMap = settings['jsx-a11y']?.components;

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

let rawType = elementType(node);
if (polymorphicProp) {
if (!polymorphicAllowList || includes(polymorphicAllowList, rawType)) {
rawType = polymorphicProp;
}
}

if (!componentMap) {
return rawType;
Expand Down