question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

does not work with babel-plugin-transform-builtin-extend

See original GitHub issue

I’ve got a source file, myError.js, being transpiled with babel-plugin-transform-builtin-extend:

export default class MyError extends Error {
    constructor() {
        super('this is a MyError');
    }
}
{
  "presets": [
    [ "es2015" ]
  ],
  "plugins": [
    [ "transform-builtin-extend", {
      "globals": [ "Error" ]
    } ]
  ]
}

the result is a file containing this:

/* _classCallCheck, _possibleConstructorReturn, etc. omitted for brevity... */

function _extendableBuiltin(cls) {
  /* some magic provided by transform-builtin-extend */
  // ...
}

var MyError = function (_extendableBuiltin2) {
    _inherits(MyError, _extendableBuiltin2);

    function MyError() {
        _classCallCheck(this, MyError);

        return _possibleConstructorReturn(this, (MyError.__proto__ || Object.getPrototypeOf(MyError)).call(this, "this is a MyError"));
    }

    return MyError;
}(_extendableBuiltin(Error));

But when I add istanbul to my list of plugins in .babelrc

  "plugins": [
    [ "transform-builtin-extend", {
      "globals": [ "Error" ]
-    } ]
+   } ],
+  [ "istanbul" ]
  ]

…some of the code generated by transform-builtin-extend seems to be missing:

/* _classCallCheck and friends are still present */

/* the '_extendableBuiltin' function is now gone :( */

var MyError = function (_ref) {
    _inherits(MyError, _ref);

    function MyError() {
        _classCallCheck(this, MyError);

        cov_1jzxyjd0v8.f[0]++;
        cov_1jzxyjd0v8.s[0]++;
        return _possibleConstructorReturn(this, (MyError.__proto__ || Object.getPrototypeOf(MyError)).call(this, "this is a MyError"));
    }

    return MyError;
}((Error)) // <-- no longer a function call, but instead an object reference

Applying istanbul without transform-builtin-extend results in the same output as described above; rearranging the order of my babel plugins does too.

I suspect the problem might be somewhere in istanbul-lib-instrument as that’s where the visitor enter and exit functions are built? But I’m not 100% sure.

I’m running node 8.9.3 with these packages:

 "dependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-plugin-istanbul": "^4.1.5",
    "babel-plugin-transform-builtin-extend": "^1.1.2",
    "babel-preset-es2015": "^6.24.1",
    "jest": "^22.0.3"
  }

thanks for any help you can provide!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:6
  • Comments:7

github_iconTop GitHub Comments

3reactions
kgregorycommented, Apr 18, 2018

There is a workaround for this issue.

Use babel-plugin-transform-builtin-extend, but for any subclasses extending built-ins like Error or Array, you need to explicitly define constructor and __proto__, as described by @xpl in this issue comment.

0reactions
tomaswitekcommented, Jun 19, 2019

Is there a workaround for babel 7? The workarounds mentioned above don’t seem to work 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

babel-plugin-transform-builtin-extend - npm
A plugin for Babel 6 supports extending from builtin types based on static analysis.. Latest version: 1.1.2, last published: 6 years ago.
Read more >
transform-builtin-extend seems does not working
@Bergi In Firefox and Chrome new class MyFormData extends FormData {} works fine. – Michał Perłakowski. Sep 18, 2016 at 12:24.
Read more >
babel/plugin-transform-classes - Babel.js
When extending a native class (e.g., class extends Array {} ), the super class needs to be wrapped. This is needed to workaround...
Read more >
babel-plugin-transform-builtin-extend - npm package - Snyk
Learn more about babel-plugin-transform-builtin-extend: package health score, ... not work on IE<=10 and any other browsers that don't support __proto__ .
Read more >
babel-plugin-transform-builtin-extend | Yarn - Package Manager
Babel Builtin Constructor extension plugin. This is a Babel 6 plugin to enable extending builtin types like "Error" and "Array" and such, which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found