Skip to content

Commit

Permalink
Fix duplicate check in registerPropertyType (#5475)
Browse files Browse the repository at this point in the history
Co-authored-by: Noeri Huisman <[email protected]>
  • Loading branch information
mrxz and mrxz authored Feb 27, 2024
1 parent 374ae6c commit 233a29c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/core/propertyTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ registerPropertyType('vec4', {x: 0, y: 0, z: 0, w: 1}, vecParse, coordinates.str
* @param {function} [stringify=defaultStringify] - Stringify to DOM function.
*/
function registerPropertyType (type, defaultValue, parse, stringify) {
if ('type' in propertyTypes) {
error('Property type ' + type + ' is already registered.');
return;
if (type in propertyTypes) {
throw new Error('Property type ' + type + ' is already registered.');
}

propertyTypes[type] = {
Expand Down
9 changes: 9 additions & 0 deletions tests/core/propertyTypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ suite('propertyTypes', function () {
assert.ok('mytype' in propertyTypes);
assert.equal(propertyTypes.mytype.default, 5);
});

test('rejects duplicate type names', function () {
assert.notOk('duplicate' in propertyTypes);
register('duplicate', 'first');
assert.equal(propertyTypes.duplicate.default, 'first');
assert.throws(function () {
register('duplicate', 'second');
}, 'Property type duplicate is already registered.');
});
});

suite('int', function () {
Expand Down
3 changes: 3 additions & 0 deletions tests/core/schema.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global assert, suite, test */
var Schema = require('core/schema');
var propertyTypes = require('core/propertyTypes').propertyTypes;
var registerPropertyType = require('core/propertyTypes').registerPropertyType;

var isSingleProperty = Schema.isSingleProperty;
Expand Down Expand Up @@ -160,6 +161,7 @@ suite('schema', function () {
});

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

suite('processSchema', function () {
test('processes all property definitions', function () {
delete propertyTypes.faketype;
registerPropertyType('faketype', 'FAKEDEFAULT');

var schema = processSchema({
Expand Down

0 comments on commit 233a29c

Please sign in to comment.