1.1.0 • Published 3 years ago

babel-plugin-sync-mode v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

babel-plugin-sync-mode NPM version NPM monthly downloads NPM total downloads Linux Build Status

Automatically await every expression in async function declaring it as sync

Example

async function test() {
  sync: {
    console.log("I should be awaited 1");
    console.log`I should be awaited 2`;
    // a await goes here
    console.log(
      // a await goes here as well
      (() => {
        console.log("I should not be awaited 3");
      })()
    );
    // a await goes here
    console.log(
      // a await goes here as well
      (async () => {
        console.log("I should not be awaited 4");
      })()
    );
    async: {
      console.log("I should not be awaited 5");
      sync_: {
        console.log(
          async () => {
            console.log("I should be awaited 6");
          },
          () => {
            console.log("I should not be awaited 7");
          }
        );
      }
    }
  }
  console.log("I should not be awaited 8");
}

sync: {
  console.log("I should not be awaited 9");
  (() => {
    console.log("I should not be awaited 10");
  })();
  (async () => {
    console.log("I should be awaited 111");
  })();
}

(async () => {
  console.log("I should not be awaited 112");
})();

Will turn:

async function test() {
  sync: {
    await console.log("I should be awaited 1");
    await console.log`I should be awaited 2`; // a await goes here

    await console.log(
      // a await goes here as well
      await (() => {
        console.log("I should not be awaited 3");
      })()
    ); // a await goes here

    await console.log(
      // a await goes here as well
      await (async () => {
        await console.log("I should not be awaited 4");
      })()
    );

    async: {
      console.log("I should not be awaited 5");

      sync_: {
        await console.log(
          async () => {
            await console.log("I should be awaited 6");
          },
          () => {
            console.log("I should not be awaited 7");
          }
        );
      }
    }
  }

  console.log("I should not be awaited 8");
}

sync: {
  console.log("I should not be awaited 9");

  (() => {
    console.log("I should not be awaited 10");
  })();

  (async () => {
    await console.log("I should be awaited 111");
  })();
}

(async () => {
  console.log("I should not be awaited 112");
})();

Installation

npm install --save-dev babel-plugin-sync-mode

or using yarn

yarn add --dev babel-plugin-sync-mode

Usage

Via .babelrc (Recommended)

.babelrc
{
  "plugins": ["sync-mode"]
}

Via CLI

babel-node --plugins sync-mode script.js

Via Node API

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