FG
🛠️ Developer Tools

[Question] Prettier Philosophy and Rationale printWidth

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score84%
84%

Problem

I am opening an issue just to get an information about one decision, not related to a specific code formatting rule, but about a core concept in prettier : the `printWidth` option. First of all, I really love prettier and the philosophy ! I configured it in all projects in my company and I am using it every day ! But....why, in 2018, are we talking about `printWidth` when we talk about formatting ? Are you still printing your code ? Are you all using the same screen resolution/scale ? If it's about removing horizontal scroll, the best option is to use a “soft wrap” setting/plugin in your editor...it will break your line visually and it will adapt this automatically if you reduce the width of your editor. The philosophy of prettier is to define a common way to format your code and stop debating... Why this common way is impacted by a printWidth ? For example, sometimes, my JS array is formatted in one line and sometimes in multiple lines just because of the printWidth….this is not a common way to format arrays…It should be even more strict than that: put my array in one line all the times and I'll soft wrapping if it's too long or...split my array in multiple lines all the times * or...format my array in one line if it contains one element and split my array in multiple lines if it contain more than one element all the times Maybe I completly misunderstanding the philosophy of prettier but I feel that the `printWidth` is not matching it. What do you think ?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Clarify Prettier's printWidth Philosophy and Improve Formatting Consistency

Medium Risk

The `printWidth` option in Prettier is designed to provide a consistent line length for code formatting, which helps maintain readability across different editors and setups. However, it can lead to inconsistencies in how arrays and other structures are formatted, as the line length may cause some arrays to be displayed in one line and others to be split across multiple lines. This behavior can be confusing and may not align with the user's expectations for consistent formatting.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Review Prettier's Configuration

    Check your project's Prettier configuration file (e.g., .prettierrc) to understand the current `printWidth` setting and its implications on formatting.

    bash
    cat .prettierrc
  2. 2

    Adjust printWidth Setting

    Consider adjusting the `printWidth` value in your Prettier configuration to a higher value if you prefer longer lines, or lower if you want stricter line breaks. This can help achieve more consistent formatting for arrays and other structures.

    json
    {
      "printWidth": 100
    }
  3. 3

    Implement Consistent Array Formatting

    To enforce consistent formatting for arrays, you can create a custom ESLint rule or Prettier plugin that formats arrays according to your preferred style (e.g., always one line for single elements, multiple lines for multiple elements).

    javascript
    module.exports = {
      rules: {
        'array-format': ['error', 'consistent']
      }
    };
  4. 4

    Utilize Editor Soft Wrap

    Encourage team members to enable soft wrap in their code editors. This allows for better visual management of long lines without affecting the actual formatting of the code.

    javascript
    editor.settings.softWrap = true;
  5. 5

    Document Formatting Guidelines

    Create a document outlining the team's formatting guidelines, including how to handle arrays and the rationale behind the chosen `printWidth`. This will help align expectations and improve consistency across the codebase.

    markdown
    ## Formatting Guidelines
    - Use printWidth of 100
    - Always format arrays with more than one element on separate lines.

Validation

To confirm the fix worked, review the formatted code in your project after making the adjustments. Ensure that arrays are consistently formatted according to the new guidelines and that the `printWidth` setting aligns with your team's preferences. Run Prettier on a sample file and check for consistency.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

prettierformattingjavascripttype:questiontype:meta