Skip to content

Commit

Permalink
feat(formats): any swift format (#734)
Browse files Browse the repository at this point in the history
✨ add new format ios-swift/any.swift with:
* objectType: type for the swift object - default is class
* import: array or string to import modules - default is ["UIKit"]
* accessControl: access level for the object - default is public
🎨 update ios-swift/class and ios-swift/enum to import any.swift template
🔥 remove class.swift.template and enum.swift.template
✨ add helper setSwiftFileProperties to format file object for swift
✅ add test for setSwiftFileProperties and snapshot for swift template
📝 update docs comment for ios-swift formats
📝 add ios-swift/any.swift to examples / basic / config.json

Co-authored-by: @antoniogamizbadger
  • Loading branch information
gabischima authored Dec 22, 2021
1 parent 7bfeb92 commit 9859a8d
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 42 deletions.
51 changes: 51 additions & 0 deletions __tests__/common/formatHelpers/setSwiftFileProperties.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.
*/

const setSwiftFileProperties = require('../../../lib/common/formatHelpers/setSwiftFileProperties');

describe('common', () => {
describe('formatHelpers', () => {

describe('setSwiftFileProperties', () => {
it('should default accessControl be public', () => {
const file = setSwiftFileProperties({}, undefined, 'ios-swift');
expect(file.accessControl).toEqual('public ');
});

it('should default objectType be class', () => {
const file = setSwiftFileProperties({}, undefined, 'ios-swift');
expect(file.objectType).toEqual('class');
});

it('should default import be ["UIKit"]', () => {
const file = setSwiftFileProperties({}, undefined, 'ios-swift');
const fileSeparate = setSwiftFileProperties({}, undefined, 'ios-swift-separate');
expect(file.import).toEqual(['UIKit']);
expect(fileSeparate.import).toEqual(['UIKit']);
});

it('should transform string import to array', () => {
const file = setSwiftFileProperties({ import: 'SwiftUI' }, undefined, 'ios-swift');
expect(file.import).toEqual(['SwiftUI']);
});

it('should file be properly configured', () => {
const file = setSwiftFileProperties({ objectType: 'extension', import: ['SwiftUI'], accessControl: 'public'}, undefined, 'ios-swift');
expect(file.objectType).toEqual('extension');
expect(file.import).toEqual(['SwiftUI']);
expect(file.accessControl).toEqual('public ');
});

});
})
})
18 changes: 18 additions & 0 deletions __tests__/formats/__snapshots__/all.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,24 @@ NSString * const color_red = #FF0000;
"
`;
exports[`formats all should match ios-swift/any.swift snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
import UIKit
public class {
public static let color_red = #FF0000 /* comment */
}
"
`;
exports[`formats all should match ios-swift/class.swift snapshot 1`] = `
"
//
Expand Down
74 changes: 74 additions & 0 deletions __tests__/formats/__snapshots__/swiftFile.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`formats ios-swift/any.swift should match default snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
import UIKit
public class StyleDictionary {
public static let colorBaseRed = UIColor(red: 1.000, green: 0.000, blue: 0.000, alpha: 1)
}
"
`;

exports[`formats ios-swift/any.swift with access control override should match snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
import UIKit
internal class StyleDictionary {
internal static let colorBaseRed = UIColor(red: 1.000, green: 0.000, blue: 0.000, alpha: 1)
}
"
`;

exports[`formats ios-swift/any.swift with import override should match snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
import UIKit
import AnotherModule
public class StyleDictionary {
public static let colorBaseRed = UIColor(red: 1.000, green: 0.000, blue: 0.000, alpha: 1)
}
"
`;

exports[`formats ios-swift/any.swift with objectType override should match snapshot 1`] = `
"
//
// __output/
//
// Do not edit directly
// Generated on Sat, 01 Jan 2000 00:00:00 GMT
import UIKit
public struct StyleDictionary {
public static let colorBaseRed = UIColor(red: 1.000, green: 0.000, blue: 0.000, alpha: 1)
}
"
`;
94 changes: 94 additions & 0 deletions __tests__/formats/swiftFile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.
*/
const formats = require('../../lib/common/formats');
const createDictionary = require('../../lib/utils/createDictionary');
const createFormatArgs = require('../../lib/utils/createFormatArgs');
const _ = require('../../lib/utils/es6_');

const originalFile = {
"destination": "__output/",
"format": "ios-swift/any.swift",
"className": "StyleDictionary",
"filter": {
"attributes": {
"category": "color"
}
},
"options": {}
};

var file = {}

const properties = {
color: {
base: {
red: {
value: 'UIColor(red: 1.000, green: 0.000, blue: 0.000, alpha: 1)',
filePath: 'tokens/color/base.json',
original: { value: '#FF0000' },
name: 'colorBaseRed',
attributes: { category: 'color', type: 'base', item: 'red' },
path: [ 'color', 'base', 'red' ]
}
}
}
};

const format = formats['ios-swift/any.swift'];
const dictionary = createDictionary({ properties });

describe('formats', () => {

describe('ios-swift/any.swift', () => {
beforeEach(() => {
file = _.cloneDeep(originalFile);
});

it('should match default snapshot', () => {
expect(format(createFormatArgs({
dictionary,
file,
platform: {}
}), {}, file)).toMatchSnapshot();
});

it('with import override should match snapshot', () => {
file.options.import = ["UIKit", "AnotherModule"];
expect(format(createFormatArgs({
dictionary,
file,
platform: {}
}), {}, file)).toMatchSnapshot();
});

it('with objectType override should match snapshot', () => {
file.options.objectType = "struct"
expect(format(createFormatArgs({
dictionary,
file,
platform: {}
}), {}, file)).toMatchSnapshot();
});

it('with access control override should match snapshot', () => {
file.options.accessControl = "internal"
expect(format(createFormatArgs({
dictionary,
file,
platform: {}
}), {}, file)).toMatchSnapshot();
});

});

});
20 changes: 17 additions & 3 deletions examples/basic/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,25 @@
"transformGroup": "ios-swift",
"buildPath": "build/ios-swift/",
"files": [{
"destination": "StyleDictionary.swift",
"destination": "StyleDictionary+Class.swift",
"format": "ios-swift/class.swift",
"className": "StyleDictionary",
"className": "StyleDictionaryClass",
"filter": {}
},{
"destination": "StyleDictionary+Enum.swift",
"format": "ios-swift/enum.swift",
"className": "StyleDictionaryEnum",
"filter": {}
},{
"destination": "StyleDictionary+Struct.swift",
"format": "ios-swift/any.swift",
"className": "StyleDictionaryStruct",
"filter": {},
"options": {
"imports": "SwiftUI",
"objectType": "struct",
"accessControl": "internal"
}
}]
},
"ios-swift-separate-enums": {
Expand All @@ -117,7 +132,6 @@
"destination": "StyleDictionarySize.swift",
"format": "ios-swift/enum.swift",
"className": "StyleDictionarySize",
"type": "float",
"filter": {
"attributes": {
"category": "size"
Expand Down
3 changes: 2 additions & 1 deletion lib/common/formatHelpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
iconsWithPrefix: require('./iconsWithPrefix'),
sortByReference: require('./sortByReference'),
sortByName: require('./sortByName'),
minifyDictionary: require('./minifyDictionary')
minifyDictionary: require('./minifyDictionary'),
setSwiftFileProperties: require('./setSwiftFileProperties')
}
55 changes: 55 additions & 0 deletions lib/common/formatHelpers/setSwiftFileProperties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.
*/

/**
* Outputs an object with swift format configurations. Sets import, object type and access control.
* @memberof module:formatHelpers
* @param {Object} options - The options object declared at configuration
* @param {String} objectType - The type of the object in the final file. Could be a class, enum, struct, etc.
* @param {String} transformGroup - The transformGroup of the file, so it can be applied proper import
* @returns {Object}
*/
function setSwiftFileProperties(options, objectType, transformGroup) {
if (typeof options.objectType === 'undefined') {
if (typeof objectType === 'undefined') {
options.objectType = 'class';
} else {
options.objectType = objectType;
}
}

if (typeof options.import === 'undefined') {
if (typeof transformGroup === 'undefined') {
options.import = ['UIKit'];
} else if (['ios-swift', 'ios-swift-separate'].includes(transformGroup)) {
options.import = ['UIKit'];
} else {
// future swift-ui transformGroup to be added here
options.import = ['SwiftUI'];
}
} else if (typeof options.import === 'string') {
options.import = [options.import];
}

if (typeof options.accessControl === 'undefined') {
options.accessControl = 'public ';
} else {
if (options.accessControl !== "") {
options.accessControl = `${options.accessControl} `;
}
}

return options
}

module.exports = setSwiftFileProperties;
Loading

0 comments on commit 9859a8d

Please sign in to comment.