From eb8024cadc6d9b313f019fef14953ba0977a3ee7 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 11 Jul 2023 16:28:59 +0000 Subject: [PATCH 1/5] Create rule: a11y-no-title-attribute --- README.md | 1 + docs/rules/a11y-no-title-attribute.md | 7 +++++ lib/index.js | 1 + lib/rules/a11y-no-title-attribute.js | 30 +++++++++++++++++++ ...-no-visually-hidden-interactive-element.js | 1 - tests/a11y-no-title-attribute.js | 28 +++++++++++++++++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 docs/rules/a11y-no-title-attribute.md create mode 100644 lib/rules/a11y-no-title-attribute.js create mode 100644 tests/a11y-no-title-attribute.js diff --git a/README.md b/README.md index 67bdfb84..4449cf81 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ This config will be interpreted in the following way: | :------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | :- | :- | :- | | [a11y-aria-label-is-well-formatted](docs/rules/a11y-aria-label-is-well-formatted.md) | [aria-label] text should be formatted as you would visual text. | ⚛️ | | | | [a11y-no-generic-link-text](docs/rules/a11y-no-generic-link-text.md) | disallow generic link text | | | ❌ | +| [a11y-no-title-attribute](docs/rules/a11y-no-title-attribute.md) | Guards against developers using the title attribute | | | | | [a11y-no-visually-hidden-interactive-element](docs/rules/a11y-no-visually-hidden-interactive-element.md) | Ensures that interactive elements are not visually hidden | ⚛️ | | | | [array-foreach](docs/rules/array-foreach.md) | enforce `for..of` loops over `Array.forEach` | ✅ | | | | [async-currenttarget](docs/rules/async-currenttarget.md) | disallow `event.currentTarget` calls inside of async functions | 🔍 | | | diff --git a/docs/rules/a11y-no-title-attribute.md b/docs/rules/a11y-no-title-attribute.md new file mode 100644 index 00000000..1da5cecc --- /dev/null +++ b/docs/rules/a11y-no-title-attribute.md @@ -0,0 +1,7 @@ +# Guards against developers using the title attribute (`github/a11y-no-title-attribute`) + + + +## Rule Details + +## Version diff --git a/lib/index.js b/lib/index.js index e3bd98a5..40f79c0e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,6 +2,7 @@ module.exports = { rules: { 'a11y-no-visually-hidden-interactive-element': require('./rules/a11y-no-visually-hidden-interactive-element'), 'a11y-no-generic-link-text': require('./rules/a11y-no-generic-link-text'), + 'a11y-no-title-attribute': require('./rules/a11y-no-title-attribute'), 'a11y-aria-label-is-well-formatted': require('./rules/a11y-aria-label-is-well-formatted'), 'array-foreach': require('./rules/array-foreach'), 'async-currenttarget': require('./rules/async-currenttarget'), diff --git a/lib/rules/a11y-no-title-attribute.js b/lib/rules/a11y-no-title-attribute.js new file mode 100644 index 00000000..1c5738d5 --- /dev/null +++ b/lib/rules/a11y-no-title-attribute.js @@ -0,0 +1,30 @@ +const {getProp, getPropValue} = require('jsx-ast-utils') +const {getElementType} = require('../utils/get-element-type') + +module.exports = { + meta: { + docs: { + description: 'Guards against developers using the title attribute', + url: require('../url')(module), + }, + schema: [], + }, + + create(context) { + return { + JSXElement: node => { + const elementType = getElementType(context, node.openingElement) + if (elementType !== `iframe`) { + const titleProp = getPropValue(getProp(node.openingElement.attributes, `title`)) + if (titleProp) { + context.report({ + node, + message: + "The title attribute is not accessible and should never be used unless for an `'}, + {code: 'some information'}, + {code: 'GitHub'}, + ], + invalid: [ + {code: 'GitHub', errors: [{message: errorMessage}]}, + {code: '', errors: [{message: errorMessage}]}, + ], +}) From b35851510864ec03c9836da58f53742eda196030 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 11 Jul 2023 16:35:56 +0000 Subject: [PATCH 2/5] Add tests that check various component mappings --- tests/a11y-no-title-attribute.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/a11y-no-title-attribute.js b/tests/a11y-no-title-attribute.js index 40924bd2..1022dbae 100644 --- a/tests/a11y-no-title-attribute.js +++ b/tests/a11y-no-title-attribute.js @@ -20,9 +20,41 @@ ruleTester.run('a11y-no-title-attribute', rule, { {code: ''}, {code: 'some information'}, {code: 'GitHub'}, + { + code: 'Submit', + settings: { + github: { + components: { + Component: 'iframe', + }, + }, + }, + }, ], invalid: [ {code: 'GitHub', errors: [{message: errorMessage}]}, {code: '', errors: [{message: errorMessage}]}, + { + code: 'Submit', + errors: [{message: errorMessage}], + settings: { + github: { + components: { + Link: 'a', + }, + }, + }, + }, + { + code: 'Submit', + errors: [{message: errorMessage}], + settings: { + github: { + components: { + Component: 'iframe', + }, + }, + }, + }, ], }) From 2941475347ada9827e07ce772a091270a8b05920 Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 11 Jul 2023 16:36:47 +0000 Subject: [PATCH 3/5] Add rule to react config --- lib/configs/react.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/configs/react.js b/lib/configs/react.js index 63021526..77a9b57a 100644 --- a/lib/configs/react.js +++ b/lib/configs/react.js @@ -11,6 +11,7 @@ module.exports = { 'jsx-a11y/role-supports-aria-props': 'off', // Override with github/role-supports-aria-props until https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/910 is resolved 'github/a11y-aria-label-is-well-formatted': 'error', 'github/a11y-no-visually-hidden-interactive-element': 'error', + 'github/a11y-no-title-attribute': 'error', 'github/role-supports-aria-props': 'error', 'jsx-a11y/no-aria-hidden-on-focusable': 'error', 'jsx-a11y/no-autofocus': 'off', From 92f656d656576bc444fb049937c9b66a06e174fb Mon Sep 17 00:00:00 2001 From: Kendall Gassner Date: Tue, 11 Jul 2023 16:55:36 +0000 Subject: [PATCH 4/5] better docs --- docs/rules/a11y-no-title-attribute.md | 36 +++++++++++++++++++++++++++ lib/rules/a11y-no-title-attribute.js | 3 +-- tests/a11y-no-title-attribute.js | 3 +-- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/docs/rules/a11y-no-title-attribute.md b/docs/rules/a11y-no-title-attribute.md index 1da5cecc..b63774ca 100644 --- a/docs/rules/a11y-no-title-attribute.md +++ b/docs/rules/a11y-no-title-attribute.md @@ -2,6 +2,42 @@ +The title attribute is strongly discouraged. The only exception is on an ` +``` + ## Version diff --git a/lib/rules/a11y-no-title-attribute.js b/lib/rules/a11y-no-title-attribute.js index 1c5738d5..8e4e9f2d 100644 --- a/lib/rules/a11y-no-title-attribute.js +++ b/lib/rules/a11y-no-title-attribute.js @@ -19,8 +19,7 @@ module.exports = { if (titleProp) { context.report({ node, - message: - "The title attribute is not accessible and should never be used unless for an `