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

fix(rome_js_analyze): fix class expr binding #3861

Merged
merged 3 commits into from
Nov 28, 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
15 changes: 10 additions & 5 deletions crates/rome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ pub struct SemanticEventExtractor {
stash: VecDeque<SemanticEvent>,
scopes: Vec<Scope>,
next_scope_id: usize,
/// At any point this is the set of available bindings and
/// the range of its declaration
bindings: FxHashMap<SyntaxTokenText, TextRange>,
}

Expand Down Expand Up @@ -199,7 +201,8 @@ struct Scope {
started_at: TextSize,
/// All bindings declared inside this scope
bindings: Vec<Binding>,
/// References that still needs to be bound
/// References that still needs to be bound and
/// will be solved at the end of the scope
references: HashMap<SyntaxTokenText, Vec<Reference>>,
/// All bindings that where shadowed and will be
/// restored after this scope ends.
Expand Down Expand Up @@ -388,8 +391,7 @@ impl SemanticEventExtractor {
self.export_declaration(node, &parent);
}
JS_CLASS_EXPRESSION => {
let hoisted_scope_id = self.scope_index_to_hoist_declarations(1);
self.push_binding_into_scope(hoisted_scope_id, &name_token);
self.push_binding_into_scope(None, &name_token);
self.export_class_expression(node, &parent);
}
JS_OBJECT_BINDING_PATTERN_SHORTHAND_PROPERTY
Expand Down Expand Up @@ -447,15 +449,17 @@ impl SemanticEventExtractor {

let (name, is_exported) = match node.kind() {
JsSyntaxKind::JS_REFERENCE_IDENTIFIER => {
let reference = node.clone().cast::<JsReferenceIdentifier>()?;
// SAFETY: kind check above
let reference = JsReferenceIdentifier::unwrap_cast(node.clone());
let name_token = reference.value_token().ok()?;
(
name_token.token_text_trimmed(),
self.is_js_reference_identifier_exported(node),
)
}
JsSyntaxKind::JSX_REFERENCE_IDENTIFIER => {
let reference = node.clone().cast::<JsxReferenceIdentifier>()?;
// SAFETY: kind check above
let reference = JsxReferenceIdentifier::unwrap_cast(node.clone());
let name_token = reference.value_token().ok()?;
(name_token.token_text_trimmed(), false)
}
Expand Down Expand Up @@ -645,6 +649,7 @@ impl SemanticEventExtractor {
}

// Remove all bindings declared in this scope

for binding in scope.bindings {
self.bindings.remove(&binding.name);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/rome_js_semantic/src/tests/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ assert_semantics! {
assert_semantics! {
ok_class_reference,
"class A/*#A*/ {} new A/*READ A*/();",
ok_class_expression_1,
"const A/*#A*/ = class B/*#B*/ {}; console.log(A/*READ A*/, B/*?*/);",
//https://github.com/rome/tools/issues/3779
ok_class_expression_2,
"const A/*#A1*/ = print(class A/*#A2*/ {}); console.log(A/*READ A1*/);",
}

// Typescript types
Expand Down