Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-block-scoping

Package Overview
Dependencies
77
Maintainers
4
Versions
107
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @babel/plugin-transform-block-scoping

Compile ES2015 block scoping (const and let) to ES5


Version published
Weekly downloads
24M
decreased by-0.46%
Maintainers
4
Created
Weekly downloads
 

Package description

What is @babel/plugin-transform-block-scoping?

The @babel/plugin-transform-block-scoping npm package is a Babel plugin that transforms JavaScript code to convert block scoping (let and const) to function scoping (var) where necessary. This is particularly useful for ensuring compatibility with older environments that do not support ES6 block-scoped variables.

What are @babel/plugin-transform-block-scoping's main functionalities?

Transform let and const to var

This feature transforms block-scoped variables (let and const) into function-scoped variables (var) to ensure compatibility with older JavaScript engines.

if (true) {
  let x = 'hello';
}
// Transformed to:
if (true) {
  var x = 'hello';
}

Block scoping for loops

This feature correctly scopes the loop variable within closures, preventing common errors when using setTimeout or similar asynchronous functions within loops.

for (let i = 0; i < 3; i++) {
  setTimeout(function() { console.log(i); }, 0);
}
// Transformed to:
for (var i = 0; i < 3; i++) {
  (function(i) {
    setTimeout(function() { console.log(i); }, 0);
  })(i);
}

Other packages similar to @babel/plugin-transform-block-scoping

Readme

Source

@babel/plugin-transform-block-scoping

Compile ES2015 block scoping (const and let) to ES5

Examples

In

{
  let a = 3
}

let a = 3

Out

{
  var _a = 3;
}

var a = 3;

Installation

npm install --save-dev @babel/plugin-transform-block-scoping

Usage

.babelrc

Without options:

{
  "plugins": ["@babel/plugin-transform-block-scoping"]
}

With options:

{
  "plugins": [
    ["@babel/plugin-transform-block-scoping", {
      "throwIfClosureRequired": true
    }]
  ]
}

Via CLI

babel --plugins @babel/plugin-transform-block-scoping script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-transform-block-scoping"]
});

Options throwIfClosureRequired

In cases such as the following it's impossible to rewrite let/const without adding an additional function and closure while transforming:

for (let i = 0; i < 5; i++) {
  setTimeout(() => console.log(i), 1);
}

In extremely performance-sensitive code, this can be undesirable. If "throwIfClosureRequired": true is set, Babel throws when transforming these patterns instead of automatically adding an additional function.

Keywords

FAQs

Last updated on 02 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc