All files / style9/src/helpers eval-node-path.js

83.33% Statements 25/30
66.67% Branches 12/18
100% Functions 3/3
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 398x 8x     1x 1x 1x 1x         4x 1x 1x 1x 1x 1x   1x     1x 1x 1x       88x 88x 7x 4x 4x   3x     8x  
const { getTypeBinding } = require('babel-type-scopes');
const translateEnumValues = require('./translate-enum-values');
 
function getTypeDec(path) {
  const name = path.node.object.name;
  const maybeTS = !path.scope.hasBinding(name);
  Eif (maybeTS) {
    return getTypeBinding(path, name);
  }
}
 
function evalDeopt(deopt) {
  if (!deopt.isMemberExpression()) return;
  Iif (!deopt.get('object').isIdentifier()) return;
  Iif (!deopt.get('property').isIdentifier()) return;
  const dec = getTypeDec(deopt);
  Iif (!dec) return;
  Iif (dec.kind !== 'declaration') return;
  // TODO mutation
  const enumValue = Object.fromEntries(
    translateEnumValues(dec.path.parentPath)
  );
  const key = deopt.node.property.name;
  Iif (!(key in enumValue)) return;
  deopt.replaceWith(enumValue[key]);
}
 
function evaluateNodePath(path, prevDeopt) {
  const { value, confident, deopt } = path.evaluate();
  if (confident) return value;
  if (deopt !== prevDeopt) {
    evalDeopt(deopt);
    return evaluateNodePath(path, deopt);
  }
  throw deopt.buildCodeFrameError('Could not evaluate value');
}
 
module.exports = evaluateNodePath;