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: add codemods for es-* #52

Merged
merged 8 commits into from
Jul 24, 2024
Merged

Conversation

merodiro
Copy link
Contributor

Add the following codemods

  • es-get-iterator
  • es-set-tostringtag
  • es-define-property

I'm not too familiar with jscodeshift so I'm not sure if there are problems with my codemods.

Also, I had some type errors. I'm not sure if this is expected or if it can be fixed

return dirtyFlag ? root.toSource(options) : file.source;
},
};
}

Choose a reason for hiding this comment

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

es-set-tostringtag sure should be replaced, but it's a little more complex than this logic (third argument and existence check). In most cases, it will work fine, but I remember that there were some potentially dangerous cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used the example from the es-set-tostringtag docs. Do you know where else I can find examples with more complex logic?

Copy link

@zloirock zloirock Jul 22, 2024

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@merodiro merodiro Jul 22, 2024

Choose a reason for hiding this comment

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

I'm not sure what is the correct way to deal with it. What should the output code look like?

Should I add a condition before calling Object.defineProperty like this if force isn't specified?

  !Object.hasOwn(tagged, Symbol.toStringTag) &&
    Object.defineProperty(tagged, Symbol.toStringTag, {
      configurable: true,
      value: "new tag",
    });

What about If I can't determine the value of force?

Should I replace it with a function that handles these cases?

Choose a reason for hiding this comment

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

What about If I can't determine the value of force?

If the third arg passed to this function - (force || !Object.hasOwn(...)) && ..., if not - just !Object.hasOwn(...) && ....

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was able to handle the case where force is set to true but I wasn't able to handle more complex cases such as

const force = true
setToStringTag(obj, 'tagged', { force });

or

const options = { force: true }
setToStringTag(obj, 'tagged', options);

Copy link
Collaborator

Choose a reason for hiding this comment

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

It could be worth doing a github search for that library and seeing the usage. It can give an insight into how many people are using it in a certain way. If not many people are using force in the first place, we can leave it for now and try to improve on it later. With codemods there will always be edgecases, not everything is very easy to statically analyze, but if we can get 90% of the usecases covered, and log a warning for cases it wasnt able to analyze/transform, I think thats fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, a quick search on https://github.com/search?q=require%28%27es-set-tostringtag+language%3AJavaScript&type=code makes it seem like the force isn't being used that much, so we can leave it for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, would you like me to change anything else?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this LGTM for now. Let's improve the codemods more when we do some battletesting with them on real world projects :)

@thepassle thepassle merged commit 714ceab into es-tooling:main Jul 24, 2024
3 checks passed
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.

4 participants