Skip to content

Commit 5f0ca35

Browse files
sandersnorta
andauthoredMay 11, 2020
Add danger for showing dts-critic suggestions (DefinitelyTyped#44430)
* First attempt * cwd churn * cwd churn 2 * cwd churn 3 * cwd churn 4 * cwd churn 5 * cwd churn 6 * dump ls * Add a TS dangerfile * Faff with the CI * try to read suggestions * add bogus aframe change * use correct dir * handle no suggestions * delete more of aframe to get dts-critic to notice * now try yauzl * move danger to travis for now * Revert "delete more of aframe to get dts-critic to notice" This reverts commit 65ea09f. * Revert "add bogus aframe change" This reverts commit a6546bd. * first try at formatting with markdown * maybe it's a line-oriented format * rebreak aframe * Better formatting Still not best * better wording, maybe better formatting * maybe I need a space to get autonumbering * even better formatting * special-case for >5 properties * Use a summary discolosure * Adds links to unpkg and the dts * Fix the unpkg link * Better unpkg/file linking 1. only show files if more than one was edited. 2. put top-level unpkg on top-level package heading * fix formatting and single-file skip * url needs trailing / * Revert "now try yauzl" This reverts commit 08ab238. * Revert "rebreak aframe" This reverts commit 6ceed76. Co-authored-by: Orta Therox <[email protected]>
1 parent 6684c8c commit 5f0ca35

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed
 

‎.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ notifications:
88
script:
99
- npm run test
1010
- if [[ $TRAVIS_EVENT_TYPE == "cron" ]]; then npm run update-codeowners || travis_terminate 1; fi
11+
- TOKEN='7469b4e94ce21b43e3ab7a'
12+
- TOKEN+='79960c12a1e067f2ec'
13+
- DANGER_GITHUB_API_TOKEN=$TOKEN yarn danger ci

‎azure-pipelines.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ jobs:
1212
displayName: 'npm install'
1313
inputs:
1414
verbose: false
15-
15+
1616
- script: |
1717
if [[ $BUILD_REASON == "Schedule" ]]; then git config --global user.email "types@microsoft.com" && git config --global user.name "TypeScript Bot" && npm run update-codeowners; fi
18-
git checkout -- . && npm run test
18+
git checkout -- .
19+
20+
npm run test
21+
1922
2023
displayName: 'npm run test'
21-
24+
2225
trigger:
2326
- master
24-

‎dangerfile.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import fs = require('fs')
2+
import os = require('os')
3+
import { markdown, danger } from "danger"
4+
const suggestionsDir = [os.homedir(), ".dts", "suggestions"].join('/')
5+
const lines: string[] = []
6+
const missingProperty = /module exports a property named '(.+?)', which is missing/
7+
8+
if (fs.existsSync(suggestionsDir)) {
9+
lines.push('Inspecting the JavaScript source for this package found some properties that are not in the .d.ts files.')
10+
lines.push('The check for missing properties isn\'t always right, so take this list as advice, not a requirement.')
11+
for (const suggestionFile of fs.readdirSync(suggestionsDir)) {
12+
const path = [suggestionsDir, suggestionFile].join('/')
13+
const suggestions = fs.readFileSync(path, "utf8").split('\n').map(x => JSON.parse(x)) as Array<{ fileName: string, ruleName: string, message: string }>
14+
const packageName = suggestionFile.slice(0, suggestionFile.indexOf('.txt'))
15+
const missingProperties: { [s: string]: string[] } = {}
16+
for (const s of suggestions) {
17+
const fileName = s.fileName.slice(s.fileName.indexOf("types/" + packageName + "/") + ("types/" + packageName + "/").length)
18+
const match = s.message.match(missingProperty)
19+
const identifier = match ? match[1] : s.message
20+
if (fileName in missingProperties) {
21+
missingProperties[fileName].push(identifier)
22+
}
23+
else {
24+
missingProperties[fileName] = [identifier]
25+
}
26+
}
27+
28+
const topUnpkgURL = `https://unpkg.com/browse/${packageName}@latest/`;
29+
lines.push("## " + packageName + ` ([<kbd>unpkg</kbd>](${topUnpkgURL}))`)
30+
for (const fileName in missingProperties) {
31+
if (Object.keys(missingProperties).length > 1) {
32+
const originalJS = fileName.replace(".d.ts", ".js")
33+
const unpkgURL = `https://unpkg.com/browse/${packageName}@latest/${originalJS}`
34+
const dtsName = packageName.replace("@", "").replace("/", "__")
35+
const dtsURL = `https://github.com/DefinitelyTyped/DefinitelyTyped/blob/${danger.github.pr.head.sha}/types/${dtsName}/${fileName}`
36+
37+
lines.push(`### ${fileName} ([<kbd>unpkg</kbd>](${unpkgURL}), [<kbd>d.ts</kbd>](${dtsURL}))`);
38+
}
39+
const properties = missingProperties[fileName]
40+
lines.push(`was missing the following properties:
41+
1. ` + properties.slice(0,5).join('\n1. '))
42+
if (properties.length > 5) {
43+
const extras = properties.slice(5)
44+
lines.push(`
45+
<details>
46+
<summary>as well as these ${extras.length} other properties...</summary>
47+
<p>${extras.join(", ")}</p>
48+
</details>
49+
`)
50+
}
51+
}
52+
}
53+
markdown(lines.join('\n'))
54+
}
55+

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"prettier": "prettier"
2424
},
2525
"devDependencies": {
26+
"danger": "^10.1.1",
2627
"dtslint": "latest",
2728
"prettier": "^2.0.2",
2829
"types-publisher": "github:Microsoft/types-publisher#production"

0 commit comments

Comments
 (0)
Please sign in to comment.