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

This won't work if the async result isn't serializable by Structured Clone Algorithm #4

Open
JounQin opened this issue Aug 31, 2022 · 5 comments

Comments

@JounQin
Copy link
Member

JounQin commented Aug 31, 2022

Structured Clone Algorithm

For example, plugins in resolveConfig.

@fisker
Copy link
Member

fisker commented Aug 31, 2022

That's limitation, maybe we should mention that.

@SimenB
Copy link
Contributor

SimenB commented Jul 25, 2023

@JounQin
Copy link
Member Author

JounQin commented Jul 25, 2023

I believe this can be done with custom worker without depending on @prettier/sync like we do in eslint-plugin-prettier

What means doing all heavy jobs in the worker, and only return the result from it to bypass the limitations.

@SimenB

@fisker
Copy link
Member

fisker commented Jul 25, 2023

Can this extract to a separate file and pass location as prettier plugin?

@remcohaszing
Copy link
Contributor

remcohaszing commented Jul 25, 2023

Prettier plugins can be specified using a path, but this puts a limitation on end users.

I suggest to add a function like this:

/**
 * Format content with the Prettier context for the file path.
 *
 * If the file is ignored by Prettier, the content is returned as-is.
 *
 * @param {string} filepath
 *   The file path to resolve the configuration for.
 * @param {string} content
 *   The content to format.
 * @param {import('prettier').Options} [options]
 *   Additional Prettier options to apply.
 * @returns {string}
 *   The formatted content, or the content as-is if it’s ignored by Prettier.
 */
async function formatWithContext(filepath, content, options) {
  const fileInfo = await getFileInfo(filepath)

  if (fileInfo.ignored) {
    return content
  }

  if (!fileInfo.inferredParser) {
    return content
  }

  const config = await resolveConfig(filepath, { editorconfig: true })

  return format(content, { ...config, ...options, filepath })
}

This means there is no need to send the Prettier options between workers. I imagine this would even be useful to add to Prettier itself, as I think it’s a common use case to format files like that.

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

No branches or pull requests

4 participants