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
6 changes: 6 additions & 0 deletions apps/oxlint/fixtures/tsgolint_fix/fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file has a fixable tsgolint error: no-unnecessary-type-assertion
// The type assertion `as string` is unnecessary because str is already a string
const str: string = 'hello';
const redundant = str as string;

export { redundant };
9 changes: 9 additions & 0 deletions apps/oxlint/fixtures/tsgolint_fix/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"strict": true,
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "bundler"
},
"include": ["*.ts"]
}
26 changes: 26 additions & 0 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,4 +1433,30 @@ mod test {
let args = &["--type-aware"];
Tester::new().with_cwd("fixtures/tsgolint_rule_options".into()).test_and_snapshot(args);
}

#[test]
#[cfg(all(not(target_os = "windows"), not(target_endian = "big")))]
fn test_tsgolint_fix() {
// Note: tsgolint fixes this lint rule by providing two string manipulations
// the first removing `as` and the second removing `string` This results in the two spaces
// after `str` but before `;`, this is ok, as it's not guaranteed that our fixers are stylistically correct.
Tester::test_fix_with_args(
"fixtures/tsgolint_fix/fix.ts",
"// This file has a fixable tsgolint error: no-unnecessary-type-assertion
// The type assertion `as string` is unnecessary because str is already a string
const str: string = 'hello';
const redundant = str as string;

export { redundant };
",
"// This file has a fixable tsgolint error: no-unnecessary-type-assertion
// The type assertion `as string` is unnecessary because str is already a string
const str: string = 'hello';
const redundant = str ;

export { redundant };
",
&["--type-aware", "-D", "no-unnecessary-type-assertion"],
);
}
}
12 changes: 10 additions & 2 deletions apps/oxlint/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,26 @@ impl Tester {
}

pub fn test_fix(file: &str, before: &str, after: &str) {
Self::test_fix_with_args(file, before, after, &[]);
}

/// Test fix with additional CLI arguments (e.g., `--type-aware` for tsgolint)
pub fn test_fix_with_args(file: &str, before: &str, after: &str, extra_args: &[&str]) {
use std::fs;
#[expect(clippy::disallowed_methods)]
let content_original = fs::read_to_string(file).unwrap().replace("\r\n", "\n");
assert_eq!(content_original, before);

Tester::new().test(&["--fix", file]);
let mut args = vec!["--fix"];
args.extend(extra_args);
args.push(file);
Tester::new().test(&args);

#[expect(clippy::disallowed_methods)]
let new_content = fs::read_to_string(file).unwrap().replace("\r\n", "\n");
assert_eq!(new_content, after);

Tester::new().test(&["--fix", file]);
Tester::new().test(&args);

// File should not be modified if no fix is applied.
let modified_before: std::time::SystemTime =
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/lint_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl LintRunner {
self.lint_service.run(fs, files.to_owned(), &tx_error);

if let Some(type_aware_linter) = self.type_aware_linter.take() {
type_aware_linter.lint(files, self.directives_store.map(), tx_error)?;
type_aware_linter.lint(files, self.directives_store.map(), tx_error, fs)?;
} else {
drop(tx_error);
}
Expand Down
Loading
Loading