Skip to content

Commit a712a67

Browse files
authored
Merge pull request #157 from evcohen/move-parser-options-to-a-mapper
Move parser options into a common module that maps them into test cases
2 parents 977b57c + 9198439 commit a712a67

33 files changed

+1190
-1428
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const defaultParserOptions = {
2+
ecmaVersion: 6,
3+
ecmaFeatures: {
4+
jsx: true,
5+
},
6+
};
7+
8+
export default function parserOptionsMapper({
9+
code,
10+
errors,
11+
options,
12+
parserOptions = {},
13+
}) {
14+
return {
15+
code,
16+
errors,
17+
options,
18+
parserOptions: {
19+
...defaultParserOptions,
20+
...parserOptions,
21+
},
22+
};
23+
}

__tests__/src/rules/accessible-emoji-test.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@
99
// -----------------------------------------------------------------------------
1010

1111
import { RuleTester } from 'eslint';
12+
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1213
import rule from '../../../src/rules/accessible-emoji';
1314

14-
const parserOptions = {
15-
ecmaVersion: 6,
16-
ecmaFeatures: {
17-
jsx: true,
18-
},
19-
};
20-
2115
// -----------------------------------------------------------------------------
2216
// Tests
2317
// -----------------------------------------------------------------------------
@@ -31,22 +25,22 @@ const expectedError = {
3125

3226
ruleTester.run('accessible-emoji', rule, {
3327
valid: [
34-
{ code: '<div />;', parserOptions },
35-
{ code: '<span />', parserOptions },
36-
{ code: '<span>No emoji here!</span>', parserOptions },
37-
{ code: '<span role="img" aria-label="Panda face">🐼</span>', parserOptions },
38-
{ code: '<span role="img" aria-label="Snowman">&#9731;</span>', parserOptions },
39-
{ code: '<span role="img" aria-labelledby="id1">🐼</span>', parserOptions },
40-
{ code: '<span role="img" aria-labelledby="id1">&#9731;</span>', parserOptions },
41-
{ code: '<span role="img" aria-labelledby="id1" aria-label="Snowman">&#9731;</span>', parserOptions },
42-
{ code: '<span>{props.emoji}</span>', parserOptions },
43-
],
28+
{ code: '<div />;' },
29+
{ code: '<span />' },
30+
{ code: '<span>No emoji here!</span>' },
31+
{ code: '<span role="img" aria-label="Panda face">🐼</span>' },
32+
{ code: '<span role="img" aria-label="Snowman">&#9731;</span>' },
33+
{ code: '<span role="img" aria-labelledby="id1">🐼</span>' },
34+
{ code: '<span role="img" aria-labelledby="id1">&#9731;</span>' },
35+
{ code: '<span role="img" aria-labelledby="id1" aria-label="Snowman">&#9731;</span>' },
36+
{ code: '<span>{props.emoji}</span>' },
37+
].map(parserOptionsMapper),
4438
invalid: [
45-
{ code: '<span>🐼</span>', errors: [expectedError], parserOptions },
46-
{ code: '<span>foo🐼bar</span>', errors: [expectedError], parserOptions },
47-
{ code: '<span>foo 🐼 bar</span>', errors: [expectedError], parserOptions },
48-
{ code: '<i role="img" aria-label="Panda face">🐼</i>', errors: [expectedError], parserOptions },
49-
{ code: '<i role="img" aria-labelledby="id1">🐼</i>', errors: [expectedError], parserOptions },
50-
{ code: '<Foo>🐼</Foo>', errors: [expectedError], parserOptions },
51-
],
39+
{ code: '<span>🐼</span>', errors: [expectedError] },
40+
{ code: '<span>foo🐼bar</span>', errors: [expectedError] },
41+
{ code: '<span>foo 🐼 bar</span>', errors: [expectedError] },
42+
{ code: '<i role="img" aria-label="Panda face">🐼</i>', errors: [expectedError] },
43+
{ code: '<i role="img" aria-labelledby="id1">🐼</i>', errors: [expectedError] },
44+
{ code: '<Foo>🐼</Foo>', errors: [expectedError] },
45+
].map(parserOptionsMapper),
5246
});

__tests__/src/rules/anchor-has-content-test.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@
99
// -----------------------------------------------------------------------------
1010

1111
import { RuleTester } from 'eslint';
12+
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1213
import rule,
1314
{ determineChildType } from '../../../src/rules/anchor-has-content';
1415

15-
const parserOptions = {
16-
ecmaVersion: 6,
17-
ecmaFeatures: {
18-
jsx: true,
19-
},
20-
};
21-
2216
// -----------------------------------------------------------------------------
2317
// Tests
2418
// -----------------------------------------------------------------------------
@@ -42,17 +36,17 @@ describe('determineChildType', () => {
4236

4337
ruleTester.run('anchor-has-content', rule, {
4438
valid: [
45-
{ code: '<div />;', parserOptions },
46-
{ code: '<a>Foo</a>', parserOptions },
47-
{ code: '<a><Bar /></a>', parserOptions },
48-
{ code: '<a>{foo}</a>', parserOptions },
49-
{ code: '<a>{foo.bar}</a>', parserOptions },
50-
{ code: '<a dangerouslySetInnerHTML={{ __html: "foo" }} />', parserOptions },
51-
{ code: '<a children={children} />', parserOptions },
52-
],
39+
{ code: '<div />;' },
40+
{ code: '<a>Foo</a>' },
41+
{ code: '<a><Bar /></a>' },
42+
{ code: '<a>{foo}</a>' },
43+
{ code: '<a>{foo.bar}</a>' },
44+
{ code: '<a dangerouslySetInnerHTML={{ __html: "foo" }} />' },
45+
{ code: '<a children={children} />' },
46+
].map(parserOptionsMapper),
5347
invalid: [
54-
{ code: '<a />', errors: [expectedError], parserOptions },
55-
{ code: '<a><Bar aria-hidden /></a>', errors: [expectedError], parserOptions },
56-
{ code: '<a>{undefined}</a>', errors: [expectedError], parserOptions },
57-
],
48+
{ code: '<a />', errors: [expectedError] },
49+
{ code: '<a><Bar aria-hidden /></a>', errors: [expectedError] },
50+
{ code: '<a>{undefined}</a>', errors: [expectedError] },
51+
].map(parserOptionsMapper),
5852
});

__tests__/src/rules/aria-activedescendant-has-tabindex-test.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
// -----------------------------------------------------------------------------
99

1010
import { RuleTester } from 'eslint';
11+
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1112
import rule from '../../../src/rules/aria-activedescendant-has-tabindex';
1213

13-
const parserOptions = {
14-
ecmaVersion: 6,
15-
ecmaFeatures: {
16-
jsx: true,
17-
},
18-
};
19-
2014
// -----------------------------------------------------------------------------
2115
// Tests
2216
// -----------------------------------------------------------------------------
@@ -33,73 +27,57 @@ ruleTester.run('aria-activedescendant-has-tabindex', rule, {
3327
valid: [
3428
{
3529
code: '<CustomComponent />;',
36-
parserOptions,
3730
},
3831
{
3932
code: '<CustomComponent aria-activedescendant={someID} />;',
40-
parserOptions,
4133
},
4234
{
4335
code: '<CustomComponent aria-activedescendant={someID} tabIndex={0} />;',
44-
parserOptions,
4536
},
4637
{
4738
code: '<CustomComponent aria-activedescendant={someID} tabIndex={-1} />;',
48-
parserOptions,
4939
},
5040
{
5141
code: '<div />;',
52-
parserOptions,
5342
},
5443
{
5544
code: '<input />;',
56-
parserOptions,
5745
},
5846
{
5947
code: '<div tabIndex={0} />;',
60-
parserOptions,
6148
},
6249
{
6350
code: '<div aria-activedescendant={someID} tabIndex={0} />;',
64-
parserOptions,
6551
},
6652
{
6753
code: '<div aria-activedescendant={someID} tabIndex="0" />;',
68-
parserOptions,
6954
},
7055
{
7156
code: '<div aria-activedescendant={someID} tabIndex={1} />;',
72-
parserOptions,
7357
},
7458
{
7559
code: '<input aria-activedescendant={someID} />;',
76-
parserOptions,
7760
},
7861
{
7962
code: '<input aria-activedescendant={someID} tabIndex={0} />;',
80-
parserOptions,
8163
},
82-
],
64+
].map(parserOptionsMapper),
8365
invalid: [
8466
{
8567
code: '<div aria-activedescendant={someID} />;',
8668
errors: [expectedError],
87-
parserOptions,
8869
},
8970
{
9071
code: '<div aria-activedescendant={someID} tabIndex={-1} />;',
9172
errors: [expectedError],
92-
parserOptions,
9373
},
9474
{
9575
code: '<div aria-activedescendant={someID} tabIndex="-1" />;',
9676
errors: [expectedError],
97-
parserOptions,
9877
},
9978
{
10079
code: '<input aria-activedescendant={someID} tabIndex={-1} />;',
10180
errors: [expectedError],
102-
parserOptions,
10381
},
104-
],
82+
].map(parserOptionsMapper),
10583
});

__tests__/src/rules/aria-props-test.js

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@
99
// -----------------------------------------------------------------------------
1010

1111
import { RuleTester } from 'eslint';
12+
import parserOptionsMapper from '../../__util__/parserOptionsMapper';
1213
import rule from '../../../src/rules/aria-props';
1314
import ariaAttributes from '../../../src/util/attributes/ARIA.json';
1415
import getSuggestion from '../../../src/util/getSuggestion';
1516

16-
const parserOptions = {
17-
ecmaVersion: 6,
18-
ecmaFeatures: {
19-
jsx: true,
20-
},
21-
};
22-
2317
// -----------------------------------------------------------------------------
2418
// Tests
2519
// -----------------------------------------------------------------------------
@@ -47,31 +41,28 @@ const errorMessage = (name) => {
4741
// Create basic test cases using all valid role types.
4842
const basicValidityTests = Object.keys(ariaAttributes).map(prop => ({
4943
code: `<div ${prop.toLowerCase()}="foobar" />`,
50-
parserOptions,
5144
}));
5245

5346
ruleTester.run('aria-props', rule, {
5447
valid: [
5548
// Variables should pass, as we are only testing literals.
56-
{ code: '<div />', parserOptions },
57-
{ code: '<div></div>', parserOptions },
58-
{ code: '<div aria="wee"></div>', parserOptions }, // Needs aria-*
59-
{ code: '<div abcARIAdef="true"></div>', parserOptions },
60-
{ code: '<div fooaria-foobar="true"></div>', parserOptions },
61-
{ code: '<div fooaria-hidden="true"></div>', parserOptions },
62-
{ code: '<Bar baz />', parserOptions },
63-
].concat(basicValidityTests),
49+
{ code: '<div />' },
50+
{ code: '<div></div>' },
51+
{ code: '<div aria="wee"></div>' }, // Needs aria-*
52+
{ code: '<div abcARIAdef="true"></div>' },
53+
{ code: '<div fooaria-foobar="true"></div>' },
54+
{ code: '<div fooaria-hidden="true"></div>' },
55+
{ code: '<Bar baz />' },
56+
].concat(basicValidityTests).map(parserOptionsMapper),
6457
invalid: [
65-
{ code: '<div aria-="foobar" />', errors: [errorMessage('aria-')], parserOptions },
58+
{ code: '<div aria-="foobar" />', errors: [errorMessage('aria-')] },
6659
{
6760
code: '<div aria-labeledby="foobar" />',
6861
errors: [errorMessage('aria-labeledby')],
69-
parserOptions,
7062
},
7163
{
7264
code: '<div aria-skldjfaria-klajsd="foobar" />',
7365
errors: [errorMessage('aria-skldjfaria-klajsd')],
74-
parserOptions,
7566
},
76-
],
67+
].map(parserOptionsMapper),
7768
});

0 commit comments

Comments
 (0)