-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathupdate-docs.ts
244 lines (212 loc) · 6.47 KB
/
update-docs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import path from "path"
import fs from "fs"
import { rules } from "../src/utils/rules"
import type { RuleModule } from "../src/types"
//eslint-disable-next-line require-jsdoc -- tools
function formatItems(items: string[]) {
if (items.length <= 2) {
return items.join(" and ")
}
return `all of ${items.slice(0, -1).join(", ")} and ${
items[items.length - 1]
}`
}
//eslint-disable-next-line require-jsdoc -- tools
function yamlValue(val: unknown) {
if (typeof val === "string") {
return `"${val.replace(/\\/gu, "\\\\").replace(/"/gu, '\\"')}"`
}
return val
}
const ROOT = path.resolve(__dirname, "../docs/rules")
//eslint-disable-next-line require-jsdoc -- tools
function pickSince(content: string): string | null {
const fileIntro = /^---\n((?:.*\n)+)---\n*/.exec(content)
if (fileIntro) {
const since = /since: "?(v\d+\.\d+\.\d+)"?/.exec(fileIntro[1])
if (since) {
return since[1]
}
}
// eslint-disable-next-line no-process-env -- ignore
if (process.env.IN_VERSION_SCRIPT) {
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports -- ignore
return `v${require("../package.json").version}`
}
return null
}
class DocFile {
private readonly rule: RuleModule
private readonly filePath: string
private content: string
private readonly since: string | null
public constructor(rule: RuleModule) {
this.rule = rule
this.filePath = path.join(ROOT, `${rule.meta.docs.ruleName}.md`)
this.content = fs.readFileSync(this.filePath, "utf8")
this.since = pickSince(this.content)
}
public static read(rule: RuleModule) {
return new DocFile(rule)
}
public updateHeader() {
const {
meta: {
fixable,
deprecated,
replacedBy,
docs: { ruleId, description, recommended },
},
} = this.rule
const title = `# ${ruleId}\n\n> ${description}`
const notes = []
if (deprecated) {
if (replacedBy) {
const replacedRules = replacedBy.map(
(name) => `[svelte/${name}](${name}.md) rule`,
)
notes.push(
`- :warning: This rule was **deprecated** and replaced by ${formatItems(
replacedRules,
)}.`,
)
} else {
notes.push("- :warning: This rule was **deprecated**.")
}
} else {
if (recommended) {
if (recommended === "base") {
notes.push(
'- :gear: This rule is included in `"plugin:svelte/base"` and `"plugin:svelte/recommended"`.',
)
} else {
notes.push(
'- :gear: This rule is included in `"plugin:svelte/recommended"`.',
)
}
}
}
if (fixable) {
notes.push(
"- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.",
)
}
if (!this.since) {
notes.unshift(
`- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>`,
)
}
// Add an empty line after notes.
if (notes.length >= 1) {
notes.push("", "")
}
const headerPattern = /(?:^|\n)#.+\n+[^\n]*\n+(?:- .+\n+)*\n*/u
const header = `\n${title}\n\n${notes.join("\n")}`
if (headerPattern.test(this.content)) {
this.content = this.content.replace(
headerPattern,
header.replace(/\$/g, "$$$$"),
)
} else {
this.content = `${header}${this.content.trim()}\n`
}
return this
}
public updateFooter() {
const { ruleName, extensionRule } = this.rule.meta.docs
const footerPattern =
/## (?:(?::mag:)? ?Implementation|:rocket: Version).+$/s
const footer = `${
this.since
? `## :rocket: Version
This rule was introduced in eslint-plugin-svelte ${this.since}
`
: ""
}## :mag: Implementation
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/${ruleName}.ts)
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/${ruleName}.ts)
${
extensionRule
? `
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/${extensionRule})</sup>
`
: ""
}`
if (footerPattern.test(this.content)) {
this.content = this.content.replace(
footerPattern,
footer.replace(/\$/g, "$$$$"),
)
} else {
this.content = `${this.content.trim()}\n\n${footer}`
}
return this
}
public updateCodeBlocks() {
const { meta } = this.rule
this.content = this.content.replace(
/<ESLintCodeBlock(.*?)>/gu,
(_t, str) => {
const ps = str
.split(/\s+/u)
.map((s: string) => s.trim())
.filter((s: string) => s && s !== "fix")
if (meta.fixable) {
ps.unshift("fix")
}
ps.unshift("<ESLintCodeBlock")
return `${ps.join(" ")}>`
},
)
return this
}
public adjustCodeBlocks() {
// Adjust the necessary blank lines before and after the code block so that GitHub can recognize `.md`.
this.content = this.content.replace(
/(<ESLintCodeBlock[\s\S]*?>)\n+```/gu,
"$1\n\n```",
)
this.content = this.content.replace(
/```\n+<\/ESLintCodeBlock>/gu,
"```\n\n</ESLintCodeBlock>",
)
return this
}
public updateFileIntro() {
const { ruleId, description } = this.rule.meta.docs
const fileIntro = {
pageClass: "rule-details",
sidebarDepth: 0,
title: ruleId,
description,
...(this.since ? { since: this.since } : {}),
}
const computed = `---\n${Object.keys(fileIntro)
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- tool
.map((key) => `${key}: ${yamlValue((fileIntro as any)[key])}`)
.join("\n")}\n---\n\n`
const fileIntroPattern = /^---\n(?:.*\n)+?---\n*/gu
if (fileIntroPattern.test(this.content)) {
this.content = this.content.replace(
fileIntroPattern,
computed.replace(/\$/g, "$$$$"),
)
} else {
this.content = `${computed}${this.content.trim()}\n`
}
return this
}
public write() {
this.content = this.content.replace(/\r?\n/gu, "\n")
fs.writeFileSync(this.filePath, this.content)
}
}
for (const rule of rules) {
DocFile.read(rule)
.updateHeader()
.updateFooter()
.updateCodeBlocks()
.updateFileIntro()
.adjustCodeBlocks()
.write()
}