diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh index 6c2f04b8bd..edcd378b06 100644 --- a/scripts/bash/create-new-feature.sh +++ b/scripts/bash/create-new-feature.sh @@ -201,12 +201,19 @@ unicode_words() { fi printf '%s' "$name" | "${python_cmd[@]}" -c ' import sys +import unicodedata value = sys.stdin.buffer.read().decode("utf-8") lower = str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") -result = "".join( - char if char.isalpha() or char.isdecimal() else sys.argv[1] - for char in value.translate(lower) -) +chars = [] +in_word = False +for char in value.translate(lower): + if char.isalpha() or char.isdecimal(): + in_word = True + elif not (in_word and unicodedata.category(char).startswith("M")): + in_word = False + char = sys.argv[1] + chars.append(char) +result = "".join(chars) sys.stdout.buffer.write(result.encode("utf-8")) ' "$separator" else diff --git a/scripts/powershell/create-new-feature.ps1 b/scripts/powershell/create-new-feature.ps1 index edb75b3222..507c1966b7 100644 --- a/scripts/powershell/create-new-feature.ps1 +++ b/scripts/powershell/create-new-feature.ps1 @@ -93,14 +93,20 @@ function ConvertTo-UnicodeWords { param([string]$Name, [string]$Separator) $lowerName = ConvertTo-AsciiLower -Name $Name - return [regex]::Replace($lowerName, '[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\p{L}\p{Nd}]', { + # Combining marks stay only after a kept letter, digit, or mark, so an orphan + # emoji variation selector (U+FE0F) becomes a separator instead of surviving. + $state = @{ InWord = $false } + return [regex]::Replace($lowerName, '[\uD800-\uDBFF][\uDC00-\uDFFF]|[\s\S]', { param($match) - if ($match.Length -eq 2) { - $category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($match.Value, 0) - if ($category.ToString() -match '(Letter|DecimalDigitNumber)$') { - return $match.Value - } + $category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($match.Value, 0).ToString() + if ($category -match '(Letter|DecimalDigitNumber)$') { + $state.InWord = $true + return $match.Value + } + if ($state.InWord -and $category -match 'Mark$') { + return $match.Value } + $state.InWord = $false return $Separator }) } diff --git a/scripts/python/create_new_feature.py b/scripts/python/create_new_feature.py index 2099a73708..9e98d0c564 100644 --- a/scripts/python/create_new_feature.py +++ b/scripts/python/create_new_feature.py @@ -9,6 +9,7 @@ import re import shlex import sys +import unicodedata from dataclasses import dataclass from pathlib import Path @@ -176,10 +177,18 @@ def _clean_branch_name(name: str) -> str: def _unicode_words(name: str, separator: str) -> str: - return "".join( - char if char.isalpha() or char.isdecimal() else separator - for char in name.translate(_ASCII_LOWER) - ) + # Combining marks stay only when they follow a kept letter, digit, or mark, + # so an orphan emoji variation selector (U+FE0F) cannot survive on its own. + chars: list[str] = [] + in_word = False + for char in name.translate(_ASCII_LOWER): + if char.isalpha() or char.isdecimal(): + in_word = True + elif not (in_word and unicodedata.category(char).startswith("M")): + in_word = False + char = separator + chars.append(char) + return "".join(chars) def _generate_branch_name(description: str) -> str: diff --git a/tests/test_create_new_feature_python_parity.py b/tests/test_create_new_feature_python_parity.py index e98f65e463..c690e9bb3e 100644 --- a/tests/test_create_new_feature_python_parity.py +++ b/tests/test_create_new_feature_python_parity.py @@ -17,6 +17,7 @@ from tests.conftest import requires_bash from tests.parity_helpers import ( HAS_POWERSHELL, + HAS_PWSH, WINDOWS_POWERSHELL, _bash_posix_path, bash_cmd, @@ -86,6 +87,8 @@ def repo_pair(tmp_path: Path) -> tuple[Path, Path]: ("添加用户", None, "添加用户", False), ("добавить", None, "добавить", False), ("!!! ??? ***", None, "", True), + ("\U0001F389", None, "", True), + ("\U0001F600 \u2764\ufe0f", None, "", True), ("添加用户", "user-auth", "user-auth", False), ("Add users", "用户", "用户", False), ("Add user authentication", None, "user-authentication", False), @@ -196,6 +199,62 @@ def test_unicode_number_categories_match( assert json_stdout(result)["BRANCH_NAME"] == expected +@pytest.mark.parametrize( + "variant", + [ + pytest.param("bash", marks=requires_bash), + "python", + pytest.param( + "pwsh", + marks=pytest.mark.skipif(not HAS_PWSH, reason="pwsh not available"), + ), + pytest.param( + "windows-powershell-5.1", + marks=pytest.mark.skipif( + not WINDOWS_POWERSHELL, reason="Windows PowerShell 5.1 not available" + ), + ), + ], +) +@pytest.mark.parametrize( + ("description", "expected"), + [ + ("नमस्ते दुनिया", "001-नमस्ते-दुनिया"), + ("สร้างระบบล็อกอิน", "001-สร้างระบบล็อกอิน"), + ("مَرْحَبًا بالعالم", "001-مَرْحَبًا-بالعالم"), + ("cafe\u0301 menu", "001-cafe\u0301-menu"), + ( + "Add \u2699\ufe0f user login screen settings", + "001-user-login-screen-settings", + ), + ("login \u2764\ufe0f screen", "001-login-screen"), + ], + ids=[ + "devanagari", + "thai", + "arabic_diacritics", + "decomposed_accent", + "orphan_variation_selector", + "variation_selector_between_words", + ], +) +def test_combining_marks_stay_inside_words( + repo: Path, variant: str, description: str, expected: str +) -> None: + if variant in ("pwsh", "windows-powershell-5.1"): + exe = "pwsh" if variant == "pwsh" else WINDOWS_POWERSHELL + script = repo / ".specify/scripts/powershell/create-new-feature.ps1" + cmd = [exe, "-NoProfile", "-File", str(script), "-Json", "-DryRun", description] + else: + command = {"bash": bash_cmd, "python": py_cmd}[variant] + cmd = command(repo, SCRIPT, "--json", "--dry-run", description) + + result = run(cmd, repo) + + assert result.returncode == 0, result.stderr + assert json_stdout(result)["BRANCH_NAME"] == expected + + @requires_bash def test_bash_reports_missing_utf8_locale_for_unicode_only( repo: Path, tmp_path: Path