containerPathVisitor.js 5.7 KB
Newer Older
C
init  
Conan 已提交
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
function getObjectKeyName(node) {
  return node.key.name;
}

function isValidProp(node) {
  const validKeys = ['props', 'data', 'computed', 'methods'];
  let keyName = getObjectKeyName(node);
  let idx = validKeys.indexOf(keyName);
  return ~idx ? validKeys[idx] : '';
}

function adoptPropResults(res, container) {
  res.forEach((prop) => {
    // fillKey is one of 'vars', 'methods', 'props' or 'events'
    container[prop.fillKey].push(prop.value);
  });
}

const propFuns = {
  props: function(path) {
    let props = [];
    path.get('value').get('properties')
      .forEach((prop) => {
        if (prop.isObjectProperty()) {
          let propName = getObjectKeyName(prop.node);
          props.push({
            fillKey: 'vars',
            value: propName
          });
          if (prop.get('value').isObjectExpression()) {
            let valueInfo = {};
            prop.get('value').get('properties')
              .forEach((childProp) => {
                let childName = getObjectKeyName(childProp.node);
                if (childName === 'type') {
                  valueInfo[childName] = childProp.get('value').isIdentifier() ? childProp.node.value.name : '';
                }
                if (childName === 'default') {
                  if (childProp.isObjectProperty()) {
                    valueInfo[childName] = ~childProp.node.value.type.indexOf('Literal') ? (childProp.node.value.value || '') : '';
                  } else {
                    valueInfo[childName] = '';
                  }
                }
                if (childName === 'required') {
                  valueInfo[childName] = childProp.get('value').isBooleanLiteral() ? childProp.node.value.value : false;
                }
              });
            props.push({
              fillKey: 'props',
              value: {
                name: propName,
                valueType: valueInfo.type || '',
                required: !!valueInfo.required || false,
                default: valueInfo['default']
              }
            });
          }
          if (prop.get('value').isIdentifier()) {
            props.push({
              fillKey: 'props',
              value: {
                name: propName,
                valueType: prop.node.value.name,
                required: false,
                default: ''
              }
            });
          }
        }
      });
    return props;
  },
  data: function(path) {
    let props = [];
    path.get('value').get('properties')
      .forEach((prop) => {
        if (prop.isObjectProperty()) {
          props.push({
            fillKey: 'vars',
            value: getObjectKeyName(prop.node)
          });
        }
      });
    return props;
  },
  computed: function(path) {
    let props = [];
    path.get('value').get('properties')
      .forEach((prop) => {
        if (prop.isObjectMethod()) {
          props.push({
            fillKey: 'vars',
            value: getObjectKeyName(prop.node)
          });
        }
        if (prop.isObjectProperty() && prop.get('value').isFunctionExpression()) {
          props.push({
            fillKey: 'vars',
            value: getObjectKeyName(prop.node)
          });
        }
        if (prop.isSpreadElement()) {
          prop.get('argument').isCallExpression() && prop.get('argument').get('arguments')
            .forEach((argItem) => {
              argItem.isObjectExpression() && argItem.get('properties').forEach((argProp) => {
                props.push({
                  fillKey: 'vars',
                  value: getObjectKeyName(argProp.node)
                });
              });
            });
        }
      });
    return props;
  },
  methods: function(path) {
    let props = [];
    path.get('value').get('properties')
      .forEach((prop) => {
        if (prop.isObjectMethod()) {
          props.push({
            fillKey: 'methods',
            value: getObjectKeyName(prop.node)
          });
        }
        if (prop.isObjectProperty() && prop.get('value').isFunctionExpression()) {
          props.push({
            fillKey: 'methods',
            value: getObjectKeyName(prop.node)
          });
        }
      });
    return props;
  }
};

module.exports.containerPathVisitor = function(path) {
  let results = {vars: [], methods: [], props: [], events: []};

  path && path.traverse({
    'ClassProperty|ObjectProperty'(path) {
      let propName = isValidProp(path.node);
      if (propName && path.get('value').isObjectExpression()) {
        adoptPropResults(propFuns[propName](path), results);
      }
    },
    MemberExpression(path) {
      if (!path.node.computed && path.get('object').isThisExpression() && path.get('property').isIdentifier()) {
        if (path.node.property.name === '$cmlEmit') {
          let parentNode = path.findParent(path => path.isCallExpression());
          if (parentNode && parentNode.get('arguments')) {
153 154 155 156 157 158 159 160 161
            let nameArg = parentNode.get('arguments')[0];
            if (nameArg.isStringLiteral()) {
              results.events.push({
                name: parentNode.get('arguments')[0].node.value,
                paramsNum: -1,
                params: []
              });
            } else if (nameArg.isIdentifier()) {
              let argBinding = nameArg.scope.getBinding(nameArg.node.name);
162
              let possibleInit = argBinding ? argBinding.path.node.init : null;
163 164 165 166 167 168 169 170 171
              // For now, we only check just one jump along its scope chain.
              if (possibleInit && possibleInit.type === 'StringLiteral') {
                results.events.push({
                  name: possibleInit.value,
                  paramsNum: -1,
                  params: []
                });
              }
            }
C
init  
Conan 已提交
172 173 174 175 176 177 178 179
          }
        }
      }
    }
  });

  return results;
}