Skip to content

Commit

Permalink
Create unit tests and extract callee resolving to a seperate function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Herre Groen committed Oct 25, 2018
1 parent fcfba1e commit 51ed9fb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 17 deletions.
19 changes: 19 additions & 0 deletions features/makepot.feature
Expand Up @@ -1429,6 +1429,13 @@ Feature: Generate a POT file of a WordPress project
<h1>{__( 'Hello JSX', 'foo-plugin' )}</h1>,
document.getElementById('root')
);
wp.i18n.__( 'wp.i18n.__', 'foo-plugin' );
const translate = wp.i18n;
translate.__( 'translate.__', 'foo-plugin' );
Object(u.__)( 'minified.__', 'foo-plugin' );
"""

When I run `wp i18n make-pot foo-plugin`
Expand Down Expand Up @@ -1474,6 +1481,18 @@ Feature: Generate a POT file of a WordPress project
"""
msgid "Hello JSX"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "wp.i18n.__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "translate.__"
"""
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "minified.__"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "wrong-domain"
Expand Down
58 changes: 41 additions & 17 deletions src/JsFunctionsScanner.php
Expand Up @@ -68,23 +68,7 @@ public function saveGettextFunctions( Translations $translations, array $options
return;
}

if ( 'Identifier' === $node->getCallee()->getType() ) {
$callee = $node->getCallee();
} else if (
'CallExpression' === $node->getCallee()->getType() &&
'Identifier' === $node->getCallee()->getCallee()->getType() &&
'Object' === $node->getCallee()->getCallee()->getName() &&
! empty( $node->getCallee()->getArguments() ) &&
'MemberExpression' === $node->getCallee()->getArguments()[0]->getType() &&
'Identifier' === $node->getCallee()->getArguments()[0]->getProperty()->getType()
) {
$callee = $node->getCallee()->getArguments()[0]->getProperty();
} else if (
'MemberExpression' === $node->getCallee()->getType() &&
'Identifier' === $node->getCallee()->getProperty()->getType()
) {
$callee = $node->getCallee()->getProperty();
}
$callee = $this->resolveExpressionCallee( $node );

if ( ! $callee || ! isset( $functions[ $callee->getName() ] ) ) {
return;
Expand Down Expand Up @@ -168,4 +152,44 @@ public function saveGettextFunctions( Translations $translations, array $options

$traverser->traverse( $ast );
}

/**
* Resolve the callee of a call expression using known formats.
*
* @param Node\CallExpression $node The call expression whose callee to resolve.
*
* @return Node\Identifier|bool The identifier if resolved. False if not.
*/
private function resolveExpressionCallee( Node\CallExpression $node ) {
// If the callee is a simple identifier it can simply be returned.
// For example: __( "translation" ).
if ( 'Identifier' === $node->getCallee()->getType() ) {
return $node->getCallee();
}

// If the callee is a member expression resolve it to the property.
// For example: wp.i18n.__( "translation" ) or u.__( "translation" ).
if (
'MemberExpression' === $node->getCallee()->getType() &&
'Identifier' === $node->getCallee()->getProperty()->getType()
) {
return $node->getCallee()->getProperty();
}

// If the callee is a call expression as created by WebPack resolve it.
// For example: Object(u.__)( "translation" ).
if (
'CallExpression' === $node->getCallee()->getType() &&
'Identifier' === $node->getCallee()->getCallee()->getType() &&
'Object' === $node->getCallee()->getCallee()->getName() &&
! empty( $node->getCallee()->getArguments() ) &&
'MemberExpression' === $node->getCallee()->getArguments()[0]->getType() &&
'Identifier' === $node->getCallee()->getArguments()[0]->getProperty()->getType()
) {
return $node->getCallee()->getArguments()[0]->getProperty();
}

// Unknown format.
return false;
}
}

0 comments on commit 51ed9fb

Please sign in to comment.