From 6be1c377b03cec0977f91b0205b95af26b43fb3c Mon Sep 17 00:00:00 2001 From: Michael Zangl Date: Sun, 28 Apr 2019 10:09:26 +0200 Subject: [PATCH 1/3] Workaround for parsing dynamic import. --- features/makepot.feature | 28 ++++++++++++++++++++++++++++ src/JsFunctionsScanner.php | 10 +++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/features/makepot.feature b/features/makepot.feature index 2cde3131..e178bf85 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -1638,6 +1638,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: Extract translator comments from JavaScript file 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..ddcb6fc4 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -38,15 +38,19 @@ public function disableCommentsExtraction() { */ 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; + $code = str_replace( '/"/', '/\"/', $code ); + $code = str_replace( '/"|\'/', '/\"|\\\'/', $code ); + // 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(); From a26eaf88f8b43cf47f1992a37b31b98493ffb11d Mon Sep 17 00:00:00 2001 From: Michael Zangl Date: Sun, 28 Apr 2019 10:21:54 +0200 Subject: [PATCH 2/3] Fix #98: Remove workaround since peast was fixed. --- src/JsFunctionsScanner.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index ddcb6fc4..a68aed6b 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -37,10 +37,8 @@ public function disableCommentsExtraction() { * {@inheritdoc} */ public function saveGettextFunctions( Translations $translations, array $options ) { - // Temporary workaround for https://github.com/wp-cli/i18n-command/issues/98. $code = $this->code; - $code = str_replace( '/"/', '/\"/', $code ); - $code = str_replace( '/"|\'/', '/\"|\\\'/', $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 ); From 4c5e30100c7e5120b260e2c51d69b6528cfad632 Mon Sep 17 00:00:00 2001 From: Michael Zangl Date: Sun, 28 Apr 2019 10:24:49 +0200 Subject: [PATCH 3/3] Fix coding style. --- src/JsFunctionsScanner.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index a68aed6b..00310cc1 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -37,11 +37,11 @@ public function disableCommentsExtraction() { * {@inheritdoc} */ public function saveGettextFunctions( Translations $translations, array $options ) { - $code = $this->code; - // See https://github.com/mck89/peast/issues/7 + $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 ); + // 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,