private String format()

in java/com/google/javascript/jscomp/JsCheckerErrorFormatter.java [66:176]


  private String format(JSError error, boolean warning) {
    SourceExcerptProvider source = getSource();
    String sourceName = error.getSourceName();
    int lineNumber = error.getLineNumber();
    int charno = error.getCharno();

    // Format the non-reverse-mapped position.
    StringBuilder b = new StringBuilder();
    StringBuilder boldLine = new StringBuilder();
    String nonMappedPosition = formatPosition(sourceName, lineNumber);

    // Check if we can reverse-map the source.
    OriginalMapping mapping =
        source == null
            ? null
            : source.getSourceMapping(error.getSourceName(), error.getLineNumber(), error.getCharno());
    if (mapping == null) {
      boldLine.append(nonMappedPosition);
    } else {
      sourceName = mapping.getOriginalFile();
      lineNumber = mapping.getLineNumber();
      charno = mapping.getColumnPosition();

      b.append(nonMappedPosition);
      b.append("\nOriginally at:\n");
      boldLine.append(formatPosition(sourceName, lineNumber));
    }

    // extract source excerpt
    String sourceExcerpt = source == null ? null :
        EXCERPT.get(
            source, sourceName, lineNumber, excerptFormatter);

    boldLine.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR));
    boldLine.append(" - ");
    boldLine.append(error.getDescription());

    b.append(maybeEmbolden(boldLine.toString()));

    b.append('\n');
    if (sourceExcerpt != null) {
      b.append(sourceExcerpt);
      b.append('\n');

      // padding equal to the excerpt and arrow at the end
      // charno == sourceExpert.length() means something is missing
      // at the end of the line
      if (0 <= charno && charno <= sourceExcerpt.length()) {
        for (int i = 0; i < charno; i++) {
          char c = sourceExcerpt.charAt(i);
          if (TokenUtil.isWhitespace(c)) {
            b.append(c);
          } else {
            b.append(' ');
          }
        }
        b.append("^\n");
      }
    }

    // Help the user know how to suppress this warning.
    String module = convertPathToModuleName(nullToEmpty(error.getSourceName()), roots).or("");
    String label = labels.get(module);
    if (label == null) {
      if (colorize) {
        b.append("\033[1;34m");
      }
      b.append("  Codes: ");
      if (colorize) {
        b.append("\033[0m");
      }
      b.append(error.getType().key);
      for (String groupName : getGroupSuppressCodes(error)) {
        if (groupName.startsWith("old")) {
          continue;
        }
        b.append(", ");
        b.append(groupName);
      }
      b.append('\n');
    } else {
      b.append("  ");
      if (colorize) {
        b.append("\033[1;34m");
      }
      b.append("ProTip:");
      if (colorize) {
        b.append("\033[0m");
      }
      b.append(" \"");
      b.append(error.getType().key);
      for (String groupName : getGroupSuppressCodes(error)) {
        if (groupName.startsWith("old")) {
          continue;
        }
        b.append("\" or \"");
        b.append(groupName);
      }
      b.append("\" can be added to the `suppress` attribute of:\n  ");
      b.append(label);
      b.append('\n');
      String jsdocSuppress = Diagnostics.JSDOC_SUPPRESS_CODES.get(error.getType());
      if (jsdocSuppress != null) {
        b.append("  Alternatively /** @suppress {");
        b.append(jsdocSuppress);
        b.append("} */ can be added to the source file.\n");
      }
    }

    return b.toString();
  }