Skip to content

Class methods should be non enumerable #1874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2015
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
35 changes: 21 additions & 14 deletions src/runtime/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,37 @@ function superSet(self, homeObject, name, value) {
throw $TypeError(`super has no setter '${name}'.`);
}

function forEachPropertyKey(object, f) {
getOwnPropertyNames(object).forEach(f);
getOwnPropertySymbols(object).forEach(f);
}

function getDescriptors(object) {
var descriptors = {};
var names = getOwnPropertyNames(object);
for (var i = 0; i < names.length; i++) {
var name = names[i];
descriptors[name] = $getOwnPropertyDescriptor(object, name);
}
var symbols = getOwnPropertySymbols(object);
for (var i = 0; i < symbols.length; i++) {
var symbol = symbols[i];
descriptors[$traceurRuntime.toProperty(symbol)] =
$getOwnPropertyDescriptor(object, $traceurRuntime.toProperty(symbol));
}
forEachPropertyKey(object, (key) => {
descriptors[key] = $getOwnPropertyDescriptor(object, key);
descriptors[key].enumerable = false;
});
return descriptors;
}

var nonEnum = {enumerable: false};

function makePropertiesNonEnumerable(object) {
forEachPropertyKey(object, (key) => {
$defineProperty(object, key, nonEnum);
});
}

// The next three functions are more or less identical to
// ClassDefinitionEvaluation in the ES6 draft.

function createClass(ctor, object, staticObject, superClass) {
$defineProperty(object, 'constructor', {
value: ctor,
configurable: true,
enumerable: false,
writable: true
configurable: true,
enumerable: false,
writable: true
});

if (arguments.length > 3) {
Expand All @@ -96,6 +102,7 @@ function createClass(ctor, object, staticObject, superClass) {
ctor.prototype = $create(getProtoParent(superClass),
getDescriptors(object));
} else {
makePropertiesNonEnumerable(object)
ctor.prototype = object;
}
$defineProperty(ctor, 'prototype', {configurable: false, writable: false});
Expand Down
4 changes: 2 additions & 2 deletions test/feature/Classes/Method.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var keys = [];
for (var key in universe) {
keys.push(key);
}
assert.isTrue(keys.indexOf('answer') !== -1);
assert.isTrue(keys.indexOf('constructor') === -1);
assert.equal(keys.indexOf('answer'), -1);
assert.equal(keys.indexOf('constructor'), -1);

for (var key in Universe) {
fail('Universe contains static member : ' + key);
Expand Down
23 changes: 23 additions & 0 deletions test/feature/Classes/NonEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class B {
a() {}
get b() {}
set c(v) {}
static d() {}
static get e() {}
static set f(v) {}
}

class D extends B {
g() {}
get h() {}
set i(v) {}
static j() {}
static get k() {}
static set l(v) {}
}

assert.equal(0, Object.keys(B).length);
assert.equal(0, Object.keys(B.prototype).length);

assert.equal(0, Object.keys(D).length);
assert.equal(0, Object.keys(D.prototype).length);
19 changes: 16 additions & 3 deletions test/feature/NumericLiteral/Simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@
static get 0O12() {}
static set 0O13(v) {}
}
assertArrayEquals(['0', '1', '2', '6', '7', '8'], Object.keys(C.prototype));
assertArrayEquals(['3', '4', '5', '9', '10', '11'], Object.keys(C));
})();

assert.isTrue(C.prototype.hasOwnProperty('0'));
assert.isTrue(C.prototype.hasOwnProperty('1'));
Copy link
Contributor

Choose a reason for hiding this comment

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

This change reduces coverage by allowing extra properties to sneak in. Maybe use gOPNs instead of O.keys?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had getOwnPropertyNames first but that includes arguments and caller which is wrong according to ES6.

assert.isTrue(C.prototype.hasOwnProperty('2'));
assert.isTrue(C.prototype.hasOwnProperty('6'));
assert.isTrue(C.prototype.hasOwnProperty('7'));
assert.isTrue(C.prototype.hasOwnProperty('8'));

assert.isTrue(C.hasOwnProperty('3'));
assert.isTrue(C.hasOwnProperty('4'));
assert.isTrue(C.hasOwnProperty('5'));
assert.isTrue(C.hasOwnProperty('9'));
assert.isTrue(C.hasOwnProperty('10'));
assert.isTrue(C.hasOwnProperty('11'));

})();