Skip to content
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

Fix duplicate check in registerPropertyType #5475

Merged
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
Fix duplicate check in registerPropertyType
  • Loading branch information
mrxz committed Feb 27, 2024
commit d5e12490f8201f60b9163bc1a9e452d73d427839
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
Loading