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