Skip to content

Commit

Permalink
feat(linter): Add title whitespace fixer for jest valid title rule (o…
Browse files Browse the repository at this point in the history
…xc-project#6669)

Supported fixer for white space in title for valid title rule.

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
tapanprakasht and autofix-ci[bot] authored Oct 20, 2024
1 parent 45f02d5 commit e9976d4
Showing 1 changed file with 86 additions and 5 deletions.
91 changes: 86 additions & 5 deletions crates/oxc_linter/src/rules/jest/valid_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ declare_oxc_lint!(
/// xtest('', () => {});
/// ```
ValidTitle,
correctness
correctness,
conditional_fix
);

impl Rule for ValidTitle {
Expand Down Expand Up @@ -297,9 +298,13 @@ fn validate_title(
return;
}

// TODO: support fixer
if !valid_title.ignore_space && title.trim() != title {
Message::AccidentalSpace.diagnostic(ctx, span);
let trimmed_title = title.trim();
if !valid_title.ignore_space && trimmed_title != title {
let (error, help) = Message::AccidentalSpace.detail();
ctx.diagnostic_with_fix(valid_title_diagnostic(error, help, span), |fixer| {
let target_span = Span::new(span.start + 1, span.end - 1);
fixer.replace(target_span, trimmed_title.to_string())
});
}

let un_prefixed_name = name.trim_start_matches(['f', 'x']);
Expand Down Expand Up @@ -891,5 +896,81 @@ fn test() {
),
];

Tester::new(ValidTitle::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
let fix = vec![
("describe(' foo', function () {})", "describe('foo', function () {})"),
("describe.each()(' foo', function () {})", "describe.each()('foo', function () {})"),
(
"describe.only.each()(' foo', function () {})",
"describe.only.each()('foo', function () {})",
),
("describe(' foo foe fum', function () {})", "describe('foo foe fum', function () {})"),
("describe('foo foe fum ', function () {})", "describe('foo foe fum', function () {})"),
("fdescribe(' foo', function () {})", "fdescribe('foo', function () {})"),
("fdescribe(' foo', function () {})", "fdescribe('foo', function () {})"),
("xdescribe(' foo', function () {})", "xdescribe('foo', function () {})"),
("it(' foo', function () {})", "it('foo', function () {})"),
("it.concurrent(' foo', function () {})", "it.concurrent('foo', function () {})"),
("fit(' foo', function () {})", "fit('foo', function () {})"),
("it.skip(' foo', function () {})", "it.skip('foo', function () {})"),
("fit('foo ', function () {})", "fit('foo', function () {})"),
("it.skip('foo ', function () {})", "it.skip('foo', function () {})"),
(
"
import { test as testThat } from '@jest/globals';
testThat('foo works ', () => {});
",
"
import { test as testThat } from '@jest/globals';
testThat('foo works', () => {});
",
),
("xit(' foo', function () {})", "xit('foo', function () {})"),
("test(' foo', function () {})", "test('foo', function () {})"),
("test.concurrent(' foo', function () {})", "test.concurrent('foo', function () {})"),
("test(` foo`, function () {})", "test(`foo`, function () {})"),
("test.concurrent(` foo`, function () {})", "test.concurrent(`foo`, function () {})"),
("test(` foo bar bang`, function () {})", "test(`foo bar bang`, function () {})"),
(
"test.concurrent(` foo bar bang`, function () {})",
"test.concurrent(`foo bar bang`, function () {})",
),
("test(` foo bar bang `, function () {})", "test(`foo bar bang`, function () {})"),
(
"test.concurrent(` foo bar bang `, function () {})",
"test.concurrent(`foo bar bang`, function () {})",
),
("xtest(' foo', function () {})", "xtest('foo', function () {})"),
("xtest(' foo ', function () {})", "xtest('foo', function () {})"),
(
"
describe(' foo', () => {
it('bar', () => {})
})
",
"
describe('foo', () => {
it('bar', () => {})
})
",
),
(
"
describe('foo', () => {
it(' bar', () => {})
})
",
"
describe('foo', () => {
it('bar', () => {})
})
",
),
];

Tester::new(ValidTitle::NAME, pass, fail)
.with_jest_plugin(true)
.expect_fix(fix)
.test_and_snapshot();
}

0 comments on commit e9976d4

Please sign in to comment.