forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule-tester.js
47 lines (39 loc) · 1.46 KB
/
rule-tester.js
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
import { RuleTester } from 'eslint';
import { version as eslintVersion } from 'eslint/package.json';
import semver from 'semver';
export const usingFlatConfig = semver.major(eslintVersion) >= 9;
export function withoutAutofixOutput(test) {
return { ...test, ...usingFlatConfig || { output: test.code } };
}
class FlatCompatRuleTester {
constructor(testerConfig = { parserOptions: { sourceType: 'script' } }) {
this._tester = new RuleTester(FlatCompatRuleTester._flatCompat(testerConfig));
}
run(ruleName, rule, tests) {
this._tester.run(ruleName, rule, {
valid: tests.valid.map((t) => FlatCompatRuleTester._flatCompat(t)),
invalid: tests.invalid.map((t) => FlatCompatRuleTester._flatCompat(t)),
});
}
static _flatCompat(config) {
if (!config || !usingFlatConfig || typeof config !== 'object') {
return config;
}
const { parser, parserOptions = {}, languageOptions = {}, ...remainingConfig } = config;
const { ecmaVersion, sourceType, ...remainingParserOptions } = parserOptions;
const parserObj = typeof parser === 'string' ? require(parser) : parser;
return {
...remainingConfig,
languageOptions: {
...languageOptions,
...parserObj ? { parser: parserObj } : {},
...ecmaVersion ? { ecmaVersion } : {},
...sourceType ? { sourceType } : {},
parserOptions: {
...remainingParserOptions,
},
},
};
}
}
export { FlatCompatRuleTester as RuleTester };