Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier reformats if / else into single line

I am trying Prettier out in a project using React and Typescript. However I am having a problem configuring multi-line if / else statements.

When I write:

if (x >=0) {
  // Do something
}
else {
  // Do something else
}

Prettier reformats it to:

if (x >=0) {
  // Do something
} else {
  // Do something else
}

I added this rule to my tslint file: "one-line": false, but Prettier is still formatting my statements.

Is this a core rule of Prettier that can't be changed through the tslint config or am I doing something wrong?

My tslint.json is:

{
  "extends": [
    "tslint:recommended",
    "tslint-react",
    "tslint-config-prettier"
  ],
  "rules": {
    "prettier": true,
    "interface-name": false,
    "no-console": [
      true,
      "log",
      "debug",
      "info",
      "time",
      "timeEnd",
      "trace"
    ],
    "one-line": false
  },
  "rulesDirectory": [
    "tslint-plugin-prettier"
  ]
}

and my .prettierrc file is:

{
  "trailingComma": "es5",
  "printWidth": 80,
  "semi": true,
  "tslintIntegration": true,
  "eslintIntegration": true,
  "jsxSingleQuote": true,
  "singleQuote": true
}
like image 733
Felipe Avatar asked Aug 08 '19 00:08

Felipe


People also ask

How can I prevent Prettier from taking multi line array to a single line?

Set the option in settings. Appending this line to the end of your settings. json file means Prettier will only wrap the line when it exceeds 1000 characters (essentially never). You can change this number to preference, for reference the default is 80 . Just make sure to save changes to the file and you are done!

How do I run Prettier from command line?

Use the prettier command to run Prettier from the command line. To run your locally installed version of Prettier, prefix the command with npx or yarn (if you use Yarn), i.e. npx prettier --help , or yarn prettier --help . To format a file in-place, use --write .


1 Answers

When combining Prettier with a linter in a project:

  • Prettier will handle all formatting rules
  • Code-quality rules will be handled by the linter (such as tslint)

Changing the config of tslint for formatting will not affect the output of prettier.
See Prettier vs Linters.

In fact, if you're not careful of how you configure tslint you can end up with conflicting rules. Which is why packages like tslint-config-prettier exist.

Prettier has very few configuration options as it is an oppinionated formatter as explained in their Option Philosophy.

like image 110
Stanislas Avatar answered Oct 12 '22 13:10

Stanislas