Skip to content
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
17 changes: 11 additions & 6 deletions apps/oxlint/src-js/js_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,19 @@ export async function loadJsConfigs(paths: string[]): Promise<string> {
throw new Error(`Configuration file must have a default export that is an object.`);
}

if (!isDefineConfig(config)) {
throw new Error(
`Configuration file must wrap its default export with defineConfig() from "oxlint".`,
);
// 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 (!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 };
}),
);
Expand Down
10 changes: 9 additions & 1 deletion apps/oxlint/src/config_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use oxc_linter::{
};
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};

use crate::{DEFAULT_JSONC_OXLINTRC_NAME, DEFAULT_OXLINTRC_NAME, DEFAULT_TS_OXLINTRC_NAME};
use crate::{
DEFAULT_JSONC_OXLINTRC_NAME, DEFAULT_OXLINTRC_NAME, DEFAULT_TS_OXLINTRC_NAME, VITE_CONFIG_NAME,
};

#[cfg(feature = "napi")]
use crate::js_config;
Expand Down Expand Up @@ -487,6 +489,12 @@ impl<'a> ConfigLoader<'a> {
return Oxlintrc::from_file(&jsonc_path).map(Some);
}

// Fallback: check for vite.config.ts with .lint field (lowest priority)
let vite_path = dir.join(VITE_CONFIG_NAME);
if vite_path.is_file() {
return self.load_root_js_config(&vite_path).map(Some);
}

Ok(None)
}

Expand Down
23 changes: 22 additions & 1 deletion apps/oxlint/src/js_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 =
Expand Down Expand Up @@ -118,7 +119,27 @@ fn parse_js_config_response(json: &str) -> Result<Vec<JsConfigResult>, Vec<OxcDi
(Vec::with_capacity(count), Vec::new()),
|(mut configs, mut errors), entry| {
let path = PathBuf::from(&entry.path);
let mut oxlintrc = match parse_js_oxlintrc(entry.config) {

// Vite config files: extract the `.lint` field
let is_vite_config = path
.file_name()
.and_then(|f| f.to_str())
.is_some_and(|name| name == VITE_CONFIG_NAME);
let config_value = if is_vite_config {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to comment on a merged PR but I don't think this allows the lint property to be optional - I'm seeing

> oxlint

Failed to parse oxlint configuration file.

  × Expected a `lint` field in the default export of /home/runner/work/project/vite.config.ts

when I want this project to just use the default configuration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is a bug - please use the v1.52.0 in the interim

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if let Some(v) = entry.config.get(VITE_OXLINT_CONFIG_FIELD).cloned() {
v
} else {
errors.push(OxcDiagnostic::error(format!(
"Expected a `{VITE_OXLINT_CONFIG_FIELD}` field in the default export of {}",
entry.path
)));
return (configs, errors);
}
} else {
entry.config
};

let mut oxlintrc = match parse_js_oxlintrc(config_value) {
Comment on lines +122 to +142
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should do this on the JS side to avoid sending excess data to rust.

Ok(config) => config,
Err(err) => {
errors.push(
Expand Down
3 changes: 3 additions & 0 deletions apps/oxlint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ static GLOBAL: mimalloc_safe::MiMalloc = mimalloc_safe::MiMalloc;
const DEFAULT_OXLINTRC_NAME: &str = ".oxlintrc.json";
const DEFAULT_JSONC_OXLINTRC_NAME: &str = ".oxlintrc.jsonc";
const DEFAULT_TS_OXLINTRC_NAME: &str = "oxlint.config.ts";
/// Vite config file that may contain oxlint config under a `.lint` field.
const VITE_CONFIG_NAME: &str = "vite.config.ts";
const VITE_OXLINT_CONFIG_FIELD: &str = "lint";

/// Return a JSON blob containing metadata for all available oxlint rules.
///
Expand Down
24 changes: 15 additions & 9 deletions apps/oxlint/src/lsp/server_linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ impl Tool for ServerLinter {
"**/.oxlintrc.json".to_string(),
"**/.oxlintrc.jsonc".to_string(),
"**/oxlint.config.ts".to_string(),
"**/vite.config.ts".to_string(),
]
}
Some(v) => vec![v.to_string()],
Expand Down Expand Up @@ -1090,10 +1091,11 @@ mod test_watchers {
let patterns =
Tester::new("fixtures/lsp/watchers/default", json!({})).get_watcher_patterns();

assert_eq!(patterns.len(), 3);
assert_eq!(patterns.len(), 4);
assert_eq!(patterns[0], "**/.oxlintrc.json".to_string());
assert_eq!(patterns[1], "**/.oxlintrc.jsonc".to_string());
assert_eq!(patterns[2], "**/oxlint.config.ts".to_string());
assert_eq!(patterns[3], "**/vite.config.ts".to_string());
}

#[test]
Expand All @@ -1106,10 +1108,11 @@ mod test_watchers {
)
.get_watcher_patterns();

assert_eq!(patterns.len(), 3);
assert_eq!(patterns.len(), 4);
assert_eq!(patterns[0], "**/.oxlintrc.json".to_string());
assert_eq!(patterns[1], "**/.oxlintrc.jsonc".to_string());
assert_eq!(patterns[2], "**/oxlint.config.ts".to_string());
assert_eq!(patterns[3], "**/vite.config.ts".to_string());
}

#[test]
Expand All @@ -1131,12 +1134,13 @@ mod test_watchers {
let patterns = Tester::new("fixtures/lsp/watchers/linter_extends", json!({}))
.get_watcher_patterns();

// The `.oxlintrc.json` extends `./lint.json` -> 4 watchers (json, jsonc, ts, lint.json)
assert_eq!(patterns.len(), 4);
// The `.oxlintrc.json` extends `./lint.json` -> 5 watchers (json, jsonc, ts, vite, lint.json)
assert_eq!(patterns.len(), 5);
assert_eq!(patterns[0], "**/.oxlintrc.json".to_string());
assert_eq!(patterns[1], "**/.oxlintrc.jsonc".to_string());
assert_eq!(patterns[2], "**/oxlint.config.ts".to_string());
assert_eq!(patterns[3], "lint.json".to_string());
assert_eq!(patterns[3], "**/vite.config.ts".to_string());
assert_eq!(patterns[4], "lint.json".to_string());
}

#[test]
Expand Down Expand Up @@ -1164,11 +1168,12 @@ mod test_watchers {
)
.get_watcher_patterns();

assert_eq!(patterns.len(), 4);
assert_eq!(patterns.len(), 5);
assert_eq!(patterns[0], "**/.oxlintrc.json".to_string());
assert_eq!(patterns[1], "**/.oxlintrc.jsonc".to_string());
assert_eq!(patterns[2], "**/oxlint.config.ts".to_string());
assert_eq!(patterns[3], "**/tsconfig*.json".to_string());
assert_eq!(patterns[3], "**/vite.config.ts".to_string());
assert_eq!(patterns[4], "**/tsconfig*.json".to_string());
}
}

Expand Down Expand Up @@ -1219,11 +1224,12 @@ mod test_watchers {
"typeAware": true
}));
assert!(watch_patterns.is_some());
assert_eq!(watch_patterns.as_ref().unwrap().len(), 4);
assert_eq!(watch_patterns.as_ref().unwrap().len(), 5);
assert_eq!(watch_patterns.as_ref().unwrap()[0], "**/.oxlintrc.json".to_string());
assert_eq!(watch_patterns.as_ref().unwrap()[1], "**/.oxlintrc.jsonc".to_string());
assert_eq!(watch_patterns.as_ref().unwrap()[2], "**/oxlint.config.ts".to_string());
assert_eq!(watch_patterns.as_ref().unwrap()[3], "**/tsconfig*.json".to_string());
assert_eq!(watch_patterns.as_ref().unwrap()[3], "**/vite.config.ts".to_string());
assert_eq!(watch_patterns.as_ref().unwrap()[4], "**/tsconfig*.json".to_string());
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions apps/oxlint/test/fixtures/vite_config_basic/files/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
debugger;
if (x == 1) {
}
3 changes: 3 additions & 0 deletions apps/oxlint/test/fixtures/vite_config_basic/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleThread": true
}
29 changes: 29 additions & 0 deletions apps/oxlint/test/fixtures/vite_config_basic/output.snap.md
Original file line number Diff line number Diff line change
@@ -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 94 rules using X threads.
```

# stderr
```
```
8 changes: 8 additions & 0 deletions apps/oxlint/test/fixtures/vite_config_basic/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
lint: {
rules: {
"no-debugger": "error",
eqeqeq: "warn",
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleThread": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Exit code
1

# stdout
```
Failed to parse oxlint configuration file.

x Expected a `lint` field in the default export of <fixture>/vite.config.ts
```

# stderr
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
plugins: [],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
debugger;
if (x == 1) {
}
3 changes: 3 additions & 0 deletions apps/oxlint/test/fixtures/vite_config_priority/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleThread": true
}
20 changes: 20 additions & 0 deletions apps/oxlint/test/fixtures/vite_config_priority/output.snap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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

Found 0 warnings and 1 error.
Finished in Xms on 1 file with 93 rules using X threads.
```

# stderr
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "#oxlint";

export default defineConfig({
rules: {
"no-debugger": "error",
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
lint: {
rules: {
eqeqeq: "warn",
},
},
};
10 changes: 8 additions & 2 deletions apps/oxlint/test/lsp/init/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ describe("LSP initialization", () => {
});

it.each([
[undefined, ["**/.oxlintrc.json", "**/.oxlintrc.jsonc", "**/oxlint.config.ts"]],
[{ configPath: "" }, ["**/.oxlintrc.json", "**/.oxlintrc.jsonc", "**/oxlint.config.ts"]],
[
undefined,
["**/.oxlintrc.json", "**/.oxlintrc.jsonc", "**/oxlint.config.ts", "**/vite.config.ts"],
],
[
{ configPath: "" },
["**/.oxlintrc.json", "**/.oxlintrc.jsonc", "**/oxlint.config.ts", "**/vite.config.ts"],
],
[{ configPath: "./custom-config.json" }, ["./custom-config.json"]],
])(
"should send correct dynamic watch pattern registration for config: %s",
Expand Down
Loading