0

Below is the minimal app which demonstrate the issue:

'use strict';

var _ = require('underscore');

class Model
{
    constructor(value) {
        this._value = value;
    }

    get value() {
        return this._value;
    }

    toJS() {
        return this.value;
    }
}

class ObjectModel extends Model
{
    static properties = {};

    constructor(value) {
        super(_.extend({}, new.target.properties, _.pick(value, _.keys(new.target.properties))));
    }
}

class PostModel extends ObjectModel
{
    static properties = {
        title: 'Hello'
        /* content: '<p>Lorem ipsum dolor sit amet</p>' */
    };
}

console.log(new PostModel({title: 'Nice day', aa: 11, bb: 22}).toJS());

It should produce {title: 'Nice day'}. Instead it even not compiles. I get this:

$ babel app.js
SyntaxError: app.js: 'this' is not allowed before super()

I understand why this was done for object properties. But I can't understand why this also was done for class variables.

In BabelJS 5 I used this trick which did the job:

class ObjectModel extends Model
{
    static properties = {};

    constructor(value) {
        if (0) { super(); }
        super(_.extend({}, this.constructor.properties, _.pick(value, _.keys(this.constructor.properties))));
    }
}

In version 6 it compiles, but when running it produces an error:

Uncaught TypeError: Cannot read property 'constructor' of undefined

Is there some way to get access to a class static variables before calling super? Using something like init() instead of a constructor is not an option. Maybe creating custom transformation plugin?

System details:

$ babel --version
6.2.0 (babel-core 6.2.1)

$ cat .babelrc
{
    "presets": ["es2015", "stage-1"]
}

$ babel-doctor

Babel Doctor
Running sanity checks on your system. This may take a few minutes...

✔ Found config at /path/to/.babelrc
✔ No duplicate babel packages found
✔ All babel packages appear to be up to date
✔ You're on npm >=3.3.0

Everything looks all right!
vbarbarosh
  • 3,502
  • 4
  • 33
  • 43
  • `new.target.properties`? – sjrd Nov 27 '15 at 17:29
  • Seems clearer to have the subclass override the constructor and pass in the properties to me, but yeah `new.target` is the way to do this, unfortunately it isn't transpiled by Babel. – loganfsmyth Nov 27 '15 at 18:19
  • I just check `new.target`. It is workable in Node 5.0 only. Chrome and FireFox show me errors. Also, BabelJS does not converts `new.target` into something ES5. I used `es2015` preset. – vbarbarosh Nov 28 '15 at 12:10

1 Answers1

0

The solution was following:

  1. Stick to the new.target as suggested by @sjrd and @loganfsmyth:

    class ObjectModel extends Model
    {
        static properties = {};
    
        constructor(value) {
            super(_.extend({}, new.target.properties, _.pick(value, _.keys(new.target.properties))));
        }
    }
    
  2. Create a transpiler which converts all of new.target (ES6) into this.constructor (ES5):

    function transpileNewTarget()
    {
        return {
            visitor: {
                MetaProperty(path) {
                    if (path.isMetaProperty() && path.node.meta.name == 'new' && path.node.property.name == 'target') {
                        path.replaceWithSourceString('this.constructor');
                    }
                }
            }
        };
    }
    
Community
  • 1
  • 1
vbarbarosh
  • 3,502
  • 4
  • 33
  • 43