All files OpenUI5DefineDependencyParserPlugin.js

58.82% Statements 30/51
38.24% Branches 13/34
100% Functions 7/7
60% Lines 30/50
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    1x 1x       1x       1x       1x   1x     1x                           1x 1x 1x 1x                             1x 1x 1x 1x     1x 1x 1x           1x         1x 1x 1x     1x 1x 1x   1x     1x 1x     1x       1x  
'use strict';
 
const OpenUI5DefineDependency = require('./OpenUI5DefineDependency');
const OpenUI5RequireItemDependency = require('./OpenUI5RequireItemDependency');
 
class OpenUI5DefineDependencyParserPlugin {
  constructor(options) {
    this.options = options;
  }
 
  newDefineDependency(range, arrayRange, functionRange) {
    return new OpenUI5DefineDependency(range, arrayRange, functionRange);
  }
 
  apply(parser) {
    const options = this.options;
 
    parser.plugin('call sap.ui.define', (expr) => {
      let array, fn;
 
      Iif (expr.arguments.length > 2) {
        if (expr.arguments[1].type === 'ArrayExpression' && expr.arguments[2].type === 'FunctionExpression') {
          array = expr.arguments[1];
          fn = expr.arguments[2];
          // TODO named module
        } else if (expr.arguments[0].type === 'ArrayExpression' && expr.arguments[1].type === 'FunctionExpression') {
          array = expr.arguments[0];
          fn = expr.arguments[1];
        } else {
          console.error('not implemented!!!!');
          console.log(expr.arguments[0].type);
          console.log(expr.arguments[1].type);
          console.log(expr.arguments[2].type);
        }
      } else Eif (expr.arguments.length > 1) {
        Eif (expr.arguments[0].type === 'ArrayExpression' && expr.arguments[1].type === 'FunctionExpression') {
          array = expr.arguments[0];
          fn = expr.arguments[1];
        } else if (expr.arguments[0].type === 'FunctionExpression') {
          fn = expr.arguments[0];
        } else {
          console.error('not implemented!!!!');
          console.log(expr.arguments[0].type);
          console.log(expr.arguments[1].type);
        }
      } else if (expr.arguments[0].type === 'FunctionExpression') {
        fn = expr.arguments[0];
      } else {
        console.error('not implemented!!!!');
        console.log(expr.arguments[0].type);
      }
 
      Eif (array) {
        const param = parser.evaluateExpression(array);
        const result = parser.applyPluginsBailResult('call define:openui5:array', expr, param);
        Iif (!result) return;
      }
 
      Eif (fn && fn.type === 'FunctionExpression') {
        Eif (fn.body.type === 'BlockStatement') {
          parser.walkStatement(fn.body);
        } else {
          parser.walkExpression(fn.body);
        }
      }
 
      const dep = this.newDefineDependency(
        expr.range,
        array ? array.range : null,
        fn ? fn.range : null,
      );
      dep.loc = expr.loc;
      parser.state.current.addDependency(dep);
      return true;
    });
 
    parser.plugin('call define:openui5:array', (expr, param) => {
      param.items.forEach((param) => {
        parser.applyPluginsBailResult('call define:openui5:item', expr, param);
      });
      return true;
    });
 
    parser.plugin('call define:openui5:item', (expr, param) => {
      const dep = new OpenUI5RequireItemDependency(param.string, param.range);
      // dep.loc = expr.loc;
      // parser.state.current.addDependency(dep);
      return true;
    });
  }
}
module.exports = OpenUI5DefineDependencyParserPlugin;