checkOrder.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. let stylelint = require('stylelint');
  2. let ruleName = require('./ruleName');
  3. let messages = require('./messages');
  4. // eslint-disable-next-line max-params, consistent-return
  5. module.exports = function checkOrder({
  6. firstNodeData,
  7. secondNodeData,
  8. allNodesData,
  9. isFixEnabled,
  10. result,
  11. unspecified,
  12. }) {
  13. let firstNodeIsSpecified = Boolean(firstNodeData.expectedPosition);
  14. let secondNodeIsSpecified = Boolean(secondNodeData.expectedPosition);
  15. // If both nodes have their position
  16. if (firstNodeIsSpecified && secondNodeIsSpecified) {
  17. return firstNodeData.expectedPosition <= secondNodeData.expectedPosition;
  18. }
  19. if (!firstNodeIsSpecified && secondNodeIsSpecified) {
  20. // If first node is unspecified, look for a specified node before it
  21. // to compare to the current node
  22. let priorSpecifiedNodeData = allNodesData
  23. .slice(0, -1)
  24. .reverse()
  25. .find((node) => Boolean(node.expectedPosition));
  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. };