From 05641623637846f9da0080181a5c28df61369c8d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 6 Oct 2026 15:32:23 +0000 Subject: [PATCH 1/2] Skip parsing JavaScript that can't contain translations Parsing JavaScript with Peast takes almost all of `make-pot`'s time on plugins that ship built bundles. Before parsing, check whether the code contains any of the translation function names as a whole word. Every call form the scanner resolves contains the name that way, so files without one are skipped. Code passed to `eval()` is the exception, since its source is escaped inside a string literal, so files that call `eval` are always parsed. Comments are only collected when they can matter: when the code contains a translator comment prefix, or a mangled webpack name like `/* __ */`. Tracking comments is a large part of the parsing cost. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS --- features/makepot.feature | 69 ++++++++++++++++++++++++++++++++++++++ src/JsFunctionsScanner.php | 67 +++++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index 2d210d3..24672f6 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -2016,6 +2016,75 @@ Feature: Generate a POT file of a WordPress project msgid "foo-plugin" """ + Scenario: Only parses JavaScript files that can contain translations + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.php file: + """ + containsName( [ 'eval' ] ); + if ( ! $has_eval && ! $this->containsName( $functions ) ) { + $file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : ''; + WP_CLI::debug( "Skipping file {$file}: no translation function calls found", 'make-pot' ); + return; + } + $peast_options = [ 'sourceType' => Peast::SOURCE_TYPE_MODULE, - 'comments' => false !== $this->extract_comments, + 'comments' => $has_eval ? false !== $this->extract_comments : $this->needsComments( $functions ), 'jsx' => true, ]; $ast = Peast::latest( $this->code, $peast_options )->parse(); @@ -286,6 +299,58 @@ function ( $node ) use ( &$translations, $options, $scanner ) { $traverser->traverse( $ast ); } + /** + * Whether the code contains any of the given names as a whole word. + * + * Every call that resolveExpressionCallee() can resolve contains the + * function name as a whole word: as an identifier (`__(`, `u.__(`, + * `(0, u.__)(`), as a string (`u["__"]`) or as a comment (`u[/* __ *\/ "a"]`). + * + * This doesn't hold for code inside string literals passed to `eval()`, + * as in `eval("\n__(...)")`, so callers must check for `eval` too. + * + * @param array $names Names to look for. + * @return bool + */ + private function containsName( array $names ) { + if ( empty( $names ) ) { + return false; + } + + $pattern = implode( '|', array_map( 'preg_quote', array_map( 'strval', $names ) ) ); + + return 1 === preg_match( '/(?code ); + } + + /** + * Whether the code needs to be parsed with comments. + * + * Comments are needed for translator comments, and for the function names + * in mangled webpack output like `u[/* __ *\/ "a"]( "translation" )`. + * + * @param array $functions Function names. + * @return bool + */ + private function needsComments( array $functions ) { + if ( false === $this->extract_comments ) { + return false; + } + + foreach ( (array) $this->extract_comments as $prefix ) { + if ( '' === $prefix || false !== strpos( $this->code, $prefix ) ) { + return true; + } + } + + if ( empty( $functions ) ) { + return false; + } + + $pattern = implode( '|', array_map( 'preg_quote', array_map( 'strval', $functions ) ) ); + + return 1 === preg_match( '#/\*\s*(?:' . $pattern . ')\s*\*/#', $this->code ); + } + /** * Resolve the callee of a call expression using known formats. * From ccbf6721d7a46f782588cfbcb068b8efb0d7791e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 6 Oct 2026 16:40:24 +0000 Subject: [PATCH 2/2] Add unit tests for the JavaScript scanner's pre-checks Cover the cases that `make-pot` itself doesn't reach: no translation functions configured, comment extraction turned off, and an empty comment prefix. Also drop a check in `needsComments()` that can't be reached, since it is only called for code that contains one of the function names. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D26yjkN2BiqCXT6p6o1WqS --- src/JsFunctionsScanner.php | 8 ++-- tests/JsFunctionsScannerTest.php | 67 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 tests/JsFunctionsScannerTest.php diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index dea2d53..a846688 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -328,7 +328,9 @@ private function containsName( array $names ) { * Comments are needed for translator comments, and for the function names * in mangled webpack output like `u[/* __ *\/ "a"]( "translation" )`. * - * @param array $functions Function names. + * Only called for code that contains at least one of the function names. + * + * @param array $functions Function names, not empty. * @return bool */ private function needsComments( array $functions ) { @@ -342,10 +344,6 @@ private function needsComments( array $functions ) { } } - if ( empty( $functions ) ) { - return false; - } - $pattern = implode( '|', array_map( 'preg_quote', array_map( 'strval', $functions ) ) ); return 1 === preg_match( '#/\*\s*(?:' . $pattern . ')\s*\*/#', $this->code ); diff --git a/tests/JsFunctionsScannerTest.php b/tests/JsFunctionsScannerTest.php new file mode 100644 index 0000000..8a0608c --- /dev/null +++ b/tests/JsFunctionsScannerTest.php @@ -0,0 +1,67 @@ + $options Extraction options. + * @return Translations + */ + private function extract( $code, array $options = [] ) { + $translations = new Translations(); + + JsCodeExtractor::fromString( $code, $translations, $options + [ 'file' => 'test.js' ] ); + + return $translations; + } + + public function test_does_not_extract_anything_without_functions() { + $translations = $this->extract( + "__( 'Hello', 'foo-plugin' );", + [ 'functions' => [] ] + ); + + $this->assertCount( 0, $translations ); + } + + public function test_ignores_comments_if_comment_extraction_is_disabled() { + $translations = $this->extract( + "// translators: A comment.\n__( 'Hello', 'foo-plugin' );", + [ 'extractComments' => false ] + ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertNotFalse( $translation ); + $this->assertSame( [], $translation->getExtractedComments() ); + } + + public function test_extracts_all_comments_with_empty_prefix() { + $translations = $this->extract( + "// Any comment.\n__( 'Hello', 'foo-plugin' );", + [ 'extractComments' => '' ] + ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertNotFalse( $translation ); + $this->assertSame( [ 'Any comment.' ], $translation->getExtractedComments() ); + } + + public function test_extracts_translator_comments_with_prefix() { + $translations = $this->extract( + "// translators: A comment.\n__( 'Hello', 'foo-plugin' );", + [ 'extractComments' => [ 'translators', 'Translators' ] ] + ); + + $translation = $translations->find( null, 'Hello' ); + $this->assertNotFalse( $translation ); + $this->assertSame( [ 'translators: A comment.' ], $translation->getExtractedComments() ); + } +}