diff --git a/apps/oxlint/src-js/index.ts b/apps/oxlint/src-js/index.ts index eaa7348ae1121..8aff3dcbe416f 100644 --- a/apps/oxlint/src-js/index.ts +++ b/apps/oxlint/src-js/index.ts @@ -15,3 +15,18 @@ export const defineRule = _defineRule; * @deprecated Import from `oxlint/plugins-dev` instead */ export const RuleTester = _RuleTester; + +export { defineConfig } from "./package/config.ts"; + +export type { + AllowWarnDeny, + OxlintEnv, + DummyRule, + DummyRuleMap, + ExternalPluginEntry, + OxlintGlobals, + RuleCategories, + ExternalPluginsConfig, + OxlintConfig, + OxlintOverride, +} from "./package/config.ts"; diff --git a/apps/oxlint/src-js/package/config.ts b/apps/oxlint/src-js/package/config.ts new file mode 100644 index 0000000000000..a859ed75ddc7b --- /dev/null +++ b/apps/oxlint/src-js/package/config.ts @@ -0,0 +1,54 @@ +/* + * `defineConfig` helper and config types. + */ + +import type { JsonObject, JsonValue } from "../plugins/json.ts"; + +export type AllowWarnDeny = "off" | "warn" | "error" | 0 | 1 | 2; + +export type DummyRule = AllowWarnDeny | [AllowWarnDeny, ...JsonValue[]]; + +export type DummyRuleMap = Record; + +export type RuleCategories = Record; +export type OxlintGlobals = Record< + string, + "readonly" | "writable" | "off" | "readable" | "writeable" | boolean +>; + +export type OxlintEnv = Record; + +export type ExternalPluginEntry = string | { name: string; specifier: string }; + +export type ExternalPluginsConfig = ExternalPluginEntry[] | null; + +export interface OxlintOverride { + files: string[]; + env?: OxlintEnv; + globals?: OxlintGlobals; + plugins?: string[]; + jsPlugins?: ExternalPluginsConfig; + rules?: DummyRuleMap; +} + +export interface OxlintConfig { + plugins?: string[]; + jsPlugins?: ExternalPluginsConfig; + categories?: RuleCategories; + rules?: DummyRuleMap; + settings?: JsonObject; + env?: OxlintEnv; + globals?: OxlintGlobals; + overrides?: OxlintOverride[]; + ignorePatterns?: string[]; +} + +/** + * Define an Oxlint configuration with type inference. + * + * @param config - Oxlint configuration + * @returns Config unchanged + */ +export function defineConfig(config: T): T { + return config; +} diff --git a/apps/oxlint/test/fixtures/js_config_define_config/files/test.js b/apps/oxlint/test/fixtures/js_config_define_config/files/test.js new file mode 100644 index 0000000000000..d4e2f029dd793 --- /dev/null +++ b/apps/oxlint/test/fixtures/js_config_define_config/files/test.js @@ -0,0 +1,3 @@ +debugger; +if (x == 1) { +} diff --git a/apps/oxlint/test/fixtures/js_config_define_config/output.snap.md b/apps/oxlint/test/fixtures/js_config_define_config/output.snap.md new file mode 100644 index 0000000000000..da3555aad9bcc --- /dev/null +++ b/apps/oxlint/test/fixtures/js_config_define_config/output.snap.md @@ -0,0 +1,29 @@ +# Exit code +1 + +# stdout +``` + x eslint(no-debugger): `debugger` statement is not allowed + ,-[files/test.js:1:1] + 1 | debugger; + : ^^^^^^^^^ + 2 | if (x == 1) { + `---- + help: Remove the debugger statement + + ! eslint(eqeqeq): Expected === and instead saw == + ,-[files/test.js:2:7] + 1 | debugger; + 2 | if (x == 1) { + : ^^ + 3 | } + `---- + help: Prefer === operator + +Found 1 warning and 1 error. +Finished in Xms on 1 file with 92 rules using X threads. +``` + +# stderr +``` +``` diff --git a/apps/oxlint/test/fixtures/js_config_define_config/oxlint.config.ts b/apps/oxlint/test/fixtures/js_config_define_config/oxlint.config.ts new file mode 100644 index 0000000000000..551ed0da6e47f --- /dev/null +++ b/apps/oxlint/test/fixtures/js_config_define_config/oxlint.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "#oxlint"; + +export default defineConfig({ + rules: { + "no-debugger": "error", + eqeqeq: "warn", + }, +});