|
1 | 1 | import { createRequire } from 'module';
|
| 2 | +import { TSESLint } from '@typescript-eslint/utils'; |
| 3 | +import { version as eslintVersion } from 'eslint/package.json'; |
| 4 | +import * as semver from 'semver'; |
2 | 5 |
|
3 | 6 | const require = createRequire(__filename);
|
4 | 7 | const eslintRequire = createRequire(require.resolve('eslint'));
|
5 | 8 |
|
6 | 9 | export const espreeParser = eslintRequire.resolve('espree');
|
| 10 | + |
| 11 | +export const usingFlatConfig = semver.major(eslintVersion) >= 9; |
| 12 | + |
| 13 | +export class FlatCompatRuleTester extends TSESLint.RuleTester { |
| 14 | + public constructor(testerConfig?: TSESLint.RuleTesterConfig) { |
| 15 | + super(FlatCompatRuleTester._flatCompat(testerConfig)); |
| 16 | + } |
| 17 | + |
| 18 | + public override run< |
| 19 | + TMessageIds extends string, |
| 20 | + TOptions extends Readonly<unknown[]>, |
| 21 | + >( |
| 22 | + ruleName: string, |
| 23 | + rule: TSESLint.RuleModule<TMessageIds, TOptions>, |
| 24 | + tests: TSESLint.RunTests<TMessageIds, TOptions>, |
| 25 | + ) { |
| 26 | + super.run(ruleName, rule, { |
| 27 | + valid: tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)), |
| 28 | + invalid: tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)), |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + /* istanbul ignore next */ |
| 33 | + private static _flatCompat< |
| 34 | + T extends |
| 35 | + | undefined |
| 36 | + | RuleTesterConfig |
| 37 | + | string |
| 38 | + | TSESLint.ValidTestCase<unknown[]> |
| 39 | + | TSESLint.InvalidTestCase<string, unknown[]>, |
| 40 | + >(config: T): T { |
| 41 | + if (!config || !usingFlatConfig || typeof config === 'string') { |
| 42 | + return config; |
| 43 | + } |
| 44 | + |
| 45 | + const obj: FlatConfig.Config & { |
| 46 | + languageOptions: FlatConfig.LanguageOptions & { |
| 47 | + parserOptions: FlatConfig.ParserOptions; |
| 48 | + }; |
| 49 | + } = { |
| 50 | + languageOptions: { parserOptions: {} }, |
| 51 | + }; |
| 52 | + |
| 53 | + for (const [key, value] of Object.entries(config)) { |
| 54 | + if (key === 'parser') { |
| 55 | + obj.languageOptions.parser = require(value); |
| 56 | + |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + if (key === 'parserOptions') { |
| 61 | + for (const [option, val] of Object.entries(value)) { |
| 62 | + if (option === 'ecmaVersion' || option === 'sourceType') { |
| 63 | + // @ts-expect-error: TS thinks the value could the opposite type of whatever option is |
| 64 | + obj.languageOptions[option] = val as FlatConfig.LanguageOptions[ |
| 65 | + | 'ecmaVersion' |
| 66 | + | 'sourceType']; |
| 67 | + |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + obj.languageOptions.parserOptions[option] = val; |
| 72 | + } |
| 73 | + |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + obj[key as keyof typeof obj] = value; |
| 78 | + } |
| 79 | + |
| 80 | + return obj as unknown as T; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type RuleTesterConfig = TSESLint.RuleTesterConfig | FlatConfig.Config; |
| 85 | + |
| 86 | +export declare namespace FlatConfig { |
| 87 | + type EcmaVersion = TSESLint.EcmaVersion; |
| 88 | + type ParserOptions = TSESLint.ParserOptions; |
| 89 | + type SourceType = TSESLint.SourceType | 'commonjs'; |
| 90 | + interface LanguageOptions { |
| 91 | + /** |
| 92 | + * The version of ECMAScript to support. |
| 93 | + * May be any year (i.e., `2022`) or version (i.e., `5`). |
| 94 | + * Set to `"latest"` for the most recent supported version. |
| 95 | + * @default "latest" |
| 96 | + */ |
| 97 | + ecmaVersion?: EcmaVersion; |
| 98 | + /** |
| 99 | + * An object specifying additional objects that should be added to the global scope during linting. |
| 100 | + */ |
| 101 | + globals?: unknown; |
| 102 | + /** |
| 103 | + * An object containing a `parse()` method or a `parseForESLint()` method. |
| 104 | + * @default |
| 105 | + * ``` |
| 106 | + * // https://github.com/eslint/espree |
| 107 | + * require('espree') |
| 108 | + * ``` |
| 109 | + */ |
| 110 | + parser?: unknown; |
| 111 | + /** |
| 112 | + * An object specifying additional options that are passed directly to the parser. |
| 113 | + * The available options are parser-dependent. |
| 114 | + */ |
| 115 | + parserOptions?: ParserOptions; |
| 116 | + /** |
| 117 | + * The type of JavaScript source code. |
| 118 | + * Possible values are `"script"` for traditional script files, `"module"` for ECMAScript modules (ESM), and `"commonjs"` for CommonJS files. |
| 119 | + * @default |
| 120 | + * ``` |
| 121 | + * // for `.js` and `.mjs` files |
| 122 | + * "module" |
| 123 | + * // for `.cjs` files |
| 124 | + * "commonjs" |
| 125 | + * ``` |
| 126 | + */ |
| 127 | + sourceType?: SourceType; |
| 128 | + } |
| 129 | + interface Config { |
| 130 | + /** |
| 131 | + * An array of glob patterns indicating the files that the configuration object should apply to. |
| 132 | + * If not specified, the configuration object applies to all files matched by any other configuration object. |
| 133 | + */ |
| 134 | + files?: string[]; |
| 135 | + /** |
| 136 | + * An array of glob patterns indicating the files that the configuration object should not apply to. |
| 137 | + * If not specified, the configuration object applies to all files matched by files. |
| 138 | + */ |
| 139 | + ignores?: string[]; |
| 140 | + /** |
| 141 | + * An object containing settings related to how JavaScript is configured for linting. |
| 142 | + */ |
| 143 | + languageOptions?: LanguageOptions; |
| 144 | + /** |
| 145 | + * An object containing settings related to the linting process. |
| 146 | + */ |
| 147 | + linterOptions?: unknown; |
| 148 | + /** |
| 149 | + * An object containing a name-value mapping of plugin names to plugin objects. |
| 150 | + * When `files` is specified, these plugins are only available to the matching files. |
| 151 | + */ |
| 152 | + plugins?: unknown; |
| 153 | + /** |
| 154 | + * An object containing the configured rules. |
| 155 | + * When `files` or `ignores` are specified, these rule configurations are only available to the matching files. |
| 156 | + */ |
| 157 | + rules?: unknown; |
| 158 | + /** |
| 159 | + * An object containing name-value pairs of information that should be available to all rules. |
| 160 | + */ |
| 161 | + settings?: unknown; |
| 162 | + } |
| 163 | +} |
0 commit comments