diff --git a/features/makepot.feature b/features/makepot.feature index f1cd83bd..6df82db5 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -1649,6 +1649,34 @@ Feature: Generate a POT file of a WordPress project msgid "wrong-domain" """ + Scenario: Ignores dynamic import in JavaScript file. + Given an empty foo-plugin directory + And a foo-plugin/foo-plugin.php file: + """ + console.log(a)) + __( '__', 'foo-plugin' ); + """ + + When I run `wp i18n make-pot foo-plugin` + Then STDOUT should be: + """ + Plugin file detected. + Success: POT file successfully generated! + """ + And the foo-plugin/foo-plugin.pot file should exist + And the foo-plugin/foo-plugin.pot file should contain: + """ + msgid "__" + """ + Scenario: Parse .js and .jsx files for javascript translations Given an empty foo-plugin directory And a foo-plugin/foo-plugin.php file: diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index 7379c779..00310cc1 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -37,16 +37,18 @@ public function disableCommentsExtraction() { * {@inheritdoc} */ public function saveGettextFunctions( Translations $translations, array $options ) { - // Temporary workaround for https://github.com/wp-cli/i18n-command/issues/98. - $this->code = str_replace( '/"/', '/\"/', $this->code ); - $this->code = str_replace( '/"|\'/', '/\"|\\\'/', $this->code ); + $code = $this->code; + // See https://github.com/mck89/peast/issues/7 + // Temporary workaround to fix dynamic imports. The τ is a greek letter. + // This will trick the parser into thinking that it is a normal method call. + $code = preg_replace( '/import(\\s*\\()/', 'imporτ$1', $code ); $peast_options = [ 'sourceType' => Peast::SOURCE_TYPE_MODULE, 'comments' => false !== $this->extract_comments, 'jsx' => true, ]; - $ast = Peast::latest( $this->code, $peast_options )->parse(); + $ast = Peast::latest( $code, $peast_options )->parse(); $traverser = new Traverser();