Brief introduction:
I'm attempting to get at line number of function definition for parsing documentation comments on only public stuff. I've gotten to the point where I can find the name of the function and if I so wanted I could execute the function, but I can't seem to figure out any way to pull out line number information. Note: this is purely for documentation purposes so cross-browser isn't necessary.
I know firebug (but not firebug lite so I don't know how possible this is) shows you the line number and script location of a referenced function on mouseover. I looked through the firebug source and got as far as finding they call (domPanel.js:536
), but can't seem to find this "addMember" function anywhere in their source:
this.addMember(object, "userFunction", userFuncs, name, val, level, 0, context);
It may be that this just isn't possible to determine. My fallbacks are using [userfunction].name
and [userfunction].toSource()
and then attempt to match source to source. But I would like to avoid these if possible since the name could be non-unique and the toSource() gives the source post-processing. Maybe there is a way to tie into the firebug api?
Minimal Explanation Code:
[Note the goal would be to get this information: window.MyWindowObject.PublicFunction: script.js line 3
]
script.js
(function () {
function referencedFunction() {
///<summary>Sample XML Doc Comment</summary>
alert('well hello there!');
}
var publicObject = window.MyWindowObject || {};
publicObject.PublicFunction = referencedFunction;
window.MyWindowObject = publicObject;
}());
index.htm
<!DOCTYPE html>
<html>
<script src="script.js"></script>
</html>
EDIT: for anyone who finds this on a search later here is some other useful related info that I've found:
Stacktrace.js: https://github.com/eriwen/javascript-stacktrace - Pretty close but not quite what I want since it doesn't seem to get the final function's location. The example on their website is not correct (though the demo "looks" like what I wanted)
In chrome (and IE9): [userfunction].toString()
preserves comments (does not in firefox), which is what I will probably end up using. I was going to use firefox's [userfunction]
.toSource() but this looks like browser-manipulated source of the function. firefox's [userfunction]
.toString() appears to preserve code, but strips comments