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

Commit

Permalink
only accept nodes that can be returned
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Leite committed Nov 22, 2022
1 parent c660de3 commit 9d7cfa7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
38 changes: 29 additions & 9 deletions crates/rome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub enum SemanticEvent {
range: TextRange,
scope_id: usize,
parent_scope_id: Option<usize>,
is_closure: bool
},

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

JS_FUNCTION_DECLARATION
| JS_FUNCTION_EXPORT_DEFAULT_DECLARATION
| JS_FUNCTION_EXPRESSION
| JS_ARROW_FUNCTION_EXPRESSION
| JS_CLASS_DECLARATION
| JS_CLASS_EXPORT_DEFAULT_DECLARATION
| JS_CLASS_EXPRESSION
| JS_CONSTRUCTOR_CLASS_MEMBER
| JS_METHOD_CLASS_MEMBER
| JS_GETTER_CLASS_MEMBER
| JS_SETTER_CLASS_MEMBER
| JS_METHOD_OBJECT_MEMBER
| JS_GETTER_OBJECT_MEMBER
| JS_SETTER_OBJECT_MEMBER
| JS_SETTER_OBJECT_MEMBER => {
self.push_scope(
node.text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
true
);
},

JS_FUNCTION_EXPORT_DEFAULT_DECLARATION
| JS_CLASS_DECLARATION
| JS_CLASS_EXPORT_DEFAULT_DECLARATION
| JS_CLASS_EXPRESSION
| JS_FUNCTION_BODY
| TS_INTERFACE_DECLARATION
| TS_ENUM_DECLARATION
Expand All @@ -284,12 +294,21 @@ impl SemanticEventExtractor {
self.push_scope(
node.text_range(),
ScopeHoisting::DontHoistDeclarationsToParent,
false
);
}

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(), ScopeHoisting::HoistDeclarationsToParent);
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(),
ScopeHoisting::HoistDeclarationsToParent,
false
);
}

_ => {}
Expand Down Expand Up @@ -516,7 +535,7 @@ impl SemanticEventExtractor {
self.stash.pop_front()
}

fn push_scope(&mut self, range: TextRange, hoisting: ScopeHoisting) {
fn push_scope(&mut self, range: TextRange, hoisting: ScopeHoisting, is_closure: bool) {
let scope_id = self.next_scope_id;
self.next_scope_id += 1;

Expand All @@ -526,6 +545,7 @@ impl SemanticEventExtractor {
range,
scope_id,
parent_scope_id,
is_closure,
});

self.scopes.push(Scope {
Expand Down
46 changes: 45 additions & 1 deletion crates/rome_js_semantic/src/semantic_model/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,49 @@ impl SemanticModelBuilder {

#[inline]
pub fn push_node(&mut self, node: &JsSyntaxNode) {
self.node_by_range.insert(node.text_range(), node.clone());
use JsSyntaxKind::*;
match node.kind() {
// Acessible from bindings and references
JS_IDENTIFIER_BINDING
| TS_IDENTIFIER_BINDING
| JS_REFERENCE_IDENTIFIER
| JSX_REFERENCE_IDENTIFIER
| JS_IDENTIFIER_ASSIGNMENT => {
self.node_by_range.insert(node.text_range(), node.clone());
}

// Acessible from scopes, closures
JS_MODULE
| JS_SCRIPT
| JS_FUNCTION_DECLARATION
| JS_FUNCTION_EXPRESSION
| JS_ARROW_FUNCTION_EXPRESSION
| JS_CONSTRUCTOR_CLASS_MEMBER
| JS_METHOD_CLASS_MEMBER
| JS_GETTER_CLASS_MEMBER
| JS_SETTER_CLASS_MEMBER
| JS_METHOD_OBJECT_MEMBER
| JS_GETTER_OBJECT_MEMBER
| JS_SETTER_OBJECT_MEMBER
| JS_FUNCTION_EXPORT_DEFAULT_DECLARATION
| JS_CLASS_DECLARATION
| JS_CLASS_EXPORT_DEFAULT_DECLARATION
| JS_CLASS_EXPRESSION
| JS_FUNCTION_BODY
| TS_INTERFACE_DECLARATION
| TS_ENUM_DECLARATION
| TS_TYPE_ALIAS_DECLARATION
| TS_FUNCTION_TYPE
| JS_BLOCK_STATEMENT
| JS_FOR_STATEMENT
| JS_FOR_OF_STATEMENT
| JS_FOR_IN_STATEMENT
| JS_SWITCH_STATEMENT
| JS_CATCH_CLAUSE => {
self.node_by_range.insert(node.text_range(), node.clone());
}
_ => {}
}
}

#[inline]
Expand All @@ -63,6 +105,7 @@ impl SemanticModelBuilder {
range,
parent_scope_id,
scope_id,
is_closure
} => {
// Scopes will be raised in order
debug_assert!(scope_id == self.scopes.len());
Expand All @@ -75,6 +118,7 @@ impl SemanticModelBuilder {
bindings_by_name: FxHashMap::default(),
read_references: vec![],
write_references: vec![],
is_closure
});

if let Some(parent_scope_id) = parent_scope_id {
Expand Down
9 changes: 3 additions & 6 deletions crates/rome_js_semantic/src/semantic_model/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ impl Iterator for AllCapturesIter {

'scopes: while let Some(scope_id) = self.scopes.pop() {
let scope = &self.data.scopes[scope_id];
let node = &self.data.node_by_range[&scope.range];

if AnyHasClosureNode::from_node(node).is_some() {
if scope.is_closure {
continue 'scopes;
} else {
self.references.clear();
Expand Down Expand Up @@ -179,8 +178,7 @@ impl Iterator for ChildrenIter {
fn next(&mut self) -> Option<Self::Item> {
while let Some(scope_id) = self.scopes.pop() {
let scope = &self.data.scopes[scope_id];
let node = &self.data.node_by_range[&scope.range];
if AnyHasClosureNode::from_node(node).is_some() {
if scope.is_closure {
return Some(Closure {
data: self.data.clone(),
scope_id,
Expand Down Expand Up @@ -209,9 +207,8 @@ impl Iterator for DescendentsIter {
fn next(&mut self) -> Option<Self::Item> {
while let Some(scope_id) = self.scopes.pop() {
let scope = &self.data.scopes[scope_id];
let node = &self.data.node_by_range[&scope.range];
self.scopes.extend(scope.children.iter());
if AnyHasClosureNode::from_node(node).is_some() {
if scope.is_closure {
return Some(Closure {
data: self.data.clone(),
scope_id,
Expand Down
2 changes: 2 additions & 0 deletions crates/rome_js_semantic/src/semantic_model/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub(crate) struct SemanticModelScopeData {
pub(crate) read_references: Vec<SemanticModelScopeReference>,
// All write references of a scope
pub(crate) write_references: Vec<SemanticModelScopeReference>,
// Identify if this scope is from a closure or not
pub(crate) is_closure: bool,
}

/// Provides all information regarding a specific scope.
Expand Down

0 comments on commit 9d7cfa7

Please sign in to comment.