fix(linter): Handle cases where createElement is an Identifier in is_create_element_call (#2474)

I fixed a false negative issue in the `is_create_element_call` function
where createElement was an Identifier.

https://github.com/oxc-project/oxc/blob/main/crates/oxc_linter/src/utils/react.rs#L13-L19

In the case of [eslint-plugin-react's button-has-type
rule](https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/button-has-type.md),
the following triggers the expected errors:
```ts
// error
React.createElement("button")
// error
createElement("button")
```

However, with Oxlint, it behaves differently:
```ts
// error
React.createElement("button")
// false negative
createElement("button")
```
This commit is contained in:
keita hino 2024-02-23 16:55:28 +09:00 committed by GitHub
parent 2714a32970
commit 35a0f895dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 0 deletions

View file

@ -223,6 +223,7 @@ fn test() {
(r"<button type={condition ? 'button' : 'submit'}/>", None),
(r"<button type={condition ? `button` : `submit`}/>", None),
(r#"<button type="button"/>"#, Some(serde_json::json!([{ "reset": false }]))),
(r#"createElement("span")"#, None),
(r#"React.createElement("span")"#, None),
(r#"React.createElement("span", {type: "foo"})"#, None),
(r#"React.createElement("button", {type: "button"})"#, None),
@ -290,6 +291,7 @@ fn test() {
r#"<button type={condition ? "reset" : "button"}/>"#,
Some(serde_json::json!([{ "reset": false }])),
),
(r#"createElement("button")"#, None),
(r#"React.createElement("button")"#, None),
(r#"React.createElement("button", {type: foo})"#, None),
(r#"React.createElement("button", {type: "foo"})"#, None),

View file

@ -108,6 +108,13 @@ expression: button_has_type
╰────
help: Change the `type` attribute to one of the allowed values: `button`, `submit`, or `reset`.
⚠ eslint-plugin-react(button-has-type): `button` elements must have an explicit `type` attribute.
╭─[button_has_type.tsx:1:1]
1 │ createElement("button")
· ───────────────────────
╰────
help: Add a `type` attribute to the `button` element.
⚠ eslint-plugin-react(button-has-type): `button` elements must have an explicit `type` attribute.
╭─[button_has_type.tsx:1:1]
1 │ React.createElement("button")

View file

@ -15,6 +15,10 @@ pub fn is_create_element_call(call_expr: &CallExpression) -> bool {
return member_expr.static_property_name() == Some("createElement");
}
if let Some(ident) = call_expr.callee.get_identifier_reference() {
return ident.name == "createElement";
}
false
}