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

Allow JS plugin to nest token output #79

Merged
merged 4 commits into from
Aug 18, 2023

Conversation

mike-engel
Copy link
Contributor

Changes

This implements a solution for #73, allowing the output from the JS plugin to be a nested object rather than a flat object with key IDs in dot-notation.

This converts JS, TS, mode, and meta data to all be nested (see the test want files). By default, this mode will be off to preserve backwards compatibility and can be turned on with the deep key in the plugin config. I'm not attached to that name, so I'm happy to change it.

Some internal type information had to change a little bit, but not much, and a new (safer? maybe, not sure) serializeTS method was added for simplicity. Additionally, a new util object.set was added for naive deep setting of a nested object key safely. It doesn't cover all the cases that something like lodash.set would, but that's ok with the current implementation of the library, and it's documented.

How to Review

Tests are the most revealing for final output, and the actual implementation is a few new functions and some refactorings to use them.

@changeset-bot
Copy link

changeset-bot bot commented Aug 18, 2023

🦋 Changeset detected

Latest commit: 7885492

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@cobalt-ui/plugin-js Minor
@cobalt-ui/utils Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

if (options?.deep) {
setToken(ts.modes, `${token.id}.${modeName}`, `${tokenTypes[token.$type]}['$value']`, true);
} else {
(ts.modes[token.id] as TSResult['modes'])[modeName] = `${tokenTypes[token.$type]}['$value']`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

😅 I’m so sorry you had to wade through the manual .d.ts file generation. I don’t think a lot of this code was written as well as I’d like to be easy to refactor / work on.

But this all looks fantastic 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly it wasn’t that bad, it was just very specific! You did most of the work with the serializer function 😄

Copy link
Collaborator

@drwpow drwpow Aug 18, 2023

Choose a reason for hiding this comment

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

For a lot of codegen stuff, taking a more AST approach is “cleaner” in some ways. But you pay for it in other ways in terms of upfront timesink / wading through the land of obscure bugs, not to mention when you set it down for a while, the confusion of “wait how does this thing work again?”

String mashing is sloppy but it’s real easy to compare “output good” vs “output bad” and know exactly where to make the adjustment

Copy link
Collaborator

@drwpow drwpow left a comment

Choose a reason for hiding this comment

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

👏 This looks great, thank you!

Also I’m not opinionated on the name either. deep makes sense to me!

I don’t have any feedback; this looks great 🙂

@drwpow drwpow merged commit 2963b21 into terrazzoapp:main Aug 18, 2023
@github-actions github-actions bot mentioned this pull request Aug 18, 2023
@drwpow drwpow mentioned this pull request Sep 5, 2023
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.

2 participants