Skip to content

Commit

Permalink
Warn when a constructor is supplied as a type definition (likely shou…
Browse files Browse the repository at this point in the history
…ld be a Type) (#205)
  • Loading branch information
bmomberger-bitovi committed Aug 30, 2017
1 parent cae36ab commit 1448610
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions can-define.js
Expand Up @@ -182,6 +182,17 @@ define.property = function(objPrototype, prop, definition, dataInitializers, com

var type = definition.type;

//!steal-remove-start
if (type && canReflect.isConstructorLike(type)) {
dev.warn(
"can-define: the definition for " +
prop +
(objPrototype.constructor.shortName ? " on " + objPrototype.constructor.shortName : "") +
" uses a constructor for \"type\". Did you mean \"Type\"?"
);
}
//!steal-remove-end

// Special case definitions that have only `type: "*"`.
if (type && onlyType(definition) && type === define.types["*"]) {
Object.defineProperty(objPrototype, prop, {
Expand Down
39 changes: 39 additions & 0 deletions define-test.js
Expand Up @@ -5,6 +5,7 @@ var CanList = require("can-define/list/list");
var canBatch = require("can-event/batch/batch");
var each = require("can-util/js/each/each");
var canSymbol = require("can-symbol");
var canDev = require("can-util/js/dev/dev");

QUnit.module("can-define");

Expand Down Expand Up @@ -1368,3 +1369,41 @@ QUnit.test('define() should add a CID (#246)', function() {
var g = new Greeting();
QUnit.ok(g._cid, "should have a CID property");
});

QUnit.test("warn on using a Constructor for small-t type definintions", function() {
expect(2);
var oldWarn = canDev.warn;
canDev.warn = function(mesg) {
QUnit.equal(mesg, "can-define: the definition for currency uses a constructor for \"type\". Did you mean \"Type\"?");
};

function Currency() {
return this;
}
Currency.prototype = {
symbol: "USD"
};

function VM() {}
define(VM.prototype, {
currency: {
type: Currency, // should be `Type: Currency`
value: new Currency({})
}
});

canDev.warn = function(mesg) {
QUnit.equal(mesg, "can-define: the definition for currency on VM2 uses a constructor for \"type\". Did you mean \"Type\"?");
};

function VM2() {}
VM2.shortName = "VM2";
define(VM2.prototype, {
currency: {
type: Currency, // should be `Type: Currency`
value: new Currency({})
}
});

canDev.warn = oldWarn;
});

0 comments on commit 1448610

Please sign in to comment.