Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-idx

Package Overview
Dependencies
Maintainers
3
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-idx - npm Package Compare versions

Comparing version 1.4.0 to 1.5.0

106

lib/babel-plugin-idx.js

@@ -12,6 +12,2 @@ /**

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
module.exports = function (context) {

@@ -22,52 +18,2 @@ var t = context.types;

var TransformedTernary = function () {
function TransformedTernary(expression, generateUid) {
_classCallCheck(this, TransformedTernary);
this.generateUid = generateUid;
this.uids = [];
this.expression = expression;
this.deepestTernary = null;
this.deepestExpression = expression;
}
_createClass(TransformedTernary, [{
key: 'constructTernary',
value: function constructTernary(oldExpression, newExpression, uid) {
return t.ConditionalExpression(t.BinaryExpression('!=', t.AssignmentExpression('=', t.Identifier(uid), oldExpression), t.NullLiteral()), newExpression, t.Identifier(uid));
}
}, {
key: 'addLevel',
value: function addLevel(expression, uid) {
var ternary = this.constructTernary(this.deepestExpression, expression, uid);
if (this.deepestTernary === null) {
this.expression = ternary;
} else {
this.deepestTernary.consequent = ternary;
}
this.deepestTernary = ternary;
this.deepestExpression = expression;
}
}, {
key: 'appendMethodCall',
value: function appendMethodCall(args) {
var uid = this.generateUid('ref').name;
var callExpression = t.CallExpression(t.Identifier(uid), args || []);
this.addLevel(callExpression, uid);
this.uids.push(uid);
}
}, {
key: 'appendPropertyAccess',
value: function appendPropertyAccess(property, computed) {
var uid = this.generateUid('ref').name;
var accessedProperty = t.MemberExpression(t.Identifier(uid), property, computed);
this.addLevel(accessedProperty, uid);
this.uids.push(uid);
}
}]);
return TransformedTernary;
}();
function checkIdxArguments(file, node) {

@@ -88,13 +34,11 @@ var args = node.arguments;

}
var bodyChainBase = getExpressionChainBase(arrowFunction.body);
if (!t.isIdentifier(arrowFunction.params[0]) || !t.isIdentifier(bodyChainBase) || arrowFunction.params[0].name !== bodyChainBase.name) {
throw file.buildCodeFrameError(arrowFunction.params[0], 'The parameter of the arrow function supplied to `idx` must match ' + 'the base of the body expression.');
var input = arrowFunction.params[0];
if (!t.isIdentifier(input)) {
throw file.buildCodeFrameError(arrowFunction.params[0], 'The parameter supplied to `idx` must be an identifier.');
}
}
function getExpressionChainBase(node) {
if (t.isCallExpression(node)) {
return getExpressionChainBase(node.callee);
} else if (t.isMemberExpression(node)) {
return getExpressionChainBase(node.object);
function makeCondition(node, state, inside) {
if (inside) {
return t.ConditionalExpression(t.BinaryExpression('!=', t.AssignmentExpression('=', state.temp, node), t.NullLiteral()), inside, state.temp);
} else {

@@ -105,17 +49,14 @@ return node;

function isIdxCall(node) {
return t.isCallExpression(node) && t.isIdentifier(node.callee) && node.callee.name === 'idx';
}
function constructTernary(base, node, generateUid) {
function makeChain(node, state, inside) {
if (t.isCallExpression(node)) {
var transformedObject = constructTernary(base, node.callee, generateUid);
transformedObject.appendMethodCall(node.arguments);
return transformedObject;
return makeChain(node.callee, state, makeCondition(t.CallExpression(state.temp, node.arguments), state, inside));
} else if (t.isMemberExpression(node)) {
var _transformedObject = constructTernary(base, node.object, generateUid);
_transformedObject.appendPropertyAccess(node.property, node.computed);
return _transformedObject;
return makeChain(node.object, state, makeCondition(t.MemberExpression(state.temp, node.property, node.computed), state, inside));
} else if (t.isIdentifier(node)) {
if (node.name !== state.base.name) {
throw state.file.buildCodeFrameError(node, 'The parameter of the arrow function supplied to `idx` must match ' + 'the base of the body expression.');
}
return makeCondition(state.input, state, inside);
} else {
return new TransformedTernary(base, generateUid);
throw state.file.buildCodeFrameError(node, 'The `idx` body can only be composed of properties and methods.');
}

@@ -127,10 +68,13 @@ }

var node = path.node;
if (isIdxCall(node)) {
if (t.isIdentifier(node.callee) && node.callee.name === 'idx') {
checkIdxArguments(state.file, node);
var ternary = constructTernary(node.arguments[0], node.arguments[1].body, path.scope.generateUidIdentifier.bind(path.scope));
path.replaceWith(ternary.expression);
for (var ii = 0; ii < ternary.uids.length; ii++) {
var uid = ternary.uids[ii];
path.scope.push({ id: t.Identifier(uid) });
}
var temp = path.scope.generateUidIdentifier('ref');
var replacement = makeChain(node.arguments[1].body, {
file: state.file,
input: node.arguments[0],
base: node.arguments[1].params[0],
temp: temp
});
path.scope.push({ id: temp });
path.replaceWith(replacement);
}

@@ -137,0 +81,0 @@ }

{
"name": "babel-plugin-idx",
"version": "1.4.0",
"version": "1.5.0",
"description": "Babel plugin for transforming the idx utility function.",

@@ -26,4 +26,5 @@ "main": "lib/babel-plugin-idx.js",

"jest": {
"testEnvironment": "node",
"rootDir": "src"
}
}

@@ -1,2 +0,2 @@

# idx
# idx [![Circle Status](https://circleci.com/gh/facebookincubator/idx/tree/master.svg?style=shield&circle-token=da61f3cf105f22309c8ca0ba4482daa538bf5349)](https://circleci.com/gh/facebookincubator/idx)

@@ -13,3 +13,3 @@ `idx` is a utility function for traversing properties on objects and arrays.

```
```javascript
const props: {

@@ -25,3 +25,3 @@ user: ?{

```
```javascript
props.user &&

@@ -35,3 +35,3 @@ props.user.friends &&

```
```javascript
idx(props, _ => _.user.friends[0].friends)

@@ -49,3 +49,3 @@ ```

```
```javascript
props.user == null ? props.user :

@@ -52,0 +52,0 @@ props.user.friends == null ? props.user.friends :

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc