diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 98cd12b9d..ed3aa04ff 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -103,6 +103,7 @@ mod jest { pub mod no_disabled_tests; pub mod no_done_callback; pub mod no_focused_tests; + pub mod no_interpolation_in_snapshots; pub mod no_test_prefixes; pub mod valid_describe_callback; } @@ -198,6 +199,7 @@ oxc_macros::declare_all_lint_rules! { jest::no_alias_methods, jest::no_conditional_expect, jest::no_done_callback, + jest::no_interpolation_in_snapshots, unicorn::no_instanceof_array, unicorn::no_unnecessary_await, import::named diff --git a/crates/oxc_linter/src/rules/jest/no_interpolation_in_snapshots.rs b/crates/oxc_linter/src/rules/jest/no_interpolation_in_snapshots.rs new file mode 100644 index 000000000..a3186db9d --- /dev/null +++ b/crates/oxc_linter/src/rules/jest/no_interpolation_in_snapshots.rs @@ -0,0 +1,114 @@ +use oxc_ast::{ + ast::{Argument, Expression}, + AstKind, +}; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::Error, +}; +use oxc_macros::declare_oxc_lint; +use oxc_span::Span; + +use crate::{context::LintContext, jest_ast_util::parse_expect_jest_fn_call, rule::Rule, AstNode}; + +#[derive(Debug, Error, Diagnostic)] +#[error("eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots")] +#[diagnostic(severity(warning), help("Remove string interpolation from snapshots"))] +struct NoInterpolationInSnapshotsDiagnostic(#[label] pub Span); + +#[derive(Debug, Default, Clone)] +pub struct NoInterpolationInSnapshots; + +declare_oxc_lint!( + /// ### What it does + /// + /// Prevents the use of string interpolations in snapshots. + /// + /// ### Why is this bad? + /// + /// Interpolation prevents snapshots from being updated. Instead, properties should + /// be overloaded with a matcher by using + /// [property matchers](https://jestjs.io/docs/en/snapshot-testing#property-matchers). + /// + /// ### Example + /// + /// ```javascript + /// expect(something).toMatchInlineSnapshot( + /// `Object { + /// property: ${interpolated} + /// }`, + /// ); + /// + /// expect(something).toMatchInlineSnapshot( + /// { other: expect.any(Number) }, + /// `Object { + /// other: Any, + /// property: ${interpolated} + /// }`, + /// ); + /// + /// expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot( + /// `${interpolated}`, + /// ); + /// ``` + NoInterpolationInSnapshots, + restriction +); + +impl Rule for NoInterpolationInSnapshots { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + let AstKind::CallExpression(call_expr) = node.kind() else { return }; + let Some(jest_fn_call) = parse_expect_jest_fn_call(call_expr, node, ctx) else { return }; + let Some(matcher) = jest_fn_call.matcher() else { + return; + }; + + if matcher.is_name_unequal("toMatchInlineSnapshot") + && matcher.is_name_unequal("toThrowErrorMatchingInlineSnapshot") + { + return; + } + + // Check all since the optional 'propertyMatchers' argument might be present + // `.toMatchInlineSnapshot(propertyMatchers?, inlineSnapshot)` + for arg in jest_fn_call.args { + if let Argument::Expression(Expression::TemplateLiteral(template_lit)) = arg { + if !template_lit.expressions.is_empty() { + ctx.diagnostic(NoInterpolationInSnapshotsDiagnostic(template_lit.span)); + } + } + } + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let pass = vec![ + ("expect('something').toEqual('else');", None), + ("expect(something).toMatchInlineSnapshot();", None), + ("expect(something).toMatchInlineSnapshot(`No interpolation`);", None), + ("expect(something).toMatchInlineSnapshot({}, `No interpolation`);", None), + ("expect(something);", None), + ("expect(something).not;", None), + ("expect.toHaveAssertions();", None), + ("myObjectWants.toMatchInlineSnapshot({}, `${interpolated}`);", None), + ("myObjectWants.toMatchInlineSnapshot({}, `${interpolated1} ${interpolated2}`);", None), + ("toMatchInlineSnapshot({}, `${interpolated}`);", None), + ("toMatchInlineSnapshot({}, `${interpolated1} ${interpolated2}`);", None), + ("expect(something).toThrowErrorMatchingInlineSnapshot();", None), + ("expect(something).toThrowErrorMatchingInlineSnapshot(`No interpolation`);", None), + ]; + + let fail = vec![ + ("expect(something).toMatchInlineSnapshot(`${interpolated}`);", None), + ("expect(something).not.toMatchInlineSnapshot(`${interpolated}`);", None), + ("expect(something).toMatchInlineSnapshot({}, `${interpolated}`);", None), + ("expect(something).not.toMatchInlineSnapshot({}, `${interpolated}`);", None), + ("expect(something).toThrowErrorMatchingInlineSnapshot(`${interpolated}`);", None), + ("expect(something).not.toThrowErrorMatchingInlineSnapshot(`${interpolated}`);", None), + ]; + + Tester::new(NoInterpolationInSnapshots::NAME, pass, fail).test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs b/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs index b2a39f4e5..43ed87a3d 100644 --- a/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs +++ b/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs @@ -59,7 +59,6 @@ declare_oxc_lint!( /// })); /// ``` ValidDescribeCallback, - // Because this rule has one test case not passed, will set to correctness when finished. restriction ); diff --git a/crates/oxc_linter/src/snapshots/no_interpolation_in_snapshots.snap b/crates/oxc_linter/src/snapshots/no_interpolation_in_snapshots.snap new file mode 100644 index 000000000..26ec0e4ba --- /dev/null +++ b/crates/oxc_linter/src/snapshots/no_interpolation_in_snapshots.snap @@ -0,0 +1,47 @@ +--- +source: crates/oxc_linter/src/tester.rs +expression: no_interpolation_in_snapshots +--- + ⚠ eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots + ╭─[no_interpolation_in_snapshots.tsx:1:1] + 1 │ expect(something).toMatchInlineSnapshot(`${interpolated}`); + · ───────────────── + ╰──── + help: Remove string interpolation from snapshots + + ⚠ eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots + ╭─[no_interpolation_in_snapshots.tsx:1:1] + 1 │ expect(something).not.toMatchInlineSnapshot(`${interpolated}`); + · ───────────────── + ╰──── + help: Remove string interpolation from snapshots + + ⚠ eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots + ╭─[no_interpolation_in_snapshots.tsx:1:1] + 1 │ expect(something).toMatchInlineSnapshot({}, `${interpolated}`); + · ───────────────── + ╰──── + help: Remove string interpolation from snapshots + + ⚠ eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots + ╭─[no_interpolation_in_snapshots.tsx:1:1] + 1 │ expect(something).not.toMatchInlineSnapshot({}, `${interpolated}`); + · ───────────────── + ╰──── + help: Remove string interpolation from snapshots + + ⚠ eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots + ╭─[no_interpolation_in_snapshots.tsx:1:1] + 1 │ expect(something).toThrowErrorMatchingInlineSnapshot(`${interpolated}`); + · ───────────────── + ╰──── + help: Remove string interpolation from snapshots + + ⚠ eslint(jest/no-interpolation-in-snapshots): Do not use string interpolation inside of snapshots + ╭─[no_interpolation_in_snapshots.tsx:1:1] + 1 │ expect(something).not.toThrowErrorMatchingInlineSnapshot(`${interpolated}`); + · ───────────────── + ╰──── + help: Remove string interpolation from snapshots + +