FG
💻 Software🛠️ Developer Tools

Leave one-line functions alone

Fresh3 days ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Prettier 1.10.2 Playground link [code block] Input: [code block] Output: [code block] Expected behavior: The input was perfectly fine as it stood; it was a nice one-liner, exactly 80 characters long. Breaking it into multiple lines just makes it less clear. Is there a specific reason prettier behaves this way?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Configure Prettier to Preserve One-Line Functions

Medium Risk

Prettier's default configuration may not recognize certain one-liner functions as needing to remain on a single line, especially if they exceed the default print width setting. This can lead to unintended formatting changes that break the clarity of the code.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Prettier Configuration

    Modify the Prettier configuration file to set the print width to 80 characters, ensuring that one-liner functions remain intact.

    javascript
    module.exports = { printWidth: 80 };
  2. 2

    Use Prettier Ignore Comments

    Add Prettier ignore comments to specific one-liner functions to prevent them from being formatted. This is useful for functions that are already correctly formatted.

    javascript
    // prettier-ignore
    const myFunction = () => { return 'Hello World'; };
  3. 3

    Run Prettier with Updated Config

    Execute Prettier on your codebase with the updated configuration to ensure that all formatting adheres to the new rules.

    bash
    npx prettier --write .
  4. 4

    Test the Formatting

    Review the formatted code to confirm that one-liner functions remain unchanged and that the overall code clarity is maintained.

    bash
    git diff

Validation

Check that the one-liner functions in your codebase remain on a single line after running Prettier. You can verify this by reviewing the changes made by Prettier or by running tests that depend on the format of these functions.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

prettierformattingjavascriptlang:javascriptlocked-due-to-inactivityarea:member-chains