Ruleset.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var hasOwnProperty = Object.prototype.hasOwnProperty;
  2. function cleanUnused(node, usageData) {
  3. return node.selector.selectors.each(function(selector, item, list) {
  4. var hasUnused = selector.sequence.some(function(node) {
  5. switch (node.type) {
  6. case 'Class':
  7. return usageData.classes && !hasOwnProperty.call(usageData.classes, node.name);
  8. case 'Id':
  9. return usageData.ids && !hasOwnProperty.call(usageData.ids, node.name);
  10. case 'Identifier':
  11. // ignore universal selector
  12. if (node.name !== '*') {
  13. // TODO: remove toLowerCase when type selectors will be normalized
  14. return usageData.tags && !hasOwnProperty.call(usageData.tags, node.name.toLowerCase());
  15. }
  16. break;
  17. }
  18. });
  19. if (hasUnused) {
  20. list.remove(item);
  21. }
  22. });
  23. }
  24. module.exports = function cleanRuleset(node, item, list, usageData) {
  25. if (usageData) {
  26. cleanUnused(node, usageData);
  27. }
  28. if (node.selector.selectors.isEmpty() ||
  29. node.block.declarations.isEmpty()) {
  30. list.remove(item);
  31. }
  32. };