babel: Bug: `super.*` should be allowed before `super()`

Input Code

"use strict";

class Cat {
    speak() {
        console.log("It speaks!");
    }
}

class Lion extends Cat {
    constructor() {
        setTimeout(() => {super.speak();}, 0);
        super();
    }
}

let lion = new Lion();

Expected Behavior

This code is really legal. On every JS engine that support ECMAScript 2015, it doesn’t throw error.

Current Behavior

Babel does an early syntax check saying super.* is not allowed before super().

Possible Solution

Suppress this early check. The possible error should be runtime, not compile time. If super.* is called before super(), the JS engine itself will throw a runtime error.

Context

The above example may be considered not very practical. Here’s a more practical example, though a bit longer:

"use strict";

class Cat {
    constructor(options) {
        let eventArg = {};
        setTimeout(() => {
            options.onWake(eventArg);
        }, 3000);
    }

    speak() {
        console.log("Cat speaks!");
    }
}

class Lion extends Cat {
    constructor() {
        let options = {
            onWake: e => {
                super.speak();
            }
        };
        super(options);
    }

    speak() {
        console.log("Lion speaks!");
    }
}

let lion = new Lion();

Your Environment

software version
Babel 5, 6
node 6
npm 4
Operating System macOS

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

This question maybe undecidable. Probably it should be a runtime error, and give warnings in compile time instead.