-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule/solidjs): add no-react-specific-props (#2427)
Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
4164331
commit fd7ba41
Showing
14 changed files
with
336 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
crates/biome_js_analyze/src/lint/nursery/no_react_specific_props.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use crate::JsRuleAction; | ||
use biome_analyze::context::RuleContext; | ||
use biome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_console::markup; | ||
use biome_diagnostics::Applicability; | ||
use biome_js_factory::make::{jsx_ident, jsx_name}; | ||
use biome_js_syntax::{AnyJsxAttributeName, JsxAttribute}; | ||
use biome_rowan::{AstNode, BatchMutationExt, TextRange}; | ||
|
||
declare_rule! { | ||
/// Prevents React-specific JSX properties from being used. | ||
/// | ||
/// This rule is intended for use in JSX-based frameworks (mainly Solid.js) | ||
/// that do not use React-style prop names. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// <Hello className="John" /> | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// <Hello class="Doe" /> | ||
/// ``` | ||
pub NoReactSpecificProps { | ||
version: "next", | ||
name: "noReactSpecificProps", | ||
sources: &[RuleSource::EslintSolid("no-react-specific-props")], | ||
recommended: false, | ||
} | ||
} | ||
|
||
const REACT_SPECIFIC_JSX_PROPS: &[&str] = &["className", "htmlFor"]; | ||
|
||
fn get_replacement_for_react_prop(str: &str) -> Option<&'static str> { | ||
match str { | ||
"className" => Some("class"), | ||
"htmlFor" => Some("for"), | ||
_ => None, | ||
} | ||
} | ||
|
||
impl Rule for NoReactSpecificProps { | ||
type Query = Ast<JsxAttribute>; | ||
type State = (TextRange, &'static str); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let attribute = ctx.query(); | ||
let name = attribute.name().ok()?; | ||
let range = name.range(); | ||
let name = name.text(); | ||
|
||
if REACT_SPECIFIC_JSX_PROPS.contains(&name.as_str()) { | ||
let replacement = get_replacement_for_react_prop(&name)?; | ||
Some((range, replacement)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, (range, _): &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup!("This JSX attribute is specific to React."), | ||
) | ||
.detail( | ||
range, | ||
"This attribute may not be supported by non-React frameworks, as it is not native to HTML.", | ||
), | ||
) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, (_, replacement): &Self::State) -> Option<JsRuleAction> { | ||
let mut mutation = ctx.root().begin(); | ||
let original_name_node = ctx.query().name().ok()?; | ||
|
||
let new_name_node = AnyJsxAttributeName::JsxName(jsx_name(jsx_ident(replacement))); | ||
mutation.replace_node(original_name_node, new_name_node); | ||
|
||
Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::Always, | ||
message: markup! { | ||
{format!("Replace this attribute name with {replacement:?}")} | ||
} | ||
.to_owned(), | ||
mutation, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
crates/biome_js_analyze/tests/specs/nursery/noReactSpecificProps/invalid.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//Case with className | ||
<div className="greeting">Hello world!</div>; | ||
|
||
//Case with className with expression | ||
<div className={"greeting"}>Hello world!</div>; | ||
|
||
//Case with htmlFor | ||
<div htmlFor="greeting">Hello world!</div>; |
Oops, something went wrong.