isv-babel-plugin-safe-member-expression
v10.0.9
Published
safe member expression
Downloads
5
Readme
babel-plugin-safe-member-expression
safe member expression
only auto enabled in BlockStatement
feature:
- Safer MemberExpression
- Safer CallExpression with MemberExpression
- Safer AssignmentExpression with MemberExpression
参数
match
默认值:/\/src.*\.jsx?/i
描述:只有被 match 中的文件路径才会进入插件转换
exclude
默认值:/\/node_modules.*\.jsx?/i
描述:被 exclude 匹配的路径不会进入插件转换
Example
Safer MemberExpression
In
const foo = hello['world'];
let hello = this.state['foo'].bar;
Out
var _object, _object2, _object3;
const foo = hello == null ? null : hello['world'];
let hello = (_object = (_object2 = (_object3 = this) == null ? null : _object3.state) == null ? null : _object2['foo']) == null ? null : _object.bar;
Safer CallExpression with MemberExpression
In
const data = this.foo.bar.ok();
Out
var _object, _object2, _object3;
const data = (_object = (_object2 = (_object3 = this) == null ? null : _object3.foo) == null ? null : _object2.bar) == null ? null : _object.ok ? _object.ok() : null;
Safer AssignmentExpression with MemberExpression
In
foo['bar'] = x.y;
x.y.z = 'xxx';
Out
var _object;
(foo || (foo = {}))['bar'] = x == null ? null : x.y;
((_object = x || (x = {})).y || (_object.y = {})).z = 'xxx';
Installation
$ npm install babel-plugin-safe-member-expression
Usage
Via .babelrc
(Recommended)
.babelrc
{
"plugins": ["safe-member-expression"]
}
Via CLI
$ babel --plugins safe-member-expression script.js
Via Node API
require("babel-core").transform("code", {
plugins: ["safe-member-expression"]
});