Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_js_analyze): improve the logic of jsx_reference_identifier_is_fragment #3592

Merged
merged 1 commit into from
Nov 8, 2022
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
53 changes: 36 additions & 17 deletions crates/rome_js_analyze/src/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ pub mod hooks;

use rome_js_semantic::SemanticModel;
use rome_js_syntax::{
JsAnyCallArgument, JsAnyExpression, JsCallExpression, JsIdentifierBinding, JsImport,
JsObjectExpression, JsPropertyObjectMember, JsxMemberName, JsxReferenceIdentifier,
JsAnyCallArgument, JsAnyExpression, JsAnyNamedImportSpecifier, JsCallExpression,
JsIdentifierBinding, JsImport, JsImportNamedClause, JsNamedImportSpecifierList,
JsNamedImportSpecifiers, JsObjectExpression, JsPropertyObjectMember, JsxMemberName,
JsxReferenceIdentifier,
};
use rome_rowan::{AstNode, AstSeparatedList};

Expand Down Expand Up @@ -331,21 +333,38 @@ pub(crate) fn jsx_reference_identifier_is_fragment(
name: &JsxReferenceIdentifier,
model: &SemanticModel,
) -> Option<bool> {
let value_token = name.value_token().ok()?;
let mut maybe_react_fragment = value_token.text_trimmed() == "Fragment";
if let Some(reference) = model.declaration(name) {
if let Some(js_import) = reference
.syntax()
.ancestors()
.find_map(|ancestor| JsImport::cast_ref(&ancestor))
{
let source_is_react = js_import.source_is("react").ok()?;
maybe_react_fragment = source_is_react;
} else {
// `Fragment` is a binding g but it doesn't come from the "react" package
maybe_react_fragment = false;
match model.declaration(name) {
Some(reference) => {
let ident = JsIdentifierBinding::cast_ref(reference.syntax())?;

let import_specifier = ident.parent::<JsAnyNamedImportSpecifier>()?;
let name_token = match &import_specifier {
JsAnyNamedImportSpecifier::JsNamedImportSpecifier(named_import) => {
named_import.name().ok()?.value().ok()?
}
JsAnyNamedImportSpecifier::JsShorthandNamedImportSpecifier(_) => {
ident.name_token().ok()?
}
JsAnyNamedImportSpecifier::JsUnknownNamedImportSpecifier(_) => {
return None;
}
};

if name_token.text_trimmed() != "Fragment" {
return Some(false);
}

let import_specifier_list = import_specifier.parent::<JsNamedImportSpecifierList>()?;
let import_specifiers = import_specifier_list.parent::<JsNamedImportSpecifiers>()?;
let import_clause = import_specifiers.parent::<JsImportNamedClause>()?;
let import = import_clause.parent::<JsImport>()?;
import.source_is("react").ok()
}
}

Some(maybe_react_fragment)
None => {
let value_token = name.value_token().ok()?;
let is_fragment = value_token.text_trimmed() == "Fragment";
Some(is_fragment)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import AwesomeReact, { Fragment as AwesomeFragment } from "noReact";
import AwesomeNoReact, { Fragment as AwesomeFragment } from "noReact";
import AwesomeReact, { StrictMode as AwesomeStrictMode } from "react";

<>
<AwesomeFragment></AwesomeFragment>
<AwesomeReact.Fragment>foo</AwesomeReact.Fragment>
<AwesomeNoReact.Fragment>foo</AwesomeNoReact.Fragment>
<AwesomeStrictMode></AwesomeStrictMode>
<AwesomeReact.StrictMode>foo</AwesomeReact.StrictMode>
</>
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 99
expression: fromImportRenameValid.jsx
---
# Input
```js
import AwesomeReact, { Fragment as AwesomeFragment } from "noReact";
import AwesomeNoReact, { Fragment as AwesomeFragment } from "noReact";
import AwesomeReact, { StrictMode as AwesomeStrictMode } from "react";

<>
<AwesomeFragment></AwesomeFragment>
<AwesomeReact.Fragment>foo</AwesomeReact.Fragment>
<AwesomeNoReact.Fragment>foo</AwesomeNoReact.Fragment>
<AwesomeStrictMode></AwesomeStrictMode>
<AwesomeReact.StrictMode>foo</AwesomeReact.StrictMode>
</>

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React, { StrictMode } from "react";

<>
<StrictMode></StrictMode>
<React.StrictMode></React.StrictMode>
</>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 99
expression: notFragmentValid.jsx
---
# Input
```js
import React, { StrictMode } from "react";

<>
<StrictMode></StrictMode>
<React.StrictMode></React.StrictMode>
</>

```


Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ function Fragment() {}
let React = { Fragment };
<>
<Fragment>test</Fragment>
<React>test</React>
<React.Fragment>test</React.Fragment>
</>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 99
expression: userDefinedValid.jsx
---
# Input
Expand All @@ -8,7 +9,7 @@ function Fragment() {}
let React = { Fragment };
<>
<Fragment>test</Fragment>
<React>test</React>
<React.Fragment>test</React.Fragment>
</>

```
Expand Down