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

feat(es/typescript): inline enum #3647

Merged
merged 4 commits into from
Feb 24, 2022
Merged

Conversation

magic-akari
Copy link
Member

Description:
Inline enum if possible.

BREAKING CHANGE:

Related issue (if exists):

@kdy1
Copy link
Member

kdy1 commented Feb 20, 2022

Does this fix #940?

@magic-akari
Copy link
Member Author

Partially.
It does not work across files.


impl Default for InlineEnumConfig {
fn default() -> Self {
Self::All(true)
Copy link
Member Author

Choose a reason for hiding this comment

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

@kdy1
I set "inline all as possible" as default option.
What do you think about it?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's good enough to be default

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!

use swc_ecma_utils::ident::IdentLike;
use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith};

pub type TSEnumLit = Rc<RefCell<AHashMap<Id, AHashMap<JsWord, Lit>>>>;
Copy link
Member

Choose a reason for hiding this comment

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

Can you change this to a struct which hides the actual type?
The field can be like

pub(crate) values: Rc<RefCell<AHashMap<Id, AHashMap<JsWord, Lit>>>>


impl Default for InlineEnumConfig {
fn default() -> Self {
Self::All(true)
Copy link
Member

Choose a reason for hiding this comment

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

If so, we have two options I guess.

The first one is adding a field to Assumptions and the second one is defaulting to EnumConst::Const.
I think the second one would be better at the moment

Copy link
Member

Choose a reason for hiding this comment

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

I prefer an assumption, we need to expose Assumptions in a near future anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see. I think the assumption is good too.

Copy link
Member

Choose a reason for hiding this comment

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

That would also need to change the options from InlineEnumConfig to bool

@magic-akari magic-akari marked this pull request as draft February 21, 2022 06:35
@magic-akari magic-akari marked this pull request as ready for review February 21, 2022 09:33
@magic-akari magic-akari requested a review from kdy1 February 21, 2022 09:37
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!

is_lhs: bool,
}

trait SkipInlineEnum {
Copy link
Member

Choose a reason for hiding this comment

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

Is this trait required?

fn skip(&self) -> bool;
}

impl SkipInlineEnum for InlineEnum {
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this is the only impl.
I think it can be a method of InlineEnum

Copy link
Member Author

Choose a reason for hiding this comment

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

I had implemented this method on another pub struct while I want to hide it, so I wrote this trait.
I am going to fix it.

@kdy1 kdy1 self-assigned this Feb 22, 2022
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

I found some tiny issues, thank you!


pub(crate) type TSEnumLit = Rc<RefCell<AHashMap<Id, AHashMap<JsWord, Lit>>>>;

pub fn inline_enum(ts_enum_lit: TSEnumLit, ts_enum_config: TSEnumConfig) -> impl Fold + VisitMut {
Copy link
Member

Choose a reason for hiding this comment

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

This function is exposing a crate-local type (TSEnumLit) in public api.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it should be pub(crate) fn?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, inline_enum should not be public API

@magic-akari magic-akari requested a review from kdy1 February 23, 2022 18:14
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!
LGTM.
Sorry for the delay, I had lots of PRs to review.


swc-bump:

  • swc_ecma_transforms_typescript --breaking

@kdy1 kdy1 enabled auto-merge (squash) February 24, 2022 08:57
@kdy1 kdy1 added this to the v1.2.145 milestone Feb 24, 2022
@kdy1 kdy1 merged commit 1743302 into swc-project:main Feb 24, 2022
@magic-akari magic-akari deleted the feat/enum-inline branch February 26, 2022 16:07
@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants