All files / dist/helpers classes.js

89.84% Statements 115/128
83.67% Branches 82/98
100% Functions 14/14
97.27% Lines 107/110
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233    1x     1x 1x 1x 1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   1x   2x   2x                     20x       20x   20x 20x 20x   20x 20x   20x   20x 2x     20x 1x     20x 36x 36x 36x 19x 19x 3x   16x 4x   16x 16x 16x   17x 17x   9x 8x 3x   5x     5x 4x   1x               20x 4x 1x       1x 1x   4x 4x             20x             20x       20x 18x 11x 11x       23x   23x 23x   23x   23x 23x 4x     19x 19x 11x     8x       23x 23x 19x   4x                   20x 20x 20x         16x       39x       23x 23x 23x 23x 23x 23x 23x 23x 1x   23x                 39x             160x   160x   160x   96x 96x 96x   6x 6x 6x   6x 6x 6x   6x  
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.convertClassToUI5Extend = convertClassToUI5Extend;
exports.getClassInfo = getClassInfo;
exports.hasExportFlag = hasExportFlag;
exports.isSAPUIDefineCallExpression = isSAPUIDefineCallExpression;
 
require('source-map-support/register');
 
var _babelTypes = require('babel-types');
 
var t = _interopRequireWildcard(_babelTypes);
 
var _templates = require('./templates');
 
var th = _interopRequireWildcard(_templates);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _objectAssignDefined = require('object-assign-defined');
 
var _objectAssignDefined2 = _interopRequireDefault(_objectAssignDefined);
 
var _jsdoc = require('./jsdoc');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
 
/**
 * Converts an ES6 class to a UI5 extend.
 * Any static methods or properties will be moved outside the class body.
 * The path will be updated with the new AST.
 */
 
// import template from 'babel-template'
function convertClassToUI5Extend(node, classInfo) {
  // eslint-disable-line no-unused-vars
  Iif (!(node.type === 'ClassDeclaration' || node.type === 'ClassExpression')) {
    return node;
  }
 
  const staticMembers = [];
 
  const classNameIdentifier = node.id;
  const superClass = node.superClass; // Identifier
  const superClassName = node.superClass.name;
 
  const extendProps = [];
  const boundProps = [];
 
  let constructor = null;
 
  if (classInfo.metadata) {
    extendProps.push(t.objectProperty(t.identifier('metadata'), t.identifier(classInfo.metadata)));
  }
 
  if (classInfo.renderer) {
    extendProps.push(t.objectProperty(t.identifier('renderer'), t.identifier(classInfo.renderer)));
  }
 
  for (const member of node.body.body) {
    const memberName = member.key.name;
    const memberExpression = member.static && t.memberExpression(classNameIdentifier, member.key);
    if (member.type === 'ClassMethod') {
      const func = t.functionExpression(null, member.params, member.body, member.generator, member.async);
      if (member.static) {
        staticMembers.push(t.expressionStatement(t.assignmentExpression('=', memberExpression, func)));
      } else {
        if (memberName === 'constructor') {
          constructor = func;
        }
        func.generator = member.generator;
        func.async = member.async;
        extendProps.push(t.objectProperty(member.key, func));
      }
    } else Eif (member.type === 'ClassProperty') {
      if (memberName === 'metadata' || memberName === 'renderer') {
        // Special handling for TypeScript limitation where metadata and renderer must be properties.
        extendProps.unshift(t.objectProperty(member.key, member.value));
      } else if (member.static) {
        staticMembers.push(t.expressionStatement(t.assignmentExpression('=', memberExpression, member.value)));
      } else {
        Iif (memberName === 'constructor') {
          constructor = member.value;
        }
        if (member.value.type === 'ArrowFunctionExpression') {
          boundProps.push(member);
        } else {
          extendProps.push(t.objectProperty(member.key, member.value));
        }
      }
    }
  }
 
  // Arrow function properties need to get moved to the constructor so that
  // they're bound properly to the class instance, to align with the spec.
  if (boundProps.length) {
    if (!constructor) {
      const fn = th.buildDefaultConstructorFunction({
        SUPER: t.identifier(superClassName)
      });
      // Need to convert FunctionDeclaration to FunctionExpression
      constructor = t.functionExpression(fn.id, fn.params, fn.body, fn.generator, fn.async);
      extendProps.unshift(t.objectProperty(t.identifier('constructor'), constructor));
    }
    constructor.body.body.push(...boundProps.map(prop => {
      return th.buildThisAssisment({
        NAME: prop.key,
        VALUE: prop.value
      });
    }));
  }
 
  const extendAssign = th.buildExtendAssign({
    NAME: classNameIdentifier,
    SUPERNAME: superClass,
    FQN: t.stringLiteral(getFullyQualifiedName(classInfo)),
    OBJECT: t.objectExpression(extendProps)
  });
 
  return [extendAssign, ...staticMembers];
}
 
function getFullyQualifiedName(classInfo) {
  if (classInfo.alias) return classInfo.alias;
  if (classInfo.name) return classInfo.name;
  const separator = classInfo.namespace ? '.' : '';
  return `${ classInfo.namespace }${ separator }${ classInfo.localName }`;
}
 
function getClassInfo(path, node, parent) {
  const fileBaseClassInfo = getFileBaseClassInfo.call(this, path);
 
  const localName = node.id.name;
  const superClassName = node.superClass && node.superClass.name;
 
  const defaults = (0, _objectAssignDefined2.default)({}, { localName, superClassName, namespace: '' }, fileBaseClassInfo);
 
  const decoratorInfo = getDecoratorClassInfo(node);
  if (decoratorInfo) {
    return (0, _objectAssignDefined2.default)(defaults, decoratorInfo);
  }
 
  const jsDocInfo = (0, _jsdoc.getJsDocClassInfo)(node, parent);
  if (jsDocInfo) {
    return (0, _objectAssignDefined2.default)(defaults, jsDocInfo);
  }
 
  return defaults;
}
 
function getDecoratorClassInfo(node) {
  const decorators = node.decorators;
  if (!decorators || !decorators.length) {
    return null;
  }
  return {
    alias: getDecoratorValue(findDecoratorByName(decorators, 'alias')),
    name: getDecoratorValue(findDecoratorByName(decorators, 'name')),
    namespace: getDecoratorValue(findDecoratorByName(decorators, 'namespace')),
    nonUI5: !!findDecoratorByName(decorators, 'nonui5'),
    ui5: getDecoratorValue(findDecoratorByName(decorators, 'ui5'))
  };
}
 
function findDecoratorByName(decorators, name) {
  return decorators && decorators.find(decorator => {
    const expression = decorator.expression;
    return equalsIgnoreCase(name, expression.name) || equalsIgnoreCase(name, expression.callee && expression.callee.name);
  });
}
 
function getDecoratorValue(decorator) {
  return (decorator && decorator.expression.arguments[0] || {}).value;
}
 
function hasExportFlag(node) {
  return (0, _jsdoc.hasJsdocExportFlag)(node);
}
 
function getFileBaseClassInfo(path) {
  const opts = path.hub.file.opts;
  const filename = _path2.default.resolve(opts.filename);
  const sourceRoot = opts.sourceRoot || process.cwd();
  Eif (filename.startsWith(sourceRoot)) {
    const filenameRelative = _path2.default.relative(sourceRoot, filename);
    const { dir } = _path2.default.parse(filenameRelative);
    const namespaceParts = dir.split(_path2.default.sep);
    if (this.namespacePrefix) {
      namespaceParts.unshift(this.namespacePrefix);
    }
    return {
      namespace: namespaceParts.join('.')
    };
  } else {
    return null;
  }
}
 
function equalsIgnoreCase(str1, str2) {
  Iif (!str1 && !str2) return true;else if (!str1 || !str2) return false;else return str1.toLowerCase() === str2.toLowerCase();
}
 
/**
 * Checks if the CallExpression is sap.ui.define(...) call
 */
function isSAPUIDefineCallExpression(node) {
  let name = null;
 
  const callee = node.callee;
 
  if (callee.type !== 'MemberExpression') return false;
 
  node = callee;
  name = node.name || node.property && node.property.name; // property won't be there for an anonymous function
  if (name !== 'define') return false;
 
  node = node.object;
  name = node.name || node.property.name;
  Iif (name !== 'ui') return false;
 
  node = node.object;
  name = node.name || node.property.name;
  Iif (name !== 'sap') return false;
 
  return true;
}