Open DarioSoller opened 1 month ago
I think this would be a good idea, we'll also need it for the built in actions and transformGroups.
I would also suggest using CONSTANT_CASE for the keys, seems to be a convention for string constants quite often in JS projects.
@jorenbroekema you're right that this constant naming convention is often used, but there are also trends in Javascript against this. Some people say that UPPER_SNAKE_CASE for constants is old fashioned and comes from times, where IDEs have not been capable to inform a developer about a variable being a constant and read-only. Angular for example does not follow this principle and they used to have it explicitly in their coding style guides, the way I did it. Couldn't find it on a quick search though, but they still stick to this rule.
Anyhow, I would prefer a little bit more modern naming approach, but I do not have strong feelings about it, as I am aware, that UPPER_SNAKE_CASE is a well known naming pattern.
I would love to bring this to a PR. Let me have a look into this and then I might need a few more answers, bevor I can provide a first solution for it.
@jorenbroekema can you answer me the following questions, before I start adding things to a PR:
lib
folder, if I am seeing this correctly? This would mean that we can't make use of the enums in the JS source right now. Are you planing to migrate the whole Style-Dictionary source code to Typescript one day?'android/copyImages'
and copy_assets
?web
, js
, scss
, css
, less
, html
, android
, compose
, ios
, 'ios-swift'
, 'ios-swift-separate'
, assets
, flutter
, 'flutter-separate'
, 'react-native'
export enum ThemeOptions {
EXCLUDED = 'excluded',
INCLUDED = 'included'
}
type ValueOf
// looping over the enum
const options = (Object.keys(ThemeOptions) as Array
2. yes
3. I think so yeah
4. As far as I can tell yep
5. Yeah there's probably tests or other places where we use the hard-coded strings
As for the naming thing, I went looking if we have some convention already in our source code for this and I stumbled upon the logger utility (`lib/utils/groupMessages.js`:
```js
this.GROUP = {
PropertyReferenceWarnings: 'Property Reference Errors',
PropertyValueCollisions: 'Property Value Collisions',
TemplateDeprecationWarnings: 'Template Deprecation Warnings',
RegisterTemplateDeprecationWarnings: 'Register Template Deprecation Warnings',
SassMapFormatDeprecationWarnings: 'Sass Map Format Deprecation Warnings',
MissingRegisterTransformErrors: 'Missing Register Transform Errors',
PropertyNameCollisionWarnings: 'Property Name Collision Warnings',
FilteredOutputReferences: 'Filtered Output Reference Warnings',
UnknownCSSFontProperties: 'Unknown CSS Font Shorthand Properties',
};
which is later used as:
const PROPERTY_VALUE_COLLISIONS = GroupMessages.GROUP.PropertyValueCollisions;
const PROPERTY_REFERENCE_WARNINGS = GroupMessages.GROUP.PropertyReferenceWarnings;
const UNKNOWN_CSS_FONT_PROPS_WARNINGS = GroupMessages.GROUP.UnknownCSSFontProperties;
const FILTER_WARNINGS = GroupMessages.GROUP.FilteredOutputReferences;
Kinda confusing coz it's mixing CONSTANT_CASE with PascalCase 😓 . I'm not really sure what's best here, I'm leaning to just converting it all to camelCase and keeping things simple. These mixed casings seem a bit outdated indeed. So let's just go with camelCase and we can bring the consistency to the other places in another PR someday.
Instead of enums, we could do this instead:
So just exporting a key value pair JS object, for each hook, and then adding an entrypoint to StyleDictionary so people can import like so:
import { formats, transforms } from 'style-dictionary/enums';
// optional, if you want to ensure that it's read-only and the members cannot be assigned to, to make it similar to enums
const formatsReadOnly = formats as const;
More info: https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums
I think I can start on a PR now! Thanks for your clarifications with the details.
I think it would be a favourable improvement, if you could centrally provide Typescript Enums for a few things, rather than having everyone writing their own. This would create a smoother DX for anyone using Style-Dictionary in a Typescript project, because a single source of truth (SSOT) for these things also make future update paths easier, cause Typescript errors can guide you for identifying breaking changes in the values for the formats or transfroms.
While I do acknowledge that enums are controversial in some scenarios, I think that they would really shine here for having more explicitly defined format and transform options. As enums can be used as a type but also as a constant, I see some unused benefits for within the Style-Dictionary code base itself and for anyone using Style-Dictionary in a Typescript project.
My proposed two enums that I created myself look like the following: sd-predefined-transforms.enum.ts
sd-file-formats.enum.ts
Usage example: