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

Commit

Permalink
fmt and clippy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Leite committed Nov 22, 2022
1 parent 689cde9 commit 874a294
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 714 deletions.
132 changes: 62 additions & 70 deletions crates/rome_js_analyze/src/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,50 +168,32 @@ pub(crate) fn is_react_call_api(
model: &SemanticModel,
lib: ReactLibrary,
api_name: &str,
) -> Option<bool> {
// we bail straight away if the API doesn't exists in React
debug_assert!(VALID_REACT_API.contains(&api_name));
Some(match expression {
JsAnyExpression::JsStaticMemberExpression(node) => {
let object = node.object().ok()?;
let member = node.member().ok()?;
let member = member.as_js_name()?;
let identifier = object.as_js_identifier_expression()?.name().ok()?;

let mut maybe_from_react = identifier.syntax().text_trimmed() == "React"
&& member.syntax().text_trimmed() == api_name;
) -> bool {
if matches!(lib, ReactLibrary::React) {
// we bail straight away if the API doesn't exists in React
debug_assert!(VALID_REACT_API.contains(&api_name));
}

if let Some(binding_identifier) = model.binding(&identifier) {
let binding_identifier =
JsIdentifierBinding::cast_ref(binding_identifier.syntax())?;
if let Some(js_import) = binding_identifier
.syntax()
.ancestors()
.find_map(|ancestor| JsImport::cast_ref(&ancestor))
{
maybe_from_react = js_import.source_is("react").ok()?;
}
}
maybe_from_react
let expr = expression.omit_parentheses();
if let Some(callee) = JsAnyMemberExpression::cast_ref(expr.syntax()) {
let Some(object) = callee.get_object_reference_identifier() else { return false };
if !callee.has_member_name(api_name) {
return false;
}
JsAnyExpression::JsIdentifierExpression(identifier) => {
let name = identifier.name().ok()?;
let mut maybe_react = identifier.syntax().text_trimmed() == api_name;
if let Some(identifier_binding) = model.binding(&name) {
let binding_identifier =
JsIdentifierBinding::cast_ref(identifier_binding.syntax())?;
if let Some(js_import) = binding_identifier
.syntax()
.ancestors()
.find_map(|ancestor| JsImport::cast_ref(&ancestor))
{
maybe_react = js_import.source_is("react").ok()?;
}
}
maybe_react
}
_ => return None,
})
return match model.binding(&object) {
Some(decl) => is_react_export(decl, lib),
None => object.has_name(lib.global_name()),
};
}

if let Some(ident) = expr.as_reference_identifier() {
return model
.binding(&ident)
.and_then(|it| is_named_react_export(it, lib, api_name))
.unwrap_or(false);
}

false
}

/// Checks if the node `JsxMemberName` is a react fragment.
Expand All @@ -227,21 +209,15 @@ pub(crate) fn jsx_member_name_is_react_fragment(
let object = member_name.object().ok()?;
let member = member_name.member().ok()?;
let object = object.as_jsx_reference_identifier()?;
let mut maybe_react_fragment = object.value_token().ok()?.text_trimmed() == "React"
&& member.value_token().ok()?.text_trimmed() == "Fragment";
if let Some(reference) = model.binding(object) {
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 && member.value_token().ok()?.text_trimmed() == "Fragment";
} else {
// `React.Fragment` is a binding but it doesn't come from the "react" package
maybe_react_fragment = false;
}

if member.value_token().ok()?.text_trimmed() != "Fragment" {
return Some(false);
}

let lib = ReactLibrary::React;
match model.binding(object) {
Some(declaration) => Some(is_react_export(declaration, lib)),
None => Some(object.value_token().ok()?.text_trimmed() == lib.global_name()),
}
}

Expand All @@ -255,19 +231,35 @@ 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.binding(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.binding(name) {
Some(reference) => is_named_react_export(reference, ReactLibrary::React, "Fragment"),
None => {
let value_token = name.value_token().ok()?;
let is_fragment = value_token.text_trimmed() == "Fragment";
Some(is_fragment)
}
}
}

fn is_react_export(binding: Binding, lib: ReactLibrary) -> bool {
binding
.syntax()
.ancestors()
.find_map(|ancestor| JsImport::cast_ref(&ancestor))
.and_then(|js_import| js_import.source_is(lib.import_name()).ok())
.unwrap_or(false)
}

fn is_named_react_export(binding: Binding, lib: ReactLibrary, name: &str) -> Option<bool> {
let ident = JsIdentifierBinding::cast_ref(binding.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 Some(false);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ impl Rule for NoRestrictedGlobals {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let model = ctx.model();

let unresolved_reference_nodes = model.all_unresolved_references()

let unresolved_reference_nodes = model
.all_unresolved_references()
.map(|reference| reference.syntax().clone());
let global_references_nodes = model.all_global_references()
let global_references_nodes = model
.all_global_references()
.map(|reference| reference.syntax().clone());

unresolved_reference_nodes.chain(global_references_nodes)
unresolved_reference_nodes
.chain(global_references_nodes)
.filter_map(|node| {
let node = AnyIdentifier::unwrap_cast(node);
let (token, binding) = match node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rome_analyze::{context::RuleContext, declare_rule, ActionCategory, Rule, Rul
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_factory::make::{ident, js_identifier_binding};
use rome_js_semantic::{SemanticScopeExtensions, ReferencesExtensions};
use rome_js_semantic::{ReferencesExtensions, SemanticScopeExtensions};
use rome_js_syntax::{
JsClassExpression, JsConstructorParameterList, JsConstructorParameters, JsFunctionDeclaration,
JsFunctionExpression, JsIdentifierBinding, JsParameterList, JsParameters, JsSyntaxKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use rome_analyze::{context::RuleContext, declare_rule, ActionCategory, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_semantic::{CanBeImportedExported, SemanticModel, ReferencesExtensions};
use rome_js_semantic::{CanBeImportedExported, ReferencesExtensions, SemanticModel};
use rome_js_syntax::{
JsFormalParameter, JsFunctionDeclaration, JsFunctionExportDefaultDeclaration,
JsGetterClassMember, JsIdentifierBinding, JsLiteralMemberName, JsMethodClassMember,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rome_console::markup;

use rome_diagnostics::Applicability;
use rome_js_factory::make;
use rome_js_semantic::{Scope, SemanticModel, SemanticScopeExtensions, ReferencesExtensions};
use rome_js_semantic::{ReferencesExtensions, Scope, SemanticModel, SemanticScopeExtensions};
use rome_js_syntax::*;
use rome_rowan::{declare_node_union, AstNode, BatchMutationExt};

Expand Down Expand Up @@ -195,7 +195,10 @@ fn check_binding_can_be_const(
_ => return None,
};

let host = write.syntax().ancestors().find_map(DestructuringHost::cast)?;
let host = write
.syntax()
.ancestors()
.find_map(DestructuringHost::cast)?;
if host.has_member_expr_assignment() || host.has_outer_variables(write.scope()) {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,16 @@ impl Rule for UseExhaustiveDependencies {
.cast::<JsIdentifierBinding>()
.map(|node| model.is_imported(&node))
.or_else(|| {
Some(
model.is_imported(&node.cast::<TsIdentifierBinding>()?),
)
Some(model.is_imported(&node.cast::<TsIdentifierBinding>()?))
})
{
None
} else {
let binding = binding
.syntax()
.clone()
.cast::<JsIdentifierBinding>()?;
let binding =
binding.syntax().clone().cast::<JsIdentifierBinding>()?;

// Ignore if constant
if let Some(declarator) =
binding.parent::<JsVariableDeclarator>()
{
if let Some(declarator) = binding.parent::<JsVariableDeclarator>() {
let declaration = declarator
.syntax()
.ancestors()
Expand Down
2 changes: 1 addition & 1 deletion crates/rome_js_analyze/src/utils/rename.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rome_js_semantic::{SemanticModel, ReferencesExtensions};
use rome_js_semantic::{ReferencesExtensions, SemanticModel};
use rome_js_syntax::{
JsIdentifierAssignment, JsIdentifierBinding, JsLanguage, JsReferenceIdentifier, JsSyntaxKind,
JsSyntaxNode, JsSyntaxToken,
Expand Down
22 changes: 9 additions & 13 deletions crates/rome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub enum SemanticEvent {
range: TextRange,
scope_id: usize,
parent_scope_id: Option<usize>,
is_closure: bool
is_closure: bool,
},

/// Tracks where a scope ends
Expand Down Expand Up @@ -262,7 +262,7 @@ impl SemanticEventExtractor {
JS_MODULE | JS_SCRIPT => self.push_scope(
node.text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
false
false,
),

JS_FUNCTION_DECLARATION
Expand All @@ -278,9 +278,9 @@ impl SemanticEventExtractor {
self.push_scope(
node.text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
true
true,
);
},
}

JS_FUNCTION_EXPORT_DEFAULT_DECLARATION
| JS_CLASS_DECLARATION
Expand All @@ -294,20 +294,16 @@ impl SemanticEventExtractor {
self.push_scope(
node.text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
false
false,
);
}

JS_BLOCK_STATEMENT
| JS_FOR_STATEMENT
| JS_FOR_OF_STATEMENT
| JS_FOR_IN_STATEMENT
| JS_SWITCH_STATEMENT
| JS_CATCH_CLAUSE => {
JS_BLOCK_STATEMENT | JS_FOR_STATEMENT | JS_FOR_OF_STATEMENT | JS_FOR_IN_STATEMENT
| JS_SWITCH_STATEMENT | JS_CATCH_CLAUSE => {
self.push_scope(
node.text_range(),
node.text_range(),
ScopeHoisting::HoistDeclarationsToParent,
false
false,
);
}

Expand Down
21 changes: 10 additions & 11 deletions crates/rome_js_semantic/src/semantic_model.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod closure;
mod is_constant;
mod binding;
mod scope;
mod builder;
mod closure;
mod globals;
mod import;
mod is_constant;
mod model;
mod reference;
mod semantic_model;
mod globals;
mod scope;

#[cfg(test)]
mod tests;
Expand All @@ -27,15 +27,15 @@ use std::{
sync::Arc,
};

pub use binding::*;
pub use builder::*;
pub use semantic_model::*;
pub use scope::*;
pub use builder::*;
pub use binding::*;
pub use reference::*;
pub use globals::*;
pub use import::*;
pub use is_constant::*;
pub use globals::*;
pub use model::*;
pub use reference::*;
pub use scope::*;

/// Extra options for the [SemanticModel] creation.
#[derive(Default)]
Expand All @@ -44,7 +44,6 @@ pub struct SemanticModelOptions {
pub globals: HashSet<String>,
}


/// Build the complete [SemanticModel] of a parsed file.
/// For a push based model to build the [SemanticModel], see [SemanticModelBuilder].
pub fn semantic_model(root: &JsAnyRoot, options: SemanticModelOptions) -> SemanticModel {
Expand Down
Loading

0 comments on commit 874a294

Please sign in to comment.