-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formats): any swift format (#734)
✨ 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
1 parent
7bfeb92
commit 9859a8d
Showing
10 changed files
with
393 additions
and
42 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
__tests__/common/formatHelpers/setSwiftFileProperties.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 '); | ||
}); | ||
|
||
}); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.