Skip to content

Commit bbae2c4

Browse files
mattxwangljharb
authored andcommitted
[New] anchor-ambiguous-text: ignore punctuation
1 parent 8b889bf commit bbae2c4

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

__tests__/src/rules/anchor-ambiguous-text-test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ ruleTester.run('anchor-ambiguous-text', rule, {
7575
{ code: '<a>HERE</a>;', errors: [expectedError] },
7676
{ code: '<a>click here</a>;', errors: [expectedError] },
7777
{ code: '<a>learn more</a>;', errors: [expectedError] },
78+
{ code: '<a>learn more</a>;', errors: [expectedError] },
79+
{ code: '<a>learn more.</a>;', errors: [expectedError] },
80+
{ code: '<a>learn more?</a>;', errors: [expectedError] },
81+
{ code: '<a>learn more,</a>;', errors: [expectedError] },
82+
{ code: '<a>learn more!</a>;', errors: [expectedError] },
83+
{ code: '<a>learn more;</a>;', errors: [expectedError] },
84+
{ code: '<a>learn more:</a>;', errors: [expectedError] },
7885
{ code: '<a>link</a>;', errors: [expectedError] },
7986
{ code: '<a>a link</a>;', errors: [expectedError] },
8087
{ code: '<a aria-label="click here">something</a>;', errors: [expectedError] },

__tests__/src/util/getAccessibleChildText-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ describe('getAccessibleChildText', () => {
6464
), elementType)).toBe('bar');
6565
});
6666

67+
it('returns trimmed literal value for JSXText child', () => {
68+
expect(getAccessibleChildText(JSXElementMock(
69+
'a',
70+
[],
71+
[{ type: 'Literal', value: ' bar ' }],
72+
), elementType)).toBe('bar');
73+
});
74+
75+
it('returns space-collapsed literal value for JSXText child', () => {
76+
expect(getAccessibleChildText(JSXElementMock(
77+
'a',
78+
[],
79+
[{ type: 'Literal', value: 'foo bar' }],
80+
), elementType)).toBe('foo bar');
81+
});
82+
83+
it('returns punctuation-stripped literal value for JSXText child', () => {
84+
expect(getAccessibleChildText(JSXElementMock(
85+
'a',
86+
[],
87+
[{ type: 'Literal', value: 'foo, bar. baz? foo; bar:' }],
88+
), elementType)).toBe('foo bar baz foo bar');
89+
});
90+
6791
it('returns recursive value for JSXElement child', () => {
6892
expect(getAccessibleChildText(JSXElementMock(
6993
'a',

docs/rules/anchor-ambiguous-text.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The logic to calculate the inner text of an anchor is as follows:
3030

3131
Note that this rule still disallows ambiguous `aria-label` or `alt` values.
3232

33-
Note that this rule is case-insensitive and trims whitespace. It only looks for **exact matches**.
33+
Note that this rule is case-insensitive, trims whitespace, and ignores certain punctuation (`[,.?¿!‽¡;:]`). It only looks for **exact matches**.
3434

3535
### Succeed
3636
```jsx
@@ -43,8 +43,15 @@ Note that this rule is case-insensitive and trims whitespace. It only looks for
4343
```jsx
4444
<a>here</a>
4545
<a>HERE</a>
46-
<a>click here</a>
4746
<a>link</a>
47+
<a>click here</a>
48+
<a>learn more</a>
49+
<a>learn more.</a>
50+
<a>learn more,</a>
51+
<a>learn more?</a>
52+
<a>learn more!</a>
53+
<a>learn more:</a>
54+
<a>learn more;</a>
4855
<a>a link</a>
4956
<a> a link </a>
5057
<a><span> click </span> here</a> // goes through element children

src/util/getAccessibleChildText.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import isHiddenFromScreenReader from './isHiddenFromScreenReader';
1010
* Returns a new "standardized" string: all whitespace is collapsed to one space,
1111
* and the string is lowercase
1212
* @param {string} input
13-
* @returns lowercase, single-spaced, trimmed string
13+
* @returns lowercase, single-spaced, punctuation-stripped, trimmed string
1414
*/
1515
function standardizeSpaceAndCase(input: string): string {
16-
return input.trim().replace(/\s\s+/g, ' ').toLowerCase();
16+
return input
17+
.trim()
18+
.replace(/[,.?¿!¡;:]/g, '') // strip punctuation
19+
.replace(/\s\s+/g, ' ') // collapse spaces
20+
.toLowerCase();
1721
}
1822

1923
/**

0 commit comments

Comments
 (0)