From 5e59db8170a8d005563dcc5a5bb3a616aeed31c8 Mon Sep 17 00:00:00 2001 From: Jonas Holst Damtoft Date: Mon, 18 Feb 2019 15:02:07 +0100 Subject: [PATCH] naive fix: only normalize if expression contains bindName --- src/transform.ts | 72 +++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/src/transform.ts b/src/transform.ts index 8a1dbfb..0d1d410 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -170,41 +170,43 @@ function normalizeExpression( bindName: string, exp: ts.Expression ): { pre: ts.Statement[]; exp: ts.Expression } { - if (ts.isCallExpression(exp)) { - const args = exp.arguments.map(a => normalizeExpression(bindName, a)); - const pre = args.flatMap(a => a.pre); - const updatedExp = ts.updateCall( - exp, - exp.expression, - undefined, - args.map(a => a.exp) - ); - const identifier = ts.createUniqueName("bind"); - const dec = ts.createVariableStatement( - undefined, - ts.createVariableDeclarationList([ - ts.createVariableDeclaration(identifier, undefined, updatedExp) - ]) - ); - return { - pre: pre.concat(dec), - exp: identifier - }; - } else if (ts.isBinaryExpression(exp)) { - const left = normalizeExpression(bindName, exp.left); - const right = normalizeExpression(bindName, exp.right); - const updatedExp = ts.updateBinary(exp, left.exp, right.exp); - return { - pre: left.pre.concat(right.pre), - exp: updatedExp - }; - } else if (ts.isParenthesizedExpression(exp)) { - const norm = normalizeExpression(bindName, exp.expression); - const updatedExp = ts.updateParen(exp, norm.exp); - return { - pre: norm.pre, - exp: updatedExp - }; + if (exp.getText().includes(bindName)) { + if (ts.isCallExpression(exp)) { + const args = exp.arguments.map(a => normalizeExpression(bindName, a)); + const pre = args.flatMap(a => a.pre); + const updatedExp = ts.updateCall( + exp, + exp.expression, + undefined, + args.map(a => a.exp) + ); + const identifier = ts.createUniqueName("bind"); + const dec = ts.createVariableStatement( + undefined, + ts.createVariableDeclarationList([ + ts.createVariableDeclaration(identifier, undefined, updatedExp) + ]) + ); + return { + pre: pre.concat(dec), + exp: identifier + }; + } else if (ts.isBinaryExpression(exp)) { + const left = normalizeExpression(bindName, exp.left); + const right = normalizeExpression(bindName, exp.right); + const updatedExp = ts.updateBinary(exp, left.exp, right.exp); + return { + pre: left.pre.concat(right.pre), + exp: updatedExp + }; + } else if (ts.isParenthesizedExpression(exp)) { + const norm = normalizeExpression(bindName, exp.expression); + const updatedExp = ts.updateParen(exp, norm.exp); + return { + pre: norm.pre, + exp: updatedExp + }; + } } return { pre: [], exp }; }