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

BUG: babel using _initialiseProps when has var names conflict (7 beta 32) #6913

Closed
ghost opened this Issue Nov 27, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Nov 27, 2017

BUG (* Code updated to reflect the problem)

Input Code

class Bork {
    constructor(param) {
      	this.param = param;
        this.myMethod();
    }

    myMethod = () => {
        let param = this.param;
        console.log(param);
    };
}

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
  "presets": [
    "@babel/preset-env",
    "@babel/es2015",
    "@babel/react"
  ],
  "plugins": [
    "@babel/external-helpers",
    ["@babel/proposal-class-properties", {"loose": true}],
    "@babel/transform-object-assign"
  ]
}

Expected Behavior

Babel transpile without _initialiseProps function, or scoping it, or create a unique name to it, like _initialisePropsClassName.

Current Behavior

Babel creating _initialiseProps function in footer of class files, and this function is global, overriding another class _initialiseProps function.

Context

One _initialiseProps are overriding the _initialiseProps of another file, making props of one class be injected on another.

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Nov 27, 2017

Collaborator

Hey @marcelobart! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Nov 27, 2017

Hey @marcelobart! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Nov 27, 2017

Member

That code doesn't use _initializeProps: https://babeljs.io/repl/build/master#?babili=false&browsers=&build=&builtIns=false&code_lz=MYGwhgzhAEBCD2AnA1tA3gKGgWwJ4FkBTAFwAt4ATaAXmgAoBKGgPnWgF8BuDdjIA&debug=false&circleciRepo=&evaluate=false&lineWrap=false&presets=stage-0&prettier=false&targets=&version=7.0.0-beta.32

Anyway, Babel assumes that your files are modules, so the top level decparations aren't in the global scope and don't conflict with each other. There is no way for Babel to know which variables are already declared in other files, so it can't avoid this kind of conflicts.

As a workaround, you can define those classes using IIFEs:

let Foo = (() => class Foo {
  /* ... */
})();
Member

nicolo-ribaudo commented Nov 27, 2017

That code doesn't use _initializeProps: https://babeljs.io/repl/build/master#?babili=false&browsers=&build=&builtIns=false&code_lz=MYGwhgzhAEBCD2AnA1tA3gKGgWwJ4FkBTAFwAt4ATaAXmgAoBKGgPnWgF8BuDdjIA&debug=false&circleciRepo=&evaluate=false&lineWrap=false&presets=stage-0&prettier=false&targets=&version=7.0.0-beta.32

Anyway, Babel assumes that your files are modules, so the top level decparations aren't in the global scope and don't conflict with each other. There is no way for Babel to know which variables are already declared in other files, so it can't avoid this kind of conflicts.

As a workaround, you can define those classes using IIFEs:

let Foo = (() => class Foo {
  /* ... */
})();
@ghost

@ghost ghost changed the title from BUG: babel still using _initialiseProps multiple times (7 beta 32) to BUG: babel using _initialiseProps when has var names conflict (7 beta 32) Nov 29, 2017

@lock lock bot added the outdated label Jul 5, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Jul 5, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.