iTranslated by AI
Trying Out dprint: A Next-Generation Formatter
Introduction
I welcome corrections or additions via comments or GitHub pull requests.
What is dprint?
It is a formatter written in Rust, available on GitHub.
The supported languages are listed below.
- TypeScript
- JavaScript
- JSON
- Markdown
- TOML
- Dockerfile
- C# (wrapping Roslyn)
Usage
Install it according to the official website and create a configuration file with dprint init.
dprint init
You will be asked which plugins to use; select them with the space bar.
A dprint.json file containing the corresponding settings will be created.
TypeScript Example
Copy the settings from the playground below and paste them into dprint.json.
{
"typescript": {
"lineWidth": 80,
"indentWidth": 4,
"useTabs": true,
"semiColons": "prefer",
"quoteStyle": "alwaysDouble",
"quoteProps": "preserve",
"newLineKind": "lf",
"useBraces": "whenNotSingleLine",
"bracePosition": "sameLineUnlessHanging",
"singleBodyPosition": "maintain",
"nextControlFlowPosition": "sameLine",
"trailingCommas": "onlyMultiLine",
"operatorPosition": "nextLine",
"preferHanging": false,
"preferSingleLine": false,
"arrowFunction.useParentheses": "maintain",
"binaryExpression.linePerExpression": false,
"jsx.bracketPosition": "nextLine",
"jsx.forceNewLinesSurroundingContent": false,
"jsx.quoteStyle": "preferDouble",
"jsx.multiLineParens": "prefer",
"memberExpression.linePerExpression": false,
"typeLiteral.separatorKind": "semiColon",
"enumDeclaration.memberSpacing": "maintain",
"spaceAround": false,
"spaceSurroundingProperties": true,
"objectExpression.spaceSurroundingProperties": true,
"objectPattern.spaceSurroundingProperties": true,
"typeLiteral.spaceSurroundingProperties": true,
"binaryExpression.spaceSurroundingBitwiseAndArithmeticOperator": true,
"commentLine.forceSpaceAfterSlashes": true,
"constructor.spaceBeforeParentheses": false,
"constructorType.spaceAfterNewKeyword": false,
"constructSignature.spaceAfterNewKeyword": false,
"doWhileStatement.spaceAfterWhileKeyword": true,
"exportDeclaration.spaceSurroundingNamedExports": true,
"forInStatement.spaceAfterForKeyword": true,
"forOfStatement.spaceAfterForKeyword": true,
"forStatement.spaceAfterForKeyword": true,
"forStatement.spaceAfterSemiColons": true,
"functionDeclaration.spaceBeforeParentheses": false,
"functionExpression.spaceBeforeParentheses": false,
"functionExpression.spaceAfterFunctionKeyword": false,
"getAccessor.spaceBeforeParentheses": false,
"ifStatement.spaceAfterIfKeyword": true,
"importDeclaration.spaceSurroundingNamedImports": true,
"jsxSelfClosingElement.spaceBeforeSlash": true,
"jsxExpressionContainer.spaceSurroundingExpression": false,
"method.spaceBeforeParentheses": false,
"setAccessor.spaceBeforeParentheses": false,
"taggedTemplate.spaceBeforeLiteral": false,
"typeAnnotation.spaceBeforeColon": false,
"typeAssertion.spaceBeforeExpression": true,
"whileStatement.spaceAfterWhileKeyword": true,
"ignoreNodeCommentText": "dprint-ignore",
"ignoreFileCommentText": "dprint-ignore-file"
},
"json": {},
"markdown": {},
"toml": {},
"includes": [
"**/*.{ts,tsx,js,jsx,cjs,mjs,json,md,toml}"
],
"excludes": [
"**/node_modules",
"**/*-lock.json"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.83.0.wasm",
"https://plugins.dprint.dev/json-0.17.0.wasm",
"https://plugins.dprint.dev/markdown-0.15.2.wasm",
"https://plugins.dprint.dev/toml-0.5.4.wasm"
]
}
You can format with dprint fmt, but if you use the VS Code extension, it will automatically format on save.
{
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true,
// or specify per language, for example
"[javascript]": {
"editor.defaultFormatter": "dprint.dprint",
"editor.formatOnSave": true
}
}
Since it's written in Rust, it's incredibly fast.
Conclusion
I used to use Rome as my TypeScript formatter, but I switched to dprint because mysterious errors would occur when copying and pasting code.
I think the era of Prettier's absolute dominance might be over.
I'm looking forward to the future of dprint.
Discussion