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

Commit 0a2d421

Browse files
committed
Merge pull request #3004 from adobe/glenn/issue-3002
Fix JS Quick Edit for projects that have a function named "hasOwnProperty"
2 parents f4af422 + 9337142 commit 0a2d421

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

src/language/JSUtils.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ define(function (require, exports, module) {
262262
rangeResults = [];
263263

264264
docEntries.forEach(function (docEntry) {
265-
if (docEntry.functions.hasOwnProperty(functionName)) {
265+
// Need to call CollectionUtils.hasProperty here since docEntry.functions could
266+
// have an entry for "hasOwnProperty", which results in an error if trying to
267+
// invoke docEntry.functions.hasOwnProperty().
268+
if (CollectionUtils.hasProperty(docEntry.functions, functionName)) {
266269
var functionsInDocument = docEntry.functions[functionName];
267270
matchedDocuments.push({doc: docEntry.doc, fileInfo: docEntry.fileInfo, functions: functionsInDocument});
268271
}

src/utils/CollectionUtils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,21 @@ define(function (require, exports, module) {
6262
}
6363
}
6464

65+
/**
66+
* Returns true if the object has the specified property.
67+
* This calls the Object.prototype.hasOwnProperty function directly, rather than
68+
* depending on the object having a function named "hasOwnProperty". This way the
69+
* object *can* have a property named "hasOwnProperty" that is not a function.
70+
* @param {*} object The object to test
71+
* @param {string} property The name of the property to query
72+
* @return {boolean} True if the object contains the property
73+
*/
74+
function hasProperty(object, property) {
75+
return Object.prototype.hasOwnProperty.apply(object, [property]);
76+
}
77+
6578
// Define public API
6679
exports.indexOf = indexOf;
6780
exports.forEach = forEach;
81+
exports.hasProperty = hasProperty;
6882
});

src/utils/StringMatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ define(function (require, exports, module) {
805805
}
806806

807807
// Load up the cached specials information (or build it if this is our first time through).
808-
var special = this._specialsCache.hasOwnProperty(str) ? this._specialsCache[str] : undefined;
808+
var special = CollectionUtils.hasProperty(this._specialsCache, str) ? this._specialsCache[str] : undefined;
809809
if (special === undefined) {
810810
special = findSpecialCharacters(str);
811811
this._specialsCache[str] = special;

test/spec/JSUtils-test-files/tricky.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ function toString() {
77
function length() {
88
return 0;
99
}
10+
11+
// This function's name collides with an Object.prototype member
12+
function hasOwnProperty() {
13+
return false;
14+
}

test/spec/JSUtils-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ define(function (require, exports, module) {
260260
runs(function () {
261261
expectFunctionRanges(this, this.fileJsContent, "toString", [ {start: 1, end: 3} ]);
262262
expectFunctionRanges(this, this.fileJsContent, "length", [ {start: 6, end: 8} ]);
263+
expectFunctionRanges(this, this.fileJsContent, "hasOwnProperty", [ {start: 11, end: 13} ]);
263264
});
264265
});
265266

@@ -462,6 +463,15 @@ define(function (require, exports, module) {
462463
expect(functions[0].lineStart).toBe(6);
463464
expect(functions[0].lineEnd).toBe(8);
464465
});
466+
467+
indexAndFind(function (fileInfos) {
468+
return JSUtils.findMatchingFunctions("hasOwnProperty", fileInfos);
469+
});
470+
runs(function () {
471+
expect(functions.length).toBe(1);
472+
expect(functions[0].lineStart).toBe(11);
473+
expect(functions[0].lineEnd).toBe(13);
474+
});
465475
});
466476
});
467477

test/spec/StringMatch-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ define(function (require, exports, module) {
641641
// Array.prototype has length
642642
var lengthResult = matcher.match("length", "l");
643643
expect(lengthResult).toBeTruthy();
644+
645+
// Object.prototype has hasOwnProperty
646+
var hasOwnPropertyResult = matcher.match("hasOwnProperty", "h");
647+
expect(hasOwnPropertyResult).toBeTruthy();
644648
});
645649
});
646650
});

0 commit comments

Comments
 (0)