Skip to content

Commit 233a29c

Browse files
authored
Fix duplicate check in registerPropertyType (#5475)
Co-authored-by: Noeri Huisman <[email protected]>
1 parent 374ae6c commit 233a29c

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/core/propertyTypes.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ registerPropertyType('vec4', {x: 0, y: 0, z: 0, w: 1}, vecParse, coordinates.str
3838
* @param {function} [stringify=defaultStringify] - Stringify to DOM function.
3939
*/
4040
function registerPropertyType (type, defaultValue, parse, stringify) {
41-
if ('type' in propertyTypes) {
42-
error('Property type ' + type + ' is already registered.');
43-
return;
41+
if (type in propertyTypes) {
42+
throw new Error('Property type ' + type + ' is already registered.');
4443
}
4544

4645
propertyTypes[type] = {

tests/core/propertyTypes.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ suite('propertyTypes', function () {
8181
assert.ok('mytype' in propertyTypes);
8282
assert.equal(propertyTypes.mytype.default, 5);
8383
});
84+
85+
test('rejects duplicate type names', function () {
86+
assert.notOk('duplicate' in propertyTypes);
87+
register('duplicate', 'first');
88+
assert.equal(propertyTypes.duplicate.default, 'first');
89+
assert.throws(function () {
90+
register('duplicate', 'second');
91+
}, 'Property type duplicate is already registered.');
92+
});
8493
});
8594

8695
suite('int', function () {

tests/core/schema.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* global assert, suite, test */
22
var Schema = require('core/schema');
3+
var propertyTypes = require('core/propertyTypes').propertyTypes;
34
var registerPropertyType = require('core/propertyTypes').registerPropertyType;
45

56
var isSingleProperty = Schema.isSingleProperty;
@@ -160,6 +161,7 @@ suite('schema', function () {
160161
});
161162

162163
test('sets default value if not defined', function () {
164+
delete propertyTypes.faketype;
163165
registerPropertyType('faketype', 'FAKEDEFAULT');
164166
var definition = processSchema({type: 'faketype'});
165167
assert.equal(definition.default, 'FAKEDEFAULT');
@@ -181,6 +183,7 @@ suite('schema', function () {
181183

182184
suite('processSchema', function () {
183185
test('processes all property definitions', function () {
186+
delete propertyTypes.faketype;
184187
registerPropertyType('faketype', 'FAKEDEFAULT');
185188

186189
var schema = processSchema({

0 commit comments

Comments
 (0)