Skip to content

Commit ee11e2d

Browse files
authored
Parse JavaScript with Unicode-escaped identifiers (#510)
1 parent d47a013 commit ee11e2d

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎src/JsFunctionsScanner.php‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

‎tests/JsFunctionsScannerTest.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,19 @@ public function test_extracts_translator_comments_with_prefix() {
6464
$this->assertNotFalse( $translation );
6565
$this->assertSame( [ 'translators: A comment.' ], $translation->getExtractedComments() );
6666
}
67+
68+
public function test_extracts_calls_with_unicode_escaped_names() {
69+
$translations = $this->extract(
70+
"\\u005f\\u005f( 'Escaped', 'foo-plugin' );\n_\\u{78}( 'Braced', 'context', 'foo-plugin' );"
71+
);
72+
73+
$this->assertNotFalse( $translations->find( null, 'Escaped' ) );
74+
$this->assertNotFalse( $translations->find( 'context', 'Braced' ) );
75+
}
76+
77+
public function test_skips_code_with_unrelated_unicode_escapes() {
78+
$translations = $this->extract( "var s = '\\u005fa'; var t = '\\u00e9';" );
79+
80+
$this->assertCount( 0, $translations );
81+
}
6782
}

0 commit comments

Comments
 (0)