visitAnnotation()

in lib/generator.js [573:713]


  visitAnnotation(annotation, level) {
    if (!annotation || !annotation.value) {
      return;
    }
    let comments = DSL.comment.getFrontComments(this.comments, annotation.index);
    this.visitComments(comments, level);
    var ast = Annotation.parse(annotation.value);
    var description = ast.items.find((item) => {
      return item.type === 'description';
    });
    var summary = ast.items.find((item) => {
      return item.type === 'summary';
    });
    var _return = ast.items.find((item) => {
      return item.type === 'return';
    });
    var deprecated = ast.items.find((item) => {
      return item.type === 'deprecated';
    });
    var params = ast.items.filter((item) => {
      return item.type === 'param';
    }).map((item) => {
      return {
        name: item.name.id,
        text: item.text.text
      };
    });
    var throws = ast.items.filter((item) => {
      return item.type === 'throws';
    }).map((item) => {
      return item.text.text;
    });

    let hasNextSection = false;
    const summaryText = summary ? _escape(summary.text.text).trimEnd() : '';
    const descriptionText = description ? _escape(description.text.text).trimEnd() : '';
    const returnText = _return ? _escape(_return.text.text).trimEnd() : '';

    if (deprecated) {
      const deprecatedText = _escape(deprecated.text.text).trimEnd();
      deprecatedText.split('\n').forEach((line, index, array) => {
        if(index === 0) {
          this.emit(`// Deprecated: ${line}\n`, level);
          if (array.length > 1) {
            this.emit(`//\n`, level);
          }
        } else {
          this.emit(`// ${line}\n`, level);
          if (index < array.length - 1){
            this.emit(`//\n`, level);
          }
        }
      });
      hasNextSection = true;
    }
    if (summaryText !== '') {
      if (hasNextSection) {
        this.emit(`// \n`, level);
      }
      this.emit(`// Summary:\n`, level);
      this.emit(`// \n`, level);
      summaryText.split('\n').forEach((line, index, array) => {
        this.emit(`// ${line}\n`, level);
        if (index < array.length - 1) {
          this.emit(`// \n`, level);
        }
      });
      hasNextSection = true;
    }
    if (descriptionText !== '') {
      if (hasNextSection) {
        this.emit(`// \n`, level);
      }
      this.emit(`// Description:\n`, level);
      this.emit(`// \n`, level);
      descriptionText.split('\n').forEach((line, index, array) => {
        this.emit(`// ${line}\n`, level);
        if (index < array.length - 1) {
          this.emit(`// \n`, level);
        }
      });
      hasNextSection = true;
    }
    if (params.length > 0) {
      if (hasNextSection) {
        this.emit(`// \n`, level);
      }
      params.forEach((item, i) => {
        this.emit(`// @param ${item.name} - `, level);
        const items = item.text.trimEnd().split('\n');
        items.forEach((line, j) => {
          if (j === 0) {
            this.emit(`${line}\n`);
          } else {
            this.emit(`// ${line}\n`, level);
          }
          if (j < items.length - 1 || (j === items.length - 1 && i < params.length -1)) {
            this.emit(`// \n`, level);
          }
        });
      });
      hasNextSection = true;
    }
    if (returnText) {
      if (hasNextSection) {
        this.emit(`// \n`, level);
      }
      this.emit(`// @return `, level);
      const returns = returnText.split('\n');
      returns.forEach((line, index) => {
        if (index === 0) {
          this.emit(`${line}\n`);
        } else {
          this.emit(`// ${line}\n`, level);
        }
        if (index < returns.length - 1) {
          this.emit(`// \n`, level);
        }
      });
      hasNextSection = true;
    }
    if (throws.length > 0) {
      if (hasNextSection) {
        this.emit(`// \n`, level);
      }
      throws.forEach((item, i) => {
        this.emit(`// @throws `, level);
        const items = item.trimEnd().split('\n');
        items.forEach((line, j) => {
          if (j === 0) {
            this.emit(`${line}\n`);
          } else {
            this.emit(`// ${line}\n`, level);
          }
          if (j < items.length - 1 || (j === items.length - 1 && i < params.length -1)) {
            this.emit(`// \n`, level);
          }
        });
      });
    }
  }