Skip to content

Commit

Permalink
safeguard against duplicate on/using calls
Browse files Browse the repository at this point in the history
  • Loading branch information
brigand committed Mar 11, 2015
1 parent 19edb02 commit 42afdb5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions es6-traits.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,31 @@ export default function({
}
});

let onCalled = false;
let usingCalled = false;
const throwDuplicateCallError = (name) => {
throw new RangeError(`traits().${name} may only be called once. Create a new on/using pair for each class`);
};

return {
on(baseclass) {
if (onCalled) {
throwDuplicateCallError('on');
} else {
onCalled = true;
}


BaseClass = baseclass;
},

using(...traits) {
if (usingCalled) {
throwDuplicateCallError('using');
} else {
usingCalled = true;
}

const traitsClassName = `${BaseClass.name}_with_${traits.map(x => x.toString().slice(8, -1)).join('_and_').replace(' ', '_')}`;

if (cache[traitsClassName]) {
Expand Down

0 comments on commit 42afdb5

Please sign in to comment.