Repository: babel/babel
File: packages/babel-generator/src/printer.ts (TypeScript)

Code before:

      return (
        (lastChar === charCodes.exclamationMark &&
          // space is mandatory to avoid outputting <!--
          // http://javascript.spec.whatwg.org/#comment-syntax
          (str === "--" ||
            // Needs spaces to avoid changing a! == 0 to a!== 0
            strFirst === charCodes.equalsTo)) ||
        // Need spaces for operators of the same kind to avoid: `a+++b`
        (strFirst === charCodes.plusSign && lastChar === charCodes.plusSign) ||
        (strFirst === charCodes.dash && lastChar === charCodes.dash) ||
        // Needs spaces to avoid changing '34' to '34.', which would still be a valid number.
        (strFirst === charCodes.dot && this._endsWithInteger)
      );
    });
    this._noLineTerminator = false;
  }

  tokenChar(char: number): void {
    this.tokenContext = 0;

    const str = String.fromCharCode(char);
    this._maybePrintInnerComments(str);

    this._maybeAddAuxComment();
    this._appendChar(char, () => {
      const lastChar = this.getLastChar();
      return (
        // Need spaces for operators of the same kind to avoid: `a+++b`
        (char === charCodes.plusSign && lastChar === charCodes.plusSign) ||
        (char === charCodes.dash && lastChar === charCodes.dash) ||
        // Needs spaces to avoid changing '34' to '34.', which would still be a valid number.
        (char === charCodes.dot && this._endsWithInteger)
      );
    });
    this._noLineTerminator = false;
  }

  /**
   * Add a newline (or many newlines), maintaining formatting.
   * This function checks the number of newlines in the queue and subtracts them.
   * It currently has some limitations.
   * @see {Buffer#getNewlineCount}
   */
  newline(i: number = 1, force?: boolean): void {
    if (i <= 0) return;

    if (!force) {
      if (this.format.retainLines || this.format.compact) return;

      if (this.format.concise) {

Review comment (suggestion):
This change has also the effect that we don't inject the space anymore if it's made unnecessary by the aux comment (e.g. `34/* aux comment*/.foo`), because now we check if we need a space right before appending the new char.

Code after:
    if (this.tokenMap) this._catchUpToCurrentToken(str, occurrenceCount);

    const lastChar = this.getLastChar();
    const strFirst = str.charCodeAt(0);
    if (
      (lastChar === charCodes.exclamationMark &&
        // space is mandatory to avoid outputting <!--
        // http://javascript.spec.whatwg.org/#comment-syntax
        (str === "--" ||
          // Needs spaces to avoid changing a! == 0 to a!== 0
          strFirst === charCodes.equalsTo)) ||
      // Need spaces for operators of the same kind to avoid: `a+++b`
      (strFirst === charCodes.plusSign && lastChar === charCodes.plusSign) ||
      (strFirst === charCodes.dash && lastChar === charCodes.dash) ||
      // Needs spaces to avoid changing '34' to '34.', which would still be a valid number.
      (strFirst === charCodes.dot && this._endsWithInteger)
    ) {
      this._space();
    }
    this._append(str, maybeNewline);
    this._noLineTerminator = false;
  }

  tokenChar(char: number): void {
    this.tokenContext = 0;

    const str = String.fromCharCode(char);
    this._maybePrintInnerComments(str);

    this._maybeAddAuxComment();

    if (this.tokenMap) this._catchUpToCurrentToken(str);

    const lastChar = this.getLastChar();
    if (
      // Need spaces for operators of the same kind to avoid: `a+++b`
      (char === charCodes.plusSign && lastChar === charCodes.plusSign) ||
      (char === charCodes.dash && lastChar === charCodes.dash) ||
      // Needs spaces to avoid changing '34' to '34.', which would still be a valid number.
      (char === charCodes.dot && this._endsWithInteger)
    ) {
      this._space();
    }
    this._appendChar(char);
    this._noLineTerminator = false;
  }

  /**
   * Add a newline (or many newlines), maintaining formatting.
   * This function checks the number of newlines in the queue and subtracts them.
   * It currently has some limitations.