src/optimizations/array-reduce.js
function writeReduceForLoop(t, path, reducePath, reduceRight) {
	const reduceCall = reducePath.node;
	const array = defineIdIfNeeded(t, reducePath.get('callee.object'), path);
	const func = extractDynamicFuncIfNeeded(t, reducePath, path);
	const acc = path.scope.generateUidIdentifier('acc');
	const i = path.scope.generateUidIdentifier('i');

	// Initialize the accumulator.
	// let acc = arguments[1] || (reduceRight ? arr[arr.length - 1] : arr[0]);
	const initVal = reduceCall.arguments[1] || arrayMember(t, array, 0, reduceRight);
	path.insertBefore(t.VariableDeclaration('let', [
		t.VariableDeclarator(acc, initVal),
	]));

	// Write the for loop.
	const newReduceCall = t.callExpression(func, [
		acc,
		t.memberExpression(array, i, true),
		i,
		array,
	]);
	const forBody = t.blockStatement([
		t.expressionStatement(
			t.assignmentExpression('=', acc, newReduceCall)
		),
	]);
	let initI;
	if (!reduceCall.arguments[1]) {
		initI = reduceRight ? arrayIndex(t, array, 1, true) : t.numericLiteral(1);
	}
	path.insertBefore(basicArrayForLoop(t, i, array, forBody, reduceRight, initI));

	return acc;
}
function writeReduceForLoop(t, path, reducePath, reduceRight) {
	const reduceCall = reducePath.node;
	const array = defineIdIfNeeded(t, reducePath.get('callee.object'), path);
	const func = extractDynamicFuncIfNeeded(t, reducePath, path);
	const acc = path.scope.generateUidIdentifier('acc');
	const i = path.scope.generateUidIdentifier('i');

	// Initialize the accumulator.
	// let acc = arguments[1] || (reduceRight ? arr[arr.length - 1] : arr[0]);
	const initVal = reduceCall.arguments[1] || arrayMember(t, array, 0, reduceRight);
	path.insertBefore(t.VariableDeclaration('let', [
		t.VariableDeclarator(acc, initVal),
	]));

	// Write the for loop.
	const newReduceCall = t.callExpression(func, [
		acc,
		t.memberExpression(array, i, true),
		i,
		array,
	]);
	const forBody = t.blockStatement([
		t.expressionStatement(
			t.assignmentExpression('=', acc, newReduceCall)
		),
	]);
	let initI;
	if (!reduceCall.arguments[1]) {
		initI = reduceRight ? arrayIndex(t, array, 1, true) : t.numericLiteral(1);
	}
	path.insertBefore(basicArrayForLoop(t, i, array, forBody, reduceRight, initI));

	return acc;
}
function writeReduceForLoop ( t , path , reducePath , reduceRight ) { const reduceCall = reducePath . node ; const array = defineIdIfNeeded ( t , reducePath . get ( 'callee.object' ) , path ) ; const func = extractDynamicFuncIfNeeded ( t , reducePath , path ) ; const acc = path . scope . generateUidIdentifier ( 'acc' ) ; const i = path . scope . generateUidIdentifier ( 'i' ) ; // Initialize the accumulator. // let acc = arguments[1] || (reduceRight ? arr[arr.length - 1] : arr[0]); const initVal = reduceCall . arguments [ 1 ] || arrayMember ( t , array , 0 , reduceRight ) ; path . insertBefore ( t . VariableDeclaration ( 'let' , [ t . VariableDeclarator ( acc , initVal ) , ] ) ) ; // Write the for loop. const newReduceCall = t . callExpression ( func , [ acc , t . memberExpression ( array , i , true ) , i , array , ] ) ; const forBody = t . blockStatement ( [ t . expressionStatement ( t . assignmentExpression ( '=' , acc , newReduceCall ) ) , ] ) ; let initI ; if ( ! reduceCall . arguments [ 1 ] ) { initI = reduceRight ? arrayIndex ( t , array , 1 , true ) : t . numericLiteral ( 1 ) ; } path . insertBefore ( basicArrayForLoop ( t , i , array , forBody , reduceRight , initI ) ) ; return acc ; }
Returns the identifier the result is stored in.
Returns the identifier the result is stored in .