fel_core/
interpolation.rs1#![allow(missing_docs)]
3#![allow(clippy::missing_docs_in_private_items)]
4
5use crate::ast::{Expr, UnaryOp};
6
7const DATA_READING_FUNCTIONS: [&str; 4] = ["prev", "next", "parent", "instance"];
11
12pub fn expr_references_instance_data(expr: &Expr) -> bool {
20 match expr {
21 Expr::FieldRef { .. } | Expr::ContextRef { .. } => true,
22 Expr::FunctionCall { name, args } => {
23 DATA_READING_FUNCTIONS.contains(&name.as_str())
24 || args.iter().any(expr_references_instance_data)
25 }
26 Expr::Null
27 | Expr::Boolean(_)
28 | Expr::Number(_)
29 | Expr::String(_)
30 | Expr::DateLiteral(_)
31 | Expr::DateTimeLiteral(_)
32 | Expr::VarRef { .. } => false,
33 Expr::Array(elems) => elems.iter().any(expr_references_instance_data),
34 Expr::Object(entries) => entries.iter().any(|(_, v)| expr_references_instance_data(v)),
35 Expr::UnaryOp { operand, .. } => expr_references_instance_data(operand),
36 Expr::PostfixAccess { expr, .. } => expr_references_instance_data(expr),
37 Expr::BinaryOp { left, right, .. }
38 | Expr::Membership {
39 value: left,
40 container: right,
41 ..
42 }
43 | Expr::NullCoalesce { left, right } => {
44 expr_references_instance_data(left) || expr_references_instance_data(right)
45 }
46 Expr::Ternary {
47 condition,
48 then_branch,
49 else_branch,
50 }
51 | Expr::IfThenElse {
52 condition,
53 then_branch,
54 else_branch,
55 } => {
56 expr_references_instance_data(condition)
57 || expr_references_instance_data(then_branch)
58 || expr_references_instance_data(else_branch)
59 }
60 Expr::LetBinding { value, body, .. } => {
61 expr_references_instance_data(value) || expr_references_instance_data(body)
62 }
63 }
64}
65
66pub fn expr_is_interpolation_static_literal(expr: &Expr) -> bool {
68 match expr {
69 Expr::Null
70 | Expr::Boolean(_)
71 | Expr::Number(_)
72 | Expr::String(_)
73 | Expr::DateLiteral(_)
74 | Expr::DateTimeLiteral(_) => true,
75 Expr::Array(elems) => elems.iter().all(expr_is_interpolation_static_literal),
76 Expr::Object(entries) => entries
77 .iter()
78 .all(|(_, v)| expr_is_interpolation_static_literal(v)),
79 Expr::UnaryOp {
80 op: UnaryOp::Not | UnaryOp::Neg,
81 operand,
82 ..
83 } => expr_is_interpolation_static_literal(operand),
84 Expr::VarRef { .. }
85 | Expr::ContextRef { .. }
86 | Expr::FunctionCall { .. }
87 | Expr::PostfixAccess { .. }
88 | Expr::BinaryOp { .. }
89 | Expr::Ternary { .. }
90 | Expr::IfThenElse { .. }
91 | Expr::Membership { .. }
92 | Expr::NullCoalesce { .. }
93 | Expr::LetBinding { .. }
94 | Expr::FieldRef { .. } => false,
95 }
96}