From 50ab6f2f509116db4eb61173dda26964f37d5c73 Mon Sep 17 00:00:00 2001 From: meloalright Date: Sun, 17 Mar 2024 15:57:56 +0800 Subject: [PATCH] feat: complete the component update case --- rustle/src/compiler/generate/mod.rs | 28 ++++++++++++++++++--- rustle/tests/component_update/Nested.rustle | 8 ++++-- rustle/tests/component_update/app.rustle | 11 ++++---- rustle/tests/component_update/index.html | 15 +++++++++++ rustle/tests/test_parsing.rs | 12 ++++++++- 5 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 rustle/tests/component_update/index.html diff --git a/rustle/src/compiler/generate/mod.rs b/rustle/src/compiler/generate/mod.rs index adf2d3a..bb66b55 100644 --- a/rustle/src/compiler/generate/mod.rs +++ b/rustle/src/compiler/generate/mod.rs @@ -12,6 +12,7 @@ struct Code { components: Vec<(String, String)>, create: Vec, update: Vec, + props_set: Vec, destroy: Vec, } @@ -22,6 +23,7 @@ pub fn generate(ast: RustleAst, analysis: AnalysisResult) -> String { components: Vec::new(), create: Vec::new(), update: Vec::new(), + props_set: Vec::new(), destroy: Vec::new(), }; @@ -65,6 +67,9 @@ pub fn generate(ast: RustleAst, analysis: AnalysisResult) -> String { update(changed) {{ {} }}, + $set(changedSet) {{ + {} + }}, destroy() {{ {} }}, @@ -86,6 +91,7 @@ pub fn generate(ast: RustleAst, analysis: AnalysisResult) -> String { .join("\n"), code.create.join("\n"), code.update.join("\n"), + code.props_set.join("\n"), code.destroy.join("\n") ) } @@ -176,11 +182,11 @@ fn traverse(node: &Fragment, parent: String, analysis: &AnalysisResult, code: &m r#" const {}_changes = {{}}; if (changed.includes('{}')) {{ - {}_changes.{} = {}; + {}_changes['{}'] = {}; }} - variable_name.$set({}_changes); + {}.$set({}_changes); "#, - variable_name, value, variable_name, attr.name, value, variable_name + variable_name, value, variable_name, attr.name, value, variable_name, variable_name )); }, _ => { @@ -246,6 +252,14 @@ fn traverse(node: &Fragment, parent: String, analysis: &AnalysisResult, code: &m "#, expression_name, variable_name, expression_name )); + code.props_set.push(format!( + r#" + if ('{}' in changedSet) {{ + {}.data = changedSet['{}']; + }} + "#, + expression_name, variable_name, expression_name + )); } else { for change in analysis.will_change.iter() { if expression_name.contains(change) { @@ -257,6 +271,14 @@ fn traverse(node: &Fragment, parent: String, analysis: &AnalysisResult, code: &m "#, change, variable_name, expression_name )); + code.props_set.push(format!( + r#" + if ('{}' in changedSet) {{ + {}.data = changedSet['{}']; + }} + "#, + expression_name, variable_name, expression_name + )); } } } diff --git a/rustle/tests/component_update/Nested.rustle b/rustle/tests/component_update/Nested.rustle index 82e3a14..679caf6 100644 --- a/rustle/tests/component_update/Nested.rustle +++ b/rustle/tests/component_update/Nested.rustle @@ -1,5 +1,9 @@ -

...don't affect this element {q}

+
+ +

...don't affect this element q={q}

+
\ No newline at end of file diff --git a/rustle/tests/component_update/app.rustle b/rustle/tests/component_update/app.rustle index 8fdb9cf..28ec17d 100644 --- a/rustle/tests/component_update/app.rustle +++ b/rustle/tests/component_update/app.rustle @@ -1,8 +1,9 @@ - - + + diff --git a/rustle/tests/component_update/index.html b/rustle/tests/component_update/index.html new file mode 100644 index 0000000..9fde04d --- /dev/null +++ b/rustle/tests/component_update/index.html @@ -0,0 +1,15 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/rustle/tests/test_parsing.rs b/rustle/tests/test_parsing.rs index cae7151..594fa12 100644 --- a/rustle/tests/test_parsing.rs +++ b/rustle/tests/test_parsing.rs @@ -70,5 +70,15 @@ fn test_parsing_nested() { #[test] fn test_tag_update() { test_parsing("tag_update".to_owned()) } +// in browser passed #[test] -fn test_component_update() { test_parsing("component_update".to_owned()) } +fn test_component_update() { + test_parsing("component_update".to_owned()); + + let source = fs::read_to_string("tests/component_update/Nested.rustle").unwrap(); + let ast = Parser::new(&source).parse(); + let analysis = analyse(&ast); + let generated = generate(ast, analysis); + + fs::write("tests/component_update/Nested.js", generated).unwrap(); +}