From 83737211d2da2e004ddfb09ede912175f5b4b1bd Mon Sep 17 00:00:00 2001 From: giovannimartusciello <44449525+giovannimartusciello@users.noreply.github.com> Date: Sun, 29 Jan 2023 05:46:32 +0100 Subject: [PATCH] feat(formats): compose object imports (#874) Co-authored-by: Giovanni Martusciello --- lib/common/formatHelpers/index.js | 3 +- .../setComposeObjectProperties.js | 30 +++++++++++++++++++ lib/common/formats.js | 5 +++- .../templates/compose/object.kt.template | 7 +++-- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 lib/common/formatHelpers/setComposeObjectProperties.js diff --git a/lib/common/formatHelpers/index.js b/lib/common/formatHelpers/index.js index 7e3ad2f09..1c7f7d2ed 100644 --- a/lib/common/formatHelpers/index.js +++ b/lib/common/formatHelpers/index.js @@ -24,5 +24,6 @@ sortByReference: require('./sortByReference'), sortByName: require('./sortByName'), minifyDictionary: require('./minifyDictionary'), - setSwiftFileProperties: require('./setSwiftFileProperties') + setSwiftFileProperties: require('./setSwiftFileProperties'), + setComposeObjectProperties: require('./setComposeObjectProperties') } \ No newline at end of file diff --git a/lib/common/formatHelpers/setComposeObjectProperties.js b/lib/common/formatHelpers/setComposeObjectProperties.js new file mode 100644 index 000000000..50ec8af34 --- /dev/null +++ b/lib/common/formatHelpers/setComposeObjectProperties.js @@ -0,0 +1,30 @@ +/* + * 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 for compose format configurations. Sets import. + * @memberof module:formatHelpers + * @param {Object} options - The options object declared at configuration + * @returns {Object} + */ + function setComposeObjectProperties(options) { + if (typeof options.import === 'undefined') { + options.import = ['androidx.compose.ui.graphics.Color', 'androidx.compose.ui.unit.*']; + } else if (typeof options.import === 'string') { + options.import = [options.import]; + } + + return options + } + + module.exports = setComposeObjectProperties; \ No newline at end of file diff --git a/lib/common/formats.js b/lib/common/formats.js index 2977be36d..d4da2aa77 100644 --- a/lib/common/formats.js +++ b/lib/common/formats.js @@ -15,7 +15,7 @@ const fs = require('fs'); const path = require('path'); const _template = require('lodash/template'); const GroupMessages = require('../utils/groupMessages'); -const { fileHeader, formattedVariables, getTypeScriptType, iconsWithPrefix, minifyDictionary, sortByReference, createPropertyFormatter, sortByName, setSwiftFileProperties } = require('./formatHelpers'); +const { fileHeader, formattedVariables, getTypeScriptType, iconsWithPrefix, minifyDictionary, sortByReference, createPropertyFormatter, sortByName, setSwiftFileProperties, setComposeObjectProperties } = require('./formatHelpers'); const SASS_MAP_FORMAT_DEPRECATION_WARNINGS = GroupMessages.GROUP.SassMapFormatDeprecationWarnings; @@ -708,6 +708,7 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul * @param {String} className The name of the generated Kotlin object * @param {String} packageName The package for the generated Kotlin object * @param {Object} options + * * @param {String[]} [options.import=androidx.compose.ui.unit.*] - Modules to import. Can be a string or array of string * @param {Boolean} [options.showFileHeader=true] - Whether or not to include a comment that has the build date * @param {Boolean} [options.outputReferences=false] - Whether or not to keep [references](/#/formats?id=references-in-output-files) (a -> b -> c) in the output. * @example @@ -742,6 +743,8 @@ declare const ${moduleName}: ${JSON.stringify(treeWalker(dictionary.tokens), nul allProperties = [...dictionary.allProperties].sort(sortByName); } + options = setComposeObjectProperties(options); + return template({allProperties, file, options, formatProperty, fileHeader}); }, diff --git a/lib/common/templates/compose/object.kt.template b/lib/common/templates/compose/object.kt.template index 997c8041b..009282bdd 100644 --- a/lib/common/templates/compose/object.kt.template +++ b/lib/common/templates/compose/object.kt.template @@ -17,9 +17,10 @@ package <%= file.packageName %>; -<% // Ideally we would selectively import the correct classes & functions based on whether the applied transforms actually need them. %> -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.unit.* +<%= options.import.map(function(item) { + return 'import ' + item + }).join('\n') +%> object <%= file.className %> { <%= allProperties.map(function(prop) {