Don't eat keys (#2280)

This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-04-08 22:31:58 -07:00
1 parent 67af2d89dd
commit 994056e16d
3 files changed
+41 -6

No files matched your search

+18 -5
View File
@@ -28,6 +28,7 @@ impl Writer<'_> {
children, children,
manual_props, manual_props,
prop_gen_args, prop_gen_args,
key,
.. ..
}: &Component, }: &Component,
) -> Result { ) -> Result {
@@ -38,7 +39,7 @@ impl Writer<'_> {
let mut opt_level = ShortOptimization::NoOpt; let mut opt_level = ShortOptimization::NoOpt;
// check if we have a lot of attributes // check if we have a lot of attributes
let attr_len = self.field_len(fields, manual_props); let attr_len = self.field_len(fields, manual_props) + self.key_len(key.as_ref());
let is_short_attr_list = attr_len < 80; let is_short_attr_list = attr_len < 80;
let is_small_children = self.is_short_children(children).is_some(); let is_small_children = self.is_short_children(children).is_some();
@@ -62,7 +63,7 @@ impl Writer<'_> {
} }
// If there's nothing at all, empty optimization // If there's nothing at all, empty optimization
if fields.is_empty() && children.is_empty() && manual_props.is_none() { if fields.is_empty() && children.is_empty() && manual_props.is_none() && key.is_none() {
opt_level = ShortOptimization::Empty; opt_level = ShortOptimization::Empty;
} }
@@ -85,7 +86,7 @@ impl Writer<'_> {
ShortOptimization::Oneliner => { ShortOptimization::Oneliner => {
write!(self.out, " ")?; write!(self.out, " ")?;
self.write_component_fields(fields, manual_props, true)?; self.write_component_fields(fields, key.as_ref(), manual_props, true)?;
if !children.is_empty() && !fields.is_empty() { if !children.is_empty() && !fields.is_empty() {
write!(self.out, ", ")?; write!(self.out, ", ")?;
@@ -103,7 +104,7 @@ impl Writer<'_> {
ShortOptimization::PropsOnTop => { ShortOptimization::PropsOnTop => {
write!(self.out, " ")?; write!(self.out, " ")?;
self.write_component_fields(fields, manual_props, true)?; self.write_component_fields(fields, key.as_ref(), manual_props, true)?;
if !children.is_empty() && !fields.is_empty() { if !children.is_empty() && !fields.is_empty() {
write!(self.out, ",")?; write!(self.out, ",")?;
@@ -114,7 +115,7 @@ impl Writer<'_> {
} }
ShortOptimization::NoOpt => { ShortOptimization::NoOpt => {
self.write_component_fields(fields, manual_props, false)?; self.write_component_fields(fields, key.as_ref(), manual_props, false)?;
if !children.is_empty() && !fields.is_empty() { if !children.is_empty() && !fields.is_empty() {
write!(self.out, ",")?; write!(self.out, ",")?;
@@ -154,11 +155,23 @@ impl Writer<'_> {
fn write_component_fields( fn write_component_fields(
&mut self, &mut self,
fields: &[ComponentField], fields: &[ComponentField],
key: Option<&IfmtInput>,
manual_props: &Option<syn::Expr>, manual_props: &Option<syn::Expr>,
sameline: bool, sameline: bool,
) -> Result { ) -> Result {
let mut field_iter = fields.iter().peekable(); let mut field_iter = fields.iter().peekable();
// write the key
if let Some(key) = key {
write!(self.out, "key: {}", ifmt_to_string(key))?;
if !fields.is_empty() {
write!(self.out, ",")?;
if sameline {
write!(self.out, " ")?;
}
}
}
while let Some(field) = field_iter.next() { while let Some(field) = field_iter.next() {
if !sameline { if !sameline {
self.out.indented_tabbed_line().unwrap(); self.out.indented_tabbed_line().unwrap();
+8 -1
View File
@@ -1,4 +1,4 @@
use dioxus_rsx::{AttributeType, BodyNode, ElementAttrValue, ForLoop, IfChain}; use dioxus_rsx::{AttributeType, BodyNode, ElementAttrValue, ForLoop, IfChain, IfmtInput};
use proc_macro2::{LineColumn, Span}; use proc_macro2::{LineColumn, Span};
use quote::ToTokens; use quote::ToTokens;
use std::{ use std::{
@@ -329,6 +329,13 @@ impl<'a> Writer<'a> {
Ok(()) Ok(())
} }
pub(crate) fn key_len(&self, key: Option<&IfmtInput>) -> usize {
match key {
Some(key) => ifmt_to_string(key).len() + 5,
None => 0,
}
}
} }
pub(crate) trait SpanLength { pub(crate) trait SpanLength {
@@ -48,6 +48,21 @@ rsx! {
} }
} }
for i in 0..10 {
Component { key: "{i}", blah: 120 }
}
for i in 0..10 {
Component { key: "{i}" }
}
for i in 0..10 {
div { key: "{i}", blah: 120 }
}
for i in 0..10 {
div { key: "{i}" }
}
div { div {
"asdbascasdbasd" "asdbascasdbasd"
"asbdasbdabsd" "asbdasbdabsd"