Skip to content

Commit 1a5925d

Browse files
committed
Auto merge of rust-lang#12294 - listochkin:prettier, r=Veykril
Switch to Prettier for TypeScript Code formatting ## Summary of changes: 1. Added [`.editorconfig` file](https://editorconfig.org) to dictate general hygienic stuff like character encoding, no trailing whitespace, new line symbols etc. for all files (e.g. Markdown). Install an editor plugin to get this rudimentary formatting assistance automatically. Prettier can read this file and, for example, use it for indentation style and size. 2. Added a minimal prettier config file. All options are default except line width, which per [Veykril](https://github.com/Veykril) suggestion is set to 100 instead of 80, because [that's what `Rustfmt` uses](https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=#max_width). 3. Change `package.json` to use Prettier instead of `tsfmt` for code formatting. 4. Performed initial formatting in a separate commit, per [bjorn3](https://github.com/bjorn3) suggestion added its hash to a `.git-blame-ignore-revs` file. For it to work you need to add a configuration to your git installation: ```Shell git config --global blame.ignoreRevsFile .git-blame-ignore-revs ``` 5. Finally, removed `typescript-formatter` from the list of dependencies. ---- What follows below is summary of the discussion we had on Zulip about the formatter switch: ## Background For the context, there are three reasons why we went with `tsfmt` originally: * stick to vscode default/built-in * don't add extra deps to package.json.lock * follow upstream (language server node I think still uses `tsfmt`) And the meta reason here was that we didn't have anyone familiar with frontend, so went for the simplest option, at the expense of features and convenience. Meanwhile, [**Prettier**](https://prettier.io) became a formatter project that JS community consolidated on a few years ago. It's similar to `go fmt` / `cargo fmt` in spirit: minimal to no configuration to promote general uniformity in the ecosystem. There are some options, that were needed early on to make sure the project gained momentum, but by no means it's a customizable formatter that is easy to adjust to reduce the number of changes for adoption. ## Overview of changes performed by Prettier Some of the changes are acceptable. Prettier dictates a unified string quoting style, and as a result half of our imports at the top are changed. No one would mind that. Some one-line changes are due to string quotes, too, and although these a re numerous, the surrounding lines aren't changed, and git blame / GitLens will still show relevant context. Some are toss ups. `trailingComma` option - set it to `none`, and get a bunch of meaningless changes in half of the code. set it to `all` and get a bunch of changes in the other half of the code. Same with using parentheses around single parameters in arrow functions: `x => x + 1` vs `(x) => x + 1`. Perrier forces one style or the other, but we use both in our code. Like I said, the changes above are Ok - they take a single line, don't disrupt GitLens / git blame much. **The big one is line width**. Prettier wants you to choose one and stick to it. The default is 80 and it forces some reformatting to squish deeply nested code or long function type declarations. If I set it to 100-120, then Prettier finds other parts of code where a multi-line expression can be smashed into a single long line. The problem is that in both cases some of the lines that get changed are interesting, they contain somewhat non-trivial logic, and if I were to work on them in future I would love to see the commit annotations that tell me something relevant. Alas, we use some of that. ## Project impact Though Prettier is a mainstream JS project it has no dependencies. We add another package so that it and ESLint work together nicely, and that's it.
2 parents 52b69fa + 00a9727 commit 1a5925d

30 files changed

+1263
-974
lines changed

.editorconfig

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# https://EditorConfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
end_of_line = lf
8+
insert_final_newline = true
9+
indent_style = space
10+
11+
[*.{rs,toml}]
12+
indent_size = 4
13+
14+
[*.ts]
15+
indent_size = 4
16+
[*.js]
17+
indent_size = 4
18+
[*.json]
19+
indent_size = 4

.git-blame-ignore-revs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# for this file to take effect make sure you use git ^2.23 and
2+
# add ignoreFile to your git configuration:
3+
# ```
4+
# git config --global blame.ignoreRevsFile .git-blame-ignore-revs
5+
# ```
6+
7+
# prettier format
8+
f247090558c9ba3c551566eae5882b7ca865225f

editors/code/.eslintrc.js

+25-28
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
module.exports = {
2-
"env": {
3-
"es6": true,
4-
"node": true
2+
env: {
3+
es6: true,
4+
node: true,
55
},
6-
"parser": "@typescript-eslint/parser",
7-
"parserOptions": {
8-
"project": "tsconfig.eslint.json",
9-
"tsconfigRootDir": __dirname,
10-
"sourceType": "module"
6+
extends: ["prettier"],
7+
parser: "@typescript-eslint/parser",
8+
parserOptions: {
9+
project: "tsconfig.eslint.json",
10+
tsconfigRootDir: __dirname,
11+
sourceType: "module",
1112
},
12-
"plugins": [
13-
"@typescript-eslint"
14-
],
15-
"rules": {
16-
"camelcase": ["error"],
17-
"eqeqeq": ["error", "always", { "null": "ignore" }],
13+
plugins: ["@typescript-eslint"],
14+
rules: {
15+
camelcase: ["error"],
16+
eqeqeq: ["error", "always", { null: "ignore" }],
17+
curly: ["error", "multi-line"],
1818
"no-console": ["error", { allow: ["warn", "error"] }],
1919
"prefer-const": "error",
2020
"@typescript-eslint/member-delimiter-style": [
2121
"error",
2222
{
23-
"multiline": {
24-
"delimiter": "semi",
25-
"requireLast": true
23+
multiline: {
24+
delimiter: "semi",
25+
requireLast: true,
2626
},
27-
"singleline": {
28-
"delimiter": "semi",
29-
"requireLast": false
30-
}
31-
}
32-
],
33-
"@typescript-eslint/semi": [
34-
"error",
35-
"always"
27+
singleline: {
28+
delimiter: "semi",
29+
requireLast: false,
30+
},
31+
},
3632
],
33+
"@typescript-eslint/semi": ["error", "always"],
3734
"@typescript-eslint/no-unnecessary-type-assertion": "error",
38-
"@typescript-eslint/no-floating-promises": "error"
39-
}
35+
"@typescript-eslint/no-floating-promises": "error",
36+
},
4037
};

editors/code/.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.vscode-test
3+
out

editors/code/.prettierrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
// use 100 because it's Rustfmt's default
3+
// https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=#max_width
4+
printWidth: 100,
5+
};

editors/code/language-configuration.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"comments": {
33
"lineComment": "//",
4-
"blockComment": [ "/*", "*/" ]
4+
"blockComment": ["/*", "*/"]
55
},
66
"brackets": [
77
["{", "}"],
88
["[", "]"],
99
["(", ")"]
1010
],
1111
"colorizedBracketPairs": [
12-
["{", "}"],
13-
["[", "]"],
14-
["(", ")"]
15-
],
12+
["{", "}"],
13+
["[", "]"],
14+
["(", ")"]
15+
],
1616
"autoClosingPairs": [
1717
{ "open": "{", "close": "}" },
1818
{ "open": "[", "close": "]" },

0 commit comments

Comments
 (0)