Partial support for static class properties from ES Class Fields & Static Properties Proposal in Acorn - a tiny, fast JavaScript parser, written completely in JavaScript.
Made to support static property initializers in React components written as ES6+ classes:
class EntityState extends Component {
static propTypes = {
entityState: PropTypes.string.isRequired
};
render() {
return <div>{this.props.entityState}</div>;
}
}
You can use module directly in order to get Acorn instance with plugin installed:
var acorn = require('acorn-static-class-property-initializer');
Or you can use inject.js
to inject multiple plugins into Acorn:
var acorn = require('acorn');
var injectAcornJsx = require('acorn-jsx/inject');
var injectAcornStaticClassPropertyInitializer = require('acorn-static-class-property-initializer/inject');
injectAcornJsx(acorn);
injectAcornStaticClassPropertyInitializer(acorn);
Then, use the plugins
option whenever you need to support static class properties while parsing:
var ast = acorn.parse(code, {
plugins: {
jsx: true, // true to enable JSX plugin
staticClassPropertyInitializer: true // true to enable staticClassPropertyInitializer plugin
}
});