Automatically add '?.'(optional chaining) to codes and transform them with @babel/babel-plugin-optional-chaining
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;
- 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~
- new Test() !=> new Test?.()
- x.a.c = 2 !=> (x?.a).c = 2
- x() !=> x?.() // manually using x?.() is Ok
npm install -D babel-plugin-optional-chaining-enhance
- 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
{
"plugins": [
['babel-plugin-optional-chaining-enchance', {loose: false, auto: ture}]
]
}
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.
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;
- complete basic functionality
- Add test
- Add travis, circleci
- Add coverage test
Give a ⭐️ if this project helped you!
Copyright © 2019 X-Jagger.
This project is MIT licensed.
This README was generated with ❤️ by readme-md-generator