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
@@ -1,5 +1,3 @@
use itertools::Itertools;

use oxc_ast::{
AstKind,
ast::{Argument, Expression},
Expand Down Expand Up @@ -93,8 +91,19 @@ impl PreferDescribeFunctionTitle {
return;
}

let mut imported_entries =
ctx.module_record().import_entries.iter().map(|entry| entry.local_name.name.as_ref());
let is_imported_name = |name: &str| {
ctx.module_record()
.import_entries
.iter()
.any(|entry| entry.local_name.name.as_ref() == name)
};

let is_value_imported_name = |name: &str| {
ctx.module_record()
.import_entries
.iter()
.any(|entry| !entry.is_type && entry.local_name.name.as_ref() == name)
};

let Some(title_arg) = call_expr.arguments.first() else {
return;
Expand All @@ -106,12 +115,19 @@ impl PreferDescribeFunctionTitle {
return;
};

if title_expression.property.name == "name"
&& !imported_entries.contains(identifier.name.as_ref())
if title_expression.property.name != "name"
|| !is_imported_name(identifier.name.as_ref())
{
return;
}

if !is_value_imported_name(identifier.name.as_ref()) {
ctx.diagnostic(prefer_describe_function_title_diagnostic(
title_expression.span,
));
return;
}

ctx.diagnostic_with_fix(
prefer_describe_function_title_diagnostic(title_expression.span),
|fixer| {
Expand All @@ -122,7 +138,7 @@ impl PreferDescribeFunctionTitle {
);
}
Argument::StringLiteral(string_title) => {
if !imported_entries.contains(string_title.value.as_ref()) {
if !is_imported_name(string_title.value.as_ref()) {
return;
}

Expand All @@ -131,6 +147,11 @@ impl PreferDescribeFunctionTitle {
return;
}

if !is_value_imported_name(string_title.value.as_ref()) {
ctx.diagnostic(prefer_describe_function_title_diagnostic(string_title.span));
return;
}

ctx.diagnostic_with_fix(
prefer_describe_function_title_diagnostic(string_title.span),
|fixer| {
Expand Down Expand Up @@ -237,18 +258,27 @@ fn test() {
),
(
r#"
import { myFunction } from "./myFunction.js"
describe(otherFunction.name, () => {})
"#,
import { myFunction } from "./myFunction.js"
describe(otherFunction.name, () => {})
"#,
None,
None,
Some(PathBuf::from("myFunction.test.ts")),
),
(
r#"
declare const myFunction: () => unknown
describe("myFunction", () => {})
"#,
import { myFunction } from "./myFunction.js"
describe(myFunction.title, () => {})
"#,
None,
None,
Some(PathBuf::from("myFunction.test.ts")),
),
(
r#"
declare const myFunction: () => unknown
describe("myFunction", () => {})
"#,
None,
None,
Some(PathBuf::from("myFunction.test.ts")),
Expand Down Expand Up @@ -320,6 +350,15 @@ fn test() {
None,
Some(PathBuf::from("myFunction.test.ts")),
),
(
r#"
import type { Button } from "./button"
describe("Button", () => {})
"#,
None,
None,
Some(PathBuf::from("button.test.ts")),
),
/*
(
r#"
Expand Down Expand Up @@ -382,6 +421,17 @@ fn test() {
"#,
None,
),
(
r#"
import type { Button } from "./button"
describe("Button", () => {})
"#,
r#"
import type { Button } from "./button"
describe("Button", () => {})
"#,
None,
),
];
Tester::new(PreferDescribeFunctionTitle::NAME, PreferDescribeFunctionTitle::PLUGIN, pass, fail)
.expect_fix(fix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ source: crates/oxc_linter/src/tester.rs
5 │ }
╰────
help: Pass the function as a description title argument or modify the description title to not match any imported function name.

⚠ eslint-plugin-vitest(prefer-describe-function-title): Title description can not have the same content as a imported function name.
╭─[prefer_describe_function_title.tsx:3:21]
2 │ import type { Button } from "./button"
3 │ describe("Button", () => {})
· ────────
4 │
╰────
help: Pass the function as a description title argument or modify the description title to not match any imported function name.
Loading