loganfsmyth / babel-plugin-transform-builtin-extend

A plugin for Babel 6 supports extending from builtin types based on static analysis.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

instanceof doesn't work

stevenvachon opened this issue · comments

This should be mentioned under "Limitations" in the readme. The work-around is to compare with the name property instead.

Got an example of your configuration? It should work.

export default class ValidationError extends Error {
  constructor(invalidations) {
    super('Invalid input');
    this.invalidations = invalidations;
    this.name = 'ValidationError';
  }
}
try {
  throw new ValidationError(['message']);
} catch(error) {
  if (error instanceof ValidationError) {
    console.log('ok');
  } else {
    throw error;
  }
}

You know, I think my current project is not importing this library (due to facebook/create-react-app#2952) Even with it added locally to babel-preset-react-app, it doesn't log "ok".

That example prints ok in my test. I'll need a reproducible repository ideally.

It works fine for me in Node, but not in browser

@panganibanpj I need example code and info about your config, like I asked above.

In my case it has something to do with using "babel-preset-env" too. Here's some package.json:

  "scripts": {
    "build-fails": "BABEL_ENV=fails babel ./main.js --out-file ./build.js",
    "build-works": "BABEL_ENV=works babel ./main.js --out-file ./build.js",
    "test": "babel-node node_modules/.bin/mocha ./main.js",
    "test-still-works": "BABEL_ENV=fails babel-node node_modules/.bin/mocha ./main.js"
  },

...

  "babel": {
    "env": {
      "fails": {
        "presets": [
          "env"
        ]
      },
      "works": {
        "presets": [
          "env"
        ],
        "plugins": [
          [
            "babel-plugin-transform-builtin-extend",
            {
              "globals": [
                "Error"
              ]
            }
          ]
        ]
      }
    },
    "plugins": [
      "transform-undefined-to-void",
      [
        "babel-plugin-transform-builtin-extend",
        {
          "globals": [
            "Error"
          ]
        }
      ]
    ]
  }

Here's the full example: extend-error.zip

My guess would be that your usage of the env block in the config to toggle based on the env var is not working right. Unfortunately that's a problem with Babel's current approach to transformation. The order that you list plugins and presets in can affect their behavior.

It's a pain and unfortunate, but I don't think there's anything that I can explicitly do in this plugin to change that. If you're using env blocks in your config, I would recommend putting your full config in each block and not relying on inheriting pieces of the config from higher levels like you are doing now.

I thought so too but I just tried with a different preset (babel-preset-stage-3) in the env block and the issue went away. Furthermore, the transform-undefined-to-void plugin in the example was meant to show that the plugin is still applied in both fails and works envs.

I think it's something to do with using the env block to apply babel-preset-env specifically, because this works fine:

  "babel": {
    "presets": ["env"],
    "plugins": [["babel-plugin-transform-builtin-extend", {"globals": ["Error"]}]]
    ]
  }

I'm ok with this issue being outside the scope of this plugin, but if it would help others, I'm hoping we could track down what's going on here and mention something in the "Limitations" section of README like OP said. I've never built a plugin or preset for babel so I don't have any intuitions about this but I wouldn't mind looking into it. Any advice on where to look would be appreciated :)

It's likely specifically the ordering between this plugin and the one that transforms class syntax, so stage-3 would not cause the issue.

meant to show that the plugin is still applied in both fails and works envs.

Right. That plugin doesn't break when the ordering changes because there isn't any dependent ordering. With this plugin, it needs to run before the classes are transformed.

If you'd like to PR an update to the documentation, I'd be happy to accept it. I don't have a ton of time to focus on this plugin at the moment.

See babel/babel#3083 for details.

Hi!

I have the same issue with the next config:

  "presets": [
    ["env", { "modules": false }],
    "react",
    "stage-2"
  ],
  "plugins": [
    ["babel-plugin-transform-builtin-extend", {"globals": ["Error"]}],
    "lodash"
  ],
  "env": {
    "development": {
      "plugins": [
        "istanbul"
      ]
    }
  }
}

What am I doing wrong? Can you help me?

I have same issues @sergej-s . It seems like istanbul plugin cause the issue. I add blow comment in my code.

/* istanbul ignore next */

And it works.