mod generate;
pub(crate) mod helpers;
use crate::ast::{CompoundExpressionChild, ExpressionNode, SimpleExpressionNode};
use super::{context::CodegenContext, helpers::escape_js_string};
use helpers::{convert_line_comments_to_block, strip_ctx_for_slot_params};
use vize_carton::String;
use vize_carton::ToCompactString;
#[allow(unused_imports)]
pub use generate::{
generate_event_handler, generate_simple_expression_with_prefix, is_inline_handler,
is_simple_member_expression,
};
pub fn generate_expression(ctx: &mut CodegenContext, expr: &ExpressionNode<'_>) {
match expr {
ExpressionNode::Simple(exp) => {
generate_simple_expression(ctx, exp);
}
ExpressionNode::Compound(comp) => {
for child in comp.children.iter() {
match child {
CompoundExpressionChild::Simple(exp) => {
generate_simple_expression(ctx, exp);
}
CompoundExpressionChild::String(s) => {
ctx.push(s);
}
CompoundExpressionChild::Symbol(helper) => {
ctx.push(ctx.helper(*helper));
}
_ => {}
}
}
}
}
}
pub fn generate_simple_expression(ctx: &mut CodegenContext, exp: &SimpleExpressionNode<'_>) {
if exp.is_static {
ctx.push("\"");
ctx.push(&escape_js_string(exp.content.as_str()));
ctx.push("\"");
} else {
let mut content: String = if ctx.options.is_ts && exp.content.contains(" as ") {
crate::transforms::strip_typescript_from_expression(&exp.content)
} else {
exp.content.to_compact_string()
};
if content.contains("//") {
content = convert_line_comments_to_block(&content);
}
if ctx.has_slot_params() && content.contains("_ctx.") {
ctx.push(&strip_ctx_for_slot_params(ctx, &content));
} else {
ctx.push(&content);
}
}
}