@@ -64,17 +64,19 @@ public function saveGettextFunctions( $translations, array $options ) {
6464
6565 // Parsing is by far the most expensive part, so skip it for code that
6666 // can't contain any translations, and skip collecting comments if
67- // none of them can matter.
68- $ has_eval = $ this ->containsName ( [ 'eval ' ] );
69- if ( ! $ has_eval && ! $ this ->containsName ( $ functions ) ) {
67+ // none of them can matter. The checks below work on the raw code, so
68+ // they don't apply to code in `eval()` strings or to identifiers
69+ // written with Unicode escapes.
70+ $ parse_all = $ this ->containsName ( [ 'eval ' ] ) || $ this ->containsEscapedName ( $ functions );
71+ if ( ! $ parse_all && ! $ this ->containsName ( $ functions ) ) {
7072 $ file = isset ( $ options ['file ' ] ) && is_scalar ( $ options ['file ' ] ) ? (string ) $ options ['file ' ] : '' ;
7173 WP_CLI ::debug ( "Skipping file {$ file }: no translation function calls found " , 'make-pot ' );
7274 return ;
7375 }
7476
7577 $ peast_options = [
7678 'sourceType ' => Peast::SOURCE_TYPE_MODULE ,
77- 'comments ' => $ has_eval ? false !== $ this ->extract_comments : $ this ->needsComments ( $ functions ),
79+ 'comments ' => $ parse_all ? false !== $ this ->extract_comments : $ this ->needsComments ( $ functions ),
7880 'jsx ' => true ,
7981 ];
8082 $ ast = Peast ::latest ( $ this ->code , $ peast_options )->parse ();
@@ -322,6 +324,41 @@ private function containsName( array $names ) {
322324 return 1 === preg_match ( '/(?<![\w$])(?: ' . $ pattern . ')(?![\w$])/ ' , $ this ->code );
323325 }
324326
327+ /**
328+ * Whether the code contains a Unicode escape for a character of any of the given names.
329+ *
330+ * Identifiers can be written with escapes, like `\u005f\u005f( 'Hello' )`
331+ * for `__( 'Hello' )`. Such a call doesn't contain the name itself.
332+ *
333+ * @param array<int|string> $names Names to look for.
334+ * @return bool
335+ */
336+ private function containsEscapedName ( array $ names ) {
337+ if ( false === strpos ( $ this ->code , '\\u ' ) ) {
338+ return false ;
339+ }
340+
341+ $ codes = [];
342+ foreach ( $ names as $ name ) {
343+ foreach ( str_split ( (string ) $ name ) as $ char ) {
344+ if ( ord ( $ char ) > 0x7f ) {
345+ // Not worth handling precisely for non-ASCII names.
346+ return true ;
347+ }
348+ $ codes [] = sprintf ( '%x ' , ord ( $ char ) );
349+ }
350+ }
351+
352+ if ( empty ( $ codes ) ) {
353+ return false ;
354+ }
355+
356+ $ pattern = implode ( '| ' , array_unique ( $ codes ) );
357+
358+ // Either `\uXXXX` with exactly four hex digits, or `\u{X…}`.
359+ return 1 === preg_match ( '/ \\\\u(?:00(?: ' . $ pattern . ')|\{0*(?: ' . $ pattern . ')\})/i ' , $ this ->code );
360+ }
361+
325362 /**
326363 * Whether the code needs to be parsed with comments.
327364 *
0 commit comments