visitAnnotation()

in lib/generator.js [2648:2771]


  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;
    });

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

    if (deprecated) {
      this.emit(`/// <term><b>Deprecated</b></term>\n`, level);
      this.emit(`/// \n`, level);
      deprecatedText.trimEnd().split('\n').forEach((line) => {
        this.emit(`/// ${line}\n`, level);
      });
      hasNextSection = true;
    }
    if (summaryText !== '') {
      if (hasNextSection) {
        this.emit(`/// \n`, level);
      }
      this.emit(`/// <term><b>Summary:</b></term>\n`, level);
      this.emit(`/// <summary>\n`, level);
      const summaryTexts = md2Xml(summaryText);
      summaryTexts.split('\n').forEach((line) => {
        this.emit(`/// ${line}\n`, level);
      });
      this.emit(`/// </summary>\n`, level);
      hasNextSection = true;
    }
    if (descriptionText !== '') {
      if (hasNextSection) {
        this.emit(`/// \n`, level);
      }
      this.emit(`/// <term><b>Description:</b></term>\n`, level);
      this.emit(`/// <description>\n`, level);
      const descriptionTexts = md2Xml(descriptionText);
      descriptionTexts.split('\n').forEach((line) => {
        this.emit(`/// ${line}\n`, level);
      });
      this.emit(`/// </description>\n`, level);
      hasNextSection = true;
    }
    if (params.length > 0) {
      if (hasNextSection) {
        this.emit(`/// \n`, level);
      }
      params.forEach((item) => {
        this.emit(`/// <param name="${item.name}">\n`, level);
        item.text.trimEnd().split('\n').forEach((line) => {
          this.emit(`/// ${line}\n`, level);
        });
        this.emit(`/// </param>\n`, level);
      });
      hasNextSection = true;
    }
    if (returnText) {
      if (hasNextSection) {
        this.emit(`/// \n`, level);
      }
      this.emit(`/// <returns>\n`, level);
      returnText.split('\n').forEach((line) => {
        this.emit(`/// ${line}\n`, level);
      });
      this.emit(`/// </returns>\n`, level);
      hasNextSection = true;
    }
    if (throws.length > 0) {
      if (hasNextSection) {
        this.emit(`/// \n`, level);
      }
      throws.forEach((item, index) => {
        this.emit(`/// <term><b>Exception:</b></term>\n`, level);
        item.trimEnd().split('\n').forEach((line) => {
          this.emit(`/// ${line}\n`, level);
        });
        if (index < throws.length - 1) {
          this.emit(`/// \n`, level);
        }
      });
    }
    if (deprecated) {
      this.emit(`[Obsolete("`, level);
      const lines = deprecatedText.trimEnd().split('\n');
      lines.forEach((line, index) => {
        if (index === lines.length - 1) {
          this.emit(`${line}`);
        } else {
          this.emit(`${line}\\n`);
        }
      });
      this.emit(`")]\n`);
    }
  }