From 3f575148b622cd8ca040916abf6c55f47b3580ea Mon Sep 17 00:00:00 2001 From: Bruno Boi Date: Thu, 27 Jan 2022 10:42:26 +0100 Subject: [PATCH] [FIX] compiler: add _ prefix to local variables while compiling an expression --- src/compiler/inline_expressions.ts | 2 ++ .../__snapshots__/event_handling.test.ts.snap | 2 +- tests/compiler/__snapshots__/misc.test.ts.snap | 2 +- tests/compiler/inline_expressions.test.ts | 13 +++++++------ .../__snapshots__/error_handling.test.ts.snap | 2 +- .../__snapshots__/event_handling.test.ts.snap | 4 ++-- tests/components/__snapshots__/props.test.ts.snap | 2 +- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/compiler/inline_expressions.ts b/src/compiler/inline_expressions.ts index 835e4754..af67df1c 100644 --- a/src/compiler/inline_expressions.ts +++ b/src/compiler/inline_expressions.ts @@ -330,6 +330,8 @@ export function compileExprToArray(expr: string): Token[] { // This assumes the expression has only one scope (incorrect but "good enough for now") for (const token of tokens) { if (token.type === "SYMBOL" && localVars.has(token.value)) { + token.originalValue = token.value; + token.value = `_${token.value}`; token.isLocal = true; } } diff --git a/tests/compiler/__snapshots__/event_handling.test.ts.snap b/tests/compiler/__snapshots__/event_handling.test.ts.snap index 6428dcbb..f1aba25b 100644 --- a/tests/compiler/__snapshots__/event_handling.test.ts.snap +++ b/tests/compiler/__snapshots__/event_handling.test.ts.snap @@ -353,7 +353,7 @@ exports[`t-on t-on modifiers (native listener) t-on with prevent modifier in t-f let key1 = ctx['project']; const v1 = ctx['onEdit']; const v2 = ctx['project']; - let hdlr1 = [\\"prevent\\", ev=>v1(v2.id,ev), ctx]; + let hdlr1 = [\\"prevent\\", _ev=>v1(v2.id,_ev), ctx]; let txt1 = ctx['project'].name; c_block2[i1] = withKey(block3([hdlr1, txt1]), key1); } diff --git a/tests/compiler/__snapshots__/misc.test.ts.snap b/tests/compiler/__snapshots__/misc.test.ts.snap index 741a11b1..6e4ff912 100644 --- a/tests/compiler/__snapshots__/misc.test.ts.snap +++ b/tests/compiler/__snapshots__/misc.test.ts.snap @@ -30,7 +30,7 @@ exports[`misc complex template 1`] = ` b3 = block3(); } ctx = Object.create(ctx); - const [k_block4, v_block4, l_block4, c_block4] = prepareList(ctx['batch'].slot_ids.filter(slot=>slot.build_id.id&&!slot.trigger_id.manual&&(ctx['options'].trigger_display[slot.trigger_id.id]))); + const [k_block4, v_block4, l_block4, c_block4] = prepareList(ctx['batch'].slot_ids.filter(_slot=>_slot.build_id.id&&!_slot.trigger_id.manual&&(ctx['options'].trigger_display[_slot.trigger_id.id]))); for (let i1 = 0; i1 < l_block4; i1++) { ctx[\`slot\`] = v_block4[i1]; let key1 = ctx['slot'].id; diff --git a/tests/compiler/inline_expressions.test.ts b/tests/compiler/inline_expressions.test.ts index 97e4cd52..031f7e25 100644 --- a/tests/compiler/inline_expressions.test.ts +++ b/tests/compiler/inline_expressions.test.ts @@ -163,17 +163,18 @@ describe("expression evaluation", () => { }); test("arrow functions", () => { - expect(compileExpr("list.map(e => e.val)")).toBe("ctx['list'].map(e=>e.val)"); - expect(compileExpr("list.map(e => a + e)")).toBe("ctx['list'].map(e=>ctx['a']+e)"); - expect(compileExpr("list.map((e) => e)")).toBe("ctx['list'].map((e)=>e)"); + expect(compileExpr("list.map(e => e.val)")).toBe("ctx['list'].map(_e=>_e.val)"); + expect(compileExpr("list.map(e => a + e)")).toBe("ctx['list'].map(_e=>ctx['a']+_e)"); + expect(compileExpr("list.map((e) => e)")).toBe("ctx['list'].map((_e)=>_e)"); expect(compileExpr("list.map((elem, index) => elem + index)")).toBe( - "ctx['list'].map((elem,index)=>elem+index)" + "ctx['list'].map((_elem,_index)=>_elem+_index)" ); - expect(compileExpr("(ev => ev)(e)")).toBe("(ev=>ev)(ctx['e'])"); + expect(compileExpr("(ev => ev)(e)")).toBe("(_ev=>_ev)(ctx['e'])"); + expect(compileExpr("(v1) => myFunc(v1)")).toBe("(_v1)=>ctx['myFunc'](_v1)"); }); test.skip("arrow functions: not yet supported", () => { // e is added to localvars in inline_expression but not removed after the arrow func body - expect(compileExpr("(e => e)(e)")).toBe("(e=>e)(ctx['e'])"); + expect(compileExpr("(e => e)(e)")).toBe("(_e=>_e)(ctx['e'])"); }); test("assignation", () => { diff --git a/tests/components/__snapshots__/error_handling.test.ts.snap b/tests/components/__snapshots__/error_handling.test.ts.snap index 0035f2a8..75f4f7f1 100644 --- a/tests/components/__snapshots__/error_handling.test.ts.snap +++ b/tests/components/__snapshots__/error_handling.test.ts.snap @@ -812,7 +812,7 @@ exports[`can catch errors catching in child makes parent render 1`] = ` let key1 = ctx['elem'][0]; const v1 = ctx['elem']; const ctx1 = capture(ctx); - c_block1[i1] = withKey(component(\`Catch\`, {onError: (error)=>this.onError(v1[0],error),slots: {'default': {__render: slot1, __ctx: ctx1}}}, key + \`__2__\${key1}\`, node, ctx), key1); + c_block1[i1] = withKey(component(\`Catch\`, {onError: (_error)=>this.onError(v1[0],_error),slots: {'default': {__render: slot1, __ctx: ctx1}}}, key + \`__2__\${key1}\`, node, ctx), key1); } return list(c_block1); } diff --git a/tests/components/__snapshots__/event_handling.test.ts.snap b/tests/components/__snapshots__/event_handling.test.ts.snap index d6b4e7be..ad9b407a 100644 --- a/tests/components/__snapshots__/event_handling.test.ts.snap +++ b/tests/components/__snapshots__/event_handling.test.ts.snap @@ -105,7 +105,7 @@ exports[`event handling objects from scope are properly captured by t-on 1`] = ` let key1 = ctx['item']; const v1 = ctx['onClick']; const v2 = ctx['item']; - let hdlr1 = [ev=>v1(v2.val,ev), ctx]; + let hdlr1 = [_ev=>v1(v2.val,_ev), ctx]; c_block2[i1] = withKey(block3([hdlr1]), key1); } let b2 = list(c_block2); @@ -146,7 +146,7 @@ exports[`event handling t-on with handler bound to dynamic argument on a t-forea let key1 = ctx['item']; const v1 = ctx['onClick']; const v2 = ctx['item']; - let hdlr1 = [ev=>v1(v2,ev), ctx]; + let hdlr1 = [_ev=>v1(v2,_ev), ctx]; c_block2[i1] = withKey(block3([hdlr1]), key1); } let b2 = list(c_block2); diff --git a/tests/components/__snapshots__/props.test.ts.snap b/tests/components/__snapshots__/props.test.ts.snap index bf5feaef..fb057ad4 100644 --- a/tests/components/__snapshots__/props.test.ts.snap +++ b/tests/components/__snapshots__/props.test.ts.snap @@ -42,7 +42,7 @@ exports[`basics arrow functions as prop correctly capture their scope 1`] = ` let key1 = ctx['item'].val; const v1 = ctx['onClick']; const v2 = ctx['item']; - c_block1[i1] = withKey(component(\`Child\`, {onClick: ev=>v1(v2.val,ev)}, key + \`__1__\${key1}\`, node, ctx), key1); + c_block1[i1] = withKey(component(\`Child\`, {onClick: _ev=>v1(v2.val,_ev)}, key + \`__1__\${key1}\`, node, ctx), key1); } return list(c_block1); }