All files / packages/sds-customization-compliance/src/audit propertyPairs.ts

91.66% Statements 22/24
70.83% Branches 17/24
100% Functions 3/3
91.3% Lines 21/23

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                                        5x                                                                                                       5x                                                 5x   5x                             5x                             5x   5x           193x     170x                       27x 27x   27x     27x 4x     23x 8x           15x 21x 11x             4x             4x          
/**
 * Classification table for CSS property-name substitutions.
 *
 * When the diff engine sees a declaration of property A removed from a
 * selector and property B added on the same (or structurally adjacent)
 * selector, it needs a deterministic rule for deciding severity:
 *
 *   - `equivalent`   — same layout model, different notation
 *                      (e.g. `padding-left` ↔ `padding-inline-start`).
 *                      Emitted as `info` only; no customer impact.
 *   - `incompatible` — different layout model; customer overrides on the
 *                      legacy property do not reach the new property
 *                      (e.g. `padding` ↔ `gap`). Emitted as `breaking`.
 *   - `unknown`      — not covered by either table. Emitted as `info` with
 *                      `needsReview: true` — the audit refuses to guess.
 *
 * All entries are normalized to lowercase before lookup. Pairs are
 * symmetric (both directions checked automatically).
 */
 
export type PairClassification = 'equivalent' | 'incompatible' | 'unknown';
 
export interface PairResult {
  kind: PairClassification;
  reason: string;
}
 
/** Pair rows where A→B preserves layout semantics. Extend freely. */
export const EQUIVALENT_PAIRS: ReadonlyArray<readonly [string, string]> = [
  // Physical ↔ logical padding.
  ['padding-left', 'padding-inline-start'],
  ['padding-right', 'padding-inline-end'],
  ['padding-top', 'padding-block-start'],
  ['padding-bottom', 'padding-block-end'],
 
  // Physical ↔ logical margin.
  ['margin-left', 'margin-inline-start'],
  ['margin-right', 'margin-inline-end'],
  ['margin-top', 'margin-block-start'],
  ['margin-bottom', 'margin-block-end'],
 
  // Physical ↔ logical border width.
  ['border-left-width', 'border-inline-start-width'],
  ['border-right-width', 'border-inline-end-width'],
  ['border-top-width', 'border-block-start-width'],
  ['border-bottom-width', 'border-block-end-width'],
 
  // Physical ↔ logical border color.
  ['border-left-color', 'border-inline-start-color'],
  ['border-right-color', 'border-inline-end-color'],
  ['border-top-color', 'border-block-start-color'],
  ['border-bottom-color', 'border-block-end-color'],
 
  // Physical ↔ logical border style.
  ['border-left-style', 'border-inline-start-style'],
  ['border-right-style', 'border-inline-end-style'],
  ['border-top-style', 'border-block-start-style'],
  ['border-bottom-style', 'border-block-end-style'],
 
  // Physical ↔ logical border radius corners.
  ['border-top-left-radius', 'border-start-start-radius'],
  ['border-top-right-radius', 'border-start-end-radius'],
  ['border-bottom-left-radius', 'border-end-start-radius'],
  ['border-bottom-right-radius', 'border-end-end-radius'],
 
  // Physical ↔ logical insets.
  ['left', 'inset-inline-start'],
  ['right', 'inset-inline-end'],
  ['top', 'inset-block-start'],
  ['bottom', 'inset-block-end'],
 
  // Physical ↔ logical sizing.
  ['width', 'inline-size'],
  ['height', 'block-size'],
  ['min-width', 'min-inline-size'],
  ['min-height', 'min-block-size'],
  ['max-width', 'max-inline-size'],
  ['max-height', 'max-block-size'],
];
 
export const BOX_SPACING: ReadonlySet<string> = new Set([
  'padding',
  'padding-left',
  'padding-right',
  'padding-top',
  'padding-bottom',
  'padding-inline',
  'padding-inline-start',
  'padding-inline-end',
  'padding-block',
  'padding-block-start',
  'padding-block-end',
  'margin',
  'margin-left',
  'margin-right',
  'margin-top',
  'margin-bottom',
  'margin-inline',
  'margin-inline-start',
  'margin-inline-end',
  'margin-block',
  'margin-block-start',
  'margin-block-end',
]);
 
export const CONTAINER_SPACING: ReadonlySet<string> = new Set(['gap', 'row-gap', 'column-gap']);
 
export const SIZING: ReadonlySet<string> = new Set([
  'width',
  'height',
  'min-width',
  'min-height',
  'max-width',
  'max-height',
  'inline-size',
  'block-size',
  'min-inline-size',
  'min-block-size',
  'max-inline-size',
  'max-block-size',
]);
 
export const FLEX_GRID_TRACK: ReadonlySet<string> = new Set([
  'flex',
  'flex-basis',
  'flex-grow',
  'flex-shrink',
  'grid-template-rows',
  'grid-template-columns',
  'grid-auto-rows',
  'grid-auto-columns',
  'grid-auto-flow',
  'grid-area',
  'grid-row',
  'grid-column',
]);
 
export const SHADOW_PROPS: ReadonlySet<string> = new Set(['box-shadow', 'text-shadow', 'filter']);
 
const INCOMPATIBLE_GROUP_PAIRS: ReadonlyArray<readonly [ReadonlySet<string>, ReadonlySet<string>]> = [
  [BOX_SPACING, CONTAINER_SPACING],
  [SIZING, FLEX_GRID_TRACK],
];
 
function equivalentKey(a: string, b: string): string {
  return [a, b].sort().join('|');
}
 
const EQUIVALENT_SET: ReadonlySet<string> = new Set(EQUIVALENT_PAIRS.map(([a, b]) => equivalentKey(a, b)));
 
/**
 * Classify a property-name swap.
 *
 * @param legacyProp Property dropped on the legacy side.
 * @param nextProp Property introduced on the effective-new side.
 */
export function classifyPair(
  legacyProp: string | null | undefined,
  nextProp: string | null | undefined,
): PairResult {
  const a = String(legacyProp ?? '').toLowerCase();
  const b = String(nextProp ?? '').toLowerCase();
 
  Iif (!a || !b) {
    return { kind: 'unknown', reason: 'Missing property on one side.' };
  }
  if (a === b) {
    return { kind: 'equivalent', reason: 'Same property.' };
  }
 
  if (EQUIVALENT_SET.has(equivalentKey(a, b))) {
    return {
      kind: 'equivalent',
      reason: 'Logical / physical equivalent within the same layout model.',
    };
  }
 
  for (const [groupA, groupB] of INCOMPATIBLE_GROUP_PAIRS) {
    if ((groupA.has(a) && groupB.has(b)) || (groupA.has(b) && groupB.has(a))) {
      return {
        kind: 'incompatible',
        reason: `\`${a}\` and \`${b}\` belong to different layout models (e.g. box-spacing vs container-spacing).`,
      };
    }
  }
 
  Iif ((SHADOW_PROPS.has(a) && !SHADOW_PROPS.has(b)) || (SHADOW_PROPS.has(b) && !SHADOW_PROPS.has(a))) {
    return {
      kind: 'incompatible',
      reason: 'Shadow property has no non-shadow equivalent.',
    };
  }
 
  return {
    kind: 'unknown',
    reason: `No classification rule for \`${a}\` ↔ \`${b}\`.`,
  };
}