Skip to content

Commit 05ed023

Browse files
JoshuaKGoldbergpeterblazejewiczjakebailey
authored
Added dprint config and commit-on-master task (DefinitelyTyped#66235)
Co-authored-by: Piotr Błażejewicz (Peter Blazejewicz) <peterblazejewicz@users.noreply.github.com> Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
1 parent a4a5e0a commit 05ed023

22 files changed

+290
-76
lines changed

.dprint.jsonc

+17-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"./types/fhir",
1111
"./types/fibjs",
1212
"./types/get-intrinsic",
13+
"./types/google-publisher-tag",
14+
"./types/google.maps",
1315
"./types/locutus",
1416
"./types/lodash",
1517
"./types/material-ui",
@@ -26,11 +28,24 @@
2628
"./types/react-native",
2729
"./types/sawtooth-sdk",
2830
"./types/three",
31+
"./types/webextension-polyfill",
2932
"./types/winrt-uwp",
30-
"./types/vscode"
33+
"./types/vscode",
34+
35+
// These files will be formatted soon, but are failing in CI for now.
36+
"./types/i18n*",
37+
"./types/inquirer*",
38+
"./types/react-blessed",
39+
"./types/react-clipboardjs-copy",
40+
"./types/react-map-gl",
41+
"./types/react-native-awesome-card-io",
42+
"./types/react-syntax-highlighter",
43+
"./types/righto",
44+
"./types/react-native-awesome-card-io"
3145
],
46+
// NOTE: if extending this list, also update settings.template.json.
3247
"plugins": [
33-
"https://plugins.dprint.dev/typescript-0.85.1.wasm"
48+
"https://plugins.dprint.dev/typescript-0.88.1.wasm"
3449
],
3550
"indentWidth": 4,
3651
"lineWidth": 120

.git-blame-ignore-revs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# The migration to dprint involved several PRs that we can ignore.
2+
3+
# Individual package PRs (most recent first)
4+
a8a179ca7520a9983c401c8c0bd4c203aa6868fc
5+
688320db4a605630c37b70610e8b97f9fd85e9de
6+
25061846f16312c6fe4de60969c4cdeb06ab8a50
7+
dd8677457ec28cd0d26ef40bca302176ceb39a71
8+
f87782a6c6f89d8b9eb0eb73500f1b522bf9442b
9+
3db0a468004b33ab29437f9174d137ae39a04aa7
10+
18d1c4e0b7b846500a2d77091c978d40fc8f7447
11+
c4c6d86caf3a4d88c5ad72579b2093faa7cf7a86
12+
13+
# Letter-based PRs (git log --reverse --format="%H" --grep="Formatted types.*with dprint")
14+
09e5259775f741b9d8baa279284de06a3f3bfd28
15+
841bf968847b863b96924eb4081df7826a8a4ef0
16+
824cf7279e4002477305abb270be17f761fc8641
17+
fae616be8a4f6952ede7a8776d157f06797c862e
18+
7e3ed2122891e449b3b42764ad693ecf637ce849
19+
05481cc1f767ee8df73f9e450075f86fce53fbd3
20+
f58cd2247f618340c2535d60c6c5dcc286c2900d
21+
ebeb12c944c2e1ce7dd9bb8e4553b5396f34bb2f
22+
b8a7ab56850afe6e06c9f7267b9c479fcdd4efb8
23+
d0c02d9f65508d834b39827ba3b9e368607cbc1f
24+
521337ec05e52cc7c740153fe839799d50021cca
25+
817274f3280152ba2929a6067c93df8b34c4c9aa
26+
1e75d3d4cab1e87643d4171b056fd303ba899ce9
27+
ac2647c1c185030de5b52dde2da7651c0f42e131
28+
b969f890000ff95740fd7b879cdf3b73e1ea0fe8
29+
b36ff6a58d3d9ef64729fa8c73f91bd110594557
30+
0be834b9b536148d6aa365572bc668ed40b8ae84
31+
3866583ca9244b47878fa0ac8cfdfb23eb2ef0c5
32+
a80f5fb4006291e76ec2fb481461b4b579fc3055
33+
72bad25d0f11bdfb26efef984a34e7f9f4eb6cc7
34+
b84746ab5db34e652635f1346d701461a1a7de2d
35+
e4dc6495cdfd9c29449bc91c01bfb9b9ab150f38
36+
da527cde4392482e6e77810c633b40b600290028
37+
3e16768859508fd925a846cfaec7c8fce3b7702d
38+
964e498ae8deab72f825e48b9ebce7c97678f0f5
39+
07b78cb9f9760717cb0880262436a6753d0a97f6
40+
41+
# Non-letter based PRs
42+
d6df71f271f38735049115008ff6b5de169c9450
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Format And Commit
2+
3+
concurrency:
4+
group: ${{ github.ref }}
5+
6+
on:
7+
push:
8+
branches:
9+
- master
10+
workflow_dispatch: ~
11+
12+
jobs:
13+
dprint-fmt:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v3
17+
with:
18+
# Use the bot token so that CI triggers on pushes; we want to know if formatting broke something.
19+
token: ${{ secrets.GH_DT_MERGEBOT_TOKEN }}
20+
- uses: actions/setup-node@v3
21+
22+
- run: npm install
23+
24+
- name: Get date
25+
id: date
26+
run: echo "date=$(/bin/date -u "+%Y-%m-%d")" >> $GITHUB_OUTPUT
27+
28+
- uses: actions/cache@v3
29+
with:
30+
# dprint caches info about the files in the repo to skip formatting them.
31+
# However, since package.json and .dprint.jsonc don't change very often,
32+
# using them solely for the cache key would mean that the cache would be
33+
# eventually outdated enough to make the cache useless. To avoid this,
34+
# we also include the date in the cache key to ensure the cache is invalidated
35+
# more often.
36+
key: ${{ runner.os }}-dprint-${{ hashFiles('package.json', '.dprint.jsonc') }}-${{ steps.date.outputs.date }}
37+
path: ~/.cache/dprint
38+
restore-keys: |
39+
${{ runner.os }}-dprint-${{ hashFiles('package.json', '.dprint.jsonc') }}
40+
${{ runner.os }}-dprint-
41+
42+
- run: npx dprint fmt
43+
44+
- uses: stefanzweifel/git-auto-commit-action@v4.16.0
45+
with:
46+
commit_author: "TypeScript Bot <typescriptbot@microsoft.com>"
47+
commit_message: "🤖 dprint fmt"
48+
commit_user_email: "typescriptbot@microsoft.com"
49+
commit_user_name: "TypeScript Bot"

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ npm-debug.log
4040
# Visual Studio Code
4141
.settings/launch.json
4242
.vs
43-
.vscode
43+
.vscode/*
44+
!.vscode/extensions.json
45+
!.vscode/settings.template.json
4446
.history
4547

4648
# yarn

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
*.json
1+
*

.prettierrc.json

-8
This file was deleted.

.vscode/extensions.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"recommendations": [
3+
"dprint.dprint"
4+
]
5+
}

.vscode/settings.template.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"[typescript][typescriptreact][javascript][javascriptreact]": {
3+
"editor.defaultFormatter": "dprint.dprint",
4+
"editor.formatOnSave": true
5+
}
6+
}

README.fr.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ Alors ils sont faux, et nous ne l'avons pas encore remarqué. Vous pouvez nous a
452452

453453
#### Puis-je modifier/renforcer les paramètres de formatage des modules ?
454454

455-
Non. Nous avons déjà essayé de rendre le formatage du code de DT cohérent, mais nous sommes arrivés à une impasse en raison de la forte activité sur le repo. Nous incluons les paramètres de formatage via [`.editorconfig`](.editorconfig) et [`.prettierrc.json`](.prettierrc.json). Ceux-ci sont exclusivement destinés à l'outillage de votre éditeur, leurs paramètres n'entrent pas en conflit et nous ne prévoyons pas de les modifier. Nous ne prévoyons pas non plus d'imposer un style spécifique dans le repo. Nous voulons garder les barrières aux contributions basses.
455+
Non. Nous avons déjà essayé de rendre le formatage du code de DT cohérent, mais nous sommes arrivés à une impasse en raison de la forte activité sur le repo. Nous incluons les paramètres de formatage via [`.editorconfig`](.editorconfig). Ceux-ci sont exclusivement destinés à l'outillage de votre éditeur, leurs paramètres n'entrent pas en conflit et nous ne prévoyons pas de les modifier. Nous ne prévoyons pas non plus d'imposer un style spécifique dans le repo. Nous voulons garder les barrières aux contributions basses.
456456

457457
#### Puis-je demander une définition ?
458458

@@ -652,4 +652,4 @@ Quand `dts-gen` est utilisé pour échafauder un paquet scopé, la propriété `
652652

653653
Ce projet est placé sous la licence MIT.
654654

655-
Les droits d'auteur sur les fichiers de définition appartiennent à chaque contributeur mentionné au début de chaque fichier de définition.
655+
Les droits d'auteur sur les fichiers de définition appartiennent à chaque contributeur mentionné au début de chaque fichier de définition.

README.it.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ Questo significa che non vanno bene e noi non ce ne siamo ancora accorti. Puoi c
419419

420420
#### Posso cambiare/imporre una specifica formattazione per alcuni moduli?
421421

422-
No. Abbiamo già provato ad avere una formattazione consistente dei package ma siamo arrivati ad un vicolo cieco data la mole di attività sulla repo. Noi alleghiamo delle impostazioni di formattazione attraverso i file [`.editorconfig`](.editorconfig) e [`.prettierrc.json`](.prettierrc.json). Questi sono esclusivamente per il tuo editor, le loro impostazioni non vanno in conflitto e non abbiamo in piano di modificarle. Né tantomeno abbiamo in piano di imporre uno stile specifico nella repository. Vogliamo tenere le barriere per le contribuzioni basse.
422+
No. Abbiamo già provato ad avere una formattazione consistente dei package ma siamo arrivati ad un vicolo cieco data la mole di attività sulla repo. Noi alleghiamo delle impostazioni di formattazione attraverso i file [`.editorconfig`](.editorconfig). Questi sono esclusivamente per il tuo editor, le loro impostazioni non vanno in conflitto e non abbiamo in piano di modificarle. Né tantomeno abbiamo in piano di imporre uno stile specifico nella repository. Vogliamo tenere le barriere per le contribuzioni basse.
423423

424424
#### Posso chiedere che venga implementata una definizione per un modulo che non le ha ancora?
425425

README.ja.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ npm パッケージは数分で更新されます。もし1時間以上かかっ
441441

442442
#### モジュールのフォーマット設定を変更/強制できますか?
443443

444-
いいえ、できません。以前、DTのコードフォーマットを一貫させる試みを行いましたが、リポジトリの高い活動度のために行き詰まりました。フォーマット設定は [.editorconfig](.editorconfig) と [.prettierrc.json](.prettierrc.json) を介して提供されています。これらはあくまでエディタ内のツールのためのものであり、設定が競合することはなく、変更する予定もありません。また、リポジトリ内で特定のスタイルを強制する予定もありません。貢献のハードルを低く保ちたいと考えています。
444+
いいえ、できません。以前、DTのコードフォーマットを一貫させる試みを行いましたが、リポジトリの高い活動度のために行き詰まりました。フォーマット設定は [.editorconfig](.editorconfig) を介して提供されています。これらはあくまでエディタ内のツールのためのものであり、設定が競合することはなく、変更する予定もありません。また、リポジトリ内で特定のスタイルを強制する予定もありません。貢献のハードルを低く保ちたいと考えています。
445445

446446
#### 型定義をリクエストしたいです。
447447

README.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,8 @@ If a file is neither tested nor referenced in `index.d.ts`, add it to a file nam
365365
#### Common mistakes
366366

367367
* First, follow advice from the [handbook](https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html).
368-
* Formatting: Use 4 spaces. Prettier is set up on this repo, so you can run `npm run prettier -- --write 'path/to/package/**/*.ts'`. [When using assertions](https://github.com/SamVerschueren/tsd#assertions), add `// prettier-ignore` exclusion to mark line(s) of code as excluded from formatting:
369-
```tsx
370-
// prettier-ignore
371-
// @ts-expect-error
372-
const incompleteThemeColorModes: Theme = { colors: { modes: { papaya: {
373-
```
368+
* Formatting: [dprint](https://dprint.dev) is set up on this repo, so you can run `npx dprint fmt -- 'path/to/package/**/*.ts'`.
369+
* Consider using the VS Code `.vscode/settings.template.json` (or equivalent for other editors) to format on save with the [VS Code dprint extension](https://marketplace.visualstudio.com/items?itemName=dprint.dprint)
374370
* `function sum(nums: number[]): number`: Use `ReadonlyArray` if a function does not write to its parameters.
375371
* `interface Foo { new(): Foo; }`:
376372
This defines a type of objects that are new-able. You probably want `declare class Foo { constructor(); }`.
@@ -450,9 +446,16 @@ If the module you're referencing is an ambient module (uses `declare module`, or
450446

451447
Then they are wrong, and we've not noticed yet. You can help by submitting a pull request to fix them.
452448

453-
#### Can I change/enforce formatting settings for modules?
449+
#### Are Files Formatted Automatically?
450+
451+
Yes, using [dprint](https://dprint.dev).
452+
We recommend using a [dprint extension for your editor](https://dprint.dev/install/#editor-extensions).
453+
454+
Pull requests do not require correct formatting to be merged.
455+
Any unformatted code will be automatically reformatted after being merged.
454456

455-
No. We've explored trying to make DT's code-formatting consistent before but reached an impasse due to the high activity on the repo. We include formatting settings via a [`.editorconfig`](.editorconfig) and [`.prettierrc.json`](.prettierrc.json). These are exclusively for tooling in your editor, their settings don't conflict and we don't plan on changing them. Nor do we plan on enforcing a specific style in the repo. We want to keep the barriers to contributions low.
457+
> 💡 If you're a VS Code user, we suggest copying the `.vscode/settings.template.json` file to `.vscode/settings.json`.
458+
> That template sets the [dprint VS Code extension](https://marketplace.visualstudio.com/items?itemName=dprint.dprint) as the default formatter in the repo.
456459

457460
#### Can I request a definition?
458461

README.zh-Hans.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ npm 软件包应该会在几分钟内更新。如果已经超过了一小时,
438438

439439
#### 我可以为模块更改/强制使用某个格式化设置吗?
440440

441-
不可以。我们之前曾试图统一 DT 代码样式,但由于本仓库过于活跃,最终目标无法达成。为了让你在编辑器中格式化文件,我们在 [`.editorconfig`](.editorconfig) 和 [`.prettierrc.json`](.prettierrc.json) 文件中加入了格式化设置,两者的内容互不冲突。我们即没有改变这些设置的计划,也没有在此仓库强制推行单一代码风格的计划,因为我们希望尽量降低贡献门槛。
441+
不可以。我们之前曾试图统一 DT 代码样式,但由于本仓库过于活跃,最终目标无法达成。为了让你在编辑器中格式化文件,我们在 [`.editorconfig`](.editorconfig) 文件中加入了格式化设置,两者的内容互不冲突。我们即没有改变这些设置的计划,也没有在此仓库强制推行单一代码风格的计划,因为我们希望尽量降低贡献门槛。
442442

443443
#### 我可以申请新增类型定义吗?
444444

dangerfile.ts

+49-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs = require('fs')
22
import os = require('os')
33
import path = require('path')
4+
import cp = require('child_process')
5+
import stripAnsi = require('strip-ansi')
46
import { markdown, danger, fail } from "danger"
57
const suggestionsDir = [os.homedir(), ".dts", "suggestions"].join('/')
68
const lines: string[] = []
@@ -67,10 +69,52 @@ newDTSFiles.forEach(dts => {
6769
}
6870
})
6971

70-
for (const filename of danger.git.modified_files.concat(danger.git.created_files)) {
71-
danger.git.diffForFile(filename).then(d => {
72-
if (d.added.indexOf("\t") > -1) { // This is a dumb check for tabs, in lieu of running `prettier` on the diff'd parts
73-
fail("The root .editorconfig style specifies spaces for whitespace. Please use spaces in new or changed types.", filename, d.after.split("\n").findIndex(e => e.indexOf("\t") > -1) + 1)
72+
function chunked<T>(arr: T[], size: number): T[][] {
73+
const result: T[][] = [];
74+
for (let i = 0; i < arr.length; i += size) {
75+
result.push(arr.slice(i, i + size))
76+
}
77+
return result;
78+
}
79+
80+
const unformatted = [];
81+
const allFiles = [...danger.git.created_files, ...danger.git.modified_files];
82+
// We batch this in chunks to avoid hitting max arg length issues.
83+
for (const files of chunked(allFiles, 50)) {
84+
const result = cp.spawnSync(
85+
process.execPath,
86+
["node_modules/dprint/bin.js", "check", ...files],
87+
{ encoding: "utf8", maxBuffer: 100 * 1024 * 1024 }
88+
);
89+
if (result.status !== 0) {
90+
for (const line of result.stdout.split(/\r?\n/)) {
91+
const match = stripAnsi(line).match(/^from (.*):$/);
92+
if (match) {
93+
unformatted.push(path.relative(process.cwd(), match[1]));
94+
}
7495
}
75-
})
96+
}
97+
}
98+
99+
if (unformatted.length > 0) {
100+
const message = [
101+
"## Formatting",
102+
"",
103+
];
104+
105+
message.push(`The following files are not formatted:
106+
1. ` + unformatted.slice(0,5).join('\n1. '))
107+
if (unformatted.length > 5) {
108+
const extras = unformatted.slice(5)
109+
message.push(`
110+
<details>
111+
<summary>as well as these ${extras.length} other files...</summary>
112+
<p>${extras.join(", ")}</p>
113+
</details>
114+
`)
115+
}
116+
117+
message.push("Consider running `npx dprint fmt` on these files to make review easier.")
118+
119+
markdown(message.join("\n"))
76120
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
"@definitelytyped/definitions-parser": "latest",
2828
"@definitelytyped/dtslint": "latest",
2929
"@definitelytyped/dtslint-runner": "latest",
30+
"@definitelytyped/eslint-plugin": "latest",
3031
"@definitelytyped/header-parser": "latest",
3132
"@definitelytyped/utils": "latest",
32-
"@definitelytyped/eslint-plugin": "latest",
3333
"@octokit/core": "^3.5.1",
3434
"@octokit/rest": "^16.0.0",
3535
"comment-json": "^4.2.3",
@@ -40,14 +40,15 @@
4040
"d3-time": "^3.0.0",
4141
"d3-time-format": "^4.0.0",
4242
"danger": "^11.2.3",
43+
"dprint": "^0.41.0",
4344
"eslint-plugin-jsdoc": "^44.2.7",
4445
"jsdom": "^17.0.0",
45-
"prettier": "^2.1.1",
4646
"remark-cli": "^11.0.0",
4747
"remark-gfm": "^3.0.0",
4848
"remark-validate-links": "^12.0.0",
4949
"shelljs": "^0.8.5",
5050
"source-map-support": "^0.5.21",
51+
"strip-ansi": "^6.0.1",
5152
"typescript": "next",
5253
"w3c-xmlserializer": "^2.0.0",
5354
"yargs": "^17.1.1"

scripts/tslint-rule-begone.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { parse } from 'comment-json';
22
import fs from 'fs';
3-
import { format } from 'prettier';
43
import sh from 'shelljs';
54
import * as path from 'path';
65

@@ -15,19 +14,14 @@ const parseAndReadFileContents = filePath => {
1514
}
1615
};
1716

18-
const prettierConfig = parseAndReadFileContents('.prettierrc.json');
19-
2017
/**
2118
* @param {string} filePath
2219
* @param {unknown} contents
2320
*/
2421
const writeFileFormatted = (filePath, contents) => {
2522
fs.writeFileSync(
2623
filePath,
27-
format(JSON.stringify(contents, null, 4), {
28-
...prettierConfig,
29-
filepath: filePath,
30-
}),
24+
JSON.stringify(contents, null, 4),
3125
);
3226
};
3327

scripts/tslint-rule-to-eslint.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { parse } from 'comment-json';
22
import fs from 'fs';
3-
import { format } from 'prettier';
43
import sh from 'shelljs';
54
import * as path from 'path';
65

@@ -19,19 +18,14 @@ const parseAndReadFileContents = filePath => {
1918
}
2019
};
2120

22-
const prettierConfig = parseAndReadFileContents('.prettierrc.json');
23-
2421
/**
2522
* @param {string} filePath
2623
* @param {unknown} contents
2724
*/
2825
const writeFileFormatted = (filePath, contents) => {
2926
fs.writeFileSync(
3027
filePath,
31-
format(JSON.stringify(contents, null, 4), {
32-
...prettierConfig,
33-
filepath: filePath,
34-
}),
28+
JSON.stringify(contents, null, 4),
3529
);
3630
};
3731

0 commit comments

Comments
 (0)