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 | 1x 1x 1x 1x 5x 5x 5x 5x 1x 91x 36x 37x 82x 82x 82x 82x 5x 5x 77x 36x 36x 36x 20x 62x 62x 87x 85x 82x 62x 11x 11x 19x 18x 16x 16x 23x 16x 13x 19x 16x 3x 3x 3x 3x 1x 1x 2x 3x 1x 1x 1x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | #!/usr/bin/env node
/**
* Generate global token allowlists from the design-tokens package.
*
* Usage:
* npx tsx scripts/generate-allowlists.ts [--theme cosmos]
*
* Source: @salesforce-ux/design-tokens/dist/themes/{theme}/{theme}.global.tokens.raw.json
* Output: src/validators/global/__generated-allowlists.ts
*/
import { readFileSync, writeFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createRequire } from 'node:module';
const __dirname = dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);
const theme = process.argv.includes('--theme') ? process.argv[process.argv.indexOf('--theme') + 1] : 'cosmos';
const OUTPUT_PATH = resolve(__dirname, '../src/validators/global/__generated-allowlists.ts');
// ---------------------------------------------------------------------------
// Resolve
// ---------------------------------------------------------------------------
export function resolveTokensPath(resolver?: (id: string) => string): string {
const resolveFn = resolver ?? ((id: string) => require.resolve(id));
try {
const pkgPath = resolveFn('@salesforce-ux/design-tokens/package.json');
return resolve(dirname(pkgPath), `dist/themes/${theme}/${theme}.global.tokens.raw.json`);
} catch {
return resolve(__dirname, `../../design-tokens/dist/themes/${theme}/${theme}.global.tokens.raw.json`);
}
}
// ---------------------------------------------------------------------------
// Tree walking (exported for testing)
// ---------------------------------------------------------------------------
export function camelToKebab(str: string): string {
return str.replaceAll(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}
interface TokenNode {
$value?: unknown;
$type?: string;
[key: string]: unknown;
}
function hasNumericRangeLeaves(obj: Record<string, TokenNode>): boolean {
return Object.entries(obj).some(
([k, v]) => /^\d+$/.test(k) && typeof v === 'object' && v !== null && '$value' in v,
);
}
function processEntry(rawKey: string, val: TokenNode, prefix: string, descriptors: Set<string>): void {
const key = camelToKebab(rawKey);
const path = prefix ? `${prefix}-${key}` : key;
const isNumericKey = /^\d+$/.test(rawKey);
if ('$value' in val && !isNumericKey) {
descriptors.add(path);
return;
}
if ('$value' in val) return;
const child = val as Record<string, TokenNode>;
if (hasNumericRangeLeaves(child)) descriptors.add(path);
for (const desc of collectDescriptors(child, path)) {
descriptors.add(desc);
}
}
export function collectDescriptors(node: Record<string, TokenNode>, prefix = ''): Set<string> {
const descriptors = new Set<string>();
for (const [rawKey, val] of Object.entries(node)) {
if (rawKey.startsWith('$')) continue;
if (typeof val !== 'object' || val === null) continue;
processEntry(rawKey, val, prefix, descriptors);
}
return descriptors;
}
export function extractColorConcepts(colorNode: Record<string, TokenNode>): { semantic: string[] } {
const semantic = new Set<string>();
for (const [rawKey, val] of Object.entries(colorNode)) {
if (rawKey.startsWith('$')) continue;
if (rawKey === 'palette') continue;
Iif (typeof val !== 'object' || val === null) continue;
const filtered = Object.fromEntries(
Object.entries(val).filter(([k]) => k !== 'base'),
) as Record<string, TokenNode>;
if (Object.keys(filtered).length === 0) continue;
for (const desc of collectDescriptors({ [rawKey]: filtered } as Record<string, TokenNode>)) {
semantic.add(desc);
}
}
return { semantic: [...semantic].sort((a, b) => a.localeCompare(b)) };
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
export function main(): void {
const tokensPath = resolveTokensPath();
console.log(`Reading tokens from: ${tokensPath}`);
let tokens: any;
try {
tokens = JSON.parse(readFileSync(tokensPath, 'utf8'));
} catch (e: any) {
console.error(`Failed to read tokens file: ${e.message}`);
process.exit(1);
}
const g = tokens?.slds?.g;
if (!g) {
console.error('Token file does not contain slds.g namespace.');
process.exit(1);
}
const color = extractColorConcepts(g.color || {});
const categories: Record<string, string[]> = {};
for (const [rawCat, catNode] of Object.entries(g)) {
if (rawCat.startsWith('$') || rawCat === 'color') continue;
const cat = camelToKebab(rawCat);
categories[cat] = [...collectDescriptors(catNode as Record<string, TokenNode>)].sort((a, b) =>
a.localeCompare(b),
);
}
const lines: string[] = [
'/**',
' * AUTO-GENERATED by scripts/generate-allowlists.ts',
` * Source: @salesforce-ux/design-tokens/dist/themes/${theme}/${theme}.global.tokens.raw.json`,
` * Generated: ${new Date().toISOString()}`,
' *',
' * DO NOT EDIT MANUALLY.',
' * Re-run the generation script after updating the design-tokens package.',
' */',
'',
'// ---------------------------------------------------------------------------',
'// Color: semantic concept allowlist',
'// Palette colors and base scale roles are validated by convention, not allowlist.',
'// ---------------------------------------------------------------------------',
'',
`export const COLOR_SEMANTIC_CONCEPTS = ${JSON.stringify(color.semantic, null, 2)};`,
'',
];
for (const [cat, descriptors] of Object.entries(categories).sort(([a], [b]) => a.localeCompare(b))) {
const title = cat.charAt(0).toUpperCase() + cat.slice(1);
const constName = `${cat.toUpperCase().replaceAll('-', '_')}_DESCRIPTORS`;
lines.push(
'// ---------------------------------------------------------------------------',
`// ${title}`,
'// ---------------------------------------------------------------------------',
'',
`export const ${constName} = ${JSON.stringify(descriptors, null, 2)};`,
'',
);
}
writeFileSync(OUTPUT_PATH, lines.join('\n'), 'utf8');
console.log(`Generated: ${OUTPUT_PATH}`);
console.log(` Color semantic concepts: ${color.semantic.length}`);
for (const [cat, descs] of Object.entries(categories).sort(([a], [b]) => a.localeCompare(b))) {
console.log(` ${cat} descriptors: ${descs.length}`);
}
}
const __filename = fileURLToPath(import.meta.url);
Iif (process.argv[1] && resolve(process.argv[1]) === __filename) {
main();
}
|