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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ arguments: -c .oxlintrc.json
working directory: fixtures/issue_10394
----------

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: "Should not have an empty title"
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: Should not have an empty title
,-[foo.test.ts:1:10]
1 | describe("", () => {
: ^^
2 | //
`----
help: "Write a meaningful title for your test"
help: Write a meaningful title for your test

Found 1 warning and 0 errors.
Finished in <variable>ms on 1 file with 90 rules using 1 threads.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ arguments: -c .oxlintrc.json
working directory: fixtures/overrides_with_plugin
----------

x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: "Should not have an empty title"
x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: Should not have an empty title
,-[index.test.ts:1:10]
1 | describe("", () => {
: ^^
2 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
`----
help: "Write a meaningful title for your test"
help: Write a meaningful title for your test

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/expect-expect.html\eslint-plugin-jest(expect-expect)]8;;\: Test has no assertions
,-[index.test.ts:4:3]
Expand All @@ -23,14 +23,14 @@ working directory: fixtures/overrides_with_plugin
`----
help: Add assertion(s) in this Test

x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: "Should not have an empty title"
x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: Should not have an empty title
,-[index.test.ts:4:6]
3 |
4 | it("", () => {});
: ^^
5 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
`----
help: "Write a meaningful title for your test"
help: Write a meaningful title for your test

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html\eslint(no-unused-vars)]8;;\: Variable 'foo' is declared but never used. Unused variables should start with a '_'.
,-[index.ts:1:7]
Expand Down
106 changes: 47 additions & 59 deletions crates/oxc_linter/src/rules/jest/valid_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,45 @@ use crate::{
utils::{JestFnKind, JestGeneralFnKind, PossibleJestNode, parse_general_jest_fn_call},
};

fn valid_title_diagnostic(error_message: &str, help_text: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{error_message:?}"))
.with_help(format!("{help_text:?}"))
fn title_must_be_string_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Title must be a string")
.with_help("Replace your title with a string")
.with_label(span)
}

fn empty_title_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Should not have an empty title")
.with_help("Write a meaningful title for your test")
.with_label(span)
}

fn duplicate_prefix_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Should not have duplicate prefix")
.with_help("The function name already has the prefix, try to remove the duplicate prefix")
.with_label(span)
}

fn accidental_space_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Should not have leading or trailing spaces")
.with_help("Remove the leading or trailing spaces")
.with_label(span)
}

fn disallowed_word_diagnostic(word: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{word} is not allowed in test title"))
.with_help("It is included in the `disallowedWords` of your config file, try to remove it from your title")
.with_label(span)
}

fn must_match_diagnostic(message: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(message.to_string())
.with_help("Make sure the title matches the `mustMatch` of your config file")
.with_label(span)
}

fn must_not_match_diagnostic(message: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(message.to_string())
.with_help("Make sure the title does not match the `mustNotMatch` of your config file")
.with_label(span)
}

Expand Down Expand Up @@ -226,12 +262,12 @@ impl ValidTitle {
return;
}
if need_report_name {
Message::TitleMustBeString.diagnostic(ctx, arg.span());
ctx.diagnostic(title_must_be_string_diagnostic(arg.span()));
}
}
_ => {
if need_report_name {
Message::TitleMustBeString.diagnostic(ctx, arg.span());
ctx.diagnostic(title_must_be_string_diagnostic(arg.span()));
}
}
}
Expand Down Expand Up @@ -360,7 +396,7 @@ fn validate_title(
ctx: &LintContext,
) {
if title.is_empty() {
Message::EmptyTitle.diagnostic(ctx, span);
ctx.diagnostic(empty_title_diagnostic(span));
return;
}

Expand All @@ -373,20 +409,14 @@ fn validate_title(
};

if let Some(matched) = disallowed_words_reg.find(title) {
let error = format!("{} is not allowed in test title", matched.as_str());
ctx.diagnostic(valid_title_diagnostic(
&error,
"It is included in the `disallowedWords` of your config file, try to remove it from your title",
span,
));
ctx.diagnostic(disallowed_word_diagnostic(matched.as_str(), span));
}
return;
}

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| {
ctx.diagnostic_with_fix(accidental_space_diagnostic(span), |fixer| {
let inner_span = span.shrink(1);
let raw_text = fixer.source_range(inner_span);
let trimmed_raw = raw_text.trim().to_string();
Expand All @@ -400,8 +430,7 @@ fn validate_title(
};

if first_word == un_prefixed_name {
let (error, help) = Message::DuplicatePrefix.detail();
ctx.diagnostic_with_fix(valid_title_diagnostic(error, help, span), |fixer| {
ctx.diagnostic_with_fix(duplicate_prefix_diagnostic(span), |fixer| {
// Use raw source text to preserve escape sequences
let inner_span = span.shrink(1);
let raw_text = fixer.source_range(inner_span);
Expand All @@ -426,11 +455,7 @@ fn validate_title(
Some(message) => message.as_str(),
None => &format!("{un_prefixed_name} should match {raw_pattern}"),
};
ctx.diagnostic(valid_title_diagnostic(
message,
"Make sure the title matches the `mustMatch` of your config file",
span,
));
ctx.diagnostic(must_match_diagnostic(message, span));
}

if let Some((regex, message)) = valid_title.must_not_match_patterns.get(&jest_fn_name)
Expand All @@ -442,11 +467,7 @@ fn validate_title(
None => &format!("{un_prefixed_name} should not match {raw_pattern}"),
};

ctx.diagnostic(valid_title_diagnostic(
message,
"Make sure the title not matches the `mustNotMatch` of your config file",
span,
));
ctx.diagnostic(must_not_match_diagnostic(message, span));
}
}

Expand All @@ -461,39 +482,6 @@ fn does_binary_expression_contain_string_node(expr: &BinaryExpression) -> bool {
}
}

enum Message {
TitleMustBeString,
EmptyTitle,
DuplicatePrefix,
AccidentalSpace,
}

impl Message {
fn detail(&self) -> (&'static str, &'static str) {
match self {
Self::TitleMustBeString => {
("Title must be a string", "Replace your title with a string")
}
Self::EmptyTitle => {
("Should not have an empty title", "Write a meaningful title for your test")
}
Self::DuplicatePrefix => (
"Should not have duplicate prefix",
"The function name has already contains the prefix, try remove the duplicate prefix",
),
Self::AccidentalSpace => (
"Should not have leading or trailing spaces",
"Remove the leading or trailing spaces",
),
}
}

fn diagnostic(&self, ctx: &LintContext, span: Span) {
let (error, help) = self.detail();
ctx.diagnostic(valid_title_diagnostic(error, help, span));
}
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down
Loading
Loading