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

Fix JS Quick Edit for projects that have a function named "hasOwnProperty" #3004

Merged
merged 5 commits into from
Mar 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
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