Skip to content

An open source design system that's fully customizable and agent ready

TypeScript
1.9 KB
Raw Blame
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
 * @file Reject stale generated theme CSS that still owns data-token defaults.
 *
 * @input Generated theme CSS
 * @output No rewritten source; a regeneration report (`RegenerationRequired`,
 *   with `astryx theme build`) for a generated theme stylesheet that still
 *   declares data-color defaults at `:root`
 * @position Next-release upgrade validation for spec:AST-066 FR19
 *
 * Raw `var(--color-data-*)` uses are not checked: prebuilt
 * `@astryxdesign/core/astryx.css` and the `@astryxdesign/build` PostCSS
 * integration define all 56 defaults, and other source builds read data
 * colors through `dataVars` (spec:AST-066 FR3/FR4).
 */

import * as path from 'node:path';
import {RegenerationRequired} from '../../outcome.mjs';

export const meta = {
  title: 'Validate data-token ownership',
  description:
    'Reports generated theme CSS that still emits the canonical data-color defaults at :root, so it can be rebuilt with `astryx theme build`.',
  fileExtensions: ['.css'],
};

const GENERATED_THEME_HEADER = '@generated by `astryx theme build`';
const ROOT_DATA_DECLARATION = /:root\s*\{[^}]*--color-data-/su;

/**
 * @param {import('../../../../authoring/codemod/type').AstryxCodemodFile} file
 * @returns {undefined}
 */
export default function validateDataTokenOwnership(file) {
  if (
    path.extname(file.path) === '.css' &&
    file.source.includes(GENERATED_THEME_HEADER) &&
    ROOT_DATA_DECLARATION.test(file.source)
  ) {
    throw new RegenerationRequired(
      'This generated theme CSS still emits canonical data defaults at :root. Rebuild it with `astryx theme build`; defaults come from @astryxdesign/core/astryx.css in prebuilt mode, and in a source build from the @astryxdesign/build PostCSS integration or dataVars imports.',
      {commands: ['astryx theme build']},
    );
  }
  return undefined;
}