Skip to content

Commit

Permalink
fix: perf improvement based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
jodarove committed Aug 20, 2019
1 parent 3327b33 commit 4c46981
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ describe('Transform property', () => {
key2: ["fixed", "array"]
},
config: function(host) {
let v1 = host.prop1;
v1 = v1 != null ? v1 : undefined;
return {
key2: ["fixed", "array"],
key1: host.prop1 != null ? host.prop1 : undefined
key1: v1
};
}
}
Expand Down Expand Up @@ -94,15 +96,17 @@ describe('Transform property', () => {
key2: ["fixed", "array"]
},
config: function(host) {
let v1 = host.prop1;
v1 =
v1 != null &&
(v1 = v1.prop2) != null &&
(v1 = v1.prop3) != null &&
(v1 = v1.prop4) != null
? v1
: undefined;
return {
key2: ["fixed", "array"],
key1:
host.prop1 != null &&
host.prop1.prop2 != null &&
host.prop1.prop2.prop3 != null &&
host.prop1.prop2.prop3.prop4 != null
? host.prop1.prop2.prop3.prop4
: undefined
key1: v1
};
}
}
Expand Down Expand Up @@ -154,11 +158,15 @@ describe('Transform property', () => {
key4: ["fixed", "array"]
},
config: function(host) {
let v1 = host.prop;
v1 = v1 != null ? v1 : undefined;
let v2 = host.prop;
v2 = v2 != null ? v2 : undefined;
return {
key3: "fixed",
key4: ["fixed", "array"],
key1: host.prop != null ? host.prop : undefined,
key2: host.prop != null ? host.prop : undefined
key1: v1,
key2: v2
};
}
}
Expand Down Expand Up @@ -565,9 +573,11 @@ describe('Transform property', () => {
key2: ["fixed"]
},
config: function(host) {
let v1 = host.prop1;
v1 = v1 != null ? v1 : undefined;
return {
key2: ["fixed"],
key1: host.prop1 != null ? host.prop1 : undefined
key1: v1
};
}
},
Expand All @@ -580,9 +590,11 @@ describe('Transform property', () => {
key2: ["array"]
},
config: function(host) {
let v1 = host.prop1;
v1 = v1 != null ? v1 : undefined;
return {
key2: ["array"],
key1: host.prop1 != null ? host.prop1 : undefined
key1: v1
};
}
}
Expand Down Expand Up @@ -633,9 +645,11 @@ describe('Transform method', () => {
},
method: 1,
config: function(host) {
let v1 = host.prop1;
v1 = v1 != null ? v1 : undefined;
return {
key2: ["fixed"],
key1: host.prop1 != null ? host.prop1 : undefined
key1: v1
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,46 @@ function getWiredParams(t, wireConfig) {
}

function getGeneratedConfig(t, wiredValue) {
let counter = 0;
const configBlockBody = [];
const configProps = [];
const generateHostMemberExpression = (path, n) => {
if (n === 0) {
return t.memberExpression(t.identifier('host'), t.identifier(path[0]));
} else {
return t.memberExpression(
generateHostMemberExpression(path, n - 1),
t.identifier(path[n])
);
}
};
const generateParameterConfigValue = (memberExprPaths) => {
const varName = "v"+(++counter);
const varDeclaration = t.variableDeclaration(
'let',
[
t.variableDeclarator(
t.identifier(varName),
t.memberExpression(t.identifier('host'), t.identifier(memberExprPaths[0]))
)
]
);

const generateDynamicConfigVal = propValue => {
let current = propValue.object;
let result = t.binaryExpression('!=', t.cloneDeep(propValue), t.nullLiteral());
let conditionTest = t.binaryExpression('!=', t.identifier(varName), t.nullLiteral());
for (let i = 1, n = memberExprPaths.length; i < n; i++) {
const nextPropValue = t.assignmentExpression(
'=',
t.identifier(varName),
t.memberExpression(t.identifier(varName), t.identifier(memberExprPaths[i]))
);

while (!t.isIdentifier(current)) {
result = t.logicalExpression(
conditionTest = t.logicalExpression(
'&&',
t.binaryExpression('!=', t.cloneDeep(current), t.nullLiteral()),
result
);
current = current.object;
conditionTest,
t.binaryExpression('!=', nextPropValue, t.nullLiteral())
)
}

return t.conditionalExpression(
result,
propValue,
t.identifier('undefined')
const assigmentExpression = t.assignmentExpression(
'=',
t.identifier(varName),
t.conditionalExpression(conditionTest, t.identifier(varName), t.identifier('undefined'))
);

return {
varDeclaration,
assigmentExpression,
};
};

if (wiredValue.static) {
Expand All @@ -77,22 +87,26 @@ function getGeneratedConfig(t, wiredValue) {
if (wiredValue.params) {
wiredValue.params.forEach(param => {
const memberExprPaths = param.value.value.split('.');
const paramConfigValue = generateParameterConfigValue(memberExprPaths);

configProps.push(
t.objectProperty(
param.key,
generateDynamicConfigVal(
generateHostMemberExpression(memberExprPaths, memberExprPaths.length - 1)
)
t.cloneDeep(paramConfigValue.varDeclaration.declarations[0].id)
)
);

configBlockBody.push(paramConfigValue.varDeclaration);
configBlockBody.push(t.expressionStatement(paramConfigValue.assigmentExpression));
});
}

configBlockBody.push(t.returnStatement(t.objectExpression(configProps)));

const fnExpression = t.functionExpression(
null,
[t.identifier('host')],
t.blockStatement([t.returnStatement(t.objectExpression(configProps))])
t.blockStatement(configBlockBody)
);

return t.objectProperty(t.identifier('config'), fnExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ describe('@wire', () => {
params: {
c: 'foo',
},
config: function(host){return{b:true,c:host.foo!=null?host.foo:undefined};},
config: function(host){
let v1=host.foo;
v1=v1!=null?v1:undefined;
return{b:true,c:v1};
},
},
});
});
Expand Down Expand Up @@ -266,8 +270,10 @@ describe('@wire', () => {
c: 'foo',
},
method: 1,
config: function(host) {
return { b: true, c: host.foo != null ? host.foo : undefined };
config: function(host){
let v1=host.foo;
v1=v1!=null?v1:undefined;
return {b:true,c:v1};
},
},
});
Expand Down

0 comments on commit 4c46981

Please sign in to comment.