Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,11 @@ Feature: Generate a POT file of a WordPress project
Object(u.__)( 'minified.__', 'foo-plugin' );
Object(j._x)( 'minified._x', 'minified._x_context', 'foo-plugin' );

/* translators: babel */
Comment thread
swissspidy marked this conversation as resolved.
(0, __)( 'babel.__', 'foo-plugin' );
(0, _i18n.__)( 'babel-i18n.__', 'foo-plugin' );
(0, _i18n._x)( 'babel-i18n._x', 'babel-i18n._x_context', 'foo-plugin' );

eval( "__( 'Hello Eval World', 'foo-plugin' );" );
"""

Expand Down Expand Up @@ -1698,6 +1703,26 @@ Feature: Generate a POT file of a WordPress project
"""
msgctxt "minified._x_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
#. translators: babel
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "babel.__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "babel-i18n.__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "babel-i18n._x"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgctxt "babel-i18n._x_context"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "Hello Eval World"
Expand Down
30 changes: 30 additions & 0 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,36 @@ private function resolveExpressionCallee( Node\CallExpression $node ) {
}
}

// If the callee is an indirect function call as created by babel, resolve it.
// For example: `(0, u.__)( "translation" )`.
if (
'ParenthesizedExpression' === $callee->getType()
&& 'SequenceExpression' === $callee->getExpression()->getType()
&& 2 === count( $callee->getExpression()->getExpressions() )
&& 'Literal' === $callee->getExpression()->getExpressions()[0]->getType()
&& [] !== $node->getArguments()
) {
// Matches any general indirect function call: `(0, __)( "translation" )`.
if ( 'Identifier' === $callee->getExpression()->getExpressions()[1]->getType() ) {
return [
'name' => $callee->getExpression()->getExpressions()[1]->getName(),
'comments' => $callee->getLeadingComments(),
];
}

// Matches indirect function calls used by babel for module imports: `(0, _i18n.__)( "translation" )`.
if ( 'MemberExpression' === $callee->getExpression()->getExpressions()[1]->getType() ) {
$property = $callee->getExpression()->getExpressions()[1]->getProperty();

if ( 'Identifier' === $property->getType() ) {
return [
'name' => $property->getName(),
'comments' => $callee->getLeadingComments(),
];
}
}
}

// Unknown format.
return false;
}
Expand Down