Repository: tailwindlabs/tailwindcss
File: packages/tailwindcss/src/utils/math-operators.ts (TypeScript)

Code before:
    // type.
    else if (char === ')') {
      result += char
      formattable.shift()
    }

    // Add spaces after commas in math functions
    else if (char === ',' && formattable[0]) {
      result += `, `
      continue
    }

    // Skip over consecutive whitespace
    else if (char === ' ' && formattable[0] && result[result.length - 1] === ' ') {
      continue
    }

    // Add whitespace around operators inside math functions
    else if ((char === '+' || char === '*' || char === '/' || char === '-') && formattable[0]) {
      let trimmed = result.trimEnd()
      let prev = trimmed[trimmed.length - 1]
      let prevCode = prev.charCodeAt(0)
      let prevprevCode = trimmed.charCodeAt(trimmed.length - 2)

      let next = input[i + 1]
      let nextCode = next?.charCodeAt(0)

      // Do not add spaces for scientific notation, e.g.: `-3.4e-2`
      if ((prev === 'e' || prev === 'E') && prevprevCode >= 48 && prevprevCode <= 57) {
        result += char
        continue
      }

      // If we're preceded by an operator don't add spaces
      else if (prev === '+' || prev === '*' || prev === '/' || prev === '-') {
        result += char
        continue
      }

      // If we're at the beginning of an argument don't add spaces
      else if (prev === '(' || prev === ',') {
        result += char
        continue
      }

      // Add spaces only after the operator if we already have spaces before it
      else if (input[i - 1] === ' ') {
        result += `${char} `
      }

      // Add spaces around the operator, if...

Review comment (suggestion):
`let nextCode = input.charCodeAt(i+1)`

`NaN` and `undefined` have the same comparison semantics for the checks we're doing I think.

Code after:

      // We've encountered nested parens inside a math function, record that and
      // keep formatting until we've closed all parens.
      else if (formattable[0] && fn === '') {
        formattable.unshift(true)
        continue
      }

      // This is not a known math function so don't format it
      formattable.unshift(false)
      continue
    }

    // We've exited the function so format according to the parent function's
    // type.
    else if (char === CLOSE_PAREN) {
      result += input[i]
      formattable.shift()
    }

    // Add spaces after commas in math functions
    else if (char === COMMA && formattable[0]) {
      result += `, `
      continue
    }

    // Skip over consecutive whitespace
    else if (char === SPACE && formattable[0] && result.charCodeAt(result.length - 1) === SPACE) {
      continue
    }

    // Add whitespace around operators inside math functions
    else if ((char === ADD || char === MUL || char === DIV || char === SUB) && formattable[0]) {
      let trimmed = result.trimEnd()
      let prev = trimmed.charCodeAt(trimmed.length - 1)
      let prevPrev = trimmed.charCodeAt(trimmed.length - 2)
      let next = input.charCodeAt(i + 1)

      // Do not add spaces for scientific notation, e.g.: `-3.4e-2`
      if ((prev === LOWER_E || prev === UPPER_E) && prevPrev >= ZERO && prevPrev <= NINE) {
        result += input[i]
        continue
      }

      // If we're preceded by an operator don't add spaces
      else if (prev === ADD || prev === MUL || prev === DIV || prev === SUB) {
        result += input[i]
        continue
      }

      // If we're at the beginning of an argument don't add spaces