|
| 1 | +"""Fixer for JSX curly braces.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import Tuple |
| 5 | + |
| 6 | +from .base import Fixer |
| 7 | + |
| 8 | + |
| 9 | +class JsxCurlyBracesFixer(Fixer): |
| 10 | + """Fix unescaped curly braces that might be interpreted as JSX. |
| 11 | +
|
| 12 | + Handles curly braces in two ways: |
| 13 | + 1. In inline code with single backticks: converts to double backticks |
| 14 | + 2. In text that looks like code (e.g., key:{value}): wraps in backticks |
| 15 | + """ |
| 16 | + |
| 17 | + @property |
| 18 | + def name(self) -> str: |
| 19 | + """Return the name of this fixer.""" |
| 20 | + return "Curly brace escaping" |
| 21 | + |
| 22 | + def fix(self, content: str) -> Tuple[str, int]: |
| 23 | + """Fix curly braces in inline code and code-like contexts. |
| 24 | +
|
| 25 | + :param content: The content to fix |
| 26 | + :returns: Tuple of (fixed_content, number_of_fixes_applied) |
| 27 | + """ |
| 28 | + in_code_block = False |
| 29 | + lines = content.split("\n") |
| 30 | + fixed_lines = [] |
| 31 | + fixes = 0 |
| 32 | + |
| 33 | + for line in lines: |
| 34 | + # Track code blocks (``` blocks) - ignore any format like ```mermaid, ```python, etc. |
| 35 | + if line.strip().startswith("```"): |
| 36 | + in_code_block = not in_code_block |
| 37 | + # Add code block delimiter without processing |
| 38 | + fixed_lines.append(line) |
| 39 | + continue |
| 40 | + |
| 41 | + # Skip processing if we're inside a code block |
| 42 | + # This ensures curly braces in code blocks (mermaid, python, etc.) are not modified |
| 43 | + if not in_code_block: |
| 44 | + original_line = line |
| 45 | + |
| 46 | + # Step 1: Convert single backticks to double if content has braces |
| 47 | + def convert_to_double_backticks(match: re.Match[str]) -> str: |
| 48 | + """Convert single backticks to double if content has braces.""" |
| 49 | + code_content = match.group(1) |
| 50 | + if "{" in code_content or "}" in code_content: |
| 51 | + return f"``{code_content}``" |
| 52 | + return str(match.group(0)) |
| 53 | + |
| 54 | + pattern1 = r"(?<!`)`([^`\n]+)`(?!`)" |
| 55 | + line = re.sub(pattern1, convert_to_double_backticks, line) |
| 56 | + |
| 57 | + # Step 2: Wrap code-like patterns with curly braces in backticks |
| 58 | + # Split line by existing backticks to process only text segments |
| 59 | + def wrap_code_like_patterns(text: str) -> str: |
| 60 | + """Wrap code-like patterns with curly braces in backticks.""" |
| 61 | + # Pattern 1: key:value:{variable} or key:{variable} (e.g., bot:health:{bot_id}) |
| 62 | + # Match word:word:{word} or word:{word} |
| 63 | + text = re.sub( |
| 64 | + r"(?<!`)(\w+:\w+:\{[\w_]+\}|\w+:\{[\w_]+\})(?!`)", |
| 65 | + r"`\1`", |
| 66 | + text, |
| 67 | + ) |
| 68 | + # Pattern 2: standalone {variable_name} that looks like code |
| 69 | + # Only if not already in backticks and not part of JSX expression |
| 70 | + text = re.sub( |
| 71 | + r"(?<!`)(?<!\w)\{[\w_]+\}(?!`)(?!\s*[:=])", |
| 72 | + r"`\0`", |
| 73 | + text, |
| 74 | + ) |
| 75 | + return text |
| 76 | + |
| 77 | + # Process text segments outside of backticks |
| 78 | + parts = re.split(r"(`+[^`]*`+)", line) |
| 79 | + processed_parts = [] |
| 80 | + for i, part in enumerate(parts): |
| 81 | + if part.startswith("`") and part.endswith("`"): |
| 82 | + # Already in backticks, keep as is |
| 83 | + processed_parts.append(part) |
| 84 | + else: |
| 85 | + # Process text segments |
| 86 | + processed_parts.append(wrap_code_like_patterns(part)) |
| 87 | + |
| 88 | + line = "".join(processed_parts) |
| 89 | + |
| 90 | + if line != original_line: |
| 91 | + fixes += 1 |
| 92 | + # If in_code_block is True, line remains unchanged and is added as-is |
| 93 | + |
| 94 | + # Add processed line (or original if inside code block) |
| 95 | + fixed_lines.append(line) |
| 96 | + |
| 97 | + return "\n".join(fixed_lines), fixes |
0 commit comments