babel-plugin-transform-isnil
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -14,6 +14,5 @@ 'use strict'; | ||
var name = ''; | ||
if (property.name === 'isNil' && path.parentPath.type !== 'CallExpression') { | ||
(function () { | ||
var name = ''; | ||
var object = (0, _flat2.default)(node); | ||
@@ -33,8 +32,15 @@ Object.keys(object).forEach(function (key) { | ||
name = name.replace(/.$/, ''); | ||
var isArray = (0, _lodash2.default)(node, 'object.computed', false); | ||
var parentObject = (0, _lodash2.default)(path, 'parentPath.node.expression.object'); | ||
var type = (0, _lodash2.default)(parentObject, 'type'); | ||
var args = (0, _lodash2.default)(parentObject, 'arguments'); | ||
if (type === 'CallExpression') { | ||
if (isArray) { | ||
if (node.object.property.name && node.object.computed === true) { | ||
name = name.replace('.' + node.object.property.name, ''); | ||
} | ||
var value = (0, _lodash2.default)(node, 'object.property.extra.raw', null) || (0, _lodash2.default)(node, 'object.property.name', null); | ||
if (value) name += '[' + value + ']'; | ||
} else if (type === 'CallExpression') { | ||
name += '('; | ||
if (args !== undefined) { | ||
if (args !== void 0) { | ||
args.forEach(function (arg) { | ||
@@ -48,3 +54,4 @@ name += arg.name; | ||
} | ||
path.replaceWithSourceString('(' + name + ' === null || ' + name + ' === undefined)'); | ||
/* eslint no-void: 0 */ | ||
path.replaceWithSourceString('(' + name + ' === null || ' + name + ' === void 0)'); | ||
})(); | ||
@@ -51,0 +58,0 @@ } |
{ | ||
"name": "babel-plugin-transform-isnil", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "replace the comparing of null or undefined with isNil", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
@@ -9,3 +9,2 @@ # babel-plugin-transform-isNil | ||
</a> | ||
<a href="https://github.com/sindresorhus/xo"> | ||
@@ -15,2 +14,26 @@ <img src="https://img.shields.io/badge/code_style-XO-5ed9c7.svg"/> | ||
About | ||
--- | ||
I like Existential Operator in `CoffeeScript`. | ||
CoffeeScript can be written as follows: | ||
```coffee | ||
hoge? | ||
``` | ||
Become to `JavaScript` | ||
```js | ||
hoge == null | ||
``` | ||
Same meaning | ||
``` | ||
hoge === null || hoge === undefined | ||
``` | ||
I want to do the same thing in `JavaScript`. | ||
Installation | ||
@@ -62,3 +85,3 @@ --- | ||
if (hoge.poge().isNil) { | ||
console.log('hoge.poge and foo.bar is null or undefined'); | ||
console.log('returned value of hoge.poge() function is null or undefined'); | ||
} | ||
@@ -71,3 +94,3 @@ ``` | ||
if (hoge.poge() === null || hoge.poge() === undefined) { | ||
console.log('hoge.poge and foo.bar is null or undefined'); | ||
console.log('returned value of hoge.poge() function is null or undefined'); | ||
} | ||
@@ -80,3 +103,3 @@ ``` | ||
if (hoge.poge(hoge).isNil) { | ||
console.log('hoge.poge and foo.bar is null or undefined'); | ||
console.log('returned value of hoge.poge() function is null or undefined'); | ||
} | ||
@@ -89,3 +112,3 @@ ``` | ||
if (hoge.poge(hoge) === null || hoge.poge(hoge) === undefined) { | ||
console.log('hoge.poge and foo.bar is null or undefined'); | ||
console.log('returned value of hoge.poge() function is null or undefined'); | ||
} | ||
@@ -96,2 +119,6 @@ ``` | ||
I can't think of implementation method. :cry: | ||
May possibly to stop the support of the fuunction. :persevere: | ||
**In** | ||
@@ -98,0 +125,0 @@ |
@@ -10,5 +10,4 @@ import flatten from 'flat' | ||
const {property} = node | ||
let name = '' | ||
if (property.name === 'isNil' && path.parentPath.type !== 'CallExpression') { | ||
let name = '' | ||
const object = flatten(node) | ||
@@ -28,8 +27,18 @@ Object.keys(object).forEach(key => { | ||
name = name.replace(/.$/, '') | ||
const isArray = _get(node, 'object.computed', false) | ||
const parentObject = _get(path, 'parentPath.node.expression.object') | ||
const type = _get(parentObject, 'type') | ||
const args = _get(parentObject, 'arguments') | ||
if (type === 'CallExpression') { | ||
if (isArray) { | ||
if (node.object.property.name && node.object.computed === true) { | ||
name = name.replace(`.${node.object.property.name}`, '') | ||
} | ||
const value = _get(node, 'object.property.extra.raw', null) || | ||
_get(node, 'object.property.name', null) | ||
if (value) { | ||
name += `[${value}]` | ||
} | ||
} else if (type === 'CallExpression') { | ||
name += '(' | ||
if (args !== undefined) { | ||
if (args !== void 0) { | ||
args.forEach(arg => { | ||
@@ -43,3 +52,4 @@ name += arg.name | ||
} | ||
path.replaceWithSourceString(`(${name} === null || ${name} === undefined)`) | ||
/* eslint no-void: 0 */ | ||
path.replaceWithSourceString(`(${name} === null || ${name} === void 0)`) | ||
} | ||
@@ -46,0 +56,0 @@ } |
@@ -17,15 +17,15 @@ import test from 'ava-spec' | ||
{ | ||
description: 'expect isNil replace to `=== null || === undefined`', | ||
description: 'expect isNil replace to `=== null || === void 0`', | ||
before: 'hoge.isNil', | ||
after: '"use strict";\n\nhoge === null || hoge === undefined;' | ||
after: '"use strict";\n\nhoge === null || hoge === void 0;' | ||
}, | ||
{ | ||
description: 'expect ! isNil replace to `! (=== null || === undefined)`', | ||
description: 'expect ! isNil replace to `! (=== null || === void 0)`', | ||
before: '!hoge.isNil', | ||
after: '"use strict";\n\n!(hoge === null || hoge === undefined);' | ||
after: '"use strict";\n\n!(hoge === null || hoge === void 0);' | ||
}, | ||
{ | ||
description: 'expect isNil replace to `=== null || === undefined`', | ||
description: 'expect isNil replace to `=== null || === void 0`', | ||
before: 'class Hoge { constructor(hoge) { if (this.hoge.isNil) { this.hoge = hoge; } } }', | ||
after: '"use strict";\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }\n\nvar Hoge = function Hoge(hoge) {\n _classCallCheck(this, Hoge);\n\n if (this.hoge === null || this.hoge === undefined) {\n this.hoge = hoge;\n }\n};' | ||
after: '"use strict";\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }\n\nvar Hoge = function Hoge(hoge) {\n _classCallCheck(this, Hoge);\n\n if (this.hoge === null || this.hoge === void 0) {\n this.hoge = hoge;\n }\n};' | ||
}, | ||
@@ -40,3 +40,3 @@ { | ||
before: 'foo.bar().isNil', | ||
after: '"use strict";\n\nfoo.bar() === null || foo.bar() === undefined;' | ||
after: '"use strict";\n\nfoo.bar() === null || foo.bar() === void 0;' | ||
}, | ||
@@ -46,5 +46,19 @@ { | ||
before: 'foo.bar(hoge).isNil', | ||
after: '"use strict";\n\nfoo.bar(hoge) === null || foo.bar(hoge) === undefined;' | ||
after: '"use strict";\n\nfoo.bar(hoge) === null || foo.bar(hoge) === void 0;' | ||
}, | ||
{ | ||
description: 'Array test 1', | ||
before: 'foo[0].isNil', | ||
after: '"use strict";\n\nfoo[0] === null || foo[0] === void 0;' | ||
}, | ||
{ | ||
description: 'Array test 2', | ||
before: 'foo.bar["hoge"].isNil', | ||
after: '"use strict";\n\nfoo.bar["hoge"] === null || foo.bar["hoge"] === void 0;' | ||
}, | ||
{ | ||
description: 'Array test 3', | ||
before: 'bar[hoge].isNil', | ||
after: '"use strict";\n\nbar[hoge] === null || bar[hoge] === void 0;' | ||
} | ||
] | ||
@@ -51,0 +65,0 @@ |
11952
10
180
161