ronantakizawa/codereview-bench huggingface dataset:
Repository: tailwindlabs/tailwindcss
File: packages/tailwindcss/src/utils/math-operators.ts (TypeScript)

Code before:
      // 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...
      else if (
        // Previous is a digit
        (prevCode >= 48 && prevCode <= 57) ||
        // Next is a digit
        (nextCode >= 48 && nextCode <= 57) ||
        // Previous is end of a function call (or parenthesized expression)
        prev === ')' ||
        // Next is start of a parenthesized expression
        next === '(' ||
        // Next is an operator
        next === '+' ||
        next === '*' ||
        next === '/' ||
        next === '-' ||
        // Previous position was a value (+ unit)
        (lastValuePos !== null && lastValuePos === i - 1)
      ) {
        result += ` ${char} `
      }

      // Everything else
      else {
        result += char
      }
    }

    // Handle all other characters
    else {
      result += char
    }
  }

  return result
}


Review comment (suggestion):
1. Can we drop `next` and just use char codes for these?
2. Can we put the char codes behind named constants (like we do in segment and other places)

Code after:
      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
      else if (prev === OPEN_PAREN || prev === COMMA) {
        result += input[i]
        continue
      }

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

      // Add spaces around the operator, if...
      else if (
        // Previous is a digit
        (prev >= ZERO && prev <= NINE) ||
        // Next is a digit
        (next >= ZERO && next <= NINE) ||
        // Previous is end of a function call (or parenthesized expression)
        prev === CLOSE_PAREN ||
        // Next is start of a parenthesized expression
        next === OPEN_PAREN ||
        // Next is an operator
        next === ADD ||
        next === MUL ||
        next === DIV ||
        next === SUB ||
        // Previous position was a value (+ unit)
        (lastValuePos !== null && lastValuePos === i - 1)
      ) {
        result += ` ${input[i]} `
      }

      // Everything else
      else {
        result += input[i]
      }
    }

    // Handle all other characters
    else {
Repository: tailwindlabs/tailwindcss
File: crates/oxide/src/extractor/pre_processors/clojure.rs (Rust)

Code before:
                b';' if matches!(cursor.next, b';') => {
                    while cursor.pos < len && cursor.curr != b'\n' {
                        cursor.advance();
                    }
                }

                // A `.` surrounded by digits is a decimal number, so we don't want to replace it.
                //
                // E.g.:
                // ```
                // gap-1.5
                //      ^
                // ``
                b'.' if cursor.prev.is_ascii_digit() && cursor.next.is_ascii_digit() => {

                    // Keep the `.` as-is
                }

                // A `:` surrounded by letters denotes a variant. Keep as is.
                //
                // E.g.:
                // ```
                // lg:pr-6"
                //   ^
                // ``
                b':' if cursor.prev.is_ascii_alphabetic() && cursor.next.is_ascii_alphabetic() => {

                    // Keep the `:` as-is
                }

                b':' | b'.' => {
                    result[cursor.pos] = b' ';
                }

                // Consume everything else
                _ => {}
            };

            cursor.advance();
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::Clojure;
    use crate::extractor::pre_processors::pre_processor::PreProcessor;

    #[test]

Review comment (suggestion):
I'd update this myself but git doesn't seem to want me to push to your branch.

```suggestion
                b':' if cursor.prev.is_ascii_alphanumeric() && cursor.next.is_ascii_alphanumeric() => {
```

Code after:
                b';' if matches!(cursor.next, b';') => {
                    while cursor.pos < len && cursor.curr != b'\n' {
                        cursor.advance();
                    }
                }

                // A `.` surrounded by digits is a decimal number, so we don't want to replace it.
                //
                // E.g.:
                // ```
                // gap-1.5
                //      ^
                // ``
                b'.' if cursor.prev.is_ascii_digit() && cursor.next.is_ascii_digit() => {

                    // Keep the `.` as-is
                }

                // A `:` surrounded by letters denotes a variant. Keep as is.
                //
                // E.g.:
                // ```
                // lg:pr-6"
                //   ^
                // ``
                b':' if cursor.prev.is_ascii_alphanumeric() && cursor.next.is_ascii_alphanumeric() => {

                    // Keep the `:` as-is
                }

                b':' | b'.' => {
                    result[cursor.pos] = b' ';
                }

                // Consume everything else
                _ => {}
            };

            cursor.advance();
        }

        result
    }
}

#[cfg(test)]
mod tests {
    use super::Clojure;
    use crate::extractor::pre_processors::pre_processor::PreProcessor;

    #[test]
Repository: tailwindlabs/tailwindcss
File: crates/oxide/src/extractor/pre_processors/clojure.rs (Rust)

Code before:

    #[test]
    fn test_ignore_comments_with_invalid_strings() {
        let input = r#"
            ;; This is an unclosed string: "
            (dom/div {:class "hover:flex px-1.5"})
        "#;

        Clojure::test_extract_contains(input, vec!["hover:flex", "px-1.5"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/17760
    #[test]
    fn test_extraction_of_classes_with_dots() {
        let input = r#"
            ($ :div {:class [:flex :gap-1.5 :p-1]} …)
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "gap-1.5", "p-1"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/18336
    #[test]
    fn test_extraction_of_pseudoclasses_from_keywords() {
        let input = r#"
            ($ :div {:class [:flex :first:lg:pr-6]} …)

            :.hover:bg-white
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "first:lg:pr-6", "hover:bg-white"]);
    }
}


Review comment (suggestion):
```suggestion
            ($ :div {:class [:flex :first:lg:pr-6 :first:2xl:pl-6 :group-hover/2:2xs:pt-6]} …)
```

Code after:

    #[test]
    fn test_ignore_comments_with_invalid_strings() {
        let input = r#"
            ;; This is an unclosed string: "
            (dom/div {:class "hover:flex px-1.5"})
        "#;

        Clojure::test_extract_contains(input, vec!["hover:flex", "px-1.5"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/17760
    #[test]
    fn test_extraction_of_classes_with_dots() {
        let input = r#"
            ($ :div {:class [:flex :gap-1.5 :p-1]} …)
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "gap-1.5", "p-1"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/18336
    #[test]
    fn test_extraction_of_pseudoclasses_from_keywords() {
        let input = r#"
            ($ :div {:class [:flex :first:lg:pr-6 :first:2xl:pl-6 :group-hover/2:2xs:pt-6]} …)

            :.hover:bg-white
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "first:lg:pr-6", "first:2xl:pl-6", "group-hover/2:2xs:pt-6", "hover:bg-white"]);
    }
}
Repository: tailwindlabs/tailwindcss
File: crates/oxide/src/extractor/pre_processors/clojure.rs (Rust)

Code before:
            (dom/div {:class "hover:flex px-1.5"})
        "#;

        Clojure::test_extract_contains(input, vec!["hover:flex", "px-1.5"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/17760
    #[test]
    fn test_extraction_of_classes_with_dots() {
        let input = r#"
            ($ :div {:class [:flex :gap-1.5 :p-1]} …)
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "gap-1.5", "p-1"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/18336
    #[test]
    fn test_extraction_of_pseudoclasses_from_keywords() {
        let input = r#"
            ($ :div {:class [:flex :first:lg:pr-6]} …)

            :.hover:bg-white
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "first:lg:pr-6", "hover:bg-white"]);
    }
}


Review comment (refactor):
```suggestion
        Clojure::test_extract_contains(input, vec!["flex", "first:lg:pr-6", "first:2xl:pl-6", "group-hover/2:2xs:pt-6", "hover:bg-white"]);
```

Code after:
            (dom/div {:class "hover:flex px-1.5"})
        "#;

        Clojure::test_extract_contains(input, vec!["hover:flex", "px-1.5"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/17760
    #[test]
    fn test_extraction_of_classes_with_dots() {
        let input = r#"
            ($ :div {:class [:flex :gap-1.5 :p-1]} …)
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "gap-1.5", "p-1"]);
    }

    // https://github.com/tailwindlabs/tailwindcss/issues/18336
    #[test]
    fn test_extraction_of_pseudoclasses_from_keywords() {
        let input = r#"
            ($ :div {:class [:flex :first:lg:pr-6 :first:2xl:pl-6 :group-hover/2:2xs:pt-6]} …)

            :.hover:bg-white
        "#;

        Clojure::test_extract_contains(input, vec!["flex", "first:lg:pr-6", "first:2xl:pl-6", "group-hover/2:2xs:pt-6", "hover:bg-white"]);
    }
}
Repository: tailwindlabs/tailwindcss
File: crates/oxide/src/extractor/pre_processors/clojure.rs (Rust)

Code before:
use crate::cursor;
use crate::extractor::pre_processors::pre_processor::PreProcessor;
use bstr::ByteSlice;

#[derive(Debug, Default)]
pub struct Clojure;

#[inline]
fn is_keyword_terminator(byte: u8) -> bool {
    matches!(
        byte,
        b'"' | b';' | b'@' | b'^' | b'`' | b'~' | b'(' | b')' | b'[' | b']' | b'{' | b'}' | b'\\'
    ) || byte.is_ascii_whitespace()
}

impl PreProcessor for Clojure {
    fn process(&self, content: &[u8]) -> Vec<u8> {
        let content = content
            .replace(":class", "      ")
            .replace(":className", "          ");
        let len = content.len();
        let mut result = content.to_vec();
        let mut cursor = cursor::Cursor::new(&content);

        while cursor.pos < len {
            match cursor.curr {
                // Consume strings as-is
                b'"' => {
                    result[cursor.pos] = b' ';
                    cursor.advance();

                    while cursor.pos < len {
                        match cursor.curr {
                            // Escaped character, skip ahead to the next character
                            b'\\' => cursor.advance_twice(),

                            // End of the string
                            b'"' => {
                                result[cursor.pos] = b' ';

Review comment (suggestion):
```suggestion
fn is_keyword_character(byte: u8) -> bool {
    matches!(
        byte,
        b'+' | b'-' | b'/' | b'*' | b'_' | b'#' | b'.' | b':' | b'?'
    ) | byte.is_ascii_alphanumeric()
}
```

I'd go for a positive check versus a negative one. Using `|` instead of `||` produces better, branchless assembly too: https://godbolt.org/z/94WqeEz1n

Code after:
use crate::cursor;
use crate::extractor::pre_processors::pre_processor::PreProcessor;
use bstr::ByteSlice;

#[derive(Debug, Default)]
pub struct Clojure;

/// This is meant to be a rough estimate of a valid ClojureScript keyword
///
/// This can be approximated by the following regex:
/// /::?[a-zA-Z0-9!#$%&*+./:<=>?_|-]+/
///
/// However, keywords are intended to be detected as utilities. Since the set
/// of valid characters in a utility (outside of arbitrary values) is smaller,
/// along with the fact that neither `[]` nor `()` are allowed in keywords we
/// can simplify this list quite a bit.
#[inline]
fn is_keyword_character(byte: u8) -> bool {
    return matches!(
        byte,
        b'!' | b'%' | b'*' | b'+' | b'-' | b'.' | b'/' | b':' | b'_'
    ) | byte.is_ascii_alphanumeric();
}

impl PreProcessor for Clojure {
    fn process(&self, content: &[u8]) -> Vec<u8> {
        let content = content
            .replace(":class", "      ")
            .replace(":className", "          ");
        let len = content.len();
        let mut result = content.to_vec();
        let mut cursor = cursor::Cursor::new(&content);

        while cursor.pos < len {
            match cursor.curr {
                // Consume strings as-is
                b'"' => {
                    result[cursor.pos] = b' ';
                    cursor.advance();
Repository: tailwindlabs/tailwindcss
File: packages/@tailwindcss-postcss/src/ast.test.ts (TypeScript)

Code before:
    @charset "UTF-8";

    @layer foo, bar, baz;

    @import 'tailwindcss';

    .foo {
      color: red;

      &:hover {
        color: blue;
      }

      .bar {
        color: green !important;
        background-color: yellow;

        @media (min-width: 640px) {
          color: orange;
        }
      }
    }
  `

  let ast = parse(input)
  let transformedAst = cssAstToPostCssAst(ast, undefined, postcss)

  expect(transformedAst.toString()).toMatchInlineSnapshot(`
    "@charset "UTF-8";
    @layer foo, bar, baz;
    @import 'tailwindcss';
    .foo {
        color: red;
        &:hover {
            color: blue;
        }
        .bar {
            color: green !important;
            background-color: yellow;
            @media (min-width: 640px) {
                color: orange;
            }
        }
    }"
  `)
})


Review comment (suggestion):
```suggestion
  let transformedAst = cssAstToPostCssAst(postcss, ast)
```

Code after:
    @charset "UTF-8";

    @layer foo, bar, baz;

    @import 'tailwindcss';

    .foo {
      color: red;

      &:hover {
        color: blue;
      }

      .bar {
        color: green !important;
        background-color: yellow;

        @media (min-width: 640px) {
          color: orange;
        }
      }
    }
  `

  let ast = parse(input)
  let transformedAst = cssAstToPostCssAst(postcss, ast)

  expect(transformedAst.toString()).toMatchInlineSnapshot(`
    "@charset "UTF-8";
    @layer foo, bar, baz;
    @import 'tailwindcss';
    .foo {
        color: red;
        &:hover {
            color: blue;
        }
        .bar {
            color: green !important;
            background-color: yellow;
            @media (min-width: 640px) {
                color: orange;
            }
        }
    }"
  `)
})
Repository: tailwindlabs/tailwindcss
File: packages/@tailwindcss-postcss/src/ast.ts (TypeScript)

Code before:
import type * as postcss from 'postcss'
import { atRule, comment, decl, rule, type AstNode } from '../../tailwindcss/src/ast'
import { createLineTable, type LineTable } from '../../tailwindcss/src/source-maps/line-table'
import type { Source, SourceLocation } from '../../tailwindcss/src/source-maps/source'
import { DefaultMap } from '../../tailwindcss/src/utils/default-map'

const EXCLAMATION_MARK = 0x21

export function cssAstToPostCssAst(
  ast: AstNode[],
  source: postcss.Source | undefined,
  postcss: postcss.Postcss,
): postcss.Root {
  let inputMap = new DefaultMap<Source, postcss.Input>((src) => {
    return new postcss.Input(src.code, {
      map: source?.input.map,
      from: src.file ?? undefined,
    })
  })

  let lineTables = new DefaultMap<Source, LineTable>((src) => createLineTable(src.code))

  let root = postcss.root()
  root.source = source

  function toSource(loc: SourceLocation | undefined): postcss.Source | undefined {
    // Use the fallback if this node has no location info in the AST
    if (!loc) return
    if (!loc[0]) return

    let table = lineTables.get(loc[0])
    let start = table.find(loc[1])
    let end = table.find(loc[2])

    return {
      input: inputMap.get(loc[0]),
      start: {

Review comment (suggestion):
```suggestion
  postcss: postcss.Postcss,
  ast: AstNode[],
  source: postcss.Source | undefined,
```

Code after:
import type * as postcss from 'postcss'
import { atRule, comment, decl, rule, type AstNode } from '../../tailwindcss/src/ast'
import { createLineTable, type LineTable } from '../../tailwindcss/src/source-maps/line-table'
import type { Source, SourceLocation } from '../../tailwindcss/src/source-maps/source'
import { DefaultMap } from '../../tailwindcss/src/utils/default-map'

const EXCLAMATION_MARK = 0x21

export function cssAstToPostCssAst(
  postcss: postcss.Postcss,
  ast: AstNode[],
  source?: postcss.Source,
): postcss.Root {
  let inputMap = new DefaultMap<Source, postcss.Input>((src) => {
    return new postcss.Input(src.code, {
      map: source?.input.map,
      from: src.file ?? undefined,
    })
  })

  let lineTables = new DefaultMap<Source, LineTable>((src) => createLineTable(src.code))

  let root = postcss.root()
  root.source = source

  function toSource(loc: SourceLocation | undefined): postcss.Source | undefined {
    // Use the fallback if this node has no location info in the AST
    if (!loc) return
    if (!loc[0]) return

    let table = lineTables.get(loc[0])
    let start = table.find(loc[1])
    let end = table.find(loc[2])

    return {
      input: inputMap.get(loc[0]),
      start: {
Repository: tailwindlabs/tailwindcss
File: packages/tailwindcss/src/design-system.ts (TypeScript)

Code before:
        } else {
          result.push(toCss(astNodes))
        }
      }

      return result
    },

    getClassOrder(classes) {
      return getClassOrder(this, classes)
    },
    getClassList() {
      return getClassList(this)
    },
    getVariants() {
      return getVariants(this)
    },

    parseCandidate(candidate: string) {
      return parsedCandidates.get(candidate)
    },
    parseVariant(variant: string) {
      return parsedVariants.get(variant)
    },
    compileAstNodes(candidate: Candidate, flags?: CompileAstFlags) {
      flags ??= CompileAstFlags.RespectImportant

      return compiledAstNodes.get(flags).get(candidate)
    },

    printCandidate(candidate: Candidate) {
      return printCandidate(designSystem, candidate)
    },
    printVariant(variant: Variant) {
      return printVariant(variant)
    },

    getVariantOrder() {
      let variants = Array.from(parsedVariants.values())
      variants.sort((a, z) => this.variants.compare(a, z))

      let order = new Map<Variant, number>()
      let prevVariant: Variant | undefined = undefined
      let index: number = 0

      for (let variant of variants) {
        if (variant === null) {
          continue
        }
        // This variant is not the same order as the previous one
        // so it goes into a new group

Review comment (suggestion):
```suggestion
    compileAstNodes(candidate: Candidate, flags = CompileAstFlags.RespectImportant) {
```


Code after:
        } else {
          result.push(toCss(astNodes))
        }
      }

      return result
    },

    getClassOrder(classes) {
      return getClassOrder(this, classes)
    },
    getClassList() {
      return getClassList(this)
    },
    getVariants() {
      return getVariants(this)
    },

    parseCandidate(candidate: string) {
      return parsedCandidates.get(candidate)
    },
    parseVariant(variant: string) {
      return parsedVariants.get(variant)
    },
    compileAstNodes(candidate: Candidate, flags = CompileAstFlags.RespectImportant) {
      return compiledAstNodes.get(flags).get(candidate)
    },

    printCandidate(candidate: Candidate) {
      return printCandidate(designSystem, candidate)
    },
    printVariant(variant: Variant) {
      return printVariant(variant)
    },

    getVariantOrder() {
      let variants = Array.from(parsedVariants.values())
      variants.sort((a, z) => this.variants.compare(a, z))

      let order = new Map<Variant, number>()
      let prevVariant: Variant | undefined = undefined
      let index: number = 0

      for (let variant of variants) {
        if (variant === null) {
          continue
        }
        // This variant is not the same order as the previous one
        // so it goes into a new group
        if (prevVariant !== undefined && this.variants.compare(prevVariant, variant) !== 0) {
          index++
Repository: tailwindlabs/tailwindcss
File: crates/oxide/src/extractor/pre_processors/elixir.rs (Rust)

Code before:
use std::str::from_utf8;

use crate::cursor;
use crate::extractor::bracket_stack::BracketStack;
use crate::extractor::pre_processors::pre_processor::PreProcessor;

#[derive(Debug, Default)]
pub struct Elixir;

impl PreProcessor for Elixir {
    fn process(&self, content: &[u8]) -> Vec<u8> {
        let mut cursor = cursor::Cursor::new(&content);
        let mut result = content.to_vec();
        let mut bracket_stack = BracketStack::default();

        while cursor.pos < content.len() {
            // Look for a sigil marker
            if cursor.curr != b'~' {
                cursor.advance();
                continue;
            }

            // Scan charlists, strings, and wordlists
            if !matches!(cursor.next, b'c' | b'C' | b's' | b'S' | b'w' | b'W') {
                cursor.advance();
                continue;
            }

            cursor.advance_twice();

            // Match the opening for a sigil
            if !matches!(cursor.curr, b'(' | b'[' | b'{') {
                continue;
            }

            // Replace the opening bracket with a space
            result[cursor.pos] = b' ';

Review comment (suggestion):
```suggestion
        let mut cursor = cursor::Cursor::new(content);
```

Code after:
use crate::cursor;
use crate::extractor::bracket_stack::BracketStack;
use crate::extractor::pre_processors::pre_processor::PreProcessor;

#[derive(Debug, Default)]
pub struct Elixir;

impl PreProcessor for Elixir {
    fn process(&self, content: &[u8]) -> Vec<u8> {
        let mut cursor = cursor::Cursor::new(content);
        let mut result = content.to_vec();
        let mut bracket_stack = BracketStack::default();

        while cursor.pos < content.len() {
            // Look for a sigil marker
            if cursor.curr != b'~' {
                cursor.advance();
                continue;
            }

            // Scan charlists, strings, and wordlists
            if !matches!(cursor.next, b'c' | b'C' | b's' | b'S' | b'w' | b'W') {
                cursor.advance();
                continue;
            }

            cursor.advance_twice();

            // Match the opening for a sigil
            if !matches!(cursor.curr, b'(' | b'[' | b'{') {
                continue;
            }

            // Replace the opening bracket with a space
            result[cursor.pos] = b' ';

            // Scan until we find a balanced closing one and replace it too