Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instanceof doesn't work #14

Open
stevenvachon opened this issue Aug 14, 2017 · 12 comments
Open

instanceof doesn't work #14

stevenvachon opened this issue Aug 14, 2017 · 12 comments

Comments

@stevenvachon
Copy link

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

@loganfsmyth
Copy link
Owner

Got an example of your configuration? It should work.

@stevenvachon
Copy link
Author

stevenvachon commented Aug 14, 2017

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".

@loganfsmyth
Copy link
Owner

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

@panganibanpj
Copy link

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

@loganfsmyth
Copy link
Owner

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

@panganibanpj
Copy link

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

@loganfsmyth
Copy link
Owner

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.

@panganibanpj
Copy link

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 :)

@loganfsmyth
Copy link
Owner

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.

@avesus
Copy link

avesus commented Oct 16, 2017

See babel/babel#3083 for details.

@sergej-s
Copy link

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?

@Jack-X-Yang
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants