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 | 3x 3x 3x 3x 3x 3x 3x 28x 28x 2x 26x 20x 20x 20x 20x 20x 20x 20x 60x 20x 20x 78x 22x 22x 22x 56x 20x 36x 2x 2x 34x 18x 18x 18x 18x 18x 18x 78x 78x 44x 22x 22x 22x 22x 20x 20x 2x 18x 18x | /**
* Parser for iOS (Swift) color token files.
* Handles direct Color(red:green:blue:) float values, palette token class naming,
* and reference assignments between token enums/classes.
*/
import {
classNameToMode,
resolvePaletteTokenName,
storeDirectToken,
resolveReferences,
} from './mobile-parser-utils.js';
const SWIFT_PALETTE_CLASS_RE = /Cosmos(.+?)Palette/;
const SWIFT_CLASS_RE = /(?:class|enum)\s+(\w+)/;
const SWIFT_STATIC_VAR_RE = /static\s+(?:let|var)\s+(\w+)(?:\s*:\s*Color)?/;
const SWIFT_RED_RE = /red:\s*([\d.]+)/;
const SWIFT_GREEN_RE = /green:\s*([\d.]+)/;
const SWIFT_BLUE_RE = /blue:\s*([\d.]+)/;
const SWIFT_REF_RE = /static\s+var\s+(\w+)\s*:\s*Color\s*\{\s*\w+\.(\w+)\s*\}/;
/**
* Convert camelCase or PascalCase to kebab-case.
* Handles boundaries before uppercase letters and before digits.
*/
function toKebab(s) {
return s
.replaceAll(/([a-z])([A-Z])/g, '$1-$2')
.replaceAll(/([a-z])(\d)/g, '$1-$2')
.replaceAll(/([A-Z])(\d)/g, '$1-$2')
.toLowerCase();
}
/**
* Convert an iOS Swift token name to the canonical CSS token name format.
* Swift uses camelCase names (e.g. 'errorBase10', 'paletteNeutral0')
* while CSS uses kebab-case with a 'color-' prefix.
* @param {string} tokenName
* @returns {string}
*/
export function normalizeSwiftTokenName(tokenName) {
if (tokenName.startsWith('palette')) {
return 'color-palette-' + toKebab(tokenName.replace(/^palette/, ''));
}
return 'color-' + toKebab(tokenName);
}
/**
* Parse a Swift direct Color(red:green:blue:) line into a typed result object.
* Converts 0–1 float RGB components to a 6-char hex string.
* @param {string} trimmed
* @param {string|null} currentClass
* @param {string|null} currentMode
* @returns {{ type: 'direct', fullTokenName: string, hex: string, mode: string|null } | null}
*/
function parseSwiftDirectColor(trimmed, currentClass, currentMode) {
const varMatch = SWIFT_STATIC_VAR_RE.exec(trimmed);
Iif (!varMatch) return null;
const redMatch = SWIFT_RED_RE.exec(trimmed);
const greenMatch = SWIFT_GREEN_RE.exec(trimmed);
const blueMatch = SWIFT_BLUE_RE.exec(trimmed);
Iif (!redMatch || !greenMatch || !blueMatch) return null;
const hex = [redMatch[1], greenMatch[1], blueMatch[1]]
.map((v) =>
Math.round(Number.parseFloat(v) * 255)
.toString(16)
.padStart(2, '0'),
)
.join('');
const fullTokenName = resolvePaletteTokenName(varMatch[1], currentClass, SWIFT_PALETTE_CLASS_RE);
return { type: 'direct', fullTokenName, hex, mode: currentMode };
}
/**
* Parse a single Swift source line into a typed result object.
* Returns null for lines that don't match any known pattern.
* @param {string} trimmed
* @param {string|null} currentClass
* @param {string|null} currentMode
* @returns {{ type: 'class', className: string, mode: string|null }
* | { type: 'direct', fullTokenName: string, hex: string, mode: string|null }
* | { type: 'ref', tokenName: string, refProperty: string, mode: string|null }
* | null}
*/
function parseSwiftLine(trimmed, currentClass, currentMode) {
if (
(trimmed.includes('public class') || trimmed.includes('public enum')) &&
(trimmed.includes('Light') || trimmed.includes('Dark'))
) {
const m = SWIFT_CLASS_RE.exec(trimmed);
Iif (!m) return null;
return { type: 'class', className: m[1], mode: classNameToMode(m[1]) };
}
if ((trimmed.includes('static let') || trimmed.includes('static var')) && trimmed.includes('Color(red:')) {
return parseSwiftDirectColor(trimmed, currentClass, currentMode);
}
if (
trimmed.includes('static var') &&
trimmed.includes(':') &&
trimmed.includes('{') &&
trimmed.includes('.')
) {
const refMatch = SWIFT_REF_RE.exec(trimmed);
Eif (refMatch) return { type: 'ref', tokenName: refMatch[1], refProperty: refMatch[2], mode: currentMode };
}
return null;
}
/**
* Parse all color tokens from a Swift source file.
* @param {string} swiftContent
* @returns {Map<string, string>} token name → hex (e.g. 'color-brand-base-50-light' → 'aabbcc')
*/
export function parseColorsFromSwift(swiftContent) {
const colors = new Map();
const directColors = new Map();
const referenceAssignments = [];
let currentMode = null;
let currentClass = null;
for (const line of swiftContent.split('\n')) {
const result = parseSwiftLine(line.trim(), currentClass, currentMode);
if (!result) continue;
if (result.type === 'class') {
currentClass = result.className;
currentMode = result.mode;
continue;
}
if (result.type === 'direct') {
storeDirectToken(
directColors,
colors,
result.fullTokenName,
result.hex,
result.mode,
normalizeSwiftTokenName,
);
continue;
}
Eif (result.type === 'ref') referenceAssignments.push(result);
}
resolveReferences(referenceAssignments, directColors, colors, normalizeSwiftTokenName);
return colors;
}
|