checkOrder.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const postcss = require('postcss');
  2. const _ = require('lodash');
  3. const checkAlphabeticalOrder = require('../checkAlphabeticalOrder');
  4. // eslint-disable-next-line consistent-return
  5. module.exports = function checkOrder({
  6. firstPropertyData,
  7. secondPropertyData,
  8. allPropertiesData,
  9. unspecified,
  10. }) {
  11. function report(isCorrect, firstNode = firstPropertyData, secondNode = secondPropertyData) {
  12. return {
  13. isCorrect,
  14. firstNode,
  15. secondNode,
  16. };
  17. }
  18. if (firstPropertyData.unprefixedName === secondPropertyData.unprefixedName) {
  19. // If first property has no prefix and second property has prefix
  20. if (
  21. !postcss.vendor.prefix(firstPropertyData.name).length &&
  22. postcss.vendor.prefix(secondPropertyData.name).length
  23. ) {
  24. return report(false);
  25. }
  26. return report(true);
  27. }
  28. const firstPropIsSpecified = Boolean(firstPropertyData.orderData);
  29. const secondPropIsSpecified = Boolean(secondPropertyData.orderData);
  30. // Check actual known properties
  31. if (firstPropIsSpecified && secondPropIsSpecified) {
  32. return report(
  33. firstPropertyData.orderData.expectedPosition <=
  34. secondPropertyData.orderData.expectedPosition
  35. );
  36. }
  37. if (!firstPropIsSpecified && secondPropIsSpecified) {
  38. // If first prop is unspecified, look for a specified prop before it to
  39. // compare to the current prop
  40. const priorSpecifiedPropData = _.findLast(allPropertiesData.slice(0, -1), (d) =>
  41. Boolean(d.orderData)
  42. );
  43. if (
  44. priorSpecifiedPropData &&
  45. priorSpecifiedPropData.orderData &&
  46. priorSpecifiedPropData.orderData.expectedPosition >
  47. secondPropertyData.orderData.expectedPosition
  48. ) {
  49. return report(false, priorSpecifiedPropData, secondPropertyData);
  50. }
  51. }
  52. // Now deal with unspecified props
  53. // Starting with bottomAlphabetical as it requires more specific conditionals
  54. if (unspecified === 'bottomAlphabetical' && firstPropIsSpecified && !secondPropIsSpecified) {
  55. return report(true);
  56. }
  57. if (unspecified === 'bottomAlphabetical' && !firstPropIsSpecified && !secondPropIsSpecified) {
  58. if (checkAlphabeticalOrder(firstPropertyData, secondPropertyData)) {
  59. return report(true);
  60. }
  61. return report(false);
  62. }
  63. if (unspecified === 'bottomAlphabetical' && !firstPropIsSpecified) {
  64. return report(false);
  65. }
  66. if (!firstPropIsSpecified && !secondPropIsSpecified) {
  67. return report(true);
  68. }
  69. if (unspecified === 'ignore' && (!firstPropIsSpecified || !secondPropIsSpecified)) {
  70. return report(true);
  71. }
  72. if (unspecified === 'top' && !firstPropIsSpecified) {
  73. return report(true);
  74. }
  75. if (unspecified === 'top' && !secondPropIsSpecified) {
  76. return report(false);
  77. }
  78. if (unspecified === 'bottom' && !secondPropIsSpecified) {
  79. return report(true);
  80. }
  81. if (unspecified === 'bottom' && !firstPropIsSpecified) {
  82. return report(false);
  83. }
  84. };