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
}
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!
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 .
When combining Prettier with a linter in a project:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With