core-decorators.js
Library of ES7 decorators inspired by languages that come with built-ins like @override, @deprecated, etc, similar to pre-defined Annotations in Java.
The idea is these decorators would be used to ensure code sanity, but would be removed in production builds via a Babel plugin.
@override
Checks that the marked method indeed overrides a function with the same signature somewhere on the prototype chain.
Works with methods and getters/setters. Will ensure name, parameter count, as well as descriptor type (accessor/data). Provides a suggestion if it finds a method with a similar signature, including slight misspellings.
import { override } from 'core-decorators';
class Parent {
kickDog(first, second) {}
}
class Child extends Parent {
@override
kickDog() {}
}
class Child extends Parent {
@override
kickDogs() {}
}
@deprecate (alias: @deprecated)
Calls console.warn()
with a deprecation message. Provide a custom message to override the default one. You can also provide an options hash with a url
, for further reading.
import { deprecate } from 'core-decorators';
class Person {
@deprecate
kickDog() {}
@deprecate('We stopped animal abuse')
kickDogHard() {}
@deprecate('We stopped animal abuse', { url: 'http://humanesociety.org/issues/abuse_neglect/' })
kickDogHarder() {}
}
let person = new Person();
person.kickDog();
person.kickDogHard();
person.kickDogHarder();
@suppressWarnings
Suppresses any JavaScript console.warn()
call while the decorated function is called. (i.e. on the stack)
Will not suppress warnings triggered in any async code within.
import { suppressWarnings } from 'core-decorators';
class Person {
@deprecated
kickDog() {}
@suppressWarnings
kickDogWithoutWarning() {
this.kickDog();
}
}
let person = new Person();
person.kickDogWithoutWarning();
@memoize
Initial implementation included, likely slow. WIP.
Disclaimer
Please don't kick dogs. It's not nice.