Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokenMap props and convertTokenData utility #1397

Merged
merged 2 commits into from
Dec 8, 2024
Merged

Conversation

jorenbroekema
Copy link
Collaborator

@jorenbroekema jorenbroekema commented Nov 25, 2024

  • Adjust / add docs for tokenMap property and flattenTokens / convertToTokenObject/Map utils
  • Refactor internals away from flattenTokens as prep for deprecation
  • Add TODOs where relevant for next PR to make full use of the tokenMap prop for perf gains
  • Add changeset

For next PR:

  • use tokenMap for transform hook (significant perf gains for large sets)
  • use tokenMap for reference resolutions (huge perf gains for large sets especially with loads of chained refs)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

This pull request is automatically being deployed by Amplify Hosting (learn more).

Access this pull request here: https://pr-1397.d16eby4ekpss5y.amplifyapp.com

@jorenbroekema jorenbroekema force-pushed the token-map branch 8 times, most recently from a07dd39 to d65595c Compare November 26, 2024 17:25
@jorenbroekema jorenbroekema changed the title WIP: Token map structure perf improvements Add tokenMap props and convertTokenData utility Nov 26, 2024
@jorenbroekema jorenbroekema marked this pull request as ready for review November 26, 2024 17:27
@jorenbroekema jorenbroekema requested a review from a team as a code owner November 26, 2024 17:27
@jorenbroekema jorenbroekema force-pushed the token-map branch 3 times, most recently from e248f97 to b906023 Compare November 26, 2024 17:39
@jorenbroekema jorenbroekema force-pushed the token-map branch 2 times, most recently from 7d7cf36 to c80f112 Compare November 29, 2024 12:54
@dbanksdesign dbanksdesign merged commit 209085d into main Dec 8, 2024
5 checks passed
@dbanksdesign dbanksdesign deleted the token-map branch December 8, 2024 18:08
tkgroot pushed a commit to tkgroot/style-dictionary that referenced this pull request Dec 9, 2024
// Stop either when we encounter a "value" prop or if we find that every prop is not an object, meaning we cannot traverse any further
if (isPlainObject(slice[key]) && Object.hasOwn(slice[key], `${usesDtcg ? '$' : ''}value`)) {
result.push({
.../** @type {T} */ (slice[key]),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbanksdesign this change introduces top-level token copy which can broke some use cases. convertTokenData process tokens the same way

This can be broken change after 4.2.0 version

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_ret.push(/** @type {Token} */ (slice[name]));

is equivalent to

result.push({ .../** @type {T} */ (slice[key]) })

given the token is an object type.. All we added is a key property to it.

I'm not sure why that would be a breaking change, could you elaborate or show an example?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the same, second is equivalent of Object.assign({}, slice[key])

It's not breaking change for me, but for someone may be.

for example

for(const token of flattenTokens(dictionary.tokens)) {
  // ...some modification of token
}

in 4.2 will affect real tokens in dictionary
in 4.3 will ignore top properties change, for example token.value

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I see. The flattenTokens util should return a value that isn't coupled to the input value imo, so mutating the result should not mutate the input, this is unintentional and a bad practice if you ask me.

I would consider this an unintentional bugfix then; while people may rely on the "bug" (unintended behavior) in their code, it doesn't make the bugfix warrant a major bump.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that. I fixed it in my pipeline.

But to make function totally pure you should use full clone instead of just shallow clone.
Because current behavior allow of update for example subproperties of token, i.e attributes, properties of object values etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I think in a lot of places I'm already using structuredClone() but we should verify for any function that is doing some kind of token handling that these functions are pure. https://github.com/amzn/style-dictionary/blob/main/lib/utils/flattenTokens.js#L62 is a good example where it's not yet being done.
Created an issue for this #1412

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants