From e83d9d09e023a92b2307e25296c1fe8d0b1b1ef9 Mon Sep 17 00:00:00 2001 From: Nano Miratus Date: Sun, 25 Feb 2024 21:08:54 +0100 Subject: [PATCH 1/3] fix a11y-svg-has-accessible-name considering whitespace JSXText --- lib/rules/a11y-svg-has-accessible-name.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/rules/a11y-svg-has-accessible-name.js b/lib/rules/a11y-svg-has-accessible-name.js index e8c92a8f..b452642a 100644 --- a/lib/rules/a11y-svg-has-accessible-name.js +++ b/lib/rules/a11y-svg-has-accessible-name.js @@ -16,10 +16,13 @@ module.exports = { const elementType = getElementType(context, node) if (elementType !== 'svg') return - // Check if there is a nested title element that is the first child of the `` + // Check if there is a nested title element that is the first non-whitespace child of the `` + const childrenWithoutWhitespace = node.parent.children + ?.filter(({type, value}) => (type === 'JSXText' && value.trim() !== '') || type !== 'JSXText') + const hasNestedTitleAsFirstChild = - node.parent.children?.[0]?.type === 'JSXElement' && - node.parent.children?.[0]?.openingElement?.name?.name === 'title' + childrenWithoutWhitespace?.[0]?.type === 'JSXElement' && + childrenWithoutWhitespace?.[0]?.openingElement?.name?.name === 'title' // Check if `aria-label` or `aria-labelledby` is set const hasAccessibleName = hasProp(node.attributes, 'aria-label') || hasProp(node.attributes, 'aria-labelledby') From 700161faaa8654476081d755362a838985a97ad3 Mon Sep 17 00:00:00 2001 From: Nano Miratus Date: Wed, 28 Feb 2024 04:37:59 +0100 Subject: [PATCH 2/3] convert confusing condition into ternary Co-authored-by: Andri Alexandrou --- lib/rules/a11y-svg-has-accessible-name.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/a11y-svg-has-accessible-name.js b/lib/rules/a11y-svg-has-accessible-name.js index b452642a..b4701f6d 100644 --- a/lib/rules/a11y-svg-has-accessible-name.js +++ b/lib/rules/a11y-svg-has-accessible-name.js @@ -18,7 +18,7 @@ module.exports = { // Check if there is a nested title element that is the first non-whitespace child of the `` const childrenWithoutWhitespace = node.parent.children - ?.filter(({type, value}) => (type === 'JSXText' && value.trim() !== '') || type !== 'JSXText') + ?.filter(({type, value}) => type === 'JSXText' ? value.trim() !== '' : type !== 'JSXText') const hasNestedTitleAsFirstChild = childrenWithoutWhitespace?.[0]?.type === 'JSXElement' && From 5f1fcb1e7f30c6f837953346e83ebc6a1fc85cd1 Mon Sep 17 00:00:00 2001 From: Nano Miratus Date: Thu, 29 Feb 2024 15:04:29 +0100 Subject: [PATCH 3/3] add test for a11y-svg-has-accessible-name fix code style --- lib/rules/a11y-svg-has-accessible-name.js | 5 +++-- tests/a11y-svg-has-accessible-name.js | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/rules/a11y-svg-has-accessible-name.js b/lib/rules/a11y-svg-has-accessible-name.js index b4701f6d..45e675fd 100644 --- a/lib/rules/a11y-svg-has-accessible-name.js +++ b/lib/rules/a11y-svg-has-accessible-name.js @@ -17,8 +17,9 @@ module.exports = { if (elementType !== 'svg') return // Check if there is a nested title element that is the first non-whitespace child of the `` - const childrenWithoutWhitespace = node.parent.children - ?.filter(({type, value}) => type === 'JSXText' ? value.trim() !== '' : type !== 'JSXText') + const childrenWithoutWhitespace = node.parent.children?.filter(({type, value}) => + type === 'JSXText' ? value.trim() !== '' : type !== 'JSXText', + ) const hasNestedTitleAsFirstChild = childrenWithoutWhitespace?.[0]?.type === 'JSXElement' && diff --git a/tests/a11y-svg-has-accessible-name.js b/tests/a11y-svg-has-accessible-name.js index c708ee9a..62442132 100644 --- a/tests/a11y-svg-has-accessible-name.js +++ b/tests/a11y-svg-has-accessible-name.js @@ -19,6 +19,12 @@ ruleTester.run('a11y-svg-has-accessible-name', rule, { { code: "Circle with a black outline and red fill", }, + { + code: ` + Circle with a black outline and red fill + + `, + }, { code: "", },