All files / src/helpers decorators.js

100% Statements 22/22
100% Branches 14/14
100% Functions 4/4
100% Lines 22/22
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 39 40 41 42 43 44 45 46 47 48  1x               1x         44x 44x 40x   4x 4x 4x 20x 20x 3x     4x 4x 4x 1x     4x       4x 4x 4x 4x 4x         20x    
 
const classInfoValueTags = [
  "alias",
  "name",
  "namespace",
  "metadata",
  "renderer"
];
 
const classInfoBoolTags = [
  "nonUI5"
];
 
export function getDecoratorClassInfo(node) {
  const decorators = node.decorators;
  if (!decorators  || !decorators.length) {
    return null;
  }
  const decoratorsByName = groupByName(decorators);
  const info = {};
  for (const tagName of classInfoValueTags) {
    const value = getDecoratorValue(decoratorsByName[tagName.toLowerCase()]);
    if (value) {
      info[tagName] = value;
    }
  }
  for (const tagName of classInfoBoolTags) {
    const value = !!(decoratorsByName[tagName.toLowerCase()]);
    if (value) {
      info[tagName] = value;
    }
  }
  return info;
}
 
function groupByName(decorators) {
  return decorators.reduce((accumulator, decorator) => {
    const expression = decorator.expression;
    const name = expression.name || expression.callee && expression.callee.name;
    accumulator[name.toLowerCase()] = decorator;
    return accumulator;
  }, {});
}
 
function getDecoratorValue(decorator) {
  return ((decorator && decorator.expression.arguments[0]) || {}).value;
}