Fix bug where update statement didn't make it into the finished js output.

This commit is contained in:
Robin Heggelund Hansen 2021-07-15 22:22:29 +02:00
parent 4a404440c0
commit 5e60da40ac
3 changed files with 30 additions and 13 deletions

View File

@ -56,6 +56,7 @@ async function run(inputFilePath: string | undefined) {
verbose: true,
assetSizes: true,
runBenchmark: [
{ browser: Browser.Chrome, headless: true },
{ browser: Browser.Safari },
{ browser: Browser.Firefox },
],

View File

@ -1,5 +1,5 @@
import ts from 'typescript';
import { ast, astNodes } from './utils/create';
import { astNodes } from './utils/create';
export const recordUpdate = (): ts.TransformerFactory<ts.SourceFile> =>
(context) => (sourceFile) => {
@ -52,18 +52,34 @@ function replaceUpdateStatements(ctx: ts.TransformationContext) {
}
const objName = visitedNode.arguments[0].text;
const props = visitedNode.arguments[1].properties;
const propSetters = props.
map((it) => `obj.${it.name.text} = ${it.initializer.text};`).
join(' ');
const propSetters = visitedNode.arguments[1].properties.
map((it) => ts.createExpressionStatement(
ts.createBinary(
ts.createPropertyAccess(
ts.createIdentifier('r'),
ts.createIdentifier(it.name.text)
),
ts.createToken(ts.SyntaxKind.EqualsToken),
it.initializer
)
));
const replacementNode = ast(`
${objName}.$clone(function(obj) {
${propSetters}
});
`);
return replacementNode;
return ts.createCall(
ts.createPropertyAccess(
ts.createIdentifier(objName),
ts.createIdentifier('$clone')
),
undefined,
[ ts.createFunctionExpression(
undefined,
undefined,
undefined,
undefined,
[ ts.createIdentifier('r') ],
undefined,
ts.createBlock(propSetters)
) ]
);
}
return visitorHelp;

View File

@ -106,6 +106,6 @@ export const benchmarkDefaults: Transforms = {
replaceListFunctions: false,
replaceStringFunctions: false,
recordUpdates: true,
v8Analysis: true,
v8Analysis: false,
replacements: null
};