forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.mjs
114 lines (102 loc) · 2.87 KB
/
test.mjs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import path from 'path';
import url from 'url';
import test from 'ava';
import {createRequire} from 'module';
import avaRuleTester from 'eslint-ava-rule-tester';
import snapshotRuleTester from './snapshot-rule-tester.mjs';
import defaultParserOptions from './default-parser-options.mjs';
const require = createRequire(import.meta.url);
class Tester {
constructor(ruleId) {
this.ruleId = ruleId;
this.rule = require(`../../rules/${ruleId}`);
}
runTest(tests) {
const {testerOptions, invalid, valid} = tests;
const tester = avaRuleTester(test, {
parserOptions: defaultParserOptions,
...testerOptions
});
return tester.run(this.ruleId, this.rule, {invalid, valid});
}
typescript(tests) {
const {testerOptions = {}} = tests;
testerOptions.parserOptions = testerOptions.parserOptions || {};
return this.runTest({
...tests,
testerOptions: {
...testerOptions,
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
...defaultParserOptions,
...testerOptions.parserOptions
}
}
});
}
babel(tests) {
const {testerOptions = {}} = tests;
testerOptions.parserOptions = testerOptions.parserOptions || {};
testerOptions.parserOptions.babelOptions = testerOptions.parserOptions.babelOptions || {};
testerOptions.parserOptions.babelOptions.parserOpts = testerOptions.parserOptions.babelOptions.parserOpts || {};
let babelPlugins = testerOptions.parserOptions.babelOptions.parserOpts.plugins || [];
babelPlugins = [
['estree', {classFeatures: true}],
'jsx',
'classProperties',
...babelPlugins
];
return this.runTest({
...tests,
testerOptions: {
...testerOptions,
parser: require.resolve('@babel/eslint-parser'),
parserOptions: {
...defaultParserOptions,
requireConfigFile: false,
sourceType: 'module',
allowImportExportEverywhere: true,
...testerOptions.parserOptions,
babelOptions: {
...testerOptions.parserOptions.babelOptions,
parserOpts: {
...testerOptions.parserOptions.babelOptions.parserOpts,
plugins: babelPlugins
}
}
}
}
});
}
snapshot(tests) {
const tester = snapshotRuleTester(test, {
parserOptions: defaultParserOptions
});
return tester.run(this.ruleId, this.rule, tests);
}
babelLegacy(tests) {
return this.runTest({
...tests,
testerOptions: {parser: require.resolve('babel-eslint')}
});
}
}
function getTester(importMeta) {
const filename = url.fileURLToPath(importMeta.url);
const ruleId = path.basename(filename, '.mjs');
const tester = new Tester(ruleId);
const test = tester.runTest.bind(tester);
test.typescript = tester.typescript.bind(tester);
test.babel = tester.babel.bind(tester);
test.snapshot = tester.snapshot.bind(tester);
test.babelLegacy = tester.babelLegacy.bind(tester);
return {
ruleId,
rule: tester.rule,
test
};
}
export {
defaultParserOptions,
getTester
};