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

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

Closed
DmitryKorol opened this issue Feb 14, 2022 · 1 comment
Closed

Comments

@DmitryKorol
Copy link

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;
}
oscarotero added a commit that referenced this issue Feb 14, 2022
@oscarotero
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants