Skip to content

Commit c9b15af

Browse files
authored
Merge pull request #10 from dev-five-git/rm-attr
Change order to remove attr
2 parents 41e9bf8 + fe37e10 commit c9b15af

File tree

5 files changed

+104
-54
lines changed

5 files changed

+104
-54
lines changed

.changeset/spotty-camels-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@devup-ui/wasm": patch
3+
---
4+
5+
Change order to remove attr

.github/FUNDING.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# These are supported funding model platforms
22

3-
github: [owjs3901]
3+
github: owjs3901
44
patreon: JeongMinOh
55
open_collective: devup-ui
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

libs/extractor/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,25 @@ mod tests {
410410
}
411411
)
412412
.unwrap());
413+
414+
reset_class_map();
415+
assert_debug_snapshot!(extract(
416+
"test.tsx",
417+
r#"import {Image} from '@devup-ui/core'
418+
<Image
419+
className={styles.logo}
420+
src="/next.svg"
421+
alt="Next.js logo"
422+
width={180}
423+
height={38}
424+
/>
425+
"#,
426+
ExtractOption {
427+
package: "@devup-ui/core".to_string(),
428+
css_file: None
429+
}
430+
)
431+
.unwrap());
413432
}
414433

415434
#[test]

libs/extractor/src/prop_modify_utils.rs

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,29 @@ pub fn modify_prop_object<'a>(
1919
let mut class_name_prop = None;
2020
let mut style_prop = None;
2121

22-
for idx in 0..props.len() {
23-
if let ObjectPropertyKind::ObjectProperty(attr) = &props[idx] {
22+
for idx in (0..props.len()).rev() {
23+
if let ObjectPropertyKind::ObjectProperty(attr) = props.remove(idx) {
2424
if let PropertyKey::StaticIdentifier(ident) = &attr.key {
2525
if ident.name == "className" {
26-
if let ObjectPropertyKind::ObjectProperty(attr) = props.remove(idx) {
27-
class_name_prop = Some(attr);
28-
}
26+
class_name_prop = Some(attr);
27+
continue;
2928
} else if ident.name == "style" {
30-
if let ObjectPropertyKind::ObjectProperty(attr) = props.remove(idx) {
31-
style_prop = Some(attr);
32-
}
29+
style_prop = Some(attr);
30+
continue;
3331
}
3432
}
33+
props.insert(idx, ObjectPropertyKind::ObjectProperty(attr));
3534
}
3635
}
3736

3837
// should modify class name prop
3938
if let Some(ex) = gen_class_names(ast_builder, &styles) {
4039
if let Some(pr) = if let Some(class_name_prop) = class_name_prop {
41-
let res = merge_expression_for_class_name(
40+
merge_expression_for_class_name(
4241
ast_builder,
4342
vec![class_name_prop.value.clone_in(ast_builder.allocator), ex],
44-
);
45-
res.map(|res| {
43+
)
44+
.map(|res| {
4645
ast_builder.alloc_object_property(
4746
SPAN,
4847
PropertyKind::Init,
@@ -75,41 +74,37 @@ pub fn modify_prop_object<'a>(
7574

7675
// should modify style prop
7776
if let Some(mut ex) = gen_styles(ast_builder, &styles) {
78-
if let Some(style_prop) = style_prop {
79-
props.push(ObjectPropertyKind::ObjectProperty(
80-
ast_builder.alloc_object_property(
81-
SPAN,
82-
PropertyKind::Init,
83-
PropertyKey::StaticIdentifier(ast_builder.alloc_identifier_name(SPAN, "style")),
84-
if ex.properties.is_empty() {
85-
Expression::ObjectExpression(ast_builder.alloc(ex))
86-
} else {
87-
ex.properties.push(ObjectPropertyKind::SpreadProperty(
88-
ast_builder.alloc_spread_element(
89-
SPAN,
90-
style_prop.value.clone_in(ast_builder.allocator),
91-
),
92-
));
93-
Expression::ObjectExpression(ast_builder.alloc(ex))
94-
},
95-
false,
96-
false,
97-
false,
98-
),
99-
));
77+
props.push(if let Some(style_prop) = style_prop {
78+
ObjectPropertyKind::ObjectProperty(ast_builder.alloc_object_property(
79+
SPAN,
80+
PropertyKind::Init,
81+
PropertyKey::StaticIdentifier(ast_builder.alloc_identifier_name(SPAN, "style")),
82+
if ex.properties.is_empty() {
83+
Expression::ObjectExpression(ast_builder.alloc(ex))
84+
} else {
85+
ex.properties.push(ObjectPropertyKind::SpreadProperty(
86+
ast_builder.alloc_spread_element(
87+
SPAN,
88+
style_prop.value.clone_in(ast_builder.allocator),
89+
),
90+
));
91+
Expression::ObjectExpression(ast_builder.alloc(ex))
92+
},
93+
false,
94+
false,
95+
false,
96+
))
10097
} else {
101-
props.push(ObjectPropertyKind::ObjectProperty(
102-
ast_builder.alloc_object_property(
103-
SPAN,
104-
PropertyKind::Init,
105-
PropertyKey::StaticIdentifier(ast_builder.alloc_identifier_name(SPAN, "style")),
106-
Expression::ObjectExpression(ast_builder.alloc(ex)),
107-
false,
108-
false,
109-
false,
110-
),
111-
));
112-
};
98+
ObjectPropertyKind::ObjectProperty(ast_builder.alloc_object_property(
99+
SPAN,
100+
PropertyKind::Init,
101+
PropertyKey::StaticIdentifier(ast_builder.alloc_identifier_name(SPAN, "style")),
102+
Expression::ObjectExpression(ast_builder.alloc(ex)),
103+
false,
104+
false,
105+
false,
106+
))
107+
});
113108
} else if let Some(style_prop) = style_prop {
114109
// re add class name prop if not modified
115110
props.push(ObjectPropertyKind::ObjectProperty(style_prop))
@@ -124,19 +119,18 @@ pub fn modify_props<'a>(
124119
let mut class_name_prop = None;
125120
let mut style_prop = None;
126121

127-
for idx in 0..props.len() {
128-
if let Attribute(attr) = &props[idx] {
122+
for idx in (0..props.len()).rev() {
123+
if let Attribute(attr) = props.remove(idx) {
129124
if let Identifier(ident) = &attr.name {
130125
if ident.name == "className" {
131-
if let Attribute(attr) = props.remove(idx) {
132-
class_name_prop = Some(attr);
133-
}
126+
class_name_prop = Some(attr);
127+
continue;
134128
} else if ident.name == "style" {
135-
if let Attribute(attr) = props.remove(idx) {
136-
style_prop = Some(attr);
137-
}
129+
style_prop = Some(attr);
130+
continue;
138131
}
139132
}
133+
props.insert(idx, Attribute(attr));
140134
}
141135
}
142136

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
expression: "extract(\"test.tsx\",\nr#\"import {Image} from '@devup-ui/core'\n <Image\n className={styles.logo}\n src=\"/next.svg\"\n alt=\"Next.js logo\"\n width={180}\n height={38}\n />\n \"#,\nExtractOption\n{ package: \"@devup-ui/core\".to_string(), css_file: None }).unwrap()"
4+
---
5+
ExtractOutput {
6+
styles: [
7+
Static(
8+
ExtractStaticStyle {
9+
property: "width",
10+
value: "720px",
11+
level: 0,
12+
selector: None,
13+
},
14+
),
15+
Static(
16+
ExtractStaticStyle {
17+
property: "height",
18+
value: "152px",
19+
level: 0,
20+
selector: None,
21+
},
22+
),
23+
],
24+
code: "import \"@devup-ui/core/devup-ui.css\";\n<img src=\"/next.svg\" alt=\"Next.js logo\" className={`d0 d1 ${styles.logo}`} />;\n",
25+
}

0 commit comments

Comments
 (0)