@bigbinary/eslint-plugin-neeto 1.1.3 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +178 -94
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -121,7 +121,11 @@
121
121
  "@bigbinary/neeto/zustand-use-prop-to-pick-single-item": "error",
122
122
  "@bigbinary/neeto/use-functions-and-hooks-from-neeto-commons-frontend": "warn",
123
123
  "@bigbinary/neeto/no-unnecessary-if-condition": "error",
124
- "@bigbinary/neeto/zustand-use-with-immutable-actions-wrapper-inside-create": "error"
124
+ "@bigbinary/neeto/zustand-use-with-immutable-actions-wrapper-inside-create": "error",
125
+ "@bigbinary/neeto/no-repetition-in-prop-key-value-names": "error",
126
+ "@bigbinary/neeto/merge-spread-attributes": "error",
127
+ "@bigbinary/neeto/props-inside-spread-attributes-should-be-sorted": "error",
128
+ "@bigbinary/neeto/props-with-different-key-value-should-be-outside-spread-attribute": "error"
125
129
  };
126
130
 
127
131
  var recommended = {
@@ -1070,7 +1074,7 @@
1070
1074
  var USE_ZUSTAND_PICK_TRANSFORMER = "Replace the boilerplate with zustand-pick syntax offered by babel-preset-neeto. Refer: https://bit.ly/zustand-pick-transform-doc";
1071
1075
  var HARD_CODED_STRING_SHOULD_BE_LOCALIZED = "Hard-coded string should be localized";
1072
1076
  var PROPS_NOT_SORTED = "Props inside spread attributes should be alphabetically sorted";
1073
- var MERGE_ADJACENT_SPREAD_ATTRIBUTES = "This spread attribute can be merged";
1077
+ var MERGE_ADJACENT_SPREAD_ATTRIBUTES = "Merge spread attributes";
1074
1078
  var DIFFERENT_PROPS_MUST_BE_OUTSIDE = "Props with different key and value should be outside the spread attribute";
1075
1079
  var PROP_KEY_VALUE_DUPLICATE = "Prop key and value names should not be repeated";
1076
1080
  var USE_WITH_IMMUTABLE_ACTIONS_WRAPPER = 'Use "withImmutableActions" wrapper from "neeto-commons-frontend" inside zustand create. Refer: https://bit.ly/withimmutableactions';
@@ -6226,12 +6230,6 @@
6226
6230
  return ((_property$key = property.key) === null || _property$key === void 0 ? void 0 : _property$key.name) !== ((_property$value = property.value) === null || _property$value === void 0 ? void 0 : _property$value.name);
6227
6231
  });
6228
6232
  };
6229
- var isOtherPropsInBetweenJSXAttribute = function isOtherPropsInBetweenJSXAttribute(attributes) {
6230
- return attributes.slice(1, -1).some(pure.matches(OTHER_PROPS));
6231
- };
6232
- var isOtherPropsAtBottomOrBetweenSpreadAttributes = function isOtherPropsAtBottomOrBetweenSpreadAttributes(properties) {
6233
- return properties.slice(1).some(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE));
6234
- };
6235
6233
  var getRepeatedKeyValueProps = function getRepeatedKeyValueProps(attributes) {
6236
6234
  return attributes.filter(function (attribute) {
6237
6235
  var _attribute$value, _attribute$value$expr;
@@ -6274,7 +6272,7 @@
6274
6272
  var jsxSpreadAttributeArray = array.map(function (item) {
6275
6273
  return typeof item === "string" ? "...".concat(item) : item.join(", ");
6276
6274
  });
6277
- return "{...{".concat(jsxSpreadAttributeArray.join(", "), "}}");
6275
+ return "{...{ ".concat(jsxSpreadAttributeArray.join(", "), " }}");
6278
6276
  };
6279
6277
  var getClosestNode = function getClosestNode(errorNode, nodes) {
6280
6278
  var areaToSearch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "before";
@@ -6285,8 +6283,13 @@
6285
6283
  try {
6286
6284
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
6287
6285
  var node = _step.value;
6288
- var distance = errorNode.range[0] - node.range[1];
6289
- if ((areaToSearch === "before" && distance > 0 || areaToSearch === "after" && distance < 0) && distance < closestDistance) {
6286
+ var distance = 0;
6287
+ if (areaToSearch === "before") {
6288
+ distance = errorNode.range[0] - node.range[1];
6289
+ } else if (areaToSearch === "after") {
6290
+ distance = node.range[0] - errorNode.range[1];
6291
+ }
6292
+ if (distance > 0 && distance < closestDistance) {
6290
6293
  closestNode = node;
6291
6294
  closestDistance = distance;
6292
6295
  }
@@ -6298,6 +6301,34 @@
6298
6301
  }
6299
6302
  return closestNode;
6300
6303
  };
6304
+ var removeNodeAndNewLine = function removeNodeAndNewLine(_ref) {
6305
+ var initialAttributes = _ref.initialAttributes,
6306
+ startNode = _ref.startNode,
6307
+ endNode = _ref.endNode,
6308
+ context = _ref.context,
6309
+ fixer = _ref.fixer,
6310
+ node = _ref.node;
6311
+ var previousNode = null;
6312
+ var index = initialAttributes.indexOf(startNode);
6313
+ if (index > 0) {
6314
+ previousNode = initialAttributes[index - 1];
6315
+ } else {
6316
+ previousNode = node.name;
6317
+ }
6318
+ return fixer.removeRange([context.getSourceCode().getIndexFromLoc({
6319
+ line: previousNode.loc.end.line,
6320
+ column: previousNode.loc.end.column
6321
+ }), context.getSourceCode().getIndexFromLoc({
6322
+ line: endNode.loc.end.line,
6323
+ column: endNode.loc.end.column
6324
+ })]);
6325
+ };
6326
+ var removeArrayElement = function removeArrayElement(array, elementToRemove) {
6327
+ var index = array.indexOf(elementToRemove);
6328
+ if (index > -1) {
6329
+ array.splice(index, 1);
6330
+ }
6331
+ };
6301
6332
 
6302
6333
  var mergeSpreadAttributes = {
6303
6334
  meta: {
@@ -6311,61 +6342,113 @@
6311
6342
  create: function create(context) {
6312
6343
  return {
6313
6344
  JSXOpeningElement: function JSXOpeningElement(node) {
6314
- var attributes = node.attributes;
6315
- var spreadAttributes = attributes.filter(pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION));
6316
- if (ramda.isEmpty(spreadAttributes)) return;
6317
- var otherProps = attributes.filter(pure.matches(OTHER_PROPS));
6318
- otherProps.forEach(function (otherProp) {
6319
- var currentIndex = attributes.findIndex(function (attr) {
6320
- return attr === otherProp;
6321
- });
6322
- var adjacentNode = null;
6323
- var positionToInsert = "top";
6324
- if (currentIndex > 0 && pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, attributes[currentIndex - 1])) {
6325
- adjacentNode = attributes[currentIndex - 1];
6326
- positionToInsert = "bottom";
6327
- } else if (currentIndex < attributes.length - 1 && pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, attributes[currentIndex + 1])) {
6328
- adjacentNode = attributes[currentIndex + 1];
6329
- }
6330
- if (adjacentNode) {
6331
- context.report({
6332
- node: otherProp,
6333
- message: MERGE_ADJACENT_SPREAD_ATTRIBUTES,
6334
- fix: function fix(fixer) {
6335
- var properties = adjacentNode.argument.properties;
6336
- var propertyToMerge = context.getSourceCode().getText(otherProp.argument);
6337
- var fixToInsertOtherPropInSpreadAttribute = positionToInsert === "top" ? fixer.insertTextBefore(properties[0], "...".concat(propertyToMerge, ", ")) : fixer.insertTextAfter(properties[properties.length - 1], ", ...".concat(propertyToMerge));
6338
- var fixToRemoveOtherProp = fixer.remove(otherProp);
6339
- return [fixToInsertOtherPropInSpreadAttribute, fixToRemoveOtherProp];
6340
- }
6341
- });
6342
- }
6343
- });
6344
- var _loop = function _loop(index) {
6345
- var startIndex = attributes.indexOf(spreadAttributes[index - 1]);
6346
- var endIndex = attributes.indexOf(spreadAttributes[index]);
6347
- var attributesInBetween = attributes.slice(startIndex, endIndex + 1);
6348
- if (!isOtherPropsInBetweenJSXAttribute(attributesInBetween)) {
6349
- var isOtherPropsPresent = spreadAttributes[index].argument.properties.find(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE));
6350
- if (attributesInBetween.length === 2 || !isOtherPropsPresent) {
6345
+ var initialAttributes = node.attributes;
6346
+ if (ramda.isEmpty(pure.filterBy(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, initialAttributes))) {
6347
+ return;
6348
+ }
6349
+ var adjacentSpreadAttributes = [];
6350
+ var isAdjacentAttributesFixApplied = false;
6351
+ initialAttributes.forEach(function (currentAttribute, index) {
6352
+ if (!pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, currentAttribute) && !pure.matches(OTHER_PROPS, currentAttribute) || index === initialAttributes.length - 1) {
6353
+ if (pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, currentAttribute) || pure.matches(OTHER_PROPS, currentAttribute)) {
6354
+ adjacentSpreadAttributes.push(currentAttribute);
6355
+ }
6356
+ if (adjacentSpreadAttributes.length > 1) {
6351
6357
  context.report({
6352
- node: spreadAttributes[index],
6358
+ node: node,
6353
6359
  message: MERGE_ADJACENT_SPREAD_ATTRIBUTES,
6354
6360
  fix: function fix(fixer) {
6355
- var propertiesToMerge = spreadAttributes[index].argument.properties.map(function (item) {
6356
- return context.getSourceCode().getText(item);
6361
+ var mergedProperties = adjacentSpreadAttributes.map(function (item) {
6362
+ if (pure.matches(OTHER_PROPS, item)) {
6363
+ return "...".concat(context.getSourceCode().getText(item.argument));
6364
+ }
6365
+ return item.argument.properties.map(function (item) {
6366
+ return context.getSourceCode().getText(item);
6367
+ }).join(", ");
6368
+ }).join(", ");
6369
+ var fixToAddProperties = fixer.insertTextAfter(ramda.last(adjacentSpreadAttributes), "\n{...{ ".concat(mergedProperties, " }}"));
6370
+ isAdjacentAttributesFixApplied = true;
6371
+ if (node.loc.start.line !== node.loc.end.line) {
6372
+ var _fixToRemoveProperties = removeNodeAndNewLine({
6373
+ initialAttributes: initialAttributes,
6374
+ startNode: adjacentSpreadAttributes[0],
6375
+ endNode: ramda.last(adjacentSpreadAttributes),
6376
+ context: context,
6377
+ fixer: fixer,
6378
+ node: node
6379
+ });
6380
+ return [fixToAddProperties, _fixToRemoveProperties];
6381
+ }
6382
+ var fixToRemoveProperties = adjacentSpreadAttributes.map(function (item) {
6383
+ return fixer.remove(item);
6357
6384
  });
6358
- var fixToMergeSpreadAttributes = fixer.insertTextAfter(spreadAttributes[index - 1].argument.properties.slice(-1)[0], ", ".concat(propertiesToMerge.join(", ")));
6359
- var fixToRemoveSpreadAttribute = fixer.remove(spreadAttributes[index]);
6360
- return [fixToRemoveSpreadAttribute, fixToMergeSpreadAttributes];
6385
+ return [fixToAddProperties].concat(_toConsumableArray(fixToRemoveProperties));
6361
6386
  }
6362
6387
  });
6363
6388
  }
6389
+ adjacentSpreadAttributes = [];
6390
+ } else {
6391
+ adjacentSpreadAttributes.push(currentAttribute);
6364
6392
  }
6365
- };
6366
- for (var index = 1; index < spreadAttributes.length; index++) {
6367
- _loop(index);
6368
- }
6393
+ });
6394
+ if (isAdjacentAttributesFixApplied) return;
6395
+ var attributes = [];
6396
+ initialAttributes.forEach(function (currentAttribute, index) {
6397
+ if (pure.matches(OTHER_PROPS, currentAttribute) || index === initialAttributes.length - 1) {
6398
+ if (!pure.matches(OTHER_PROPS, currentAttribute)) {
6399
+ attributes.push(currentAttribute);
6400
+ }
6401
+ if (pure.isNotEmpty(attributes)) {
6402
+ var spreadAttributes = pure.filterBy(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, attributes);
6403
+ var spreadAttributesWithoutOtherProps = spreadAttributes.filter(function (item) {
6404
+ return !item.argument.properties.find(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE));
6405
+ }).reverse();
6406
+ if (pure.isNotEmpty(spreadAttributesWithoutOtherProps) && spreadAttributes.length > 1) {
6407
+ context.report({
6408
+ node: node,
6409
+ message: MERGE_ADJACENT_SPREAD_ATTRIBUTES,
6410
+ fix: function fix(fixer) {
6411
+ var fix = spreadAttributesWithoutOtherProps.flatMap(function (errorNode) {
6412
+ var fixToAddProperties = null;
6413
+ var properties = errorNode.argument.properties.map(function (item) {
6414
+ return context.getSourceCode().getText(item);
6415
+ }).join(", ");
6416
+ var closestNode = getClosestNode(errorNode, spreadAttributes);
6417
+ var isClosestNodeNotAdjacent = closestNode && errorNode.range[1] !== closestNode.range[0];
6418
+ if (isClosestNodeNotAdjacent) {
6419
+ fixToAddProperties = fixer.insertTextAfter(ramda.last(closestNode.argument.properties), ", \n".concat(properties));
6420
+ } else {
6421
+ closestNode = getClosestNode(errorNode, spreadAttributes, "after");
6422
+ var _isClosestNodeNotAdjacent = closestNode && closestNode.range[1] !== errorNode.range[0];
6423
+ if (_isClosestNodeNotAdjacent) {
6424
+ fixToAddProperties = fixer.insertTextBefore(closestNode.argument.properties[0], "".concat(properties, ",\n"));
6425
+ }
6426
+ }
6427
+ removeArrayElement(spreadAttributesWithoutOtherProps, closestNode);
6428
+ removeArrayElement(spreadAttributes, errorNode);
6429
+ if (fixToAddProperties) {
6430
+ var fixToRemoveProperties = node.loc.start.line !== node.loc.end.line ? removeNodeAndNewLine({
6431
+ initialAttributes: initialAttributes,
6432
+ startNode: errorNode,
6433
+ endNode: errorNode,
6434
+ context: context,
6435
+ fixer: fixer,
6436
+ node: node
6437
+ }) : fixer.remove(errorNode);
6438
+ return [fixToAddProperties, fixToRemoveProperties];
6439
+ }
6440
+ return null;
6441
+ });
6442
+ return fix.filter(Boolean);
6443
+ }
6444
+ });
6445
+ }
6446
+ attributes = [];
6447
+ }
6448
+ } else {
6449
+ attributes.push(currentAttribute);
6450
+ }
6451
+ });
6369
6452
  }
6370
6453
  };
6371
6454
  }
@@ -6412,7 +6495,7 @@
6412
6495
  fixToAddProperty = fixer.insertTextBefore(closestSpreadAttribute.argument.properties[0], "".concat(errorNodeName, ", "));
6413
6496
  }
6414
6497
  } else {
6415
- fixToAddProperty = fixer.insertTextBefore(attributes[0], "{...{ ".concat(errorNodeName, "}}"));
6498
+ return fixer.replaceText(errorNode, "{...{ ".concat(errorNodeName, " }}"));
6416
6499
  }
6417
6500
  return [fixToAddProperty, fixer.remove(errorNode)];
6418
6501
  });
@@ -6473,44 +6556,45 @@
6473
6556
  },
6474
6557
  create: function create(context) {
6475
6558
  return {
6476
- JSXOpeningElement: function JSXOpeningElement(node) {
6477
- var existingSpreadAttributesProperties = node.attributes.flatMap(function (item) {
6478
- return pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, item) ? item.argument.properties : [];
6479
- });
6480
- var nonRepeatedKeyValueProperties = getNonRepeatedKeyValueProperties(existingSpreadAttributesProperties);
6481
- if (ramda.isEmpty(nonRepeatedKeyValueProperties)) return;
6482
- var isNotFixable = isOtherPropsInBetweenJSXAttribute(node.attributes) || isOtherPropsAtBottomOrBetweenSpreadAttributes(existingSpreadAttributesProperties);
6483
- if (isNotFixable) {
6484
- nonRepeatedKeyValueProperties.map(function (errorNode) {
6485
- return context.report({
6486
- node: errorNode,
6487
- message: DIFFERENT_PROPS_MUST_BE_OUTSIDE
6488
- });
6489
- });
6559
+ JSXSpreadAttribute: function JSXSpreadAttribute(node) {
6560
+ if (!pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, node)) {
6490
6561
  return;
6491
6562
  }
6492
- nonRepeatedKeyValueProperties.map(function (errorNode) {
6493
- return context.report({
6494
- node: errorNode,
6495
- message: DIFFERENT_PROPS_MUST_BE_OUTSIDE,
6496
- fix: function fix(fixer) {
6497
- var fixToRemoveComma = null;
6498
- var fixToRemoveErrorNode = fixer.remove(errorNode);
6499
- var commaTokenBefore = context.getTokenBefore(errorNode);
6500
- var commaTokenAfter = context.getTokenAfter(errorNode);
6501
- if (commaTokenAfter.value === ",") {
6502
- fixToRemoveComma = fixer.remove(commaTokenAfter);
6503
- } else if (commaTokenBefore.value === ",") {
6504
- fixToRemoveComma = fixer.remove(commaTokenBefore);
6505
- } else {
6506
- fixToRemoveErrorNode = fixer.remove(errorNode.parent.parent);
6563
+ var properties = node.argument.properties;
6564
+ var nonRepeatedKeyValueProperties = getNonRepeatedKeyValueProperties(properties);
6565
+ if (ramda.isEmpty(nonRepeatedKeyValueProperties)) return;
6566
+ nonRepeatedKeyValueProperties.forEach(function (property) {
6567
+ var positionToInsert = null;
6568
+ var index = properties.indexOf(property);
6569
+ var nodesBeforeTarget = properties.slice(0, index);
6570
+ var nodesAfterTarget = properties.slice(index + 1);
6571
+ if (!nodesAfterTarget.find(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE))) {
6572
+ positionToInsert = "below";
6573
+ } else if (!nodesBeforeTarget.find(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE))) {
6574
+ positionToInsert = "above";
6575
+ }
6576
+ if (positionToInsert) {
6577
+ context.report({
6578
+ node: property,
6579
+ message: DIFFERENT_PROPS_MUST_BE_OUTSIDE,
6580
+ fix: function fix(fixer) {
6581
+ var fixToRemoveComma = null;
6582
+ var fixToRemoveErrorNode = fixer.remove(property);
6583
+ var tokenBefore = context.getTokenBefore(property);
6584
+ var tokenAfter = context.getTokenAfter(property);
6585
+ if (tokenAfter.value === ",") {
6586
+ fixToRemoveComma = fixer.remove(tokenAfter);
6587
+ } else if (tokenBefore.value === ",") {
6588
+ fixToRemoveComma = fixer.remove(tokenBefore);
6589
+ } else {
6590
+ fixToRemoveErrorNode = fixer.remove(node);
6591
+ }
6592
+ var indentLevel = context.getSourceCode().getFirstToken(node.parent.attributes[0]).loc.start.column;
6593
+ var fixToAddProperty = positionToInsert === "below" ? fixer.insertTextAfter(node, "\n".concat(" ".repeat(indentLevel)).concat(property.key.name, "={").concat(property.value.name, "}")) : fixer.insertTextBefore(node, "".concat(property.key.name, "={").concat(property.value.name, "}\n").concat(" ".repeat(indentLevel)));
6594
+ return [fixToRemoveComma, fixToRemoveErrorNode, fixToAddProperty].filter(Boolean);
6507
6595
  }
6508
- var indentLevel = context.getSourceCode().getFirstToken(node.attributes[0]).loc.start.column;
6509
- var newAttribute = "\n".concat(" ".repeat(indentLevel)).concat(errorNode.key.name, "={").concat(errorNode.value.name, "}");
6510
- var fixToAddNewAttribute = fixer.insertTextAfter(errorNode.parent.parent, newAttribute);
6511
- return [fixToRemoveComma, fixToRemoveErrorNode, fixToAddNewAttribute].filter(Boolean);
6512
- }
6513
- });
6596
+ });
6597
+ }
6514
6598
  });
6515
6599
  }
6516
6600
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {