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

feat(rome_js_syntax): better api for picking jsx attributes #3458

Merged
merged 8 commits into from
Oct 21, 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
31 changes: 31 additions & 0 deletions crates/rome_js_analyze/src/utils/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,34 @@ macro_rules! assert_remove_ok {
)*
};
}

#[test]
pub fn ok_find_attributes_by_name() {
let r = rome_js_parser::parse(r#"<a a="A" c="C" b="B" />"#, 0.into(), SourceType::jsx());
let list = r
.syntax()
.descendants()
.filter_map(rome_js_syntax::JsxAttributeList::cast)
.next()
.unwrap();
let [a, c, d] = list.find_attributes_by_name(["a", "c", "d"]);
assert_eq!(
a.unwrap()
.initializer()
.unwrap()
.value()
.unwrap()
.to_string(),
"\"A\" "
);
assert_eq!(
c.unwrap()
.initializer()
.unwrap()
.value()
.unwrap()
.to_string(),
"\"C\" "
);
assert!(d.is_none());
}
52 changes: 52 additions & 0 deletions crates/rome_js_syntax/src/jsx_ext.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashSet;

use crate::{
JsxAttribute, JsxAttributeList, JsxName, JsxOpeningElement, JsxSelfClosingElement, JsxString,
TextSize,
Expand Down Expand Up @@ -137,6 +139,56 @@ impl JsxSelfClosingElement {
}
}

impl JsxAttributeList {
/// Find and return the `JsxAttribute` that matches the given name like [find_attribute_by_name].
/// Only attributes with name as [JsxName] can be returned.
///
/// Each name of "names_to_lookup" should be unique.
///
/// Supports maximum of 16 names to avoid stack overflow. Eeach attribute will consume:
///
/// - 8 bytes for the [Option<JsxAttribute>] result;
/// - plus 16 bytes for the [&str] argument.
pub fn find_attributes_by_name<const N: usize>(
&self,
names_to_lookup: [&str; N],
) -> [Option<JsxAttribute>; N] {
// assert there are no duplicates
debug_assert!(HashSet::<_>::from_iter(names_to_lookup).len() == N);
debug_assert!(N <= 16);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it necessary that N is smaller than 16?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to put a safeguard, given that everything is allocated on the stack.

Copy link
Contributor

@ematipico ematipico Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind add a comment to explain why? I personally don't know why that is safe and against what. That would be appreciated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


const INIT: Option<JsxAttribute> = None;
let mut results = [INIT; N];

let mut missing = N;

'attributes: for att in self.iter() {
if let Some(attribute) = att.as_jsx_attribute() {
if let Some(name) = attribute
.name()
.ok()
.and_then(|x| x.as_jsx_name()?.value_token().ok())
{
let name = name.text_trimmed();
for i in 0..N {
if results[i].is_none() && names_to_lookup[i] == name {
results[i] = Some(attribute.clone());
if missing == 1 {
break 'attributes;
} else {
missing -= 1;
break;
}
}
}
}
}
}

results
}
}

pub fn find_attribute_by_name(
attributes: JsxAttributeList,
name_to_lookup: &str,
Expand Down