forked from jsx-eslint/eslint-plugin-jsx-a11y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanchor-ambiguous-text.js
71 lines (58 loc) · 2.04 KB
/
anchor-ambiguous-text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @fileoverview Enforce anchor text to not exactly match 'click here', 'here', 'link', 'learn more', and user-specified words.
* @author Matt Wang
* @flow
*/
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
import type { ESLintConfig, ESLintContext } from '../../flow/eslint';
import { arraySchema, generateObjSchema } from '../util/schemas';
import getAccessibleChildText from '../util/getAccessibleChildText';
import getElementType from '../util/getElementType';
const DEFAULT_AMBIGUOUS_WORDS = [
'click here',
'here',
'link',
'a link',
'learn more',
];
const schema = generateObjSchema({
words: arraySchema,
});
export default ({
meta: {
docs: {
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/anchor-ambiguous-text.md',
description: 'Enforce `<a>` text to not exactly match "click here", "here", "link", or "a link".',
},
schema: [schema],
},
create: (context: ESLintContext) => {
const elementType = getElementType(context);
const typesToValidate = ['a'];
const options = context.options[0] || {};
const { words = DEFAULT_AMBIGUOUS_WORDS } = options;
const ambiguousWords = new Set(words);
return {
JSXOpeningElement: (node) => {
const nodeType = elementType(node);
// Only check anchor elements and custom types.
if (typesToValidate.indexOf(nodeType) === -1) {
return;
}
const nodeText = getAccessibleChildText(node.parent, elementType);
if (!ambiguousWords.has(nodeText)) { // check the value
return;
}
context.report({
node,
message: 'Ambiguous text within anchor. Screen reader users rely on link text for context; the words "{{wordsList}}" are ambiguous and do not provide enough context.',
data: {
wordsList: words.join('", "'),
},
});
},
};
},
}: ESLintConfig);