babel-plugin-transform-isnil
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -0,1 +1,5 @@ | ||
v0.0.7 | ||
--- | ||
Reworked transform to use a helper function. | ||
v0.0.5 | ||
@@ -2,0 +6,0 @@ --- |
@@ -10,3 +10,3 @@ 'use strict'; | ||
visitor: { | ||
MemberExpression: function MemberExpression(path) { | ||
MemberExpression: function MemberExpression(path, state) { | ||
var node = path.node; | ||
@@ -55,4 +55,7 @@ var property = node.property; | ||
} | ||
var isNilWrapper = addIsNilHelper.call(state.file).name; | ||
/* eslint no-void: 0 */ | ||
path.replaceWithSourceString('\n (((typeof window === \'undefined\' ? global : window).__TMP_VAL__ = ' + name + ') || true) &&\n ((typeof (typeof window === \'undefined\' ? global : window).__TMP_VAL__)) === \'undefined\' ||\n (typeof window === \'undefined\' ? global : window).__TMP_VAL__ === null\n '); | ||
path.replaceWithSourceString(isNilWrapper + '(' + name + ')'); | ||
})(); | ||
@@ -73,2 +76,54 @@ } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _babelTemplate = require('babel-template'); | ||
var _babelTemplate2 = _interopRequireDefault(_babelTemplate); | ||
var _babelTypes = require('babel-types'); | ||
var t = _interopRequireWildcard(_babelTypes); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var isNilWrapper = (0, _babelTemplate2.default)('(function (val) { return val === null || typeof val === \'undefined\' })()'); | ||
function addIsNilHelper() { | ||
// Modified from https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/file/index.js#L280 | ||
var name = 'isNilWrapper'; | ||
var declar = this.declarations[name]; | ||
if (declar) { | ||
return declar; | ||
} | ||
if (!this.usedHelpers[name]) { | ||
this.metadata.usedHelpers.push(name); | ||
this.usedHelpers[name] = true; | ||
} | ||
var generator = this.get('helperGenerator'); | ||
var runtime = this.get('helpersNamespace'); | ||
if (generator) { | ||
var res = generator(name); | ||
if (res) { | ||
return res; | ||
} | ||
} else if (runtime) { | ||
return t.memberExpression(runtime, t.identifier(name)); | ||
} | ||
var ref = isNilWrapper().expression; | ||
var uid = this.declarations[name] = this.scope.generateUidIdentifier(name); | ||
ref._compact = true; | ||
this.scope.push({ | ||
id: uid, | ||
init: ref, | ||
unique: true | ||
}); | ||
return uid; | ||
} |
{ | ||
"name": "babel-plugin-transform-isnil", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "replace the comparing of null or undefined with isNil", | ||
@@ -5,0 +5,0 @@ "main": "lib", |
import flatten from 'flat' | ||
import _get from 'lodash.get' | ||
import template from 'babel-template' | ||
import * as t from 'babel-types' | ||
const isNilWrapper = template('(function (val) { return val === null || typeof val === \'undefined\' })()') | ||
function addIsNilHelper() { | ||
// Modified from https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/file/index.js#L280 | ||
const name = 'isNilWrapper' | ||
let declar = this.declarations[name] | ||
if (declar) { | ||
return declar | ||
} | ||
if (!this.usedHelpers[name]) { | ||
this.metadata.usedHelpers.push(name) | ||
this.usedHelpers[name] = true | ||
} | ||
let generator = this.get('helperGenerator') | ||
let runtime = this.get('helpersNamespace') | ||
if (generator) { | ||
let res = generator(name) | ||
if (res) { | ||
return res | ||
} | ||
} else if (runtime) { | ||
return t.memberExpression(runtime, t.identifier(name)) | ||
} | ||
let ref = isNilWrapper().expression | ||
let uid = this.declarations[name] = this.scope.generateUidIdentifier(name) | ||
ref._compact = true | ||
this.scope.push({ | ||
id: uid, | ||
init: ref, | ||
unique: true | ||
}) | ||
return uid | ||
} | ||
export default function () { | ||
return { | ||
visitor: { | ||
MemberExpression(path) { | ||
MemberExpression(path, state) { | ||
const {node} = path | ||
@@ -50,8 +94,7 @@ const {property} = node | ||
} | ||
const isNilWrapper = addIsNilHelper.call(state.file).name | ||
/* eslint no-void: 0 */ | ||
path.replaceWithSourceString(` | ||
(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = ${name}) || true) && | ||
((typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__)) === 'undefined' || | ||
(typeof window === 'undefined' ? global : window).__TMP_VAL__ === null | ||
`) | ||
path.replaceWithSourceString(`${isNilWrapper}(${name})`) | ||
} | ||
@@ -62,2 +105,1 @@ } | ||
} | ||
@@ -15,2 +15,4 @@ import test from 'ava-spec' | ||
const helper = 'var _isNilWrapper = function (val) { return val === null || typeof val === \'undefined\'; }();' | ||
const specs = [ | ||
@@ -20,3 +22,3 @@ { | ||
before: 'hoge.isNil', | ||
after: `"use strict";\n\n(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = hoge) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null;` | ||
after: `"use strict";\n\n${helper}\n\n_isNilWrapper(hoge);` | ||
}, | ||
@@ -26,3 +28,3 @@ { | ||
before: '!hoge.isNil', | ||
after: `"use strict";\n\n!((((typeof window === 'undefined' ? global : window).__TMP_VAL__ = hoge) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null);` | ||
after: `"use strict";\n\n${helper}\n\n!_isNilWrapper(hoge);` | ||
}, | ||
@@ -37,3 +39,3 @@ { | ||
before: 'foo.bar().isNil', | ||
after: `"use strict";\n\n(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = foo.bar()) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null;` | ||
after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar());` | ||
}, | ||
@@ -43,3 +45,3 @@ { | ||
before: 'foo.bar(hoge).isNil', | ||
after: `"use strict";\n\n(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = foo.bar(hoge)) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null;` | ||
after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar(hoge));` | ||
}, | ||
@@ -49,3 +51,3 @@ { | ||
before: 'foo[0].isNil', | ||
after: `"use strict";\n\n(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = foo[0]) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null;` | ||
after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo[0]);` | ||
}, | ||
@@ -55,3 +57,3 @@ { | ||
before: 'foo.bar["hoge"].isNil', | ||
after: `"use strict";\n\n(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = foo.bar["hoge"]) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null;` | ||
after: `"use strict";\n\n${helper}\n\n_isNilWrapper(foo.bar["hoge"]);` | ||
}, | ||
@@ -61,3 +63,3 @@ { | ||
before: 'bar[hoge].isNil', | ||
after: `"use strict";\n\n(((typeof window === 'undefined' ? global : window).__TMP_VAL__ = bar[hoge]) || true) && typeof (typeof window === 'undefined' ? global : window).__TMP_VAL__ === 'undefined' || (typeof window === 'undefined' ? global : window).__TMP_VAL__ === null;` | ||
after: `"use strict";\n\n${helper}\n\n_isNilWrapper(bar[hoge]);` | ||
} | ||
@@ -64,0 +66,0 @@ ] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14368
250