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,56 @@ 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" )`. + * + * 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 ) { + if ( false === $this->extract_comments ) { + return false; + } + + foreach ( (array) $this->extract_comments as $prefix ) { + if ( '' === $prefix || false !== strpos( $this->code, $prefix ) ) { + return true; + } + } + + $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. * 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() ); + } +}