Skip to content

[babel 7] class.method(arg = this.something) becomes: arg = undefined.something #7264

Closed

Description

@drcmda

Choose one: is this a bug report or feature request?

bug

screen shot 2018-01-23 at 20 56 08

It seems to transpile this in args to undefined, but only if an import is made above it. I tried several, always crashes, if i remove the import, it's fine again ...

Input Code

import React from 'react'

class A {
    constructor() {
        this.bla = 1
    }
    heck(bla = this.bla) {
        console.log(bla)
    }
}

const a = new A()
a.heck()

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

const options = {
    babelrc: false,
    cacheDirectory: true,
    presets: [
        ['@babel/preset-env', { modules: false, loose: true, targets: { chrome: 60 } }],
        '@babel/preset-stage-0',
        '@babel/preset-react',
    ],
    plugins: [
        '@babel/plugin-proposal-decorators',
    ],
}

PS. There seems to be a problem with plugin-proposal-decorators as well, without it yields:

Module build failed: Error: We don't know what to do with this node type. We were previously a Statement but we can't fit in here?

Your Environment

software version(s)
Babel 7.0.0-beta.38
Babylon
node 9.4.0
npm 5.6.0
Operating System MacOS high sierra

Activity

  1. babel-bot commented on Jan 23, 2018

    @babel-bot
    Collaborator

    Hey @drcmda! 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.

  2. Andarist commented on Jan 24, 2018

    @Andarist
    Member

    Could u run node in ur project and execute this code?

    var res = require('@babel/core').transform(`
    
    import React from 'react'
    
    class A {
        constructor() {
            this.bla = 1
        }
        heck(bla = this.bla) {
            console.log(bla)
        }
    }
    
    const a = new A()
    a.heck()
    
    `, { 
      presets: [
        ['@babel/preset-env', { modules: false, loose: true, targets: { chrome: 60 } }],
        '@babel/preset-stage-0',
        '@babel/preset-react',
      ],
      plugins: [
        '@babel/plugin-proposal-decorators',
      ]
    });
    res.code

    Would be helpful to see a result, Im suspecting this might a webpack issue - would be great to rule this out (or confirm).

  3. drcmda commented on Jan 24, 2018

    @drcmda
    Author

    @Andarist

    Indeed, it returned:

    'import React from \'react\';\n\nclass A {\n  constructor() {\n    this.bla = 1;\n  }\n\n  heck(bla = this.bla) {\n    console.log(bla);\n  }\n\n}\n\nconst a = new A();\na.heck();'
    

    Do you have any idea how webpack (4) could have done this? I thought it's never going to touch code like that ...

    If i use ['@babel/preset-env', { modules: false, useBuiltIns: 'usage' }], in my project this works like before, something strange seems to provoke it.

  4. Andarist commented on Jan 24, 2018

    @Andarist
    Member

    @drcmda according to latest announcement webpack:

    [new] this is now undefined in ESM

    I suspect that webpack thought that this was referring here to "top level", but actually it refers to the class' instance. You can file a bug issue on their boards.

  5. Andarist commented on Jan 25, 2018

    @Andarist
    Member

    I see you have started rolling ball on webpack's board - going to close this issue then.

  6. drcmda commented on Jan 25, 2018

    @drcmda
    Author

    @Andarist thanks for your help!

  7. added
    outdated
    on May 2, 2018
  8. locked as resolved and limited conversation to collaborators on May 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions