From cbe2aaf89783ca9809ee8c6bfdc70592d12686f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20G=C3=BCnter?= Date: Sun, 1 Mar 2020 13:10:00 +0100 Subject: [PATCH] fix(make-pot): parses mangled webpack statements (closes #203) --- features/makepot.feature | 5 +++++ src/JsFunctionsScanner.php | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/features/makepot.feature b/features/makepot.feature index 6de1f3ec..47b6a39f 100644 --- a/features/makepot.feature +++ b/features/makepot.feature @@ -1592,6 +1592,7 @@ Feature: Generate a POT file of a WordPress project translate.__( 'translate.__', 'foo-plugin' ); Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__["__"])( 'webpack.__', 'foo-plugin' ); + Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__[/* __ */ "a"])( 'webpack.mangle.__', 'foo-plugin' ); Object(u.__)( 'minified.__', 'foo-plugin' ); Object(j._x)( 'minified._x', 'minified._x_context', 'foo-plugin' ); @@ -1660,6 +1661,10 @@ Feature: Generate a POT file of a WordPress project """ msgid "webpack.__" """ + And the foo-plugin/foo-plugin.pot file should contain: + """ + msgid "webpack.mangle.__" + """ And the foo-plugin/foo-plugin.pot file should contain: """ msgid "minified.__" diff --git a/src/JsFunctionsScanner.php b/src/JsFunctionsScanner.php index efd6629a..82b3822e 100644 --- a/src/JsFunctionsScanner.php +++ b/src/JsFunctionsScanner.php @@ -231,8 +231,17 @@ private function resolveExpressionCallee( Node\CallExpression $node ) { // Matches unminified webpack statements: // Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__["__"])( "translation" ); if ( 'Literal' === $property->getType() ) { + $name = $property->getValue(); + + // Matches mangled webpack statement: + // Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__[/* __ */ "a"])( "translation" ); + $leading_property_comments = $property->getLeadingComments(); + if ( count( $leading_property_comments ) === 1 && $leading_property_comments[0]->getKind() === 'multiline' ) { + $name = trim( $leading_property_comments[0]->getText() ); + } + return [ - 'name' => $property->getValue(), + 'name' => $name, 'comments' => $callee->getCallee()->getLeadingComments(), ]; }