php-gettext/JS-Scanner

Wrong logic to get function name from js on difficult expressions.

DmitryKorol opened this issue · 1 comments

Hello, trying to parse big legacy mess of code for extracting translations.
But while this process getting errors on some difficult expressions, by example:

Fatal error: Uncaught Error: Call to undefined method Peast\Syntax\Node\MemberExpression::getName() in ./vendor/gettext/js-scanner/src/JsNodeVisitor.php:90

return $callee->getProperty()->getName();

Some broken examples
// occured: Peast\Syntax\Node\StringLiteral::getName()
// source: var REACT_ELEMENT_TYPE = typeof Symbol === 'function' && Symbol['for'] && Symbol['for']('react.element') || 0xeac7;
// problem: Symbol['for']('react.element');

// occured: Peast\Syntax\Node\BinaryExpression::getName()
// source: reset: function(e){var t=this.dimension();return this.$element.removeClass("collapse")[t](e||"auto")[0].offsetWidth,this.$element[e!==null?"addClass":"removeClass"]("collapse"),this}
// problem: reset = $element[e!==null?"addClass":"removeClass"]("collapse")

// occured: Peast\Syntax\Node\ConditionalExpression::getName()
// source: reset: function(e){var t=this.dimension();return this.$element.removeClass("collapse")[t](e||"auto")[0].offsetWidth,this.$element[e!==null?"addClass":"removeClass"]("collapse"),this}
// problem: this.$element[e!==null?"addClass":"removeClass"]("collapse")

That problem looks like global , because can be reproduced many times with many of code variations.

FYI: For fast fix i'm used this hack:

switch ($callee->getType()) {
    case 'Identifier':
        return $callee->getName();
    case 'MemberExpression':
        if (!method_exists($callee->getProperty(), 'getName')) {
            return null;
        }
        return $callee->getProperty()->getName();
    default:
        return null;
}

Thanks for let me know.
I just released the version 1.1.2 that fixes this.