All files / packages/core/src/native-theme assert.ts

81.81% Statements 9/11
66.66% Branches 4/6
100% Functions 3/3
81.81% Lines 9/11

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                                              1x 1x 1x                 1x 1x     1x                   1x 1x 1x        
// SPDX-License-Identifier: MIT
/**
 * Assertion helpers for site-native theme files (#944).
 *
 * Drop this into a consuming repo's CI/test suite to gate a native theme
 * file against the token contract:
 *
 * ```ts
 * import { assertNativeTheme } from '@lgtm-hq/turbo-themes/native-theme';
 * import { readFileSync } from 'node:fs';
 *
 * assertNativeTheme(readFileSync('src/styles/my-theme.css', 'utf8'), 'my-theme');
 * ```
 */
 
import {
  formatNativeThemeValidation,
  validateNativeThemeCss,
  validateNativeThemeTokens,
  type NativeThemeValidation,
} from './validator.js';
 
export class NativeThemeValidationError extends Error {
  constructor(readonly validation: NativeThemeValidation, message: string) {
    super(message);
    this.name = 'NativeThemeValidationError';
  }
}
 
/**
 * Assert a native theme given as CSS source; throws with a full report when
 * the theme does not satisfy the contract.
 */
export function assertNativeTheme(css: string, source = 'native theme'): NativeThemeValidation {
  const result = validateNativeThemeCss(css);
  Iif (!result.valid) {
    throw new NativeThemeValidationError(result, formatNativeThemeValidation(result, source));
  }
  return result;
}
 
/**
 * Assert a native theme given as a token map (`--turbo-x` → value).
 */
export function assertNativeThemeTokens(
  tokens: Record<string, unknown>,
  source = 'native theme',
): NativeThemeValidation {
  const result = validateNativeThemeTokens(tokens);
  Eif (!result.valid) {
    throw new NativeThemeValidationError(result, formatNativeThemeValidation(result, source));
  }
  return result;
}