Skip to content

Commit

Permalink
feat(types): loosening the Platform type to allow for extension (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanksdesign authored Mar 23, 2023
1 parent c01f4cf commit c43263b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
4 changes: 2 additions & 2 deletions types/Platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { Options } from './Options';
import { File } from './File';

export interface Platform {
export type Platform<PlatformType = Record<string, any>> = {
transformGroup?: string;
transforms?: string[];
basePxFontSize?: number;
Expand All @@ -23,4 +23,4 @@ export interface Platform {
files?: File[];
actions?: string[];
options?: Options;
}
} & PlatformType;
17 changes: 10 additions & 7 deletions types/Transform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@ import { Matcher } from './Matcher';
import { TransformedToken } from './TransformedToken';
import { Platform } from './Platform';

export interface NameTransform {
export interface NameTransform<PlatformType = Record<string,any>> {
type: "name";
matcher?: Matcher;
transformer: (
token: TransformedToken,
options?: Platform
options: Platform<PlatformType>
) => string;
}

export interface ValueTransform {
export interface ValueTransform<PlatformType = Record<string,any>> {
type: "value";
transitive?: boolean;
matcher?: Matcher;
transformer: (
token: TransformedToken,
options?: Platform
options: Platform<PlatformType>
) => any;
}

export interface AttributeTransform {
export interface AttributeTransform<PlatformType = Record<string,any>> {
type: "attribute";
matcher?: Matcher;
transformer: (
token: TransformedToken,
options?: Platform
options: Platform<PlatformType>
) => { [key: string]: any };
}

export type Transform = NameTransform | ValueTransform | AttributeTransform;
export type Transform<PlatformType = Record<string,any>> =
| NameTransform<PlatformType>
| ValueTransform<PlatformType>
| AttributeTransform<PlatformType>;
6 changes: 3 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ declare namespace StyleDictionary {
type Matcher = _Matcher;
type Options = _Options;
type Parser = _Parser;
type Platform = _Platform;
type Transform = _Transform;
type Platform<PlatformType = Record<string,any>> = _Platform<PlatformType>;
type Transform<PlatformType = Record<string,any>> = _Transform<PlatformType>;
type TransformedToken = _TransformedToken;
type TransformedTokens = _TransformedTokens;
type TransformGroup = _TransformGroup;
Expand Down Expand Up @@ -97,7 +97,7 @@ declare namespace StyleDictionary {
* });
* ```
*/
registerTransform(transform: Named<Transform>): this;
registerTransform<PlatformType>(transform: Named<Transform<PlatformType>>): this;

/**
* Add a custom transformGroup to the Style Dictionary, which is a
Expand Down
29 changes: 29 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ expectType<StyleDictionary.Core>(
})
);

type CustomPlatform = {
colorMode: 'light' | 'dark';
}

expectType<StyleDictionary.Core>(
StyleDictionary.registerTransform<CustomPlatform>({
name: "colormode",
type: "value",
matcher: function (token) {
return token.attributes?.category === "color";
},
transformer: function (token, platform) {
expectType<StyleDictionary.TransformedToken>(token);
expectType<StyleDictionary.Platform<CustomPlatform>>(platform);
if (platform.colorMode === 'light') {
return 'light'
} else {
return 'dark'
}
},
})
);

expectType<StyleDictionary.Core>(
StyleDictionary.registerTransformGroup({
name: "Swift",
Expand Down Expand Up @@ -154,6 +177,12 @@ expectAssignable<StyleDictionary.Platform>({
transforms: [`attribute/cti`],
});

expectAssignable<StyleDictionary.Platform>({
colorMode: 'dark',
files: [],
transformGroup: 'scss',
});

expectAssignable<StyleDictionary.Platform>({
transforms: [`attribute/cti`],
files: [{
Expand Down

0 comments on commit c43263b

Please sign in to comment.