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

feat(rome_js_semantic): support for jsx reference inside the semantic model #3055

Merged
merged 1 commit into from
Aug 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export function exported_function() {}

function exported_function_2() {}
export { exported_function_2 };

let value;
function Button() {}
console.log(<Button att={value}/>);
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export function exported_function() {}
function exported_function_2() {}
export { exported_function_2 };
let value;
function Button() {}
console.log(<Button att={value}/>);
```

# Diagnostics
Expand Down
35 changes: 25 additions & 10 deletions crates/rome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rome_js_syntax::{
JsAnyAssignment, JsAnyAssignmentPattern, JsAssignmentExpression, JsForVariableDeclaration,
JsIdentifierAssignment, JsIdentifierBinding, JsLanguage, JsReferenceIdentifier, JsSyntaxKind,
JsSyntaxNode, JsSyntaxToken, JsVariableDeclaration, JsVariableDeclarator,
JsVariableDeclaratorList, TextRange, TextSize,
JsVariableDeclaratorList, JsxReferenceIdentifier, TextRange, TextSize,
};
use rome_rowan::{syntax::Preorder, AstNode, SyntaxNodeCast, SyntaxTokenText};

Expand Down Expand Up @@ -217,8 +217,8 @@ impl SemanticEventExtractor {
JS_IDENTIFIER_BINDING => {
self.enter_js_identifier_binding(node);
}
JS_REFERENCE_IDENTIFIER => {
self.enter_js_reference_identifier(node);
JS_REFERENCE_IDENTIFIER | JSX_REFERENCE_IDENTIFIER => {
self.enter_reference_identifier(node);
}
JS_IDENTIFIER_ASSIGNMENT => {
self.enter_js_identifier_assignment(node);
Expand Down Expand Up @@ -315,14 +315,29 @@ impl SemanticEventExtractor {
Some(())
}

fn enter_js_reference_identifier(&mut self, node: &JsSyntaxNode) -> Option<()> {
debug_assert!(matches!(node.kind(), JsSyntaxKind::JS_REFERENCE_IDENTIFIER));
fn enter_reference_identifier(&mut self, node: &JsSyntaxNode) -> Option<()> {
debug_assert!(matches!(
node.kind(),
JsSyntaxKind::JS_REFERENCE_IDENTIFIER | JsSyntaxKind::JSX_REFERENCE_IDENTIFIER
));

let reference = node.clone().cast::<JsReferenceIdentifier>()?;
let name_token = reference.value_token().ok()?;
let name = name_token.token_text_trimmed();
let (name, is_exported) = match node.kind() {
JsSyntaxKind::JS_REFERENCE_IDENTIFIER => {
let reference = node.clone().cast::<JsReferenceIdentifier>()?;
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>()?;
let name_token = reference.value_token().ok()?;
(name_token.token_text_trimmed(), false)
}
_ => return None,
};

let is_exported = self.is_reference_identifier_exported(node);
let current_scope = self.current_scope_mut();
let references = current_scope.references.entry(name).or_default();
references.push(Reference::Read {
Expand Down Expand Up @@ -710,7 +725,7 @@ impl SemanticEventExtractor {
}

// Check if a reference is exported and raise the [Exported] event.
fn is_reference_identifier_exported(&mut self, reference: &JsSyntaxNode) -> bool {
fn is_js_reference_identifier_exported(&mut self, reference: &JsSyntaxNode) -> bool {
use JsSyntaxKind::*;
debug_assert!(matches!(reference.kind(), JS_REFERENCE_IDENTIFIER));

Expand Down
6 changes: 5 additions & 1 deletion crates/rome_js_semantic/src/tests/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ use std::collections::{BTreeMap, HashMap};
/// if(true) ;/*NOEVENT*/;
/// ```
pub fn assert(code: &str, test_name: &str) {
let r = rome_js_parser::parse(code, 0, SourceType::js_module());
let r = rome_js_parser::parse(
code,
0,
SourceType::js_module().with_variant(rome_js_syntax::LanguageVariant::Jsx),
);

if r.has_errors() {
let files = SimpleFile::new(test_name.to_string(), code.into());
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 @@ -124,6 +124,11 @@ assert_semantics! {
ok_scope_function_expression_read2, "let f/*#F1*/ = 1; let g = function f/*#F2*/() {console.log(2, f/*READ F2*/);}; console.log(f/*READ F1*/);",
}

// Imports
assert_semantics! {
ok_import_used_in_jsx, r#"import A/*#A*/ from 'a.js'; console.log(<A/*READ A*//>);"#,
}

assert_semantics! {
ok_unmatched_reference, r#"a/*?*/"#,
ok_function_expression_read,"let f/*#F*/ = function g/*#G*/(){}; g/*?*/();",
Expand Down