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.

[Bug]: rest operator produces global variables

See original GitHub issue

šŸ’»

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

`export const Event = (function init() { function getsomething(){ let o = { ā€˜siteIds’:122, ā€˜sitename’:ā€˜test’, ā€˜sitecity’: ā€˜san fran’ }; return o; } const { siteIds, …rest } = getsomething();

return {getsomething}; })();

` reproducible here https://babeljs.io/repl#?browsers=defaults%2C not ie 11%2C not ie_mob 11&build=&builtIns=false&corejs=3.6&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAYwgOwM7wKIDdjPgXjgAoAzAV2QRgEsU5rlqYiBKOAbwChzKa6BzYDFQQAtkIAWDfqy5x5cADZC4EOITkK4AclRNgASQAmqbQC4AjACYrAGk5ademLgCG489pfpt9x7v0EJgBPcydXZDgSKAjtBy0AXwBuePkoITIoSIgUhNTEFHQOOGdDE1s4ADpq9KKE9Xz5QWExSWlWFPl49JhMyPZmkXEYKWR-ZM4Elg6gA&debug=false&forceAllTransforms=true&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.15.3&externalPlugins=&assumptions={}

In v7.15.3 -> the compiled output creates a global variable called _excluded. in v7.13.17 -> the compiled output does not create a global variable, instead it places the array inline.

Is this expected? is there a way to turn this off and revert to use v7.13.17 behavior?

Multiple scripts will collide with same global variable.

Configuration file name

No response

Configuration

No response

Current and expected behavior

In v7.15.3 -> the compiled output creates a global variable called _excluded. in v7.13.17 -> the compiled output does not create a global variable, instead it places the array inline.

Is this expected? is there a way to turn this off and revert to use v7.13.17 behavior?

Multiple scripts will collide with same global variable.

Environment

see https://babeljs.io/repl#?browsers=defaults%2C not ie 11%2C not ie_mob 11&build=&builtIns=false&corejs=3.6&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAYwgOwM7wKIDdjPgXjgAoAzAV2QRgEsU5rlqYiBKOAbwChzKa6BzYDFQQAtkIAWDfqy5x5cADZC4EOITkK4AclRNgASQAmqbQC4AjACYrAGk5ademLgCG489pfpt9x7v0EJgBPcydXZDgSKAjtBy0AXwBuePkoITIoSIgUhNTEFHQOOGdDE1s4ADpq9KKE9Xz5QWExSWlWFPl49JhMyPZmkXEYKWR-ZM4Elg6gA&debug=false&forceAllTransforms=true&shippedProposals=false&circleciRepo=&evaluate=true&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.15.3&externalPlugins=&assumptions={}

Possible solution

No response

Additional context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jakeye-nuvolocommented, Aug 26, 2021

Thanks for the responses. This issue arises from our setup actually. We are using babel to transpile typescript back to a 3rd party SaaS platform as javascript files. The final destination of the script files are recompiled into basically a single JS file for the sake of this argument.

Are there ways or options to work around this and keep the original behavior of inline placement of the variables?

Appreciate the help and assistance on this matter.

1reaction
jakeye-nuvolocommented, Aug 25, 2021

unexpected output

`ā€œuse strictā€;

Object.defineProperty(exports, ā€œ__esModuleā€, { value: true }); exports.Event = void 0; var _excluded = [ā€œsiteIdsā€];

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Event = function init() { function getsomething() { var o = { ā€˜siteIds’: 122, ā€˜sitename’: ā€˜test’, ā€˜sitecity’: ā€˜san fran’ }; return o; }

var _getsomething = getsomething(), siteIds = _getsomething.siteIds, rest = _objectWithoutProperties(_getsomething, _excluded);

return { getsomething: getsomething }; }();

exports.Event = Event;`

Expected Output

ā€œuse strictā€;

Object.defineProperty(exports, ā€œ__esModuleā€, { value: true }); exports.Event = void 0;

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Event = function init() { function getsomething() { var o = { ā€˜siteIds’: 122, ā€˜sitename’: ā€˜test’, ā€˜sitecity’: ā€˜san fran’ }; return o; }

var _getsomething = getsomething(), siteIds = _getsomething.siteIds, rest = _objectWithoutProperties(_getsomething, [ā€œsiteIdsā€]);

return { getsomething: getsomething }; }();

exports.Event = Event;

Read more comments on GitHub >

github_iconTop Results From Across the Web

I have a problem with Python global variables. Is this a bug or ...
Isn't global scope variables defined outside of a function? This was defined inside a function, called makeVariables(). – Unnamed. Mar 27 at 14:Ā ......
Read more >
The Problems with Global Variables - Embedded Artistry
Consequences: Using too many global variables increases software complexity and can be reasonably expected to increase the number of bugs, asĀ ...
Read more >
Rest parameters - JavaScript - MDN Web Docs
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, providing a way to represent variadicĀ ......
Read more >
Understanding Destructuring, Rest Parameters, and Spread ...
In this article, you will learn how to destructure objects and arrays, how to use the spread operator to unpack objects and arrays,...
Read more >
Rest Operator – What Is a JavaScript Rest Parameter?
The rest operator typically gets used as a prefix of the destructuring assignment's last variable. ... The rest operator ( ... ) instructs...
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