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

refactor(rome_js_semantic): deduplicate code #4738

Merged
merged 1 commit into from
Jul 29, 2023
Merged
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
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