Review Request 117516: Recognize the type of function expressions

Denis Steckelmacher steckdenis at yahoo.fr
Sat Apr 12 18:35:29 UTC 2014


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://git.reviewboard.kde.org/r/117516/
-----------------------------------------------------------

Review request for KDevelop.


Repository: kdev-qmljs


Description
-------

In Javascript, functions can be declared on their own (using "function foo() {}") or be anonymous and declared in an expression ("var f = function(){}", and also "setTimeout(function() { ... })"). This latter form is very common in Javascript, as callbacks are widely used.

ContextBuilder already supports function expressions (it correctly builds contexts for them and for their arguments). This patch adds support for function expressions in DeclarationBuilder and ExpressionVisitor. DeclarationBuilder builds anonymous declarations for the functions, and ExpressionVisitor uses the fact that anonymous functions open a context of which they are the owner in order to find their type.

When a function is declared, its return type is left to NULL. It is set to void only when DeclarationBuilder ends visiting a function expression or declaration. I do this because the next patch will add support for unsure types, and every return statement will add its type to the return type of the function, so this return type needs to be empty at the beginning of the function. Having all the functions returning "void or int" or "void or string" is useless.

This patch also fixes a small bug in return statements: "return;" was setting the return type of the function to "mixed" instead of "void", because ContextBuilder::findType returns mixed if the node passed as argument is NULL.


Diffs
-----

  duchain/declarationbuilder.h 8d8f771 
  duchain/declarationbuilder.cpp 1d5610e 
  duchain/expressionvisitor.h 4e8ac29 
  duchain/expressionvisitor.cpp 418f1a9 
  tests/files/helloworld.js df86d4f 

Diff: https://git.reviewboard.kde.org/r/117516/diff/


Testing
-------

This patch adds various tests for function expressions. Here is an example of the expressions recognized:

var f = function() { return 2; };        // function int ()
var g = function(a, b) { return a + b; } // function mixed (mixed, mixed)
var a = (function() { return true; })(); // bool, the function has been called


Thanks,

Denis Steckelmacher

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kdevelop-devel/attachments/20140412/2db36540/attachment.html>


More information about the KDevelop-devel mailing list