All files / packages/design-tokens/src/style-dictionary/utils flat-token-fields.ts

90.9% Statements 50/55
79.24% Branches 42/53
100% Functions 12/12
91.66% Lines 44/48

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            3x           3x 60x   34x 2x     32x                   3x 20x 20x   20x             69x 20x     20x   3x 20x 20x               3x 20x   3x 20x   3x 20x           3x 20x 20x     3x 20x 11x 3x                                           3x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x   20x                                   3x             9x 9x    
import type { TransformedToken } from 'style-dictionary/types';
 
import { CSS_PROPERTIES_EXTENSION_KEY, MODE_EXTENSION_KEY } from './constants.js';
import { getFlatTypeInfo } from './flat-type-map.js';
import { isValueUnitObject, valueUnitToString } from './value-unit-helpers.js';
 
const scopeMap: Record<string, string> = {
  g: 'global',
  s: 'shared',
  r: 'reference',
};
 
const toDisplayString = (value: unknown, tokenPath: string): string | undefined => {
  if (value === undefined || value === null) return undefined;
 
  if (isValueUnitObject(value)) {
    return valueUnitToString(value, tokenPath);
  }
 
  Eif (typeof value === 'string') return value;
  if (typeof value === 'number' || typeof value === 'boolean') return String(value);
 
  try {
    return JSON.stringify(value);
  } catch {
    return String(value);
  }
};
 
const getCssProperties = (token: TransformedToken): string[] | undefined => {
  const originalExtensions = token.original?.$extensions;
  const extensions = token.$extensions;
 
  const candidates = [
    originalExtensions?.cssProperties,
    originalExtensions?.[CSS_PROPERTIES_EXTENSION_KEY],
    extensions?.cssProperties,
    extensions?.[CSS_PROPERTIES_EXTENSION_KEY],
  ];
 
  const cssProperties = candidates.find((value) => Array.isArray(value));
  return Array.isArray(cssProperties) && cssProperties.length > 0 ? cssProperties : undefined;
};
 
const getTokenName = (token: TransformedToken): string => token.name ?? token.path?.join('-') ?? '';
 
const getTokenSegments = (tokenName: string) => {
  const [namespace = '', shortScope = '', category = ''] = tokenName.split('-');
  return {
    namespace,
    shortScope,
    category,
    scope: scopeMap[shortScope] ?? shortScope,
  };
};
 
const getOriginalValue = (token: TransformedToken): string | undefined =>
  toDisplayString(token.original?.$value, token.path?.join('.') ?? token.name ?? '');
 
const getResolvedValue = (token: TransformedToken): string | undefined =>
  toDisplayString(token.$value ?? token.value, token.path?.join('.') ?? token.name ?? '');
 
const getDarkValue = (token: TransformedToken): string | undefined =>
  toDisplayString(
    token.$extensions?.[MODE_EXTENSION_KEY]?.dark?.$value ??
      token.original?.$extensions?.[MODE_EXTENSION_KEY]?.dark?.$value,
    token.path?.join('.') ?? token.name ?? '',
  );
 
const getDescription = (token: TransformedToken): string | undefined => {
  const description = token.original?.$description ?? token.$description;
  return typeof description === 'string' && description.length > 0 ? description : undefined;
};
 
const getInherits = (originalValue?: string): true | undefined => {
  if (!originalValue) return undefined;
  if (!/^\{[^{}]+\}$/.test(originalValue)) return undefined;
  return true;
};
 
type FlatTokenFields = {
  name: string;
  value: string;
  originalValue?: string;
  type: string;
  syntax?: string;
  category: string;
  scope: string;
  namespace: string;
  description?: string;
  inherits?: true;
  deprecated?: true;
  darkValue?: string;
  themeOwned: boolean;
  cssProperties?: string[];
};
 
type FlatTokenMetadataFields = Omit<FlatTokenFields, 'value' | 'description' | 'deprecated' | 'darkValue'>;
 
export const flatTokenFields = (token: TransformedToken): FlatTokenFields => {
  const name = getTokenName(token);
  const value = getResolvedValue(token);
  const originalValue = getOriginalValue(token);
  const { type, syntax } = getFlatTypeInfo(token.$type);
  const { namespace, scope, category } = getTokenSegments(name);
  const description = getDescription(token);
  const inherits = getInherits(originalValue);
  const deprecated = token.$deprecated || token.original?.$deprecated ? true : undefined;
  const darkValue = getDarkValue(token);
  const cssProperties = getCssProperties(token);
 
  return {
    name,
    value: value ?? '',
    originalValue,
    type,
    syntax,
    category,
    scope,
    namespace,
    description,
    inherits,
    deprecated,
    darkValue,
    themeOwned: Boolean(token.isSource),
    cssProperties,
  };
};
 
export const flatTokenMetadataFields = (token: TransformedToken): FlatTokenMetadataFields => {
  const {
    value: _value,
    description: _description,
    deprecated: _deprecated,
    darkValue: _darkValue,
    ...metadata
  } = flatTokenFields(token);
  return metadata;
};