|
| 1 | +import { RuleTester } from 'eslint'; |
| 2 | +import { version as eslintVersion } from 'eslint/package.json'; |
| 3 | +import semver from 'semver'; |
| 4 | + |
| 5 | +export const usingFlatConfig = semver.major(eslintVersion) >= 9; |
| 6 | + |
1 | 7 | export function withoutAutofixOutput(test) {
|
2 |
| - return { ...test, output: test.code }; |
| 8 | + return { ...test, ...usingFlatConfig || { output: test.code } }; |
| 9 | +} |
| 10 | + |
| 11 | +class FlatCompatRuleTester extends RuleTester { |
| 12 | + constructor(testerConfig = { parserOptions: { sourceType: 'script' } }) { |
| 13 | + super(FlatCompatRuleTester._flatCompat(testerConfig)); |
| 14 | + } |
| 15 | + |
| 16 | + run(ruleName, rule, tests) { |
| 17 | + super.run(ruleName, rule, { |
| 18 | + valid: tests.valid.map((t) => FlatCompatRuleTester._flatCompat(t)), |
| 19 | + invalid: tests.invalid.map((t) => FlatCompatRuleTester._flatCompat(t)), |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + static _flatCompat(config) { |
| 24 | + if (!config || !usingFlatConfig || typeof config !== 'object') { |
| 25 | + return config; |
| 26 | + } |
| 27 | + |
| 28 | + const { parser, parserOptions = {}, languageOptions = {}, ...remainingConfig } = config; |
| 29 | + const { ecmaVersion, sourceType, ...remainingParserOptions } = parserOptions; |
| 30 | + const parserObj = typeof parser === 'string' ? require(parser) : parser; |
| 31 | + |
| 32 | + return { |
| 33 | + ...remainingConfig, |
| 34 | + languageOptions: { |
| 35 | + ...languageOptions, |
| 36 | + ...parserObj ? { parser: parserObj } : {}, |
| 37 | + ...ecmaVersion ? { ecmaVersion } : {}, |
| 38 | + ...sourceType ? { sourceType } : {}, |
| 39 | + parserOptions: { |
| 40 | + ...remainingParserOptions, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }; |
| 44 | + } |
3 | 45 | }
|
4 | 46 |
|
5 |
| -export { RuleTester } from 'eslint'; |
| 47 | +export { FlatCompatRuleTester as RuleTester }; |
0 commit comments