From 201172c2abf9daf029bbbfc2b794de11fc8b6f8a Mon Sep 17 00:00:00 2001 From: Daiki Nishikawa Date: Sun, 18 Jun 2023 23:57:25 +0900 Subject: [PATCH] fix(rome_js_analyze): noRedundantUseStrict check only 'use strict' directive (#4591) * fix: noRedundantUseStrict check only 'use strict' directive * docs: update changelog * docs: update --- CHANGELOG.md | 5 ++++ .../suspicious/no_redundant_use_strict.rs | 3 +++ .../validReactDirectives.tsx | 14 +++++++++++ .../validReactDirectives.tsx.snap | 24 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx create mode 100644 crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index f783ca64db6..44c84ed4e13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -117,6 +117,11 @@ parameter decorators: } ``` +- [noRedundantUseStrict](https://docs.rome.tools/lint/rules/noredundantusestrict/) check only `'use strict'` directive to resolve false positive diagnostics. + + React introduce new directives, "use client" and "use server". + The rule raises false positive errors about these directives. + ### Parser ### VSCode diff --git a/crates/rome_js_analyze/src/analyzers/suspicious/no_redundant_use_strict.rs b/crates/rome_js_analyze/src/analyzers/suspicious/no_redundant_use_strict.rs index 818c1e92153..9bb1432d2b2 100644 --- a/crates/rome_js_analyze/src/analyzers/suspicious/no_redundant_use_strict.rs +++ b/crates/rome_js_analyze/src/analyzers/suspicious/no_redundant_use_strict.rs @@ -91,6 +91,9 @@ impl Rule for NoRedundantUseStrict { fn run(ctx: &RuleContext) -> Self::Signals { let node = ctx.query(); + if node.inner_string_text().ok()? != "use strict" { + return None; + } let mut outer_most: Option = None; let root = ctx.root(); match root { diff --git a/crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx b/crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx new file mode 100644 index 00000000000..37ded15cf5b --- /dev/null +++ b/crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx @@ -0,0 +1,14 @@ +'use client' + +import { useState } from "react" + +export default function Counter() { + const [count, setCount] = useState(0) + + return ( +
+

You clicked {count} times

+ +
+ ) +} diff --git a/crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx.snap b/crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx.snap new file mode 100644 index 00000000000..c0e974f2246 --- /dev/null +++ b/crates/rome_js_analyze/tests/specs/suspicious/noRedundantUseStrict/validReactDirectives.tsx.snap @@ -0,0 +1,24 @@ +--- +source: crates/rome_js_analyze/tests/spec_tests.rs +expression: validReactDirectives.tsx +--- +# Input +```js +'use client' + +import { useState } from "react" + +export default function Counter() { + const [count, setCount] = useState(0) + + return ( +
+

You clicked {count} times

+ +
+ ) +} + +``` + +