Leave one-line functions alone
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
Configure Prettier to Preserve One-Line Functions
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
Update Prettier Configuration
Modify the Prettier configuration file to set the print width to 80 characters, ensuring that one-liner functions remain intact.
javascriptmodule.exports = { printWidth: 80 }; - 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
Run Prettier with Updated Config
Execute Prettier on your codebase with the updated configuration to ensure that all formatting adheres to the new rules.
bashnpx prettier --write . - 4
Test the Formatting
Review the formatted code to confirm that one-liner functions remain unchanged and that the overall code clarity is maintained.
bashgit 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
Alex Chen
2450 rep