Coder Social home page Coder Social logo

neyio / babel-plugin-optional-chaining-enhance Goto Github PK

View Code? Open in Web Editor NEW

This project forked from x-jagger/babel-plugin-optional-chaining-enhance

0.0 1.0 0.0 91 KB

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

JavaScript 100.00%

babel-plugin-optional-chaining-enhance's Introduction

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

babel-plugin-optional-chaining-enhance's People

Contributors

x-jagger avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.