Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/bold-pandas-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@terrazzo/plugin-sass": minor
"@terrazzo/token-tools": minor
"@terrazzo/plugin-css": minor
"@terrazzo/cli": minor
"@terrazzo/parser": minor
---

feat: add typography shorthand, improve Sass plugin
54 changes: 16 additions & 38 deletions packages/parser/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import type * as momoa from '@humanwhocodes/momoa';
import type { Group, TokenNormalized, TokenNormalizedSet } from '@terrazzo/token-tools';
import type {
Group,
TokenNormalized,
TokenNormalizedSet,
TokenTransformed,
TokenTransformedBase,
} from '@terrazzo/token-tools';
import type ytm from 'yaml-to-momoa';
import type Logger from './logger.js';

// Export some types as a convenience, because they originally came from this package
export type {
Group,
TokenNormalized,
TokenNormalizedSet,
TokenTransformed,
TokenTransformedBase,
} from '@terrazzo/token-tools';

export interface PluginHookContext {
logger: Logger;
}
Expand Down Expand Up @@ -406,43 +421,6 @@ export interface ResolverSetNormalized {
$defs: Record<string, unknown> | undefined;
}

interface TokenTransformedBase {
/** Original Token ID */
id: string;
/** ID unique to this format. */
localID?: string;
/**
* The mode of this value
* @default "."
*/
mode: string;
/** The original token. */
token: TokenNormalized;
/** Arbitrary metadata set by plugins. */
meta?: Record<string | number | symbol, unknown> & {
/**
* Metadata for the token-listing plugin. Plugins can
* set this to be the name of a token as it appears in code,
* and the token-listing plugin will pick it up and use it.
*/
'token-listing'?: { name: string | undefined };
};
}

/** Transformed token with a single value. Note that this may be any type! */
export interface TokenTransformedSingleValue extends TokenTransformedBase {
type: 'SINGLE_VALUE';
value: string;
}

/** Transformed token with multiple values. Note that this may be any type! */
export interface TokenTransformedMultiValue extends TokenTransformedBase {
type: 'MULTI_VALUE';
value: Record<string, string>;
}

export type TokenTransformed = TokenTransformedSingleValue | TokenTransformedMultiValue;

export interface TransformParams {
/** ID of an existing format */
format: string;
Expand Down
24 changes: 13 additions & 11 deletions packages/plugin-css/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,20 @@ export default function buildFormat({

// multi-value token
else if (token.type === 'MULTI_VALUE') {
const shorthand = generateShorthand({ $type: token.token.$type, localID });
if (shorthand) {
rootRule.declarations[token.localID ?? token.token.id] = {
value: shorthand,
description: token.token.$description,
};
}
for (const [name, value] of Object.entries(token.value)) {
rootRule.declarations[name === '.' ? localID : [localID, name].join('-')] = {
value,
description: token.token.$description,
};
}
// Note: always place shorthand after other values
const shorthand = generateShorthand({ token: { ...token.token, $value: token.value as any }, localID });
if (shorthand) {
rootRule.declarations[token.localID ?? token.token.id] = {
value: shorthand,
description: token.token.$description,
};
}
}
}
}
Expand Down Expand Up @@ -166,13 +167,14 @@ export default function buildFormat({

// multi-value token
else {
const shorthand = generateShorthand({ $type: token.token.$type, localID });
if (shorthand) {
selectorRule.declarations[localID] = { value: shorthand, description: token.token.$description };
}
for (const [name, subvalue] of Object.entries(token.value)) {
selectorRule.declarations[`${localID}-${name}`] = { value: subvalue, description: token.token.$description };
}
// Note: always generate shorthand after other declarations
const shorthand = generateShorthand({ token: { ...token.token, $value: token.value as any }, localID });
if (shorthand) {
selectorRule.declarations[localID] = { value: shorthand, description: token.token.$description };
}
}

// redeclare aliases so they have the correct scope
Expand Down
5 changes: 1 addition & 4 deletions packages/plugin-css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ export default function cssPlugin(options?: CSSPluginOptions): Plugin {
let listingName: string | undefined = localID;

// Composite tokens without a shorthand won't get generated in the output, so we don't list them.
if (
typeof transformedValue === 'object' &&
generateShorthand({ $type: token.$type, localID }) === undefined
) {
if (typeof transformedValue === 'object' && generateShorthand({ token, localID }) === undefined) {
listingName = undefined;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-css/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ function _printRule(rule: CSSRule): string {
indent += ' ';
}

const declarations = Object.entries(rule.declarations);
declarations.sort((a, b) => a[0].localeCompare(b[0], 'en-us', { numeric: true }));
for (const [k, d] of declarations) {
for (const [k, d] of Object.entries(rule.declarations)) {
output.push(`${indent}${k}: ${d.value};${d.description ? ` /* ${d.description} */` : ''}`);
}

Expand Down
Loading