Skip to content

Commit

Permalink
Fix JS string extraction with unsupported argument type
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Dec 17, 2021
1 parent 280e6c6 commit 7c6b6fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,8 @@ Feature: Generate a POT file of a WordPress project
(0, _i18n._x)( 'babel-i18n._x', 'babel-i18n._x_context', 'foo-plugin' );
eval( "__( 'Hello Eval World', 'foo-plugin' );" );
__( `This is a ${ bug }`, 'foo-plugin' );
"""

When I run `wp i18n make-pot foo-plugin`
Expand Down Expand Up @@ -1908,6 +1910,10 @@ Feature: Generate a POT file of a WordPress project
"""
msgid "wrong-domain"
"""
And the foo-plugin/foo-plugin.pot file should not contain:
"""
msgid "foo-plugin"
"""

Scenario: Ignores dynamic import in JavaScript file.
Given an empty foo-plugin directory
Expand Down Expand Up @@ -2444,6 +2450,8 @@ Feature: Generate a POT file of a WordPress project
__( 'wrong-domain', 'wrong-domain' );
__( 'Hello JS' );
__( `This is a ${ bug }`, 'do-not-import-me' );
"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot --domain=bar --ignore-domain`
Expand Down Expand Up @@ -2477,6 +2485,10 @@ Feature: Generate a POT file of a WordPress project
"""
msgid "Hello JS"
"""
And the foo-plugin.pot file should not contain:
"""
msgid "do-not-import-me"
"""

Scenario: Associates comments with the right source string
Given an empty foo-plugin directory
Expand Down
7 changes: 7 additions & 0 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
'Expression' === substr( $argument->getType(), -strlen( 'Expression' ) )
) {
$args[] = ''; // The value doesn't matter as it's unused.
continue;
}

if ( 'Literal' === $argument->getType() ) {
/** @var Node\Literal $argument */
$args[] = $argument->getValue();
continue;
}

if ( 'TemplateLiteral' === $argument->getType() && 0 === count( $argument->getExpressions() ) ) {
Expand All @@ -132,7 +134,12 @@ function ( $node ) use ( &$translations, $options, &$all_comments ) {
// Since there are no expressions within the TemplateLiteral, there is only one TemplateElement.
$parts = $argument->getParts();
$args[] = $parts[0]->getValue();
continue;
}

// If we reach this, an unsupported argument type has been encountered.
// Do not try to parse this function call at all.
return;
}

switch ( $functions[ $callee['name'] ] ) {
Expand Down

0 comments on commit 7c6b6fd

Please sign in to comment.