@@ -170,6 +170,42 @@ const EXCLUDED_FILE_RE = /(?:Tests?|UITests?|Previews?|Testing)\.(?:swift|kt|kts
170170const BUILD_SETTING_RE = / \$ \( [ A - Z a - z 0 - 9 _ . - ] + \) / gu;
171171const NATIVE_I18N_LOCALE_SET = new Set < string > ( NATIVE_I18N_LOCALES ) ;
172172
173+ function isAsciiLowercaseLetter ( character : string ) : boolean {
174+ return character >= "a" && character <= "z" ;
175+ }
176+
177+ function isAsciiUppercaseLetter ( character : string ) : boolean {
178+ return character >= "A" && character <= "Z" ;
179+ }
180+
181+ function isAsciiAlphaNumeric ( character : string ) : boolean {
182+ return (
183+ isAsciiLowercaseLetter ( character ) ||
184+ isAsciiUppercaseLetter ( character ) ||
185+ ( character >= "0" && character <= "9" )
186+ ) ;
187+ }
188+
189+ export function isConditionalBranchIdentifier ( source : string ) : boolean {
190+ let index = 0 ;
191+ while ( index < source . length && isAsciiLowercaseLetter ( source [ index ] ) ) {
192+ index += 1 ;
193+ }
194+
195+ // Keep this scanner linear: PR-controlled native source passes through CI,
196+ // so a backtracking regex here can become a cheap native-i18n DoS trigger.
197+ if ( index === 0 || index >= source . length || ! isAsciiUppercaseLetter ( source [ index ] ) ) {
198+ return false ;
199+ }
200+
201+ for ( index += 1 ; index < source . length ; index += 1 ) {
202+ if ( ! isAsciiAlphaNumeric ( source [ index ] ) ) {
203+ return false ;
204+ }
205+ }
206+ return true ;
207+ }
208+
173209function isTranslatableCandidate ( source : string , kind : string ) : boolean {
174210 if ( BUILD_SETTING_RE . test ( source ) ) {
175211 BUILD_SETTING_RE . lastIndex = 0 ;
@@ -180,7 +216,7 @@ function isTranslatableCandidate(source: string, kind: string): boolean {
180216 if ( ! isDirectUiText && ( / ^ [ a - z 0 - 9 _ . : / $ - ] + $ / u. test ( source ) || / ^ [ A - Z 0 - 9 _ . : / $ - ] + $ / u. test ( source ) ) ) {
181217 return false ;
182218 }
183- if ( kind === "conditional-branch" && / ^ [ a - z ] + (?: [ A - Z ] [ A - Z a - z 0 - 9 ] * ) + $ / u . test ( source ) ) {
219+ if ( kind === "conditional-branch" && isConditionalBranchIdentifier ( source ) ) {
184220 return false ;
185221 }
186222 if ( / [ { } [ \] ] / u. test ( source ) && ! / (?: \\ \( | \$ \{ ) / u. test ( source ) ) {
0 commit comments