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

Cannot read property 'add' of undefined #974

Open
mblout opened this issue Jan 17, 2020 · 23 comments
Open

Cannot read property 'add' of undefined #974

mblout opened this issue Jan 17, 2020 · 23 comments

Comments

@mblout
Copy link

mblout commented Jan 17, 2020

seeing " Cannot read property 'add' of undefined" with the latest

sample code, junk.js

const y = () => {};
function x(foo = y) {}

command: npx babel junk.js --presets=minify

package.json

{
  "name": "babelbug",
  "version": "1.0.0",
  "description": "",
  "devDependencies": {
    "@babel/core": "^7.7.0",
    "@babel/preset-env": "^7.7.0",
    "@babel/cli": "^7.7.0",
    "babel-preset-minify": "~0.5.0"
  }
}

stack

{ TypeError: C:\babelbug\junk.js: Cannot read property 'add' of undefined
    at ScopeTracker.addReference (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\scope-tracker.js:47:34)
    at ReferencedIdentifier (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:196:26)
    at newFn (C:\babelbug\node_modules\@babel\traverse\lib\visitors.js:220:17)
    at bfsTraverse (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\bfs-traverse.js:32:43)
    at Mangler.collect (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:229:7)
    at Mangler.run (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:54:12)
    at PluginPass.exit (C:\babelbug\node_modules\babel-plugin-minify-mangle-names\lib\index.js:558:19)
    at newFn (C:\babelbug\node_modules\@babel\traverse\lib\visitors.js:179:21)
    at NodePath._call (C:\babelbug\node_modules\@babel\traverse\lib\path\context.js:55:20)
    at NodePath.call (C:\babelbug\node_modules\@babel\traverse\lib\path\context.js:42:17) code: 'BABEL_TRANSFORM_ERROR' }
@anthony-quiros
Copy link

I have the same error with this code:

  const defaultLib = require('./default-lib');

  foo(lib = defaultLib) {
    this.lib = lib;
  }

@mjakobis
Copy link

The issue seems to be with default functional parameters set to a constant.

@arantes555
Copy link

Seeing the same problem here... setting mangle: false in the options seems to work-around the problem.

@arantes555
Copy link

Also, seems there is a similar old issue : #556

@somewhatabstract
Copy link

I see the same issue when using rollup-babel-minify. Dependabot tried to update to latest babel and couldn't due to this issue.

TypeError: unknown: Cannot read property 'add' of undefined
    at ScopeTracker.addReference (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34)
    at ReferencedIdentifier (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:196:26)
    at newFn (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/visitors.js:220:17)
    at bfsTraverse (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:32:43)
    at Mangler.collect (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:229:7)
    at Mangler.run (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:54:12)
    at PluginPass.exit (/Users/jeffyates/git/checksync/node_modules/babel-plugin-minify-mangle-names/lib/index.js:558:19)
    at newFn (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/visitors.js:179:21)
    at NodePath._call (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/Users/jeffyates/git/checksync/node_modules/@babel/traverse/lib/path/context.js:42:17)

@SalvatorePreviti
Copy link

Same issue here.

@mnosuk
Copy link

mnosuk commented Feb 26, 2020

same issue here :(

@DiegoRBaquero
Copy link

Same issue here to build webtorrent

@kpierro914
Copy link

I'm having the same issue when I'm assigning a default value to a parameter. As soon as I remove the default value, the issue is gone.

@monokee
Copy link

monokee commented Jun 16, 2020

Same issue

@arantes555
Copy link

I have noticed a weird behaviour :

  • I have @babel/core@7.5.5, and I do not hit this issue
  • If I update @babel/core to any superior version, I start hitting the issue
  • If I then rollback @babel/core to 7.5.5, I still hit the issue.

I think this means the issue is caused by one of @babel/core's dependencies. Not sure how to pinpoint which one :/

@DeividVeloso
Copy link

Same issue here.

@alfokaha-taima
Copy link

same issue :(

@meduzen
Copy link

meduzen commented Nov 14, 2020

The issue seems to be with default functional parameters set to a constant.

Same for me on both babel-preset-minify 0.4.3 and 0.5.1.

This doesn’t compile:

import { doc } from '../utils/Document';

const DEFAULT_DEBOUNCE_DELAY = 100

let resizeTimer = null

export default function debounceResize(callback, delay = DEFAULT_DEBOUNCE_DELAY) {
  clearTimeout(resizeTimer)
  doc.classList.add('resizing')

  resizeTimer = setTimeout(() => {
    doc.classList.remove('resizing')
    callback()
  }, delay)
}
Stack trace:
Module build failed (from ./node_modules/babel-loader/lib/index.js):
    TypeError: /resources/js/events/resize.js: Cannot read property 'add' of undefined
        at ScopeTracker.addReference (/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js:47:34)
        at ReferencedIdentifier (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:196:26)
        at newFn (/node_modules/@babel/traverse/lib/visitors.js:216:17)
        at bfsTraverse (/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js:32:43)
        at Mangler.collect (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:229:7)
        at Mangler.run (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:54:12)
        at PluginPass.exit (/node_modules/babel-plugin-minify-mangle-names/lib/index.js:526:19)
        at newFn (/node_modules/@babel/traverse/lib/visitors.js:175:21)
        at NodePath._call (/node_modules/@babel/traverse/lib/path/context.js:55:20)
        at NodePath.call (/node_modules/@babel/traverse/lib/path/context.js:42:17)
        at NodePath.visit (/node_modules/@babel/traverse/lib/path/context.js:101:8)
        at TraversalContext.visitQueue (/node_modules/@babel/traverse/lib/context.js:112:16)
        at TraversalContext.visitSingle (/node_modules/@babel/traverse/lib/context.js:84:19)
        at TraversalContext.visit (/node_modules/@babel/traverse/lib/context.js:140:19)
        at Function.traverse.node (/node_modules/@babel/traverse/lib/index.js:82:17)
        at traverse (/node_modules/@babel/traverse/lib/index.js:64:12)
        at transformFile (/node_modules/@babel/core/lib/transformation/index.js:107:29)
        at transformFile.next (<anonymous>)
        at run (/node_modules/@babel/core/lib/transformation/index.js:35:12)
        at run.next (<anonymous>)
        at Function.transform (/node_modules/@babel/core/lib/transform.js:27:41)
        at transform.next (<anonymous>)
        at step (/node_modules/gensync/index.js:254:32)
        at /node_modules/gensync/index.js:266:13
        at async.call.result.err.err (/node_modules/gensync/index.js:216:11)
     @ ./resources/js/events/index.js 1:89-126 1:217-231
     @ ./resources/js/app.js

This compiles properly:

import { doc } from '../utils/Document';

let resizeTimer = null

export default function debounceResize(callback, delay = 100) {
  clearTimeout(resizeTimer)
  doc.classList.add('resizing')

  resizeTimer = setTimeout(() => {
    doc.classList.remove('resizing')
    callback()
  }, delay)
}

@arantes555
Copy link

I'm still stuck on @babel/core@7.5.5 because of this. Any way we can help fix this ?

@s100
Copy link

s100 commented Feb 11, 2021

I was able to work around this problem by using @babel/plugin-transform-parameters to transpile away the default parameters syntax prior to minification. I would still be interested to see the bug fixed though.

fwextensions added a commit to fwextensions/QuicKey that referenced this issue Mar 29, 2021
Babel has this bug which appeared after trying to update to the latest version, and remained even after rolling back to an older version.  Extremely annoying.  babel/minify#974
Add grunt-terser.
Remove babel, core-js, lodash, which was used just once in build-popup.js, and react-virtualized, which wasn't used at all.
Remove package-lock.json from .gitignore and commit it, to make it easier to track exact package changes.
@fwextensions
Copy link

I was seeing the same thing as @arantes555, where upgrading to the latest package started causing the issue, but rolling back to the previously working version still produced the bug, even after wiping and reinstalling node_modules. I eventually gave up and switched to terser, which has been working fine.

@arantes555
Copy link

@fwextensions I ended up switching to terser too...

For those who, for some reason, cannot switch, the trick in this case is to wipe node_modules, and roll-back both your package.json and pacakge-lock.json, then re-installing.

@ruymaster
Copy link

I got same error.

fwextensions added a commit to fwextensions/QuicKey that referenced this issue May 4, 2021
Fix #57.

Squashed commit of the following:

commit d10eca3
Author: John Dunning <fw@johndunning.com>
Date:   Tue May 4 15:54:50 2021 -0700

    Update version to 1.6.1

    Update README.md, releases/index.md and the store descriptions to include the new keyboard shortcuts.

commit 1852914
Author: John Dunning <fw@johndunning.com>
Date:   Thu Apr 29 12:39:39 2021 -0700

    Add IsLinux constant

    Use ctrl-alt-W on Linux/ChromeOS for closing a tab in the menu, since ctrl-W is intercepted by the browser.

commit 12809d5
Author: John Dunning <fw@johndunning.com>
Date:   Tue Mar 30 12:09:29 2021 -0700

    Update dependencies to the latest

commit ce05780
Author: John Dunning <fw@johndunning.com>
Date:   Mon Mar 29 11:29:26 2021 -0700

    Add ctrl-P/N and ctrl-K/J shortcuts for moving up/down

    Fix error in error-handler.js.  :|
    Let newer key bindings override older ones with the same shortcuts.

commit 03a044f
Author: John Dunning <fw@johndunning.com>
Date:   Sun Mar 28 17:27:40 2021 -0700

    Switch to using terser instead of babel

    Babel has this bug which appeared after trying to update to the latest version, and remained even after rolling back to an older version.  Extremely annoying.  babel/minify#974
    Add grunt-terser.
    Remove babel, core-js, lodash, which was used just once in build-popup.js, and react-virtualized, which wasn't used at all.
    Remove package-lock.json from .gitignore and commit it, to make it easier to track exact package changes.

commit e6efa20
Author: John Dunning <fw@johndunning.com>
Date:   Sat Mar 27 18:06:55 2021 -0700

    Fix build-popup.js

    Change npm build to run grunt build.
@dhlolo
Copy link

dhlolo commented Jul 25, 2022

Any update? Seem there is an PR checked but not merged.

@TiagoSilvaPereira
Copy link

TiagoSilvaPereira commented Jan 17, 2023

I was able to work around this problem by using @babel/plugin-transform-parameters to transpile away the default parameters syntax prior to minification. I would still be interested to see the bug fixed though.

Confirming it works with @babel/plugin-transform-parameters Thank you very much @s100

@ochodek
Copy link

ochodek commented Mar 10, 2023

Any update? I found that it started to fail after a change in @babel/types@7.8.0 - babel/babel#10917
Hopefuly #1024 will be merged soon.

@jamonholmgren
Copy link

@ochodek I used Yarn's built-in yarn patch feature to apply #1024's changes (if you're not using Yarn 3+, you can use patch-package instead). It fixed the issue for me.

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