Skip to content

Commit

Permalink
fix(template-compiler): prevent throw on template function call on no…
Browse files Browse the repository at this point in the history
…n-function value
  • Loading branch information
LastLeaf committed Sep 11, 2023
1 parent a6f0f1d commit 682379b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
3 changes: 2 additions & 1 deletion glass-easel-template-compiler/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,9 @@ impl TmplExpr {
pas
}
TmplExpr::FuncCall(x, y) => {
write!(value, "P(")?;
x.to_proc_gen_rec_and_end_path(w, scopes, TmplExprLevel::Cond, path_calc, value)?;
write!(value, "(")?;
write!(value, ")(")?;
for (i, y) in y.iter().enumerate() {
if i > 0 {
write!(value, ",")?;
Expand Down
5 changes: 3 additions & 2 deletions glass-easel-template-compiler/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::*;
// M: = R.m
// N: the `Node` to operate (may be undefined)
// O: = R.r
// P: the current path
// P (preserved for runtime)
// Q (extra runtime helpers)
// R: the global script module / the `ProcGenWrapper` object
// S: the `DefineSlot` function
Expand All @@ -40,13 +40,14 @@ use super::*;
// A: filter binding function in change properties bindings
// B: filter binding function in event bindings

const RUNTIME_ITEMS: [(&'static str, &'static str); 3] = [
const RUNTIME_ITEMS: [(&'static str, &'static str); 4] = [
("X", "function(a){return a==null?Object.create(null):a}"),
("Y", "function(a){return a==null?'':String(a)}"),
(
"Z",
"function(a,b){if(a===true)return true;if(a)return a[b]}",
),
("P", "function(a){return typeof a==='function'?a:()=>{}}"),
];

const WXS_RUNTIME: &'static str = r#"
Expand Down
6 changes: 1 addition & 5 deletions glass-easel-template-compiler/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,6 @@ impl TmplTree {
) -> Result<(), TmplError> {
w.paren(|w| {
w.function(|w| {
w.expr_stmt(|w| {
write!(w, "var P={}", gen_lit_str(&self.path))?;
Ok(())
})?;
w.expr_stmt(|w| {
write!(w, "var H={{}}")?;
Ok(())
Expand Down Expand Up @@ -391,7 +387,7 @@ impl TmplTree {
has_scripts,
)?;
w.expr_stmt(|w| {
write!(w, "return function(P){{return H[P]}}")?;
write!(w, "return function(R){{return H[R]}}")?;
Ok(())
})?;
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion glass-easel/tests/tmpl/expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ describe('binding expression', () => {
test('function call', () => {
const def = glassEasel.registerElement({
template: tmpl(`
<div id="n1" data-a="{{ f(a, 1) }}"></div>
<div id="n1" data-a="{{ f(a, 1) }}" data-b="{{ noop() }}"></div>
`),
data: {
a: 2,
Expand All @@ -452,6 +452,7 @@ describe('binding expression', () => {
const elem = glassEasel.createElement('root', def.general())
const n1 = elem.getShadowRoot()!.getElementById('n1')!
expect(n1.dataset.a).toEqual(3)
expect(n1.dataset.b).toBeUndefined()
elem.setData({ a: 10 })
expect(n1.dataset.a).toEqual(11)
})
Expand Down

0 comments on commit 682379b

Please sign in to comment.