Created
August 27, 2015 03:16
-
-
Save Alxandr/6ec89369f6fd51332ee6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function abstract() { | |
return (cls, prop, descriptor) => { | |
if (prop === undefined) { | |
return class Abstract extends cls { | |
constructor(...args) { | |
super(...args); | |
if (this.constructor === Abstract) { | |
throw new Error('Class is abstract, and should not be instansiated.'); | |
} | |
} | |
}; | |
} | |
function abstract() { | |
throw new Error(`Method "${prop}" is abstract, and should have been overridden in a subclass.`); | |
} | |
if (descriptor.value) { | |
// this is a method | |
descriptor.value = abstract; | |
} else { | |
// property (getter and/or setter) | |
if (descriptor.get) { | |
descriptor.get = abstract; | |
} | |
if (descriptor.set) { | |
descriptor.set = abstract; | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@abstract() | |
class Abstract { | |
@abstract() | |
get foo() { | |
} | |
@abstract() | |
bar() { } | |
} | |
class Actual extends Abstract { | |
get foo() { | |
return 'foo'; | |
} | |
bar() { | |
return 'bar'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment