Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export plugin metadata #8331

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/eslint-plugin-internal/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import rules from './rules';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
const { name, version } = require('../package.json') as {
name: string;
version: string;
};

export = {
rules,
};
meta: {
name,
version,
},
} satisfies Linter.Plugin;
4 changes: 3 additions & 1 deletion packages/eslint-plugin-internal/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import noPoorlyTypedTsProps from './no-poorly-typed-ts-props';
import noTypescriptDefaultImport from './no-typescript-default-import';
import noTypescriptEstreeImport from './no-typescript-estree-import';
Expand All @@ -10,4 +12,4 @@ export default {
'no-typescript-estree-import': noTypescriptEstreeImport,
'plugin-test-formatting': pluginTestFormatting,
'prefer-ast-types-enum': preferASTTypesEnum,
};
} satisfies Linter.PluginRules;
15 changes: 14 additions & 1 deletion packages/eslint-plugin-tslint/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import configRule from './rules/config';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
const { name, version } = require('../package.json') as {
name: string;
version: string;
};

export const meta: Linter.PluginMeta = {
name,
version,
};

/**
* Expose a single rule called "config", which will be accessed in the user's eslint config files
* via "tslint/config"
*/
export const rules = {
export const rules: Linter.PluginRules = {
config: configRule,
};
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand Down Expand Up @@ -151,4 +153,4 @@ export = {
'@typescript-eslint/unbound-method': 'error',
'@typescript-eslint/unified-signatures': 'error',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
parser: '@typescript-eslint/parser',
parserOptions: { sourceType: 'module' },
plugins: ['@typescript-eslint'],
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/disable-type-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
parserOptions: { project: null, program: null },
rules: {
Expand Down Expand Up @@ -59,4 +61,4 @@ export = {
'@typescript-eslint/switch-exhaustiveness-check': 'off',
'@typescript-eslint/unbound-method': 'off',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/eslint-recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* - disables rules from eslint:recommended which are already handled by TypeScript.
* - enables rules that make sense due to TS's typechecking / transpilation.
*/
import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
overrides: [
{
Expand Down Expand Up @@ -31,4 +33,4 @@ export = {
},
},
],
};
} satisfies ClassicConfig.Config;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand Down Expand Up @@ -50,4 +52,4 @@ export = {
'@typescript-eslint/triple-slash-reference': 'error',
'@typescript-eslint/unbound-method': 'error',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand All @@ -29,4 +31,4 @@ export = {
'@typescript-eslint/prefer-as-const': 'error',
'@typescript-eslint/triple-slash-reference': 'error',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/strict-type-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand Down Expand Up @@ -75,4 +77,4 @@ export = {
'@typescript-eslint/unbound-method': 'error',
'@typescript-eslint/unified-signatures': 'error',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand Down Expand Up @@ -39,4 +41,4 @@ export = {
'@typescript-eslint/triple-slash-reference': 'error',
'@typescript-eslint/unified-signatures': 'error',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/stylistic-type-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand All @@ -31,4 +33,4 @@ export = {
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/prefer-string-starts-ends-with': 'error',
},
};
} satisfies ClassicConfig.Config;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/configs/stylistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// For developers working in the typescript-eslint monorepo:
// You can regenerate it using `yarn generate:configs`

import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
extends: ['./configs/base', './configs/eslint-recommended'],
rules: {
Expand All @@ -25,4 +27,4 @@ export = {
'@typescript-eslint/prefer-function-type': 'error',
'@typescript-eslint/prefer-namespace-keyword': 'error',
},
};
} satisfies ClassicConfig.Config;
14 changes: 13 additions & 1 deletion packages/eslint-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import all from './configs/all';
import base from './configs/base';
import disableTypeChecked from './configs/disable-type-checked';
Expand All @@ -10,6 +12,12 @@ import stylistic from './configs/stylistic';
import stylisticTypeChecked from './configs/stylistic-type-checked';
import rules from './rules';

// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
const { name, version } = require('../package.json') as {
name: string;
version: string;
};

export = {
configs: {
all,
Expand All @@ -25,5 +33,9 @@ export = {
stylistic,
'stylistic-type-checked': stylisticTypeChecked,
},
meta: {
name,
version,
},
rules,
};
} satisfies Linter.Plugin;
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import adjacentOverloadSignatures from './adjacent-overload-signatures';
import arrayType from './array-type';
import awaitThenable from './await-thenable';
Expand Down Expand Up @@ -278,4 +280,4 @@ export default {
typedef: typedef,
'unbound-method': unboundMethod,
'unified-signatures': unifiedSignatures,
};
} satisfies Linter.PluginRules;
6 changes: 5 additions & 1 deletion packages/repo-tools/src/generate-configs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ async function main(): Promise<void> {
console.log(chalk.blueBright(`\n${hyphens} ${name}.ts ${hyphens}`));

// note: we use `export =` because ESLint will import these configs via a commonjs import
const code = `export = ${JSON.stringify(getConfig())};`;
const code = [
"import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';",
'',
`export = ${JSON.stringify(getConfig())} satisfies ClassicConfig.Config;`,
].join('\n');
const configStr = await prettier.format(addAutoGeneratedComment(code), {
parser: 'typescript',
...prettierConfig,
Expand Down
22 changes: 12 additions & 10 deletions packages/utils/src/ts-eslint/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ export namespace SharedConfig {
}

export type ParserOptions = ParserOptionsTypes.ParserOptions;

export interface PluginMeta {
/**
* The meta.name property should match the npm package name for your plugin.
*/
name: string;
/**
* The meta.version property should match the npm package version for your plugin.
*/
version: string;
}
}

export namespace ClassicConfig {
Expand Down Expand Up @@ -121,6 +132,7 @@ export namespace FlatConfig {
export type GlobalsConfig = SharedConfig.GlobalsConfig;
export type Parser = ParserType.LooseParserModule;
export type ParserOptions = SharedConfig.ParserOptions;
export type PluginMeta = SharedConfig.PluginMeta;
export type Processor = ProcessorType.ProcessorModule;
export type RuleEntry = SharedConfig.RuleEntry;
export type RuleLevel = SharedConfig.RuleLevel;
Expand All @@ -134,16 +146,6 @@ export namespace FlatConfig {
export interface SharedConfigs {
[key: string]: Config;
}
export interface PluginMeta {
/**
* The meta.name property should match the npm package name for your plugin.
*/
name: string;
/**
* The meta.version property should match the npm package version for your plugin.
*/
version: string;
}
export interface Plugin {
/**
* Shared configurations bundled with the plugin.
Expand Down
22 changes: 20 additions & 2 deletions packages/utils/src/ts-eslint/Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { Linter as ESLintLinter } from 'eslint';
import type { ClassicConfig, FlatConfig, SharedConfig } from './Config';
import type { Parser } from './Parser';
import type { Processor as ProcessorType } from './Processor';
import type { RuleCreateFunction, RuleFix, RuleModule } from './Rule';
import type {
AnyRuleCreateFunction,
AnyRuleModule,
RuleCreateFunction,
RuleFix,
RuleModule,
} from './Rule';
import type { SourceCode } from './SourceCode';

export type MinimalRuleModule<
Expand Down Expand Up @@ -118,6 +124,7 @@ namespace Linter {
export type GlobalVariableOption = SharedConfig.GlobalVariableOption;
export type GlobalVariableOptionBase = SharedConfig.GlobalVariableOptionBase;
export type ParserOptions = SharedConfig.ParserOptions;
export type PluginMeta = SharedConfig.PluginMeta;
export type RuleEntry = SharedConfig.RuleEntry;
export type RuleLevel = SharedConfig.RuleLevel;
export type RuleLevelAndOptions = SharedConfig.RuleLevelAndOptions;
Expand Down Expand Up @@ -262,6 +269,13 @@ namespace Linter {
parserOptions?: ParserOptions;
}

// TODO - RuleCreateFunction is no longer supported in ESLint v9
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
export type LegacyPluginRules = Record<
string,
AnyRuleCreateFunction | AnyRuleModule
>;
export type PluginRules = Record<string, AnyRuleModule>;

export interface Plugin {
/**
* The definition of plugin configs.
Expand All @@ -271,14 +285,18 @@ namespace Linter {
* The definition of plugin environments.
*/
environments?: Record<string, Environment>;
/**
* Metadata about your plugin for easier debugging and more effective caching of plugins.
*/
meta?: PluginMeta;
/**
* The definition of plugin processors.
*/
processors?: Record<string, ProcessorType.ProcessorModule>;
/**
* The definition of plugin rules.
*/
rules?: Record<string, RuleCreateFunction | RuleModule<string, unknown[]>>;
rules?: LegacyPluginRules;
}
}

Expand Down
Loading