Skip to content

Commit

Permalink
feat: add types for flat config files
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Jul 19, 2023
1 parent f2aed1b commit 96b6e74
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 194 deletions.
4 changes: 2 additions & 2 deletions packages/eslint-plugin/index.d.ts
@@ -1,9 +1,9 @@
import type { TSESLint } from '@typescript-eslint/utils';
import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

import type rules from './rules';

declare const cjsExport: {
configs: Record<string, TSESLint.Linter.Config>;
configs: Record<string, ClassicConfig.Config>;
rules: typeof rules;
};
export = cjsExport;
18 changes: 12 additions & 6 deletions packages/eslint-plugin/tools/generate-configs.ts
@@ -1,4 +1,10 @@
import type { TSESLint } from '@typescript-eslint/utils';
import type {
ClassicConfig,
Linter,
RuleListener,
RuleModule,
RuleRecommendation,
} from '@typescript-eslint/utils/ts-eslint';
import * as fs from 'fs';
import * as path from 'path';
import prettier from 'prettier';
Expand Down Expand Up @@ -48,10 +54,10 @@ async function main(): Promise<void> {
const prettierConfig = prettier.resolveConfig.sync(__dirname);

interface LinterConfigRules {
[name: string]: TSESLint.Linter.RuleLevel;
[name: string]: ClassicConfig.RuleLevel;
}

interface LinterConfig extends TSESLint.Linter.Config {
interface LinterConfig extends ClassicConfig.Config {
extends?: string[] | string;
plugins?: string[];
}
Expand All @@ -78,7 +84,7 @@ async function main(): Promise<void> {

type RuleEntry = [
string,
TSESLint.RuleModule<string, readonly unknown[], TSESLint.RuleListener>,
RuleModule<string, readonly unknown[], RuleListener>,
];

const allRuleEntries: RuleEntry[] = Object.entries(rules).sort((a, b) =>
Expand All @@ -89,7 +95,7 @@ async function main(): Promise<void> {
deprecated?: 'exclude';
typeChecked?: 'exclude' | 'include-only';
baseRuleForExtensionRule?: 'exclude';
forcedRuleLevel?: TSESLint.Linter.RuleLevel;
forcedRuleLevel?: Linter.RuleLevel;
}

/**
Expand Down Expand Up @@ -190,7 +196,7 @@ async function main(): Promise<void> {
}

function filterRuleEntriesTo(
...recommendations: (TSESLint.RuleRecommendation | undefined)[]
...recommendations: (RuleRecommendation | undefined)[]
): RuleEntry[] {
return allRuleEntries.filter(([, rule]) =>
recommendations.includes(rule.meta.docs?.recommended),
Expand Down
3 changes: 2 additions & 1 deletion packages/rule-tester/src/RuleTester.ts
Expand Up @@ -10,6 +10,7 @@ import { deepMerge } from '@typescript-eslint/utils/eslint-utils';
import type {
AnyRuleCreateFunction,
AnyRuleModule,
Parser,
ParserOptions,
RuleContext,
RuleModule,
Expand Down Expand Up @@ -521,7 +522,7 @@ export class RuleTester extends TestFramework {

this.#linter.defineParser(
config.parser,
wrapParser(require(config.parser) as Linter.ParserModule),
wrapParser(require(config.parser) as Parser.ParserModule),
);

if (schema) {
Expand Down
7 changes: 5 additions & 2 deletions packages/rule-tester/src/types/RuleTesterConfig.ts
@@ -1,8 +1,11 @@
import type { Linter, ParserOptions } from '@typescript-eslint/utils/ts-eslint';
import type {
ClassicConfig,
ParserOptions,
} from '@typescript-eslint/utils/ts-eslint';

import type { DependencyConstraint } from './DependencyConstraint';

export interface RuleTesterConfig extends Linter.Config {
export interface RuleTesterConfig extends ClassicConfig.Config {
/**
* The default parser to use for tests.
* @default '@typescript-eslint/parser'
Expand Down
5 changes: 3 additions & 2 deletions packages/rule-tester/src/types/ValidTestCase.ts
@@ -1,4 +1,5 @@
import type {
Linter,
ParserOptions,
SharedConfigurationSettings,
} from '@typescript-eslint/utils/ts-eslint';
Expand All @@ -17,15 +18,15 @@ export interface ValidTestCase<TOptions extends Readonly<unknown[]>> {
/**
* Environments for the test case.
*/
readonly env?: Readonly<Record<string, boolean>>;
readonly env?: Readonly<Linter.EnvironmentConfig>;
/**
* The fake filename for the test case. Useful for rules that make assertion about filenames.
*/
readonly filename?: string;
/**
* The additional global variables.
*/
readonly globals?: Record<string, 'off' | 'readonly' | 'writable' | true>;
readonly globals?: Readonly<Linter.GlobalsConfig>;
/**
* Options for the test case.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/rule-tester/src/utils/validationHelpers.ts
@@ -1,6 +1,6 @@
import { simpleTraverse } from '@typescript-eslint/typescript-estree';
import type { TSESTree } from '@typescript-eslint/utils';
import type { Linter, SourceCode } from '@typescript-eslint/utils/ts-eslint';
import type { Parser, SourceCode } from '@typescript-eslint/utils/ts-eslint';

/*
* List every parameters possible on a test case that are not related to eslint
Expand Down Expand Up @@ -75,7 +75,7 @@ const parserSymbol = Symbol.for('eslint.RuleTester.parser');
* Wraps the given parser in order to intercept and modify return values from the `parse` and `parseForESLint` methods, for test purposes.
* In particular, to modify ast nodes, tokens and comments to throw on access to their `start` and `end` properties.
*/
export function wrapParser(parser: Linter.ParserModule): Linter.ParserModule {
export function wrapParser(parser: Parser.ParserModule): Parser.ParserModule {
/**
* Define `start`/`end` properties of all nodes of the given AST as throwing error.
*/
Expand Down Expand Up @@ -121,7 +121,7 @@ export function wrapParser(parser: Linter.ParserModule): Linter.ParserModule {
return {
// @ts-expect-error -- see above
[parserSymbol]: parser,
parseForESLint(...args): Linter.ESLintParseResult {
parseForESLint(...args): Parser.ParseResult {
const ret = parser.parseForESLint(...args);

defineStartEndAsErrorInTree(ret.ast, ret.visitorKeys);
Expand Down
4 changes: 2 additions & 2 deletions packages/scope-manager/src/ScopeManager.ts
@@ -1,4 +1,4 @@
import type { TSESTree } from '@typescript-eslint/types';
import type { SourceType, TSESTree } from '@typescript-eslint/types';

import { assert } from './assert';
import type { Scope } from './scope';
Expand Down Expand Up @@ -27,7 +27,7 @@ import type { Variable } from './variable';

interface ScopeManagerOptions {
globalReturn?: boolean;
sourceType?: 'module' | 'script';
sourceType?: SourceType;
impliedStrict?: boolean;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/scope-manager/src/analyze.ts
@@ -1,4 +1,4 @@
import type { Lib, TSESTree } from '@typescript-eslint/types';
import type { Lib, SourceType, TSESTree } from '@typescript-eslint/types';
import { visitorKeys } from '@typescript-eslint/visitor-keys';

import type { ReferencerOptions } from './referencer';
Expand Down Expand Up @@ -55,7 +55,7 @@ interface AnalyzeOptions {
/**
* The source type of the script.
*/
sourceType?: 'module' | 'script';
sourceType?: SourceType;

/**
* Emit design-type metadata for decorated declarations in source.
Expand Down
10 changes: 8 additions & 2 deletions packages/types/src/parser-options.ts
Expand Up @@ -16,21 +16,27 @@ type EcmaVersion =
| 11
| 12
| 13
| 14
| 15
| 2015
| 2016
| 2017
| 2018
| 2019
| 2020
| 2021
| 2022;
| 2022
| 2023
| 2024;

type SourceType = 'module' | 'script';
type SourceTypeClassic = 'module' | 'script';
type SourceType = SourceTypeClassic | 'commonjs';

interface ParserOptions {
ecmaFeatures?: {
globalReturn?: boolean;
jsx?: boolean;
[key: string]: unknown;
};
ecmaVersion?: EcmaVersion | 'latest';

Expand Down

0 comments on commit 96b6e74

Please sign in to comment.