visitType()

in lib/generator.js [767:831]


  visitType(ast, level) {
    if (ast.type === 'array') {
      this.pushImports('array');
      this.emit('List[');
      this.visitType(ast.subType, level);
      this.emit(']');
    } else if (ast.type === 'moduleModel') {
      const [moduleId, ...rest] = ast.path;
      const modelName = _subModelName(rest.map((item) => item.lexeme).join('.'));
      let type = 'model';
      const externEx = this.usedExternException.get(_name(moduleId));
      if (externEx && externEx.has(modelName)) {
        type = 'exception';
      }
      this.emit(this.getModelName(modelName, _name(moduleId), type));
    } else if (ast.type === 'subModel') {
      const [moduleId, ...rest] = ast.path;
      const modelName = _subModelName([moduleId.lexeme, ...rest.map((item) => {
        return item.lexeme;
      })].join('.'));
      this.emit(this.getModelName(modelName));
    } else if (ast.type === 'moduleTypedef') {
      const [moduleId, ...rest] = ast.path;
      const type = rest.map((item) => { return item.lexeme; }).join('.');
      this.emit(this.visitTypedef(type, moduleId));
    } else if (ast.type === 'typedef' || ast.idType === 'typedef') {
      this.emit(this.visitTypedef(ast));
    } else if (ast.type === 'map') {
      // 记录使用了Dict
      this.pushImports('map');
      this.emit('Dict[');
      this.visitType(ast.keyType, level);
      this.emit(', ');
      this.visitType(ast.valueType, level);
      this.emit(']');
    } else if (ast.type === 'model') {
      const predefined = ast.moduleName ? this.usedExternException.get(ast.moduleName) : this.predefined;
      let type = 'model';
      if (predefined[_name(ast)] && predefined[_name(ast)].isException) {
        type = 'exception';
      }
      this.emit(this.getModelName(_upperFirst(_name(ast)), ast.moduleName, type));
    } else if (ast.idType === 'model') {
      const predefined = ast.moduleName ? this.usedExternException.get(ast.moduleName) : this.predefined;
      let type = 'model';
      if (predefined[_name(ast)] && predefined[_name(ast)].isException) {
        type = 'exception';
      }
      this.emit(this.getModelName(_upperFirst(_name(ast)), ast.moduleName, type));
    } else if (ast.type === 'module') {
      this.emit(this.getModelName('', _name(ast), 'module'));
    } else if (ast.idType === 'module') {
      this.emit(this.getModelName('', _name(ast), 'module'));
    } else if (this.isIterator(ast)) {
      this.visitType(ast.valueType);
    } else if (ast.type === 'entry') {
      this.emit('[str, ');
      this.visitType(ast.valueType);
      this.emit(']');
    } else {
      const typeName = _type(_name(ast));
      this.pushImports(typeName);
      this.emit(typeName);
    }
  }