All files / packages/design-system/scripts/validation/plugins css-analyzer.js

100% Statements 51/51
96.15% Branches 25/26
100% Functions 5/5
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                      26x     26x     26x 31x 31x 31x   31x 29x   31x     26x                   13x 13x   13x 13x 13x     13x 14x 1x               13x 15x 2x         13x 14x 14x   13x 11x                 13x                               25x 16x     9x   9x 9x   9x           9x              
/**
 * CSS Analyzer Plugin
 * Analyzes CSS property value changes and provides detailed insights
 */
 
/**
 * Parse CSS properties from content
 * @param {string} css - CSS content
 * @returns {Map<string, Set<string>>} Map of property names to values
 */
function parseCSSProperties(css) {
  const properties = new Map();
 
  // Simple regex to find property: value pairs
  const propertyRegex = /([a-z-]+)\s*:\s*([^;{}]+)/gi;
  let match;
 
  while ((match = propertyRegex.exec(css)) !== null) {
    const [, prop, value] = match;
    const normalizedProp = prop.toLowerCase().trim();
    const normalizedValue = value.trim();
 
    if (!properties.has(normalizedProp)) {
      properties.set(normalizedProp, new Set());
    }
    properties.get(normalizedProp).add(normalizedValue);
  }
 
  return properties;
}
 
/**
 * Analyze property value changes between baseline and current
 * @param {string} baseline - Baseline CSS content
 * @param {string} current - Current CSS content
 * @returns {Object} Analysis results
 */
export function analyzePropertyValueChanges(baseline, current) {
  const baselineProps = parseCSSProperties(baseline);
  const currentProps = parseCSSProperties(current);
 
  const addedProperties = [];
  const removedProperties = [];
  const changedProperties = [];
 
  // Find removed properties
  for (const [prop, values] of baselineProps) {
    if (!currentProps.has(prop)) {
      removedProperties.push({
        property: prop,
        values: Array.from(values),
      });
    }
  }
 
  // Find added and changed properties
  for (const [prop, values] of currentProps) {
    if (!baselineProps.has(prop)) {
      addedProperties.push({
        property: prop,
        values: Array.from(values),
      });
    } else {
      const baselineValues = baselineProps.get(prop);
      const added = Array.from(values).filter((v) => !baselineValues.has(v));
      const removed = Array.from(baselineValues).filter((v) => !values.has(v));
 
      if (added.length > 0 || removed.length > 0) {
        changedProperties.push({
          property: prop,
          addedValues: added,
          removedValues: removed,
        });
      }
    }
  }
 
  return {
    addedProperties,
    removedProperties,
    changedProperties,
  };
}
 
/**
 * Plugin hook: afterCompare
 * Adds CSS analysis data to comparison results
 * @param {Object} result - Comparison result
 * @param {string} relativePath - Relative file path
 * @returns {Object} Enhanced result
 */
export function afterCompare(result, relativePath) {
  // Only analyze CSS files with differences
  if (!relativePath.endsWith('.css') || result.status === 'identical') {
    return result;
  }
 
  const { baselineContent, currentContent } = result;
 
  Eif (result.status === 'different' && baselineContent && currentContent) {
    const analysis = analyzePropertyValueChanges(baselineContent, currentContent);
 
    result.pluginData = {
      ...result.pluginData,
      cssAnalysis: analysis,
    };
  }
 
  return result;
}
 
export default {
  name: 'css-analyzer',
  afterCompare,
};