getOrderData.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const isStandardSyntaxProperty = require('./isStandardSyntaxProperty');
  2. const isCustomProperty = require('./isCustomProperty');
  3. const isDollarVariable = require('./isDollarVariable');
  4. const isAtVariable = require('./isAtVariable');
  5. const calcAtRulePatternPriority = require('./calcAtRulePatternPriority');
  6. const calcRulePatternPriority = require('./calcRulePatternPriority');
  7. module.exports = function getOrderData(expectedOrder, node) {
  8. let nodeType;
  9. if (isAtVariable(node)) {
  10. nodeType = 'at-variables';
  11. } else if (node.type === 'decl') {
  12. if (isCustomProperty(node.prop)) {
  13. nodeType = 'custom-properties';
  14. } else if (isDollarVariable(node.prop)) {
  15. nodeType = 'dollar-variables';
  16. } else if (isStandardSyntaxProperty(node.prop)) {
  17. nodeType = 'declarations';
  18. }
  19. } else if (node.type === 'rule') {
  20. nodeType = {
  21. type: 'rule',
  22. selector: node.selector,
  23. };
  24. const rules = expectedOrder.rule;
  25. // Looking for most specified pattern, because it can match many patterns
  26. if (rules && rules.length) {
  27. let prioritizedPattern;
  28. let max = 0;
  29. rules.forEach(function(pattern) {
  30. const priority = calcRulePatternPriority(pattern, nodeType);
  31. if (priority > max) {
  32. max = priority;
  33. prioritizedPattern = pattern;
  34. }
  35. });
  36. if (max) {
  37. return prioritizedPattern;
  38. }
  39. }
  40. } else if (node.type === 'atrule') {
  41. nodeType = {
  42. type: 'at-rule',
  43. name: node.name,
  44. hasBlock: false,
  45. };
  46. if (node.nodes && node.nodes.length) {
  47. nodeType.hasBlock = true;
  48. }
  49. if (node.params && node.params.length) {
  50. nodeType.parameter = node.params;
  51. }
  52. const atRules = expectedOrder['at-rule'];
  53. // Looking for most specified pattern, because it can match many patterns
  54. if (atRules && atRules.length) {
  55. let prioritizedPattern;
  56. let max = 0;
  57. atRules.forEach(function(pattern) {
  58. const priority = calcAtRulePatternPriority(pattern, nodeType);
  59. if (priority > max) {
  60. max = priority;
  61. prioritizedPattern = pattern;
  62. }
  63. });
  64. if (max) {
  65. return prioritizedPattern;
  66. }
  67. }
  68. }
  69. if (expectedOrder[nodeType]) {
  70. return expectedOrder[nodeType];
  71. }
  72. // Return null if there no patterns for that node
  73. return null;
  74. };