-
-
Notifications
You must be signed in to change notification settings - Fork 642
no-static-element-interactions
custom attribute mapping
#1052
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
Open
sarahvharris
wants to merge
8
commits into
jsx-eslint:main
Choose a base branch
from
sarahvharris:issue1051
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc2c3d1
Adds custom config option for no-static-element-interactions to allow…
sarahvharris 7a4a9c5
[New]: add attributes rule option for `no-static-element-interactions…
sarahvharris d1fa087
[Feat]: add `attributes` option to the global `components` option
sarahvharris f43e0c7
[chore]: revert md format changes
sarahvharris c5f68b9
[chore]: tests, getAttributes clean-up
sarahvharris da0ca32
[chore]: rename getAttributes to getSettingsAttributes
sarahvharris 42396c2
[chore]: add more tests, fix logic for non-mapped attrs in settings
sarahvharris fe44166
[chore]: outlier test cases
sarahvharris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import test from 'tape'; | ||
|
||
import getSettingsAttributes from '../../../src/util/getSettingsAttributes'; | ||
import JSXElementMock from '../../../__mocks__/JSXElementMock'; | ||
import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock'; | ||
|
||
test('getSettingsAttributes', (t) => { | ||
t.test('no settings in context', (st) => { | ||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('a', [JSXAttributeMock('foo', 'path/to/page')]).openingElement, {}), | ||
[JSXAttributeMock('foo', 'path/to/page')], | ||
'returns existing attributes when no component settings exist', | ||
); | ||
|
||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('Link', [JSXAttributeMock('foo', 'path/to/page')]).openingElement, {}), | ||
[JSXAttributeMock('foo', 'path/to/page')], | ||
'returns existing attributes when no component settings exist', | ||
); | ||
|
||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('Link', [JSXAttributeMock('foo', 'path/to/page')]).openingElement, { | ||
'jsx-a11y': {}, | ||
}), | ||
[JSXAttributeMock('foo', 'path/to/page')], | ||
'returns existing attributes when `components` is empty', | ||
); | ||
|
||
st.end(); | ||
}); | ||
|
||
t.test('with component settings mapping', (st) => { | ||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('Link', [JSXAttributeMock('foo', 'path/to/page')]).openingElement, { | ||
'jsx-a11y': { | ||
components: { | ||
Link: { | ||
component: 'a', | ||
attributes: { | ||
href: ['foo'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
[JSXAttributeMock('href', 'path/to/page')], | ||
'returns the exisiting attributes and the mapped attributes', | ||
); | ||
|
||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('Link', [JSXAttributeMock('bar', 'path/to/page')]).openingElement, { | ||
'jsx-a11y': { | ||
components: { | ||
Link: { | ||
component: 'a', | ||
attributes: { | ||
href: ['foo', 'bar'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
[JSXAttributeMock('href', 'path/to/page')], | ||
'returns the exisiting attributes and the mapped attributes', | ||
); | ||
|
||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('button', [JSXAttributeMock('foo', 'path/to/page')]).openingElement, { | ||
'jsx-a11y': { | ||
components: { | ||
Link: { | ||
component: 'a', | ||
attributes: { | ||
href: ['foo'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
[JSXAttributeMock('foo', 'path/to/page')], | ||
'should return the existing attributes when no mapping exists', | ||
); | ||
|
||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('Link', [JSXAttributeMock('bar', 'path/to/page')]).openingElement, { | ||
'jsx-a11y': { | ||
components: { | ||
Link: { | ||
component: 'a', | ||
attributes: { | ||
href: ['foo'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
[JSXAttributeMock('bar', 'path/to/page')], | ||
'returns the exisiting attributes when no mapping exists', | ||
); | ||
|
||
st.deepEqual( | ||
getSettingsAttributes(JSXElementMock('Link', [JSXAttributeMock('foo', 'path/to/page')]).openingElement, { | ||
settings: { | ||
'jsx-a11y': { | ||
components: { | ||
Link: 'a', | ||
}, | ||
}, | ||
}, | ||
}), | ||
[JSXAttributeMock('foo', 'path/to/page')], | ||
'returns existing attributes when components mapping to the component does not have attributes', | ||
); | ||
|
||
st.end(); | ||
}); | ||
|
||
t.end(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// @flow | ||
|
||
import type { Node, JSXOpeningElement } from 'ast-types-flow'; | ||
import { elementType, getProp, propName } from 'jsx-ast-utils'; | ||
import type { ESLintSettings } from '../../flow/eslint'; | ||
|
||
/** | ||
* Looks at the `settings` global config for custom component attribute mappings | ||
* This works for use cases where a custom component uses a prop name that renders to a specific attribute on the DOM element | ||
* | ||
* @example | ||
* { | ||
* 'jsx-a11y': { | ||
* components: { | ||
* Link: { | ||
* component: 'a', | ||
* attributes: { | ||
* href: ['foo'], | ||
* }, | ||
* }, | ||
* }, | ||
* }, | ||
* } | ||
* | ||
* <Link foo="path/to/page" /> // will be checked as if <a href="path/to/page" /> | ||
*/ | ||
const getSettingsAttributes = (node: JSXOpeningElement, settings: ESLintSettings): Node[] => { | ||
const { attributes: rawAttributes } = node; | ||
const { components } = settings?.['jsx-a11y'] || {}; | ||
|
||
if (!components || typeof components !== 'object') return rawAttributes; | ||
|
||
const jsxElementName = elementType(node); | ||
const componentConfig = components[jsxElementName]; | ||
|
||
const { attributes: settingsAttributes } = typeof componentConfig === 'object' ? componentConfig : {}; | ||
|
||
if (!settingsAttributes || typeof settingsAttributes !== 'object') return rawAttributes; | ||
|
||
const mappedRawAttrNames = new Set(); | ||
|
||
const mappedAttributes = Object.entries(settingsAttributes).flatMap(([originalAttr, mappedAttrs]) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ljharb Object.entires and flatMap failing on older node versions - should I be pulling from |
||
if (!Array.isArray(mappedAttrs)) return []; | ||
|
||
return mappedAttrs.flatMap((mappedAttr) => { | ||
const originalProp = getProp(rawAttributes, mappedAttr); | ||
|
||
if (originalProp) { | ||
mappedRawAttrNames.add(mappedAttr); | ||
return [{ | ||
...originalProp, | ||
name: { | ||
...originalProp.name, | ||
name: originalAttr, | ||
}, | ||
}]; | ||
} | ||
return []; | ||
}); | ||
}); | ||
|
||
// raw attributes that don't have mappings | ||
const unmappedAttributes = rawAttributes.filter((attr) => !mappedRawAttrNames.has(propName(attr))); | ||
|
||
return [...unmappedAttributes, ...mappedAttributes]; | ||
}; | ||
|
||
export default getSettingsAttributes; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.