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

fix: prettier run on newText #353

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dylanarmstrong
Copy link

@dylanarmstrong dylanarmstrong commented Jun 17, 2023

Determine quote style from first found import statement in machine file, then use that for quoting the typegen import.

Closes #351

@changeset-bot
Copy link

changeset-bot bot commented Jun 17, 2023

🦋 Changeset detected

Latest commit: 3db3cfe

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

This PR includes changesets to release 2 packages
Name Type
@xstate/cli Patch
@xstate/tools-shared Patch

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

@@ -110,7 +116,11 @@ const writeToFiles = async (uriArray: string[], { cwd }: { cwd: string }) => {

const edits = getTsTypesEdits(types);
if (edits.length > 0) {
const newFile = processFileEdits(fileContents, edits);
const newFile = await prettify(
Copy link
Member

Choose a reason for hiding this comment

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

Ideally, I'd like to remove the dependency on Prettier altogether. I'd much more prefer to detect the preferred quotes style and just use that within the edit.

Copy link
Author

Choose a reason for hiding this comment

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

Simplest (and hackiest) way I can think of is a check in newFile for number of double quotes vs. number of single quotes and go with whichever is more common. That would remove requirement for prettier, but would be quite ugly.

If prettier was kept, we could check for quotes by using something like:

const quote = (await prettier.resolveConfig(cwd)).singleQuote ? "'" : '"';

This all has a downside of ignoring non-quote prettier features that we may violate like line length and trailing commas.

Copy link
Member

Choose a reason for hiding this comment

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

It's rather a safe bet to assume that there is some import statement in the file and we could piggy-back our quotes detection based on that with smth like /import\s(.|\n)*(['"])/.

This all has a downside of ignoring non-quote prettier features that we may violate like line length and trailing commas.

I understand the concern but we are not quite in the code formatting business. There are multiple formatters available out there and it would be great to use some pretty generic code that could play well with all of them. I think it's fine that sometimes an additional formatting edit might be required after we insert something into the file, the goal is to minimize the chances of that happening.

Prettier is, of course, vastly popular so we could try to special-case it but I think that this should be done by treating is an optional peer dependency~ whereas right now we always fall back to the local version installed with the CLI if we can't load Prettier relative to cwd.

Copy link
Author

Choose a reason for hiding this comment

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

I've updated the PR to match this on the CLI, but I'm not able to update the vscode extension to do the same.

I kept the prettify method because It's a useful abstraction that can be removed in the future if prettier is entirely removed.

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.

Prettier not run on newText
2 participants