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

Commit

Permalink
refactor(rome_js_semantic): deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jul 29, 2023
1 parent f13de15 commit 243d09f
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions crates/rome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ impl SemanticEventExtractor {
self.push_binding_into_scope(hoisted_scope_id, &name_token, &parent_kind);
self.export_function_declaration(node, &parent);
}
JS_FUNCTION_EXPRESSION => {
JS_CLASS_EXPRESSION | JS_FUNCTION_EXPRESSION => {
self.push_binding_into_scope(None, &name_token, &parent_kind);
self.export_function_expression(node, &parent);
self.export_declaration_expression(node, &parent);
}
JS_CLASS_DECLARATION
| JS_CLASS_EXPORT_DEFAULT_DECLARATION
Expand All @@ -409,10 +409,6 @@ impl SemanticEventExtractor {
self.push_binding_into_scope(parent_scope, &name_token, &parent_kind);
self.export_declaration(node, &parent);
}
JS_CLASS_EXPRESSION => {
self.push_binding_into_scope(None, &name_token, &parent_kind);
self.export_class_expression(node, &parent);
}
JS_BINDING_PATTERN_WITH_DEFAULT
| JS_OBJECT_BINDING_PATTERN
| JS_OBJECT_BINDING_PATTERN_REST
Expand Down Expand Up @@ -855,30 +851,18 @@ impl SemanticEventExtractor {
}
}

// Check if a function is exported and raise the [Exported] event.
fn export_function_expression(
// Check if a function or class expression is exported and raise the [Exported] event.
fn export_declaration_expression(
&mut self,
binding: &JsSyntaxNode,
function_expression: &JsSyntaxNode,
declaration_expression: &JsSyntaxNode,
) {
use JsSyntaxKind::*;
debug_assert!(matches!(function_expression.kind(), JS_FUNCTION_EXPRESSION));
let is_module_exports = function_expression
.parent()
.map(|x| self.is_assignment_left_side_module_exports(&x))
.unwrap_or(false);
if is_module_exports {
self.stash.push_back(SemanticEvent::Exported {
range: binding.text_range(),
});
}
}

// Check if a function is exported and raise the [Exported] event.
fn export_class_expression(&mut self, binding: &JsSyntaxNode, class_expression: &JsSyntaxNode) {
use JsSyntaxKind::*;
debug_assert!(matches!(class_expression.kind(), JS_CLASS_EXPRESSION));
let is_module_exports = class_expression
debug_assert!(matches!(
declaration_expression.kind(),
JS_FUNCTION_EXPRESSION | JS_CLASS_EXPRESSION
));
let is_module_exports = declaration_expression
.parent()
.map(|x| self.is_assignment_left_side_module_exports(&x))
.unwrap_or(false);
Expand Down

0 comments on commit 243d09f

Please sign in to comment.