All files / src/generator ternary-expression.js

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  22x 22x 22x     22x           2x 2x         2x        
// @flow
import mapSyntax from './map-syntax';
import mergeBlock from './merge-block';
import opcode from '../emitter/opcode';
import type { GeneratorType } from './flow/types';
 
const generateTernary: GeneratorType = (node, parent) => {
  // TernaryExpression has a param layout of 3(TWO) total parameters.
  // [truthy, falsy, condition]
  // The whole thing is encoded as an Select opcode
  //
  // NOTE: The use of select means both "branches" are evaluated, even if not selected
  const block = node.params.map(mapSyntax(parent)).reduce(mergeBlock, []);
  block.push({
    kind: opcode.Select,
    params: [],
  });
 
  return block;
};
 
export default generateTernary;