Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract strings contained within eval() #222

Merged
merged 7 commits into from Mar 5, 2021
Merged

Conversation

@dd32
Copy link
Contributor

@dd32 dd32 commented Jul 24, 2020

Some build systems can result in the built files gettext functions being inside eval() calls.

One such example of this, is a webpack'd built file in this plugin: https://plugins.trac.wordpress.org/browser/mathml-block/trunk/dist/mathml-block.js?rev=2276811#L245

Currently, the relevant output is this:

#: src/mathml-block.js:35
msgid "MathML formula:"
msgstr ""

#: src/mathml-inline.js:10
msgid "MathML"
msgstr ""

which doesn't work for WordPress Translation files, as those src files are never enqueued, only the built files.
After the change, here's the output:

#: dist/mathml-block.js:245
#: src/mathml-block.js:35
msgid "MathML formula:"
msgstr ""

#: dist/mathml-block.js:257
#: src/mathml-inline.js:10
msgid "MathML"
msgstr ""
@dd32 dd32 requested a review from wp-cli/committers as a code owner Jul 24, 2020
@dd32
Copy link
Contributor Author

@dd32 dd32 commented Jul 25, 2020

The unit tests keep timing out on trunk only - I'm not sure why as I've not been able to run then locally.

@swissspidy
Copy link
Member

@swissspidy swissspidy commented Jul 30, 2020

Interesting edge case & nice solution for the line number!

Would you be able to add some Behat tests for this? Could probably just copy some lines from the MathML plugin and then verify that strings are extracted as expected with the correct line number.

@dd32
Copy link
Contributor Author

@dd32 dd32 commented Jul 31, 2020

I'll get some tests added @swissspidy - Hopefully I can figure out how to run the tests locally

@dd32
Copy link
Contributor Author

@dd32 dd32 commented Jul 31, 2020

Unfortunately I wasn't able to get tests running locally, and there are no existing tests for the JS set of extraction methods, so I can't really fly into it blind either.

@swissspidy
Copy link
Member

@swissspidy swissspidy commented Jul 31, 2020

There are no unit tests for it, but Behat tests, starting here:

Scenario: Extracts functions from JavaScript file
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
"""
<?php
/**
* Plugin Name: Foo Plugin
*/
"""
And a foo-plugin/foo-plugin.js file:
"""
// Included to test if peast correctly parses regexes containing a quote.
// See: https://github.com/wp-cli/i18n-command/issues/98
n = n.replace(/"/g, '&quot;');
n = n.replace(/"|'/g, '&quot;');
__( '__', 'foo-plugin' );
_x( '_x', '_x_context', 'foo-plugin' );
_n( '_n_single', '_n_plural', number, 'foo-plugin' );
_nx( '_nx_single', '_nx_plural', number, '_nx_context', 'foo-plugin' );
__( 'wrong-domain', 'wrong-domain' );

If it's not working, feel free to add me to your fork so I could amend, otherwise I can open a new PR with your commit in it and the tests.

@swissspidy
Copy link
Member

@swissspidy swissspidy commented Sep 19, 2020

@schlessera Any idea about the test timeouts? I can't re-start Travis in case it's just a flake.

@schlessera schlessera force-pushed the dd32:extractJSEval branch from 7083bcf to 1ebe57f Mar 5, 2021
@schlessera schlessera added this to the 2.2.7 milestone Mar 5, 2021
@schlessera schlessera merged commit 9cdde49 into wp-cli:master Mar 5, 2021
24 checks passed
24 checks passed
@github-actions
Lint PHP files
Details
@github-actions
Unit test / PHP 5.6
Details
@github-actions
Unit test / PHP 7.0
Details
@github-actions
Unit test / PHP 7.1
Details
@github-actions
Unit test / PHP 7.2
Details
@github-actions
Unit test / PHP 7.3
Details
@github-actions
Unit test / PHP 7.4
Details
@github-actions
Unit test / PHP 8.0
Details
@github-actions
PHPCS
Details
@github-actions
Functional - WP latest on PHP 5.6 with MySQL 8.0
Details
@github-actions
Functional - WP latest on PHP 7.0 with MySQL 8.0
Details
@github-actions
Functional - WP latest on PHP 7.1 with MySQL 8.0
Details
@github-actions
Functional - WP latest on PHP 7.2 with MySQL 8.0
Details
@github-actions
Functional - WP latest on PHP 7.3 with MySQL 8.0
Details
@github-actions
Functional - WP latest on PHP 7.4 with MySQL 8.0
Details
@github-actions
Functional - WP latest on PHP 8.0 with MySQL 8.0
Details
@github-actions
Functional - WP trunk on PHP 5.6 with MySQL 8.0
Details
@github-actions
Functional - WP trunk on PHP 5.6 with MySQL 5.7
Details
@github-actions
Functional - WP trunk on PHP 5.6 with MySQL 5.6
Details
@github-actions
Functional - WP trunk on PHP 7.4 with MySQL 8.0
Details
@github-actions
Functional - WP trunk on PHP 8.0 with MySQL 8.0
Details
@github-actions
Functional - WP trunk on PHP 8.0 with MySQL 5.7
Details
@github-actions
Functional - WP trunk on PHP 8.0 with MySQL 5.6
Details
@github-actions
Functional - WP 3.7 on PHP 5.6 with MySQL 5.6
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants