| 1 | /**
|
|---|
| 2 | * @fileoverview Enforce img alt attribute does not have the word image, picture, or photo.
|
|---|
| 3 | * @author Ethan Cohen
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | // -----------------------------------------------------------------------------
|
|---|
| 7 | // Requirements
|
|---|
| 8 | // -----------------------------------------------------------------------------
|
|---|
| 9 |
|
|---|
| 10 | import { RuleTester } from 'eslint';
|
|---|
| 11 | import semver from 'semver';
|
|---|
| 12 | import { version as eslintVersion } from 'eslint/package.json';
|
|---|
| 13 | import parserOptionsMapper from '../../__util__/parserOptionsMapper';
|
|---|
| 14 | import parsers from '../../__util__/helpers/parsers';
|
|---|
| 15 | import rule from '../../../src/rules/img-redundant-alt';
|
|---|
| 16 |
|
|---|
| 17 | // -----------------------------------------------------------------------------
|
|---|
| 18 | // Tests
|
|---|
| 19 | // -----------------------------------------------------------------------------
|
|---|
| 20 |
|
|---|
| 21 | const array = [{
|
|---|
| 22 | components: ['Image'],
|
|---|
| 23 | words: ['Word1', 'Word2'],
|
|---|
| 24 | }];
|
|---|
| 25 |
|
|---|
| 26 | const componentsSettings = {
|
|---|
| 27 | 'jsx-a11y': {
|
|---|
| 28 | components: {
|
|---|
| 29 | Image: 'img',
|
|---|
| 30 | },
|
|---|
| 31 | },
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | const ruleTester = new RuleTester();
|
|---|
| 35 |
|
|---|
| 36 | const expectedError = {
|
|---|
| 37 | message: 'Redundant alt attribute. Screen-readers already announce `img` tags as an image. You don’t need to use the words `image`, `photo,` or `picture` (or any specified custom words) in the alt prop.',
|
|---|
| 38 | type: 'JSXOpeningElement',
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | ruleTester.run('img-redundant-alt', rule, {
|
|---|
| 42 | valid: parsers.all([].concat(
|
|---|
| 43 | { code: '<img alt="foo" />;' },
|
|---|
| 44 | { code: '<img alt="picture of me taking a photo of an image" aria-hidden />' },
|
|---|
| 45 | { code: '<img aria-hidden alt="photo of image" />' },
|
|---|
| 46 | { code: '<img ALt="foo" />;' },
|
|---|
| 47 | { code: '<img {...this.props} alt="foo" />' },
|
|---|
| 48 | { code: '<img {...this.props} alt={"foo"} />' },
|
|---|
| 49 | { code: '<img {...this.props} alt={alt} />' },
|
|---|
| 50 | { code: '<a />' },
|
|---|
| 51 | { code: '<img />' },
|
|---|
| 52 | { code: '<IMG />' },
|
|---|
| 53 | { code: '<img alt={undefined} />' },
|
|---|
| 54 | { code: '<img alt={`this should pass for ${now}`} />' },
|
|---|
| 55 | { code: '<img alt={`this should pass for ${photo}`} />' },
|
|---|
| 56 | { code: '<img alt={`this should pass for ${image}`} />' },
|
|---|
| 57 | { code: '<img alt={`this should pass for ${picture}`} />' },
|
|---|
| 58 | { code: '<img alt={`${photo}`} />' },
|
|---|
| 59 | { code: '<img alt={`${image}`} />' },
|
|---|
| 60 | { code: '<img alt={`${picture}`} />' },
|
|---|
| 61 | { code: '<img alt={"undefined"} />' },
|
|---|
| 62 | { code: '<img alt={() => {}} />' },
|
|---|
| 63 | { code: '<img alt={function(e){}} />' },
|
|---|
| 64 | { code: '<img aria-hidden={false} alt="Doing cool things." />' },
|
|---|
| 65 | { code: '<UX.Layout>test</UX.Layout>' },
|
|---|
| 66 | { code: '<img alt />' },
|
|---|
| 67 | { code: '<img alt={imageAlt} />' },
|
|---|
| 68 | { code: '<img alt={imageAlt.name} />' },
|
|---|
| 69 | semver.satisfies(eslintVersion, '>= 6') ? [
|
|---|
| 70 | { code: '<img alt={imageAlt?.name} />', languageOptions: { ecmaVersion: 2020 } },
|
|---|
| 71 | { code: '<img alt="Doing cool things" aria-hidden={foo?.bar}/>', languageOptions: { ecmaVersion: 2020 } },
|
|---|
| 72 | ] : [],
|
|---|
| 73 | { code: '<img alt="Photography" />;' },
|
|---|
| 74 | { code: '<img alt="ImageMagick" />;' },
|
|---|
| 75 | { code: '<Image alt="Photo of a friend" />' },
|
|---|
| 76 | { code: '<Image alt="Foo" />', settings: componentsSettings },
|
|---|
| 77 | { code: '<img alt="画像" />', options: [{ words: ['イメージ'] }] },
|
|---|
| 78 | )).map(parserOptionsMapper),
|
|---|
| 79 | invalid: parsers.all([].concat(
|
|---|
| 80 | { code: '<img alt="Photo of friend." />;', errors: [expectedError] },
|
|---|
| 81 | { code: '<img alt="Picture of friend." />;', errors: [expectedError] },
|
|---|
| 82 | { code: '<img alt="Image of friend." />;', errors: [expectedError] },
|
|---|
| 83 | { code: '<img alt="PhOtO of friend." />;', errors: [expectedError] },
|
|---|
| 84 | { code: '<img alt={"photo"} />;', errors: [expectedError] },
|
|---|
| 85 | { code: '<img alt="piCTUre of friend." />;', errors: [expectedError] },
|
|---|
| 86 | { code: '<img alt="imAGE of friend." />;', errors: [expectedError] },
|
|---|
| 87 | {
|
|---|
| 88 | code: '<img alt="photo of cool person" aria-hidden={false} />',
|
|---|
| 89 | errors: [expectedError],
|
|---|
| 90 | },
|
|---|
| 91 | {
|
|---|
| 92 | code: '<img alt="picture of cool person" aria-hidden={false} />',
|
|---|
| 93 | errors: [expectedError],
|
|---|
| 94 | },
|
|---|
| 95 | {
|
|---|
| 96 | code: '<img alt="image of cool person" aria-hidden={false} />',
|
|---|
| 97 | errors: [expectedError],
|
|---|
| 98 | },
|
|---|
| 99 | { code: '<img alt="photo" {...this.props} />', errors: [expectedError] },
|
|---|
| 100 | { code: '<img alt="image" {...this.props} />', errors: [expectedError] },
|
|---|
| 101 | { code: '<img alt="picture" {...this.props} />', errors: [expectedError] },
|
|---|
| 102 | {
|
|---|
| 103 | code: '<img alt={`picture doing ${things}`} {...this.props} />',
|
|---|
| 104 | errors: [expectedError],
|
|---|
| 105 | },
|
|---|
| 106 | {
|
|---|
| 107 | code: '<img alt={`photo doing ${things}`} {...this.props} />',
|
|---|
| 108 | errors: [expectedError],
|
|---|
| 109 | },
|
|---|
| 110 | {
|
|---|
| 111 | code: '<img alt={`image doing ${things}`} {...this.props} />',
|
|---|
| 112 | errors: [expectedError],
|
|---|
| 113 | },
|
|---|
| 114 | {
|
|---|
| 115 | code: '<img alt={`picture doing ${picture}`} {...this.props} />',
|
|---|
| 116 | errors: [expectedError],
|
|---|
| 117 | },
|
|---|
| 118 | {
|
|---|
| 119 | code: '<img alt={`photo doing ${photo}`} {...this.props} />',
|
|---|
| 120 | errors: [expectedError],
|
|---|
| 121 | },
|
|---|
| 122 | {
|
|---|
| 123 | code: '<img alt={`image doing ${image}`} {...this.props} />',
|
|---|
| 124 | errors: [expectedError],
|
|---|
| 125 | },
|
|---|
| 126 | { code: '<Image alt="Photo of a friend" />', errors: [expectedError], settings: componentsSettings },
|
|---|
| 127 |
|
|---|
| 128 | // TESTS FOR ARRAY OPTION TESTS
|
|---|
| 129 | { code: '<img alt="Word1" />;', options: array, errors: [expectedError] },
|
|---|
| 130 | { code: '<img alt="Word2" />;', options: array, errors: [expectedError] },
|
|---|
| 131 | { code: '<Image alt="Word1" />;', options: array, errors: [expectedError] },
|
|---|
| 132 | { code: '<Image alt="Word2" />;', options: array, errors: [expectedError] },
|
|---|
| 133 |
|
|---|
| 134 | { code: '<img alt="イメージ" />', options: [{ words: ['イメージ'] }], errors: [expectedError] },
|
|---|
| 135 | { code: '<img alt="イメージです" />', options: [{ words: ['イメージ'] }], errors: [expectedError] },
|
|---|
| 136 | )).map(parserOptionsMapper),
|
|---|
| 137 | });
|
|---|