|
4 | 4 |
|
5 | 5 | use Gettext\Utils\JsFunctionsScanner as GettextJsFunctionsScanner; |
6 | 6 | use Gettext\Utils\ParsedComment; |
| 7 | +use WP_CLI; |
7 | 8 | use Peast\Peast; |
8 | 9 | use Peast\Syntax\Node; |
9 | 10 | use Peast\Traverser; |
@@ -59,9 +60,21 @@ public function saveGettextFunctions( $translations, array $options ) { |
59 | 60 | $translations = $translations[0]; |
60 | 61 | } |
61 | 62 |
|
| 63 | + $functions = isset( $options['functions'] ) && is_array( $options['functions'] ) ? array_keys( $options['functions'] ) : []; |
| 64 | + |
| 65 | + // Parsing is by far the most expensive part, so skip it for code that |
| 66 | + // 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 ) ) { |
| 70 | + $file = isset( $options['file'] ) && is_scalar( $options['file'] ) ? (string) $options['file'] : ''; |
| 71 | + WP_CLI::debug( "Skipping file {$file}: no translation function calls found", 'make-pot' ); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
62 | 75 | $peast_options = [ |
63 | 76 | 'sourceType' => Peast::SOURCE_TYPE_MODULE, |
64 | | - 'comments' => false !== $this->extract_comments, |
| 77 | + 'comments' => $has_eval ? false !== $this->extract_comments : $this->needsComments( $functions ), |
65 | 78 | 'jsx' => true, |
66 | 79 | ]; |
67 | 80 | $ast = Peast::latest( $this->code, $peast_options )->parse(); |
@@ -286,6 +299,56 @@ function ( $node ) use ( &$translations, $options, $scanner ) { |
286 | 299 | $traverser->traverse( $ast ); |
287 | 300 | } |
288 | 301 |
|
| 302 | + /** |
| 303 | + * Whether the code contains any of the given names as a whole word. |
| 304 | + * |
| 305 | + * Every call that resolveExpressionCallee() can resolve contains the |
| 306 | + * function name as a whole word: as an identifier (`__(`, `u.__(`, |
| 307 | + * `(0, u.__)(`), as a string (`u["__"]`) or as a comment (`u[/* __ *\/ "a"]`). |
| 308 | + * |
| 309 | + * This doesn't hold for code inside string literals passed to `eval()`, |
| 310 | + * as in `eval("\n__(...)")`, so callers must check for `eval` too. |
| 311 | + * |
| 312 | + * @param array<int|string> $names Names to look for. |
| 313 | + * @return bool |
| 314 | + */ |
| 315 | + private function containsName( array $names ) { |
| 316 | + if ( empty( $names ) ) { |
| 317 | + return false; |
| 318 | + } |
| 319 | + |
| 320 | + $pattern = implode( '|', array_map( 'preg_quote', array_map( 'strval', $names ) ) ); |
| 321 | + |
| 322 | + return 1 === preg_match( '/(?<![\w$])(?:' . $pattern . ')(?![\w$])/', $this->code ); |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Whether the code needs to be parsed with comments. |
| 327 | + * |
| 328 | + * Comments are needed for translator comments, and for the function names |
| 329 | + * in mangled webpack output like `u[/* __ *\/ "a"]( "translation" )`. |
| 330 | + * |
| 331 | + * Only called for code that contains at least one of the function names. |
| 332 | + * |
| 333 | + * @param array<int|string> $functions Function names, not empty. |
| 334 | + * @return bool |
| 335 | + */ |
| 336 | + private function needsComments( array $functions ) { |
| 337 | + if ( false === $this->extract_comments ) { |
| 338 | + return false; |
| 339 | + } |
| 340 | + |
| 341 | + foreach ( (array) $this->extract_comments as $prefix ) { |
| 342 | + if ( '' === $prefix || false !== strpos( $this->code, $prefix ) ) { |
| 343 | + return true; |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + $pattern = implode( '|', array_map( 'preg_quote', array_map( 'strval', $functions ) ) ); |
| 348 | + |
| 349 | + return 1 === preg_match( '#/\*\s*(?:' . $pattern . ')\s*\*/#', $this->code ); |
| 350 | + } |
| 351 | + |
289 | 352 | /** |
290 | 353 | * Resolve the callee of a call expression using known formats. |
291 | 354 | * |
|
0 commit comments