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

Bump aria-query and update to fix tests #448

Merged
merged 19 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/rules/role-supports-aria-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const elementRolesMap = new ObjectMap()
for (const [key, value] of elementRoles.entries()) {
// - Remove unused `constraints` key
delete key.constraints
key.attributes = key.attributes?.filter(attribute => !('constraints' in attribute))
// - Remove empty `attributes` key
if (!key.attributes || key.attributes?.length === 0) {
delete key.attributes
Expand Down Expand Up @@ -41,16 +40,31 @@ module.exports = {
// - Get the element’s name
const key = {name: getElementType(context, node)}
// - Get the element’s disambiguating attributes
for (const prop of ['aria-expanded', 'type', 'size', 'role', 'href', 'multiple', 'scope']) {
// - Only provide `aria-expanded` when it’s required for disambiguation
if (prop === 'aria-expanded' && key.name !== 'summary') continue
for (const prop of [
'aria-label',
'aria-labelledby',
'alt',
'type',
'size',
'role',
'href',
'multiple',
'scope',
'name',
]) {
Copy link
Contributor Author

@khiga8 khiga8 Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this doesn't consider ALL constraints, but I think it's fine for now since it will cover most cases we're concerned about (e.g. <span aria-label>.

Whatever isn't matched should be skipped and we should avoid raising false positives.

if ((prop === 'aria-labelledby' || prop === 'aria-label') && !['section', 'aside', 'form'].includes(key.name))
continue
if (prop === 'name' && key.name !== 'form') continue

const value = getPropValue(getProp(node.attributes, prop))
if (value) {
if (!('attributes' in key)) {
key.attributes = []
}
if (prop === 'href') {
key.attributes.push({name: prop})
if (prop === 'href' || prop === 'aria-labelledby' || prop === 'aria-label') {
key.attributes.push({name: prop, constraints: ['set']})
} else if (prop === 'alt' && value !== '') {
key.attributes.push({name: prop, constraints: ['set']})
} else {
key.attributes.push({name: prop, value})
}
Expand Down
31 changes: 22 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@github/browserslist-config": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^5.1.0",
"@typescript-eslint/parser": "^5.1.0",
"aria-query": "^5.1.3",
"aria-query": "^5.3.0",
"eslint-config-prettier": ">=8.0.0",
"eslint-plugin-escompat": "^3.3.3",
"eslint-plugin-eslint-comments": "^3.2.0",
Expand Down
54 changes: 16 additions & 38 deletions tests/role-supports-aria-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ ruleTester.run('role-supports-aria-props', rule, {
{code: '<a href="#" aria-owns />'},
{code: '<a href="#" aria-relevant />'},

// this will have global
{code: '<a aria-checked />'},
Comment on lines -60 to -61
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now treated as generic


// AREA TESTS - implicit role is `link`
{code: '<area href="#" aria-expanded />'},
{code: '<area href="#" aria-atomic />'},
Expand All @@ -78,30 +75,6 @@ ruleTester.run('role-supports-aria-props', rule, {
{code: '<area href="#" aria-owns />'},
{code: '<area href="#" aria-relevant />'},

// this will have global
{code: '<area aria-checked />'},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now treated as generic


// LINK TESTS - implicit role is `link`
Copy link
Contributor Author

@khiga8 khiga8 Jul 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<link> maps to nothing now so I'm deleting these tests.

{code: '<link href="#" aria-expanded />'},
{code: '<link href="#" aria-atomic />'},
{code: '<link href="#" aria-busy />'},
{code: '<link href="#" aria-controls />'},
{code: '<link href="#" aria-describedby />'},
{code: '<link href="#" aria-disabled />'},
{code: '<link href="#" aria-dropeffect />'},
{code: '<link href="#" aria-flowto />'},
{code: '<link href="#" aria-grabbed />'},
{code: '<link href="#" aria-hidden />'},
{code: '<link href="#" aria-haspopup />'},
{code: '<link href="#" aria-label />'},
{code: '<link href="#" aria-labelledby />'},
{code: '<link href="#" aria-live />'},
{code: '<link href="#" aria-owns />'},
{code: '<link href="#" aria-relevant />'},

// this will have global
{code: '<link aria-checked />'},

// this will have role of `img`
{code: '<img alt="foobar" aria-busy />'},

Expand Down Expand Up @@ -344,20 +317,25 @@ ruleTester.run('role-supports-aria-props', rule, {
{code: '<datalist aria-expanded />'},
{code: '<div role="heading" aria-level />'},
{code: '<div role="heading" aria-level="1" />'},
{code: '<link href="#" aria-expanded />'}, // link maps to nothing
],

invalid: [
// implicit basic checks
{
code: '<a href="#" aria-checked />',
errors: [getErrorMessage('aria-checked', 'link')],
code: '<area aria-checked />',
errors: [getErrorMessage('aria-checked', 'generic')],
},
{
code: '<area href="#" aria-checked />',
code: '<a aria-checked />',
errors: [getErrorMessage('aria-checked', 'generic')],
},
{
code: '<a href="#" aria-checked />',
errors: [getErrorMessage('aria-checked', 'link')],
},
{
code: '<link href="#" aria-checked />',
code: '<area href="#" aria-checked />',
errors: [getErrorMessage('aria-checked', 'link')],
},
{
Expand All @@ -370,7 +348,7 @@ ruleTester.run('role-supports-aria-props', rule, {
},
{
code: '<aside aria-checked />',
errors: [getErrorMessage('aria-checked', 'complementary')],
errors: [getErrorMessage('aria-checked', 'generic')],
},
{
code: '<ul aria-expanded />',
Expand All @@ -386,15 +364,15 @@ ruleTester.run('role-supports-aria-props', rule, {
},
{
code: '<aside aria-expanded />',
errors: [getErrorMessage('aria-expanded', 'complementary')],
errors: [getErrorMessage('aria-expanded', 'generic')],
},
{
code: '<article aria-expanded />',
errors: [getErrorMessage('aria-expanded', 'article')],
},
{
code: '<body aria-expanded />',
errors: [getErrorMessage('aria-expanded', 'document')],
errors: [getErrorMessage('aria-expanded', 'generic')],
},
{
code: '<li aria-expanded />',
Expand All @@ -414,6 +392,10 @@ ruleTester.run('role-supports-aria-props', rule, {
},
{
code: '<section aria-expanded />',
errors: [getErrorMessage('aria-expanded', 'generic')],
},
{
code: '<section aria-label="something" aria-expanded />',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If <section aria-label>, we treat it as region.

errors: [getErrorMessage('aria-expanded', 'region')],
},
{
Expand Down Expand Up @@ -480,10 +462,6 @@ ruleTester.run('role-supports-aria-props', rule, {
code: '<menu type="toolbar" aria-expanded />',
errors: [getErrorMessage('aria-expanded', 'toolbar')],
},
{
code: '<link href="#" aria-invalid />',
errors: [getErrorMessage('aria-invalid', 'link')],
},
{
code: '<area href="#" aria-invalid />',
errors: [getErrorMessage('aria-invalid', 'link')],
Expand Down