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

Commit

Permalink
Merge pull request #3004 from adobe/glenn/issue-3002
Browse files Browse the repository at this point in the history
Fix JS Quick Edit for projects that have a function named "hasOwnProperty"
  • Loading branch information
peterflynn committed Mar 28, 2013
2 parents f4af422 + 9337142 commit 0a2d421
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/language/JSUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ define(function (require, exports, module) {
rangeResults = [];

docEntries.forEach(function (docEntry) {
if (docEntry.functions.hasOwnProperty(functionName)) {
// Need to call CollectionUtils.hasProperty here since docEntry.functions could
// have an entry for "hasOwnProperty", which results in an error if trying to
// invoke docEntry.functions.hasOwnProperty().
if (CollectionUtils.hasProperty(docEntry.functions, functionName)) {
var functionsInDocument = docEntry.functions[functionName];
matchedDocuments.push({doc: docEntry.doc, fileInfo: docEntry.fileInfo, functions: functionsInDocument});
}
Expand Down
14 changes: 14 additions & 0 deletions src/utils/CollectionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ define(function (require, exports, module) {
}
}

/**
* Returns true if the object has the specified property.
* This calls the Object.prototype.hasOwnProperty function directly, rather than
* depending on the object having a function named "hasOwnProperty". This way the
* object *can* have a property named "hasOwnProperty" that is not a function.
* @param {*} object The object to test
* @param {string} property The name of the property to query
* @return {boolean} True if the object contains the property
*/
function hasProperty(object, property) {
return Object.prototype.hasOwnProperty.apply(object, [property]);
}

// Define public API
exports.indexOf = indexOf;
exports.forEach = forEach;
exports.hasProperty = hasProperty;
});
2 changes: 1 addition & 1 deletion src/utils/StringMatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ define(function (require, exports, module) {
}

// Load up the cached specials information (or build it if this is our first time through).
var special = this._specialsCache.hasOwnProperty(str) ? this._specialsCache[str] : undefined;
var special = CollectionUtils.hasProperty(this._specialsCache, str) ? this._specialsCache[str] : undefined;
if (special === undefined) {
special = findSpecialCharacters(str);
this._specialsCache[str] = special;
Expand Down
5 changes: 5 additions & 0 deletions test/spec/JSUtils-test-files/tricky.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ function toString() {
function length() {
return 0;
}

// This function's name collides with an Object.prototype member
function hasOwnProperty() {
return false;
}
10 changes: 10 additions & 0 deletions test/spec/JSUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ define(function (require, exports, module) {
runs(function () {
expectFunctionRanges(this, this.fileJsContent, "toString", [ {start: 1, end: 3} ]);
expectFunctionRanges(this, this.fileJsContent, "length", [ {start: 6, end: 8} ]);
expectFunctionRanges(this, this.fileJsContent, "hasOwnProperty", [ {start: 11, end: 13} ]);
});
});

Expand Down Expand Up @@ -462,6 +463,15 @@ define(function (require, exports, module) {
expect(functions[0].lineStart).toBe(6);
expect(functions[0].lineEnd).toBe(8);
});

indexAndFind(function (fileInfos) {
return JSUtils.findMatchingFunctions("hasOwnProperty", fileInfos);
});
runs(function () {
expect(functions.length).toBe(1);
expect(functions[0].lineStart).toBe(11);
expect(functions[0].lineEnd).toBe(13);
});
});
});

Expand Down
4 changes: 4 additions & 0 deletions test/spec/StringMatch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ define(function (require, exports, module) {
// Array.prototype has length
var lengthResult = matcher.match("length", "l");
expect(lengthResult).toBeTruthy();

// Object.prototype has hasOwnProperty
var hasOwnPropertyResult = matcher.match("hasOwnProperty", "h");
expect(hasOwnPropertyResult).toBeTruthy();
});
});
});
Expand Down

0 comments on commit 0a2d421

Please sign in to comment.