checkOrder.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. let stylelint = require('stylelint');
  2. let _ = require('lodash');
  3. let ruleName = require('./ruleName');
  4. let messages = require('./messages');
  5. // eslint-disable-next-line max-params, consistent-return
  6. module.exports = function checkOrder({
  7. firstNodeData,
  8. secondNodeData,
  9. allNodesData,
  10. isFixEnabled,
  11. result,
  12. unspecified,
  13. }) {
  14. let firstNodeIsSpecified = Boolean(firstNodeData.expectedPosition);
  15. let secondNodeIsSpecified = Boolean(secondNodeData.expectedPosition);
  16. // If both nodes have their position
  17. if (firstNodeIsSpecified && secondNodeIsSpecified) {
  18. return firstNodeData.expectedPosition <= secondNodeData.expectedPosition;
  19. }
  20. if (!firstNodeIsSpecified && secondNodeIsSpecified) {
  21. // If first node is unspecified, look for a specified node before it
  22. // to compare to the current node
  23. let priorSpecifiedNodeData = _.findLast(allNodesData.slice(0, -1), (d) =>
  24. Boolean(d.expectedPosition)
  25. );
  26. if (
  27. priorSpecifiedNodeData &&
  28. priorSpecifiedNodeData.expectedPosition &&
  29. priorSpecifiedNodeData.expectedPosition > secondNodeData.expectedPosition
  30. ) {
  31. if (isFixEnabled) {
  32. // Don't go further, fix will be applied
  33. return false;
  34. }
  35. stylelint.utils.report({
  36. message: messages.expected(
  37. secondNodeData.description,
  38. priorSpecifiedNodeData.description
  39. ),
  40. node: secondNodeData.node,
  41. result,
  42. ruleName,
  43. });
  44. // avoid logging another warning
  45. return true;
  46. }
  47. }
  48. if (!firstNodeIsSpecified && !secondNodeIsSpecified) {
  49. return true;
  50. }
  51. if (unspecified === 'ignore' && (!firstNodeIsSpecified || !secondNodeIsSpecified)) {
  52. return true;
  53. }
  54. if (unspecified === 'top' && !firstNodeIsSpecified) {
  55. return true;
  56. }
  57. if (unspecified === 'top' && !secondNodeIsSpecified) {
  58. return false;
  59. }
  60. if (unspecified === 'bottom' && !secondNodeIsSpecified) {
  61. return true;
  62. }
  63. if (unspecified === 'bottom' && !firstNodeIsSpecified) {
  64. return false;
  65. }
  66. };