question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function argument destructuring and default value generates "syntax" error

See original GitHub issue

This is a:

  • Bug Report
  • Feature Request
  • Question
  • Other

Which concerns:

  • flow-runtime
  • babel-plugin-flow-runtime
  • flow-runtime-validators
  • flow-runtime-mobx
  • flow-config-parser
  • The documentation website

What is the current behaviour?

const defaultTrx = 'abc'
class Test {
  static async getLastUpdateTimestamp ({ trx }: { trx: any } = { trx: defaultTrx }) { /* do nothing */ }
}
  • flow check itself (flow check --strip-root) does not complain about this
  • this function works as expected on runtime

However when I enable babel-plugin-flow-runtime and then I start my program I get following error:

Duplicate declaration "trx"

.babelrc.js:

module.exports = {
  presets: [
    '@babel/flow',
    [ '@babel/env', {
      targets: {
        node: 'current'
      }
    }]
  ],
  plugins: [
    '@babel/proposal-class-properties',
    '@babel/proposal-object-rest-spread',
    '@babel/proposal-export-namespace',
    '@babel/proposal-decorators',
    ['root-import', {
      rootPathPrefix: '@',
      rootPathSuffix: 'src'
    }],
    ['flow-runtime', {
      assert: false,
      annotate: false
    }]
  ],
  env: {
    test: {
      plugins: ['istanbul']
    }
  }
}

Without flow-runtime plugin in .babelrc.js it works during runtime (no syntax errors) and also flow does not complain about it during flow check.


What is the expected behaviour?

It should not throw error, this is correct syntax and it’s supported by flow.


Which package versions are you using?

"flow-runtime": "^0.17.0",
"babel-plugin-flow-runtime": "^0.17.0",
"@babel/preset-flow": "^7.0.0-beta.32"

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

1reaction
s-olcommented, Sep 19, 2018

I’m having the same issue:

/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:347
      throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
      ^

TypeError: /mnt/space/notification_worker/index.handler.sms.js: Duplicate declaration "timestamp"
  107 |                 })
  108 |               */
> 109 |                 .then(({ timestamp, data }: Notification) => {
      |                          ^
  110 |                   console.log(`${timestamp}: handling ${msg.fields.routingKey} event...`);
  111 |                   return data;
  112 |                 })
    at File.buildCodeFrameError (/mnt/space/notification_worker/node_modules/@babel/core/lib/transformation/file/file.js:261:12)
    at Scope.checkBlockScopedCollisions (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:347:22)
    at Scope.registerBinding (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:504:16)
    at Scope.registerDeclaration (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:444:14)
    at Object.BlockScoped (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:189:28)
    at Object.newFn (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:230:17)
    at NodePath._call (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:36:14)
    at NodePath.visit (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)

if I work around the destructuring I get another one in a much weirder place:

/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:347
      throw this.hub.buildError(id, `Duplicate declaration "${name}"`, TypeError);
      ^

TypeError: /mnt/space/notification_worker/api/collection.js: Duplicate declaration "cache" (This is an error on an internal node. Probably an internal error.)
    at File.buildCodeFrameError (/mnt/space/notification_worker/node_modules/@babel/core/lib/transformation/file/file.js:261:12)
    at Scope.checkBlockScopedCollisions (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:347:22)
    at Scope.registerBinding (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:504:16)
    at Scope.registerDeclaration (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:444:14)
    at Object.BlockScoped (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/scope/index.js:189:28)
    at Object.newFn (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:230:17)
    at NodePath._call (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:36:14)
    at NodePath.visit (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/mnt/space/notification_worker/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:118:16)

the variable ‘cache’ is the one in this helper (verified by renaming, the error log changes with it):

  buildCache() {
    type Extended = Raw & Helpers;
    const cache: { [string]: Extended } = {};

    return (_id: string): Promise<Extended> => {
      if (cache[_id]) return Promise.resolve(cache[_id]);

      return this.findOne({ _id })
        .then(doc => {
          cache[_id] = doc;
          return doc;
        });
    };
  }

personally I don’t see any commonality between these two cases but maybe it can help someone else figure the issue out as it seems to be related as per the error message. Luckily in my project flow-runtime isn’t used extensively so I will be able to remove it for the time being.

0reactions
linonetwocommented, May 2, 2019

My God! Finally, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Destructuring assignment - JavaScript - MDN Web Docs
The destructuring assignment syntax is a JavaScript expression that makes it ... Each destructured property can have a default value.
Read more >
Destructuring and Function Arguments - David Walsh Blog
The basic syntax for destructuring is fairly simple but using ... The following is a function with arguments having default values:
Read more >
ES6 destructuring object assignment function parameter ...
The destructuring with defaults only does its thing when you pass an object which doesn't have the respective properties.
Read more >
Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty {} to prevent it from throwing an error! function hi(person)...
Read more >
Destructuring and parameter handling in ECMAScript 6 - 2ality
ECMAScript 6 (ES6) supports destructuring, a convenient way to extract values from data stored in (possibly nested) objects and arrays.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found