From 394b4981bf48e41966c31ab8b997e20e80954e43 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 14 Jun 2011 19:44:11 -0400 Subject: [PATCH] Can no longer override getters/setters with methods --- lib/member_builder.js | 8 ++++ test/test-member_builder-method.js | 72 ++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/lib/member_builder.js b/lib/member_builder.js index 5b8571f..a3c6225 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -153,6 +153,14 @@ function validateMethod( keywords, prev_data, value, name ) ); } + // do not allow overriding getters/setters + if ( prev_data && ( prev_data.get || prev_data.set ) ) + { + throw TypeError( + "Cannot override getter/setter '" + name + "' with method" + ); + } + // search for any previous instances of this member if ( prev ) { diff --git a/test/test-member_builder-method.js b/test/test-member_builder-method.js index e6f7775..e092c69 100644 --- a/test/test-member_builder-method.js +++ b/test/test-member_builder-method.js @@ -291,3 +291,75 @@ mb_common.assertCommon(); }, TypeError, "Cannot declare private abstract method" ); } )(); + +/** + * While getters are technically methods, it doesn't make sense to override + * getters/setters with methods because they are fundamentally different. + */ +( function testCannotOverrideGetters() +{ + mb_common.members[ 'public' ] = {}; + Object.defineProperty( mb_common.members[ 'public' ], mb_common.name, { + get: function() {}, + } ); + + try + { + mb_common.value = function() {}; + mb_common.buildMemberQuick( {}, true ); + } + catch ( e ) + { + assert.ok( e.message.search( mb_common.name ) !== -1, + "Method override getter failure should contain method name" + ); + + // ensure we have the correct error + assert.ok( e.message.search( 'getter' ) !== -1, + "Proper error is thrown for getter override failure" + ); + + return; + } + + assert.fail( + "Should not be permitted to override getters with methods" + ); +} )(); + + +/** + * While setters are technically methods, it doesn't make sense to override + * getters/setters with methods because they are fundamentally different. + */ +( function testCannotOverrideSetters() +{ + mb_common.members[ 'public' ] = {}; + Object.defineProperty( mb_common.members[ 'public' ], mb_common.name, { + set: function() {}, + } ); + + try + { + mb_common.value = function() {}; + mb_common.buildMemberQuick( {}, true ); + } + catch ( e ) + { + assert.ok( e.message.search( mb_common.name ) !== -1, + "Method override setter failure should contain method name" + ); + + // ensure we have the correct error + assert.ok( e.message.search( 'setter' ) !== -1, + "Proper error is thrown for setter override failure" + ); + + return; + } + + assert.fail( + "Should not be permitted to override setters with methods" + ); +} )(); +