All files / dist/helpers decorators.js

100% Statements 25/25
100% Branches 14/14
100% Functions 4/4
100% Lines 25/25
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    1x     1x   1x   1x   1x     29x 29x 25x   4x 4x 4x 20x 20x 3x     4x 4x 4x 1x     4x       4x 4x 4x 4x 4x         20x  
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDecoratorClassInfo = getDecoratorClassInfo;
 
require('source-map-support/register');
 
const classInfoValueTags = ['alias', 'name', 'namespace', 'metadata', 'renderer'];
 
const classinfoBoolTags = ['nonUI5'];
 
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;
}