1. 1 : import findDeclarationByKey from '../AST/findDeclarationByKey';
  2. 2 : import { Unset, UpdateDeclarations, } from '../CSSTransformers';
  3. 3 :
  4. 4 : import diffDeclarations from './diffDeclarations';
  5. 5 :
  6. 6 :
  7. 7 : /**
  8. 8 : * Compare declarations between rules and apply diff transformation.
  9. 9 : * @memberof diff
  10. 10 : * @param {Rule} originalRule Original rule.
  11. 11 : * @param {Rule} expectedRule Expected rule.
  12. 12 : * @returns {Rule}
  13. 13 : */
  14. 14 : function transformDiffDeclarations(originalRule, expectedRule) {
  15. 15 : const diff = diffDeclarations(originalRule, expectedRule);
  16. 16 :
  17. 17 : diff.map(({ state, declaration, }) => {
  18. 18 : switch (state) {
  19. 19 : case -1:
  20. 20 : Unset.transform(
  21. 21 : findDeclarationByKey(
  22. 22 : declaration.props, originalRule.children
  23. 23 : )
  24. 24 : );
  25. 25 : break;
  26. 26 : case 1:
  27. 27 : originalRule.children.push(declaration);
  28. 28 : break;
  29. 29 : case 0: {
  30. 30 : const { original: originalDeclaration, expected: expectedDeclaration, } = declaration;
  31. 31 :
  32. 32 : if (originalDeclaration.children !== expectedDeclaration.children) {
  33. 33 : originalDeclaration.children = expectedDeclaration.children;
  34. 34 : UpdateDeclarations.transform(originalDeclaration);
  35. 35 : } else {
  36. 36 : originalRule.children.splice(originalRule.children.indexOf(originalDeclaration), 1);
  37. 37 : }
  38. 38 :
  39. 39 : break;
  40. 40 : }
  41. 41 : }
  42. 42 : });
  43. 43 :
  44. 44 : return originalRule;
  45. 45 : }
  46. 46 :
  47. 47 : export default transformDiffDeclarations;