Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Interpret max_line_length=off as printWidth: Number.POSITIVE_INFINITY #10

Merged
merged 3 commits into from
Mar 16, 2023

Conversation

josephfrazier
Copy link
Owner

@josephfrazier josephfrazier commented Mar 16, 2023

This fixes #7, see prettier/prettier#14514 for more context:

TL;DR: Should max_line_length=off in .editorconfig get interpreted as printWidth: Number.MAX_SAFE_INTEGER, or should we continue ignoring it, or something else?

Hey everyone, I'm the maintainer of editorconfig-to-prettier, which Prettier uses to convert EditorConfig configuration to Prettier configuration.

Over 5 years ago, bug #3410 was reported regarding the handling of max_line_length=off, and resulted in @duailibe fixing editorconfig-to-prettier to ignore max_line_length=off in .editorconfig files (the corresponding dependency version bump can be found here).

However, there's been some renewed interest from @celluj34 in having Prettier interpret max_line_length=off to mean e.g. printWidth: A_REALLY_BIG_NUMBER like Number.MAX_SAFE_INTEGER.

My question is, would this cause unintended consequences? For example, would Prettier attempt to generate very long lines for files where max_line_length=off? I see that https://prettier.io/docs/en/options.html#print-width says:

In other words, don’t try to use printWidth as if it was ESLint’s max-len – they’re not the same. max-len just says what the maximum allowed line length is, but not what the generally preferred length is – which is what printWidth specifies.

but I wonder if it would be ok in practice. I took a quick look at the playground with --print-width set to 9999, and the output seems reasonable-ish:

function HelloWorld({ greeting = "hello", greeted = '"World"', silent = false, onMouseOver }) {
  if (!greeting) {
    return null;
  }

  // TODO: Don't use random in render
  let num = Math.floor(Math.random() * 1e7)
    .toString()
    .replace(/\.\d+/gi, "");

  return (
    <div className="HelloWorld" title={`You are visitor number ${num}`} onMouseOver={onMouseOver}>
      <strong>{greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase()}</strong>
      {greeting.endsWith(",") ? " " : <span style={{ color: "grey" }}>", "</span>}
      <em>{greeted}</em>
      {silent ? "." : "!"}
    </div>
  );
}

Part of me thinks that if someone is explicitly putting max_line_length=off in their .editorconfig, then they should be ok with this formatting. On the other hand, I haven't really used Prettier for a while now, and I'm not very familiar with the variety of use cases people have for it, so I thought I'd ask here.

What do you think? Should max_line_length=off in .editorconfig get interpreted as printWidth: Number.MAX_SAFE_INTEGER, or should we continue ignoring it, or something else?

Thanks!

It make sense to support it. Let's parse it as Infinity.

https://github.com/prettier/prettier/blob/5446a8f32a2717762c4d7a1bd5fe2615d76ebec7/src/language-js/print/template-literal.js#L55

Oh, thanks for pointing that out, we're already using a "really big number" in a handful of places.

I'll probably wait on some other maintainers to chime in before making any changes in my package, but I appreciate the feedback 👍🏻

I think we can also improve the doc printer by skipping this check if printWidth is Infinity.

https://github.com/prettier/prettier/blob/5446a8f32a2717762c4d7a1bd5fe2615d76ebec7/src/document/doc-printer.js#L162

I'll probably wait on some other maintainers to chime in before making any changes in my package, but I appreciate the feedback 👍🏻

Oh lol, I see now that you're the primary contributor nowadays (this year so far)

I'd be happy to publish a new version of editorconfig-to-prettier that converts max_line_length=off to printWidth: Number.POSITIVE_INFINITY, but I'm not sure if I can help out with the adding tests/optimizations here in the Prettier repo (but I might try!). How does that sound?

@josephfrazier josephfrazier marked this pull request as ready for review March 16, 2023 13:05
@josephfrazier josephfrazier merged commit f5cd33f into main Mar 16, 2023
@josephfrazier josephfrazier deleted the max.line.length.off.printwidth.infinity branch March 16, 2023 13:05
josephfrazier added a commit to prettier/prettier that referenced this pull request Mar 18, 2023
This fixes #14514 by
upgrading `editorconfig-to-prettier` with the fix from josephfrazier/editorconfig-to-prettier#10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support "max_line_length=off"
1 participant