Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Allow findAllMatchingFunctionsInText to iterate over array-like objects (#1390) #2317

Merged
merged 3 commits into from
Dec 21, 2012
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/language/JSUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,24 @@ define(function (require, exports, module) {
var allFunctions = _findAllFunctionsInText(text);
var result = [];
var lines = text.split("\n");
var key;

$.each(allFunctions, function (index, functions) {
if (index === functionName || functionName === "*") {
functions.forEach(function (funcEntry) {
var endOffset = _getFunctionEndOffset(text, funcEntry.offsetStart);
result.push({
name: index,
lineStart: StringUtils.offsetToLineNum(lines, funcEntry.offsetStart),
lineEnd: StringUtils.offsetToLineNum(lines, endOffset)
});
});
function addFunctionEntry(funcEntry) {
var endOffset = _getFunctionEndOffset(text, funcEntry.offsetStart);
result.push({
name: key,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using key like that seems dangerous but JSLint complains about creating functions inside loops otherwise. Is there a better way to do it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating functions in a loop is certainly legal JavaScript, so I thought I'd Google to see what the Crockford-approved approach is. I was not let down

lineStart: StringUtils.offsetToLineNum(lines, funcEntry.offsetStart),
lineEnd: StringUtils.offsetToLineNum(lines, endOffset)
});
}

for (key in allFunctions) {
if (allFunctions.hasOwnProperty(key)) {
if (key === functionName || functionName === "*") {
allFunctions[key].forEach(addFunctionEntry);
}
}
});
}

return result;
}
Expand Down