Skip to content

Commit

Permalink
Merge pull request #164 from michaelzangl/dynamic-import-workaround
Browse files Browse the repository at this point in the history
Workaround for parsing dynamic import.
  • Loading branch information
schlessera authored Jul 24, 2019
2 parents 4f49fe1 + af1a33b commit 70c2129
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
28 changes: 28 additions & 0 deletions features/makepot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,34 @@ Feature: Generate a POT file of a WordPress project
msgid "wrong-domain"
"""

Scenario: Ignores dynamic import in 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:
"""
// This should not trigger a compiler error
import('./some-file.js').then(a => console.log(a))
__( '__', 'foo-plugin' );
"""

When I run `wp i18n make-pot foo-plugin`
Then STDOUT should be:
"""
Plugin file detected.
Success: POT file successfully generated!
"""
And the foo-plugin/foo-plugin.pot file should exist
And the foo-plugin/foo-plugin.pot file should contain:
"""
msgid "__"
"""

Scenario: Parse .js and .jsx files for javascript translations
Given an empty foo-plugin directory
And a foo-plugin/foo-plugin.php file:
Expand Down
10 changes: 6 additions & 4 deletions src/JsFunctionsScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ public function disableCommentsExtraction() {
* {@inheritdoc}
*/
public function saveGettextFunctions( Translations $translations, array $options ) {
// Temporary workaround for https://github.com/wp-cli/i18n-command/issues/98.
$this->code = str_replace( '/"/', '/\"/', $this->code );
$this->code = str_replace( '/"|\'/', '/\"|\\\'/', $this->code );
$code = $this->code;
// See https://github.com/mck89/peast/issues/7
// Temporary workaround to fix dynamic imports. The τ is a greek letter.
// This will trick the parser into thinking that it is a normal method call.
$code = preg_replace( '/import(\\s*\\()/', 'imporτ$1', $code );

$peast_options = [
'sourceType' => Peast::SOURCE_TYPE_MODULE,
'comments' => false !== $this->extract_comments,
'jsx' => true,
];
$ast = Peast::latest( $this->code, $peast_options )->parse();
$ast = Peast::latest( $code, $peast_options )->parse();

$traverser = new Traverser();

Expand Down

0 comments on commit 70c2129

Please sign in to comment.