/* * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with * the License. A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ import { expectType, expectError, expectAssignable } from 'tsd'; import StyleDictionary from './index'; expectType(StyleDictionary.buildAllPlatforms()); expectType(StyleDictionary.buildPlatform('web')); expectType(StyleDictionary.cleanAllPlatforms()); expectType(StyleDictionary.cleanPlatform('web')); expectType(StyleDictionary.exportPlatform('web')); expectType>(StyleDictionary.extend('config.json')); expectType>( StyleDictionary.extend({ source: ['tokens/**/*.json'], platforms: { scss: { transformGroup: 'scss', buildPath: 'build/', files: [ { destination: 'variables.scss', format: 'scss/variables', }, ], }, }, }), ); expectType( StyleDictionary.registerAction({ name: 'copy_assets', do: function (dictionary, config) { console.log('Copying assets directory', 'assets', config.buildPath + 'assets'); }, undo: function (dictionary, config) { console.log('Cleaning assets directory', config.buildPath + 'assets'); }, }), ); expectType( StyleDictionary.registerFilter({ name: 'isColor', matcher: function (token) { return token.attributes?.category === 'color'; }, }), ); expectType( StyleDictionary.registerFormat({ name: 'json', formatter: function ({ dictionary, file, options, platform }) { expectType(dictionary); expectType(file); expectType(options); expectType(platform); return JSON.stringify(dictionary.tokens, null, 2); }, }), ); expectType( StyleDictionary.registerTemplate({ name: 'Swift/colors', template: __dirname + '/templates/swift/colors.template', }), ); expectType( StyleDictionary.registerTransform({ name: 'time/seconds', type: 'value', matcher: function (token) { return token.attributes?.category === 'time'; }, transformer: function (token) { expectType(token); return (parseInt(token.original.value) / 1000).toString() + 's'; }, }), ); type CustomPlatform = { colorMode: 'light' | 'dark'; }; expectType( StyleDictionary.registerTransform({ name: 'colormode', type: 'value', matcher: function (token) { return token.attributes?.category === 'color'; }, transformer: function (token, platform) { expectType(token); expectType>(platform); if (platform.colorMode === 'light') { return 'light'; } else { return 'dark'; } }, }), ); expectType( StyleDictionary.registerTransformGroup({ name: 'Swift', transforms: ['attribute/cti', 'size/pt', 'name/cti'], }), ); expectType( StyleDictionary.registerParser({ pattern: /\.json$/, parse: ({ contents, filePath }) => { return {}; }, }), ); expectType( StyleDictionary.registerPreprocessor({ name: 'foo', preprocessor: (dictionary) => dictionary, }), ); const file: StyleDictionary.File = { destination: `somePath.json`, format: `css/variables`, filter: (token) => { expectType(token); return false; }, options: { showFileHeader: true, }, }; expectType(file.options); // Files need a destination expectError({ format: `css/variables`, }); expectAssignable({ format: `css/variables`, destination: `variables.css`, }); expectAssignable({ basePxFontSize: 16, }); expectAssignable({ transformGroup: `css`, }); expectAssignable({ transforms: [`attribute/cti`], }); expectAssignable({ colorMode: 'dark', files: [], transformGroup: 'scss', }); expectAssignable({ transforms: [`attribute/cti`], files: [ { destination: `destination`, }, ], }); expectError({ transforms: `css`, }); expectError({ transformGroup: [`attribute/cti`], }); /** * Testing Options type. * fileHeader needs to be a string or a function that returns a string[] * showFileHeader must be a boolean */ expectError({ fileHeader: false }); expectAssignable({ fileHeader: 'fileHeader' }); expectError({ fileHeader: () => 42 }); expectAssignable({ fileHeader: () => [`hello`] }); expectError({ showFileHeader: 'false' });