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

JS extractor chokes on IIFEs #122

Closed
lxg opened this issue Aug 28, 2016 · 2 comments
Closed

JS extractor chokes on IIFEs #122

lxg opened this issue Aug 28, 2016 · 2 comments

Comments

@lxg
Copy link
Contributor

lxg commented Aug 28, 2016

Considering the following test code:

(function(){
    var foo = ["string1", "string2", "string3"];
})();

This is something the JS parser should ignore. Instead it somehow tries to interpret the array members as function arguments, which causes an exception in vendor/gettext/gettext/src/Utils/FunctionsScanner.php in line 29 “Notice: Undefined offset: 1”.

What helps is to check if the function definition is complete before adding it to the list. For example, one could replace lines 129-131 of Utils/JsFunctionsScanner.php with something like

if (!empty($bufferFunctions)) {
    $function = array_shift($bufferFunctions);

    if (isset($function[0]) && isset($function[1]) && isset($function[2]))
        $functions[] = $function;
}

Though this doesn’t solve the underlying problem with the parser.

@oscarotero
Copy link
Member

Ok, I think its fixed. The problem was that $this->upStatus() must be called on close the function, in order to back to the previous status. You can see it here: https://github.com/oscarotero/Gettext/blob/master/src/Utils/JsFunctionsScanner.php#L133
Thanks for the useful feedback.

@lxg
Copy link
Contributor Author

lxg commented Aug 28, 2016

Yes, this seems to catch it. Running the parser on my relatively large JS code base works flawlessly and seems to catch all translations. Thanks again for the great support! :)

@lxg lxg closed this as completed Aug 28, 2016
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