Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): loosening the Platform type to allow for extension #926

Merged
merged 2 commits into from
Mar 23, 2023
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
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