@bigbinary/eslint-plugin-neeto 1.1.2 → 1.1.4

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 +137 -52
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1070,7 +1070,7 @@
1070
1070
  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
1071
  var HARD_CODED_STRING_SHOULD_BE_LOCALIZED = "Hard-coded string should be localized";
1072
1072
  var PROPS_NOT_SORTED = "Props inside spread attributes should be alphabetically sorted";
1073
- var MERGE_ADJACENT_SPREAD_ATTRIBUTES = "This spread attribute can be merged";
1073
+ var MERGE_ADJACENT_SPREAD_ATTRIBUTES = "Merge spread attributes";
1074
1074
  var DIFFERENT_PROPS_MUST_BE_OUTSIDE = "Props with different key and value should be outside the spread attribute";
1075
1075
  var PROP_KEY_VALUE_DUPLICATE = "Prop key and value names should not be repeated";
1076
1076
  var USE_WITH_IMMUTABLE_ACTIONS_WRAPPER = 'Use "withImmutableActions" wrapper from "neeto-commons-frontend" inside zustand create. Refer: https://bit.ly/withimmutableactions';
@@ -6274,7 +6274,7 @@
6274
6274
  var jsxSpreadAttributeArray = array.map(function (item) {
6275
6275
  return typeof item === "string" ? "...".concat(item) : item.join(", ");
6276
6276
  });
6277
- return "{...{".concat(jsxSpreadAttributeArray.join(", "), "}}");
6277
+ return "{...{ ".concat(jsxSpreadAttributeArray.join(", "), " }}");
6278
6278
  };
6279
6279
  var getClosestNode = function getClosestNode(errorNode, nodes) {
6280
6280
  var areaToSearch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "before";
@@ -6285,8 +6285,13 @@
6285
6285
  try {
6286
6286
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
6287
6287
  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) {
6288
+ var distance = 0;
6289
+ if (areaToSearch === "before") {
6290
+ distance = errorNode.range[0] - node.range[1];
6291
+ } else if (areaToSearch === "after") {
6292
+ distance = node.range[0] - errorNode.range[1];
6293
+ }
6294
+ if (distance > 0 && distance < closestDistance) {
6290
6295
  closestNode = node;
6291
6296
  closestDistance = distance;
6292
6297
  }
@@ -6298,6 +6303,34 @@
6298
6303
  }
6299
6304
  return closestNode;
6300
6305
  };
6306
+ var removeNodeAndNewLine = function removeNodeAndNewLine(_ref) {
6307
+ var initialAttributes = _ref.initialAttributes,
6308
+ startNode = _ref.startNode,
6309
+ endNode = _ref.endNode,
6310
+ context = _ref.context,
6311
+ fixer = _ref.fixer,
6312
+ node = _ref.node;
6313
+ var previousNode = null;
6314
+ var index = initialAttributes.indexOf(startNode);
6315
+ if (index > 0) {
6316
+ previousNode = initialAttributes[index - 1];
6317
+ } else {
6318
+ previousNode = node.name;
6319
+ }
6320
+ return fixer.removeRange([context.getSourceCode().getIndexFromLoc({
6321
+ line: previousNode.loc.end.line,
6322
+ column: previousNode.loc.end.column
6323
+ }), context.getSourceCode().getIndexFromLoc({
6324
+ line: endNode.loc.end.line,
6325
+ column: endNode.loc.end.column
6326
+ })]);
6327
+ };
6328
+ var removeArrayElement = function removeArrayElement(array, elementToRemove) {
6329
+ var index = array.indexOf(elementToRemove);
6330
+ if (index > -1) {
6331
+ array.splice(index, 1);
6332
+ }
6333
+ };
6301
6334
 
6302
6335
  var mergeSpreadAttributes = {
6303
6336
  meta: {
@@ -6311,61 +6344,113 @@
6311
6344
  create: function create(context) {
6312
6345
  return {
6313
6346
  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) {
6347
+ var initialAttributes = node.attributes;
6348
+ if (ramda.isEmpty(pure.filterBy(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, initialAttributes))) {
6349
+ return;
6350
+ }
6351
+ var adjacentSpreadAttributes = [];
6352
+ var isAdjacentAttributesFixApplied = false;
6353
+ initialAttributes.forEach(function (currentAttribute, index) {
6354
+ if (!pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, currentAttribute) && !pure.matches(OTHER_PROPS, currentAttribute) || index === initialAttributes.length - 1) {
6355
+ if (pure.matches(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, currentAttribute) || pure.matches(OTHER_PROPS, currentAttribute)) {
6356
+ adjacentSpreadAttributes.push(currentAttribute);
6357
+ }
6358
+ if (adjacentSpreadAttributes.length > 1) {
6351
6359
  context.report({
6352
- node: spreadAttributes[index],
6360
+ node: node,
6353
6361
  message: MERGE_ADJACENT_SPREAD_ATTRIBUTES,
6354
6362
  fix: function fix(fixer) {
6355
- var propertiesToMerge = spreadAttributes[index].argument.properties.map(function (item) {
6356
- return context.getSourceCode().getText(item);
6363
+ var mergedProperties = adjacentSpreadAttributes.map(function (item) {
6364
+ if (pure.matches(OTHER_PROPS, item)) {
6365
+ return "...".concat(context.getSourceCode().getText(item.argument));
6366
+ }
6367
+ return item.argument.properties.map(function (item) {
6368
+ return context.getSourceCode().getText(item);
6369
+ }).join(", ");
6370
+ }).join(", ");
6371
+ var fixToAddProperties = fixer.insertTextAfter(ramda.last(adjacentSpreadAttributes), "\n{...{ ".concat(mergedProperties, " }}"));
6372
+ isAdjacentAttributesFixApplied = true;
6373
+ if (node.loc.start.line !== node.loc.end.line) {
6374
+ var _fixToRemoveProperties = removeNodeAndNewLine({
6375
+ initialAttributes: initialAttributes,
6376
+ startNode: adjacentSpreadAttributes[0],
6377
+ endNode: ramda.last(adjacentSpreadAttributes),
6378
+ context: context,
6379
+ fixer: fixer,
6380
+ node: node
6381
+ });
6382
+ return [fixToAddProperties, _fixToRemoveProperties];
6383
+ }
6384
+ var fixToRemoveProperties = adjacentSpreadAttributes.map(function (item) {
6385
+ return fixer.remove(item);
6357
6386
  });
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];
6387
+ return [fixToAddProperties].concat(_toConsumableArray(fixToRemoveProperties));
6361
6388
  }
6362
6389
  });
6363
6390
  }
6391
+ adjacentSpreadAttributes = [];
6392
+ } else {
6393
+ adjacentSpreadAttributes.push(currentAttribute);
6364
6394
  }
6365
- };
6366
- for (var index = 1; index < spreadAttributes.length; index++) {
6367
- _loop(index);
6368
- }
6395
+ });
6396
+ if (isAdjacentAttributesFixApplied) return;
6397
+ var attributes = [];
6398
+ initialAttributes.forEach(function (currentAttribute, index) {
6399
+ if (pure.matches(OTHER_PROPS, currentAttribute) || index === initialAttributes.length - 1) {
6400
+ if (!pure.matches(OTHER_PROPS, currentAttribute)) {
6401
+ attributes.push(currentAttribute);
6402
+ }
6403
+ if (pure.isNotEmpty(attributes)) {
6404
+ var spreadAttributes = pure.filterBy(SPREAD_ATTRIBUTE_WITH_OBJECT_EXPRESSION, attributes);
6405
+ var spreadAttributesWithoutOtherProps = spreadAttributes.filter(function (item) {
6406
+ return !item.argument.properties.find(pure.matches(OTHER_PROPS_INSIDE_SPREAD_ATTRIBUTE));
6407
+ }).reverse();
6408
+ if (pure.isNotEmpty(spreadAttributesWithoutOtherProps) && spreadAttributes.length > 1) {
6409
+ context.report({
6410
+ node: node,
6411
+ message: MERGE_ADJACENT_SPREAD_ATTRIBUTES,
6412
+ fix: function fix(fixer) {
6413
+ var fix = spreadAttributesWithoutOtherProps.flatMap(function (errorNode) {
6414
+ var fixToAddProperties = null;
6415
+ var properties = errorNode.argument.properties.map(function (item) {
6416
+ return context.getSourceCode().getText(item);
6417
+ }).join(", ");
6418
+ var closestNode = getClosestNode(errorNode, spreadAttributes);
6419
+ var isClosestNodeNotAdjacent = closestNode && errorNode.range[1] !== closestNode.range[0];
6420
+ if (isClosestNodeNotAdjacent) {
6421
+ fixToAddProperties = fixer.insertTextAfter(ramda.last(closestNode.argument.properties), ", \n".concat(properties));
6422
+ } else {
6423
+ closestNode = getClosestNode(errorNode, spreadAttributes, "after");
6424
+ var _isClosestNodeNotAdjacent = closestNode && closestNode.range[1] !== errorNode.range[0];
6425
+ if (_isClosestNodeNotAdjacent) {
6426
+ fixToAddProperties = fixer.insertTextBefore(closestNode.argument.properties[0], "".concat(properties, ",\n"));
6427
+ }
6428
+ }
6429
+ removeArrayElement(spreadAttributesWithoutOtherProps, closestNode);
6430
+ removeArrayElement(spreadAttributes, errorNode);
6431
+ if (fixToAddProperties) {
6432
+ var fixToRemoveProperties = node.loc.start.line !== node.loc.end.line ? removeNodeAndNewLine({
6433
+ initialAttributes: initialAttributes,
6434
+ startNode: errorNode,
6435
+ endNode: errorNode,
6436
+ context: context,
6437
+ fixer: fixer,
6438
+ node: node
6439
+ }) : fixer.remove(errorNode);
6440
+ return [fixToAddProperties, fixToRemoveProperties];
6441
+ }
6442
+ return null;
6443
+ });
6444
+ return fix.filter(Boolean);
6445
+ }
6446
+ });
6447
+ }
6448
+ attributes = [];
6449
+ }
6450
+ } else {
6451
+ attributes.push(currentAttribute);
6452
+ }
6453
+ });
6369
6454
  }
6370
6455
  };
6371
6456
  }
@@ -6412,7 +6497,7 @@
6412
6497
  fixToAddProperty = fixer.insertTextBefore(closestSpreadAttribute.argument.properties[0], "".concat(errorNodeName, ", "));
6413
6498
  }
6414
6499
  } else {
6415
- fixToAddProperty = fixer.insertTextBefore(attributes[0], "{...{ ".concat(errorNodeName, "}}"));
6500
+ return fixer.replaceText(errorNode, "{...{ ".concat(errorNodeName, " }}"));
6416
6501
  }
6417
6502
  return [fixToAddProperty, fixer.remove(errorNode)];
6418
6503
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigbinary/eslint-plugin-neeto",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "main": "dist/index.js",
5
5
  "license": "UNLICENSED",
6
6
  "scripts": {