Skip to content

Commit ea877c4

Browse files
uncommon-typeljharb
authored andcommittedFeb 18, 2022
[Tests] img-redundant-alt-test: add passing tests
- Add test cases with optional chaining - Add parserOptions prop to each test case to configure parser behaviour Closes #794
1 parent f878d3b commit ea877c4

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed
 

‎__tests__/src/rules/img-redundant-alt-test.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// -----------------------------------------------------------------------------
99

1010
import { RuleTester } from 'eslint';
11+
import semver from 'semver';
12+
import { version as eslintVersion } from 'eslint/package.json';
1113
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1214
import rule from '../../../src/rules/img-redundant-alt';
1315

@@ -28,7 +30,7 @@ const expectedError = {
2830
};
2931

3032
ruleTester.run('img-redundant-alt', rule, {
31-
valid: [
33+
valid: [].concat(
3234
{ code: '<img alt="foo" />;' },
3335
{ code: '<img alt="picture of me taking a photo of an image" aria-hidden />' },
3436
{ code: '<img aria-hidden alt="photo of image" />' },
@@ -52,11 +54,16 @@ ruleTester.run('img-redundant-alt', rule, {
5254
{ code: '<img alt={function(e){}} />' },
5355
{ code: '<img aria-hidden={false} alt="Doing cool things." />' },
5456
{ code: '<UX.Layout>test</UX.Layout>' },
55-
{ code: '<img alt={imageAlt} />' },
5657
{ code: '<img alt />' },
58+
{ code: '<img alt={imageAlt} />' },
59+
{ code: '<img alt={imageAlt.name} />' },
60+
semver.satisfies(eslintVersion, '>= 6') ? [
61+
{ code: '<img alt={imageAlt?.name} />', parserOptions: { ecmaVersion: 2020 } },
62+
{ code: '<img alt="Doing cool things" aria-hidden={foo?.bar}/>', parserOptions: { ecmaVersion: 2020 } },
63+
] : [],
5764
{ code: '<img alt="Photography" />;' },
5865
{ code: '<img alt="ImageMagick" />;' },
59-
].map(parserOptionsMapper),
66+
).map(parserOptionsMapper),
6067
invalid: [
6168
{ code: '<img alt="Photo of friend." />;', errors: [expectedError] },
6269
{ code: '<img alt="Picture of friend." />;', errors: [expectedError] },

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"has": "^1.0.3",
7474
"jsx-ast-utils": "^3.2.1",
7575
"language-tags": "^1.0.5",
76-
"minimatch": "^3.0.5"
76+
"minimatch": "^3.0.5",
77+
"semver": "^6.0.0"
7778
},
7879
"peerDependencies": {
7980
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8"

0 commit comments

Comments
 (0)
Please sign in to comment.