Skip to content

Commit

Permalink
fix: get rid of lodash, refactor templates to ES6 (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Apr 16, 2024
1 parent 6cfce97 commit 3485467
Show file tree
Hide file tree
Showing 44 changed files with 906 additions and 761 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-oranges-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': minor
---

Fix some inconsistencies in some of the templates, usually with regards to spaces/newlines
6 changes: 3 additions & 3 deletions __integration__/__snapshots__/flutter.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ snapshots["integration flutter flutter/class.dart should match snapshot"] =
import 'dart:ui';
class StyleDictionary {
StyleDictionary._();
StyleDictionary._();
static const colorBackgroundDanger = Color(0xFFFFEAE9);
static const colorBackgroundDisabled = Color(0xFFDEE1E1);
Expand Down Expand Up @@ -196,7 +196,7 @@ snapshots["integration flutter flutter/class.dart with references should match s
import 'dart:ui';
class StyleDictionary {
StyleDictionary._();
StyleDictionary._();
static const sizePaddingXl = 16.00;
static const sizePaddingLarge = 16.00;
Expand Down Expand Up @@ -377,7 +377,7 @@ snapshots["integration flutter flutter/class.dart separate should match snapshot
import 'dart:ui';
class StyleDictionaryColor {
StyleDictionaryColor._();
StyleDictionaryColor._();
static const colorBackgroundDanger = Color(0xFFFFEAE9);
static const colorBackgroundDisabled = Color(0xFFDEE1E1);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/common/formatHelpers/setSwiftFileProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('common', () => {
describe('setSwiftFileProperties', () => {
it('should default accessControl be public', () => {
const file = setSwiftFileProperties({}, undefined, 'ios-swift');
expect(file.accessControl).to.equal('public ');
expect(file.accessControl).to.equal('public');
});

it('should default objectType be class', () => {
Expand Down Expand Up @@ -50,7 +50,7 @@ describe('common', () => {
);
expect(file.objectType).to.equal('extension');
expect(file.import).to.eql(['SwiftUI']);
expect(file.accessControl).to.equal('public ');
expect(file.accessControl).to.equal('public');
});
});
});
Expand Down
31 changes: 15 additions & 16 deletions __tests__/formats/__snapshots__/all.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,18 @@ snapshots["formats all should match android/colors snapshot"] =
-->
<resources>
<color name="color_red">#FF0000</color><!-- comment -->
</resources>`;
/* end snapshot formats all should match android/colors snapshot */

snapshots["formats all should match android/dimens snapshot"] =
`xml version="1.0" encoding="UTF-8"?>
`<?xml version="1.0" encoding="UTF-8"?>
<!--
Do not edit directly
Generated on Sat, 01 Jan 2000 00:00:00 GMT
-->
<resources>
</resources>`;
/* end snapshot formats all should match android/dimens snapshot */

Expand All @@ -280,7 +279,7 @@ snapshots["formats all should match android/fontDimens snapshot"] =
Generated on Sat, 01 Jan 2000 00:00:00 GMT
-->
<resources>
</resources>`;
/* end snapshot formats all should match android/fontDimens snapshot */

Expand All @@ -292,7 +291,7 @@ snapshots["formats all should match android/integers snapshot"] =
Generated on Sat, 01 Jan 2000 00:00:00 GMT
-->
<resources>
</resources>`;
/* end snapshot formats all should match android/integers snapshot */

Expand All @@ -304,7 +303,7 @@ snapshots["formats all should match android/strings snapshot"] =
Generated on Sat, 01 Jan 2000 00:00:00 GMT
-->
<resources>
</resources>`;
/* end snapshot formats all should match android/strings snapshot */

Expand Down Expand Up @@ -364,7 +363,7 @@ snapshots["formats all should match ios/plist snapshot"] =
<real>NaN</real>
<key>a</key>
<real>1</real>
</dict>
</dict>
<!-- comment -->
</dict>
</plist>`;
Expand Down Expand Up @@ -428,7 +427,7 @@ snapshots["formats all should match ios/singleton.h snapshot"] =
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface : NSObject
@interface : NSObject
+ (NSDictionary *)properties;
+ (NSDictionary *)getProperty:(NSString *)keyPath;
Expand All @@ -449,7 +448,7 @@ snapshots["formats all should match ios/static.h snapshot"] =
#import <Foundation/Foundation.h>
extern const color_red;`;
extern const color_red;`;
/* end snapshot formats all should match ios/static.h snapshot */

snapshots["formats all should match ios/static.m snapshot"] =
Expand All @@ -465,7 +464,7 @@ snapshots["formats all should match ios/static.m snapshot"] =
#import ".h"
const color_red = #FF0000;`;
const color_red = #FF0000;`;
/* end snapshot formats all should match ios/static.m snapshot */

snapshots["formats all should match ios/colors.h snapshot"] =
Expand All @@ -484,7 +483,7 @@ typedef NS_ENUM(NSInteger, ) {
color_red
};
@interface : NSObject
@interface : NSObject
+ (NSArray *)values;
+ (UIColor *)color:()color;
@end`;
Expand Down Expand Up @@ -594,7 +593,7 @@ snapshots["formats all should match ios-swift/class.swift snapshot"] =
import UIKit
public class {
public class {
public static let color_red = #FF0000 /* comment */
}`;
/* end snapshot formats all should match ios-swift/class.swift snapshot */
Expand All @@ -611,7 +610,7 @@ snapshots["formats all should match ios-swift/enum.swift snapshot"] =
import UIKit
public enum {
public enum {
public static let color_red = #FF0000 /* comment */
}`;
/* end snapshot formats all should match ios-swift/enum.swift snapshot */
Expand All @@ -628,7 +627,7 @@ snapshots["formats all should match ios-swift/any.swift snapshot"] =
import UIKit
public class {
public class {
public static let color_red = #FF0000 /* comment */
}`;
/* end snapshot formats all should match ios-swift/any.swift snapshot */
Expand Down Expand Up @@ -718,8 +717,8 @@ snapshots["formats all should match flutter/class.dart snapshot"] =
import 'dart:ui';
class {
._();
class {
._();
static const color_red = #FF0000; /* comment */
}`;
Expand Down
10 changes: 6 additions & 4 deletions examples/advanced/custom-formats-with-templates/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { dirname } from 'path';
import { fileURLToPath } from 'url';
import StyleDictionary from 'style-dictionary';
import fs from 'fs';
import _ from 'lodash';
import handlebars from 'handlebars';
import pug from 'pug';
import webScssTemplate from './templates/web-scss.template.js';
import plistTemplate from './templates/plist.template.js';
import androidTemplate from './templates/android-xml.template.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

Expand All @@ -19,17 +21,17 @@ console.log('\n==============================================');

sd.registerFormat({
name: 'custom/format/scss',
formatter: _.template(fs.readFileSync(__dirname + '/templates/web-scss.template')),
formatter: ({ dictionary }) => webScssTemplate({ allTokens: dictionary.allTokens }),
});

sd.registerFormat({
name: 'custom/format/ios-plist',
formatter: _.template(fs.readFileSync(__dirname + '/templates/ios-plist.template')),
formatter: async ({ dictionary }) => plistTemplate({ dictionary }),
});

sd.registerFormat({
name: 'custom/format/android-xml',
formatter: _.template(fs.readFileSync(__dirname + '/templates/android-xml.template')),
formatter: async ({ dictionary }) => androidTemplate({ dictionary }),
});

// In this case we are using an alternative templating engine (Handlebars)
Expand Down
3 changes: 1 addition & 2 deletions examples/advanced/custom-formats-with-templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"license": "Apache-2.0",
"devDependencies": {
"handlebars": "^4.7.7",
"lodash": "^4.17.21",
"pug": "^3.0.2",
"style-dictionary": "4.0.0-prerelease.23"
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @typedef {import('../../../../types/DesignToken.d.ts').Dictionary} Dictionary
* @typedef {import('../../../../types/DesignToken.d.ts').TransformedToken} TransformedToken
*/

export default ({ dictionary }) => `<?xml version="1.0" encoding="UTF-8"?>
<resources>
${dictionary.allTokens
.map((token) => {
if (token.attributes.category === 'color') {
token.tag = 'color';
} else if (token.attributes.category === 'size') {
token.tag = 'dimen';
} else if (
token.attributes.category === 'time' ||
token.attributes.category === 'opacity' ||
token.attributes.category === 'multiplier'
) {
token.tag = 'double';
} else if (token.attributes.category === 'content') {
token.tag = 'string';
} else {
token.tag = 'item';
}
return token;
})
.map(
(token) =>
` <${token.tag} name="${token.name}">${token.value}</${token.tag}>${
token.comment ? `<!-- ${token.comment} -->` : ''
}`,
)
.join('\n')}
</resources>`;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @typedef {import('../../../../types/DesignToken.d.ts').Dictionary} Dictionary
* @typedef {import('../../../../types/DesignToken.d.ts').TransformedToken} TransformedToken
*/

/**
*
* @param {TransformedToken} token
*/
const tokenTemplate = (token) => {
let output = ` <key>${token.name}</key>\n`;
if (token.type === 'color') {
output += ` <dict>
<key>r</key>
<real>${token.value[0] / 255}</real>
<key>g</key>
<real>${token.value[1] / 255}</real>
<key>b</key>
<real>${token.value[2] / 255}</real>
<key>a</key>
<real>1</real>
</dict>`;
} else if (token.type === 'dimension') {
output += `<integer>${token.value}</integer>`;
} else {
output += `<string>${token.value}</string>`;
}

if (token.comment) {
output += `\n <!-- ${token.comment} -->`;
}
return output;
};

/**
* @param {{
* dictionary: Dictionary
* }} opts
*/
export default ({ dictionary }) => `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
${dictionary.allTokens
.filter(
(token) =>
token.type !== 'asset' &&
token.type !== 'border' &&
token.type !== 'shadow' &&
token.type !== 'transition',
)
.map((token) => tokenTemplate(token))
.join('\n')}
</dict>
</plist>`;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @typedef {import('../../../../types/DesignToken.d.ts').TransformedToken} TransformedToken
*/

/**
* @param {{
* allTokens: TransformedToken[]
* }} opts
*/
export default ({ allTokens }) => `${allTokens
.map((token) => `$${token.name}: ${token.value};${token.comment ? ` // ${token.comment}` : ''}`)
.join('\n')}
`;
Loading

0 comments on commit 3485467

Please sign in to comment.