diff --git a/apps/oxlint/src-js/js_config.ts b/apps/oxlint/src-js/js_config.ts index d6d49e12838b3..50cd0aeafb759 100644 --- a/apps/oxlint/src-js/js_config.ts +++ b/apps/oxlint/src-js/js_config.ts @@ -1,3 +1,5 @@ +import { basename as pathBasename } from "node:path"; + import { getErrorMessage } from "./utils/utils.ts"; import { isDefineConfig } from "./package/config.ts"; import { DateNow, JSONStringify } from "./utils/globals.ts"; @@ -12,6 +14,9 @@ type LoadJsConfigsResult = | { Failures: { path: string; error: string }[] } | { Error: string }; +const VITE_CONFIG_NAME = "vite.config.ts"; +const VITE_OXLINT_CONFIG_FIELD = "lint"; + function validateConfigExtends(root: object): void { const visited = new WeakSet(); const inStack = new WeakSet(); @@ -95,7 +100,7 @@ export async function loadJsConfigs(paths: string[]): Promise { // Bypass Node.js module cache to allow reloading changed config files (used for LSP, where we reload configs after important changes) const fileUrl = new URL(`file://${path}?cache=${cacheKey}`); const module = await import(fileUrl.href); - const config = module.default; + let config = module.default; if (config === undefined) { throw new Error(`Configuration file has no default export.`); @@ -105,19 +110,23 @@ export async function loadJsConfigs(paths: string[]): Promise { throw new Error(`Configuration file must have a default export that is an object.`); } - // Vite config files (e.g. `vite.config.ts`) are not Oxlint configs, - // so skip `defineConfig()` and `extends` validation. - // The `.lint` field extraction is handled on the Rust side. - if (!path.endsWith("/vite.config.ts")) { + if (pathBasename(path) === VITE_CONFIG_NAME) { + config = (config as Record)[VITE_OXLINT_CONFIG_FIELD] ?? {}; + if (typeof config !== "object" || config === null || Array.isArray(config)) { + throw new Error( + `The \`${VITE_OXLINT_CONFIG_FIELD}\` field in the default export must be an object.`, + ); + } + } else { if (!isDefineConfig(config)) { throw new Error( `Configuration file must wrap its default export with defineConfig() from "oxlint".`, ); } - - validateConfigExtends(config as object); } + validateConfigExtends(config as object); + return { path, config }; }), ); diff --git a/apps/oxlint/src/js_config.rs b/apps/oxlint/src/js_config.rs index 3b5bb9430ca35..c4e52f7872e05 100644 --- a/apps/oxlint/src/js_config.rs +++ b/apps/oxlint/src/js_config.rs @@ -5,7 +5,6 @@ use oxc_diagnostics::OxcDiagnostic; use oxc_linter::Oxlintrc; use crate::run::JsLoadJsConfigsCb; -use crate::{VITE_CONFIG_NAME, VITE_OXLINT_CONFIG_FIELD}; /// Callback type for loading JavaScript/TypeScript config files. pub type JsConfigLoaderCb = @@ -119,27 +118,7 @@ fn parse_js_config_response(json: &str) -> Result, Vec config, Err(err) => { errors.push( @@ -185,3 +164,36 @@ fn parse_js_config_response(json: &str) -> Result, Vec/vite.config.ts +Found 1 warning and 0 errors. +Finished in Xms on 1 file with 93 rules using X threads. ``` # stderr