This plugin replaces objectPath.get (object-path) to new optional chaining syntax and remove unused object-path imports.
WORKING ONLY WITH BABEL 7. THIS PLUGIN NOT WORKING WITH BABEL 6.
Babel install:
npm install --save-dev @babel/core @babel/cli @babel/preset-env
Plugin install:
npm i -D babel-plugin-object-path-get-to-optional-chaining
.babelrc
{
"presets": [ "@babel/env" ],
"plugins": [
"object-path-get-to-optional-chaining"
]
}
./node_modules/.bin/babel src --out-dir src
from:
import objectPath from 'object-path'
objectPath.get(foo, 'bar.baz')
objectPath.get(foo.bar.baz.bar, 'baz')
objectPath.get(foo.bar, 'baz.bar')
to:
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
from:
import { get, set } from 'object-path'
get(foo, 'bar.baz')
get(foo.bar.baz.bar, 'baz')
get(foo.bar, 'baz.bar')
to:
import { set } from 'object-path';
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
from:
import { get } from 'object-path'
get(foo, 'bar.baz')
get(foo.bar.baz.bar, 'baz')
get(foo.bar, 'baz.bar')
to:
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
from:
import { get as objGet } from 'object-path'
objGet(foo, 'bar.baz')
objGet(foo.bar.baz.bar, 'baz')
objGet(foo.bar, 'baz.bar')
to:
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
from:
import { get as objGet, set as objSet } from 'object-path'
objGet(foo, 'bar.baz')
objGet(foo.bar.baz.bar, 'baz')
objGet(foo.bar, 'baz.bar')
to:
import { set as objSet } from 'object-path';
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
from:
import objectPath, { get as objGet, set as objSet } from 'object-path'
objGet(foo, 'bar.baz')
objGet(foo.bar.baz.bar, 'baz')
objGet(foo.bar, 'baz.bar')
to:
import { set as objSet } from 'object-path';
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
from:
import objectPath, { get as objGet, set as objSet } from 'object-path'
objGet(foo, 'bar.baz')
objGet(foo.bar.baz.bar, 'baz')
objGet(foo.bar, 'baz.bar')
objectPath.set(foo, 'baz')
to:
import objectPath, { set as objSet } from 'object-path';
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
objectPath.set(foo, 'baz');
from:
import objectPath, { get as objGet, set as objSet } from 'object-path'
objGet(foo, 'bar.baz')
objGet(foo.bar.baz.bar, 'baz')
objGet(foo.bar, 'baz.bar')
objectPath[cond ? 'get' : 'set'](foo, 'bar')
to:
import objectPath, { set as objSet } from 'object-path';
foo?.bar?.baz;
foo.bar.baz.bar?.baz;
foo.bar?.baz?.bar;
objectPath[cond ? 'get' : 'set'](foo, 'bar');