babel-plugin-optional-chaining-enhance

0.0.9 • Public • Published

Babel-plugin-optional-chaining-enhance

Travis Status CircleCI Status Coverage Status Maintenance License: MIT

Automatically add '?.'(optional chaining) to codes and transform them with @babel/babel-plugin-optional-chaining

✨ Example

You just need to write the codes, and babel-plugin-optional-chaining-plugin will do all for you.

x.a => x?.a => var _x;(_x = x) === null || _x === void 0 ? void 0 : _x.a;

Support

  • y = a.b.c.d.e.f => y = a?.b?.c?.d?.e?.f => transform by @babel/babel-plugin-optional-chaining
  • const x = a.b.c.d.e => var x = a?.b?.c?.d?.e => transform~
  • fn(a.b.c.d.e) => fn(a?.b?.c?.d?.e) => transform~
  • z.a.b.c() => z?.a?.b?.c() => transform~
  • z.a.b.c[1] => z?.a?.b?.c?.[1] => transform~
  • x['123'].vc.s => x?.['123']?.vc?.s => transform~
  • if (x.a.b) {} => if (x?.a?.b) {} => transform~

Not support

  • new Test() !=> new Test?.()
  • x.a.c = 2 !=> (x?.a).c = 2
  • x() !=> x?.() // manually using x?.() is Ok

Install

npm install -D babel-plugin-optional-chaining-enhance

🚀 Usage

  • Because this plugin has already integrated @babel/babel-plugin-optional-chaining, please don't use @babel/babel-plugin-optional-chaining anymore.
  • Put it at the last position of all plugins

1 .babelrc/babel.config.js

{
  "plugins": [
     'babel-plugin-optional-chaining-enchance' // put it at the last of all plugins
    ]
}

2 Enjoy writing clean codes

Options

{
  "plugins": [
     ['babel-plugin-optional-chaining-enchance', {loose: false, auto: ture}]
    ]
}

auto

boolean, defaults to true

when true, this transform will automatically add '?.' to all the relevant codes and transform them.

when false, this plugin is @babel/plugin-proposal-optional-chaining, only transform the codes with '?.' before.

loose

boolean, defaults to false.

When true, this transform will pretend document.all does not exist, and perform loose equality checks with null instead of strict equality checks against both null and undefined.

Example In

foo?.bar;

Out (loose === true)

foo == null ? void 0 : foo.bar;

Out (loose === false)

foo === null || foo === void 0 ? void 0 : foo.bar;

TODO

  • complete basic functionality
  • Add test
  • Add travis, circleci
  • Add coverage test

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2019 X-Jagger.
This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

Package Sidebar

Install

npm i babel-plugin-optional-chaining-enhance

Weekly Downloads

2

Version

0.0.9

License

MIT

Unpacked Size

24.7 kB

Total Files

5

Last publish

Collaborators

  • leijieur