wp-cli / i18n-command

Provides internationalization tools for WordPress projects.
MIT License
96 stars 52 forks source link

make-pot failing to parse specific JavaScript code #347

Closed markhowellsmead closed 1 year ago

markhowellsmead commented 1 year ago

Bug Report

Describe the current, buggy behavior

When running wp i18n make-pot . languages/shp_gantrisch_adb.pot on the command line in a site shell from the Local app on Mac OSX, a very long error message is generated. The initial message is Warning: Uncaught Error in exception handling during call to Error::__toString() in phar:///Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/wp-cli.phar/vendor/mck89/peast/lib/Peast/Syntax/Scanner.php on line 0

Describe how other contributors can replicate this bug Run wp i18n make-pot . languages/shp_gantrisch_adb.pot on the command line in a site shell from the Local app on Mac OSX.

Describe what you would expect as the correct outcome

The POT file be generated without any error messages.

Let us know what environment you are running this on

OS: Darwin 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 x86_64
Shell:  /bin/zsh
PHP binary: /Users/mark/Library/Application Support/Local/lightning-services/php-8.0.22+5/bin/darwin/bin/php
PHP version:    8.0.22
php.ini used:   /Users/mark/Library/Application Support/Local/run/_qwoyVNya/conf/php/php.ini
MySQL binary:   /Applications/Local.app/Contents/Resources/extraResources/lightning-services/mysql-8.0.16+6/bin/darwin/bin/mysql
MySQL version:  mysql  Ver 8.0.16 for macos10.14 on x86_64 (MySQL Community Server - GPL)
SQL modes:  ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
WP-CLI root dir:    phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:  phar://wp-cli.phar/vendor
WP_CLI phar path:   /Users/mark/Projects/fse/app/public/wp-content/plugins/shp_gantrisch_adb
WP-CLI packages dir:    
WP-CLI global config:   /Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/config.yaml
WP-CLI project config:  
WP-CLI version: 2.6.0
swissspidy commented 1 year ago

The environment does not seem relevant here. Typically an error with the Peast scanner indicates an issue with parsing JavaScript files, for which we'd have to file an issue upstream in the Peast repository.

Does the error still occur when you run wp i18n make-pot . languages/shp_gantrisch_adb.pot --skip-js? If not, then you'll need to find the offending JS file / function and ideally share it here for further debugging.

markhowellsmead commented 1 year ago

Thanks for the swift response @swissspidy. Excluding JS files stops the error from occurring. I'll look into that further.

markhowellsmead commented 1 year ago

I've tracked it down in one project to a piece of code where I'm using useState from the @wordpress/elements package. (The code is compiled to JS using this Webpack task.)

const [processing, setProcessing] = useState(null);

…Various code…

if(condition){
    setProcessing(true);
}

If I comment out this single instance of setProcessing(true); and recompile my JS, then the POT can be created. If not, then it throws the long error message.

swissspidy commented 1 year ago

Cool, then I recommend opening an issue at https://github.com/mck89/peast with this minimal code to reproduce so that it can be fixed there.

swissspidy commented 1 year ago

@markhowellsmead FWIW, that code is not it.

If I test with Peast myself:

<?php

require_once "vendor/autoload.php";

$source = <<<'JS'

const [processing, setProcessing] = useState(null);

// …Various code…

if(condition){
    setProcessing(true);
}

JS;

$ast = Peast\Peast::latest($source, $options)->parse(); //Parse it!

var_dump($ast);

This throws no error.

If the compiled code is throwing errors, you need to share the compiled code.

But again, I recommend opening an issue on the Peast repo yourself.

(The code is compiled to JS using this Webpack task.)

FWIW this repo is not public.

markhowellsmead commented 1 year ago

> But again, I recommend opening an issue on the Peast repo yourself.

I will as soon as I get time.

(The code is compiled to JS using this Webpack task.)

FWIW this repo is not public.

Sorry: it's public now.

markhowellsmead commented 1 year ago

Response is that they can't help with the bug. See https://github.com/mck89/peast/issues/54#issuecomment-1342648900.

swissspidy commented 1 year ago

The code you shared at https://github.com/mck89/peast/issues/54#issuecomment-1339532089 is not causing any errors for me either, so that response is understandable. Both Peast itself (using the approach shared above) and wp i18n make-pot parse that code without issues.

In other words, I cannot reproduce the issue with the code you shared.