Skip to content

Commit fa36f2b

Browse files
authored
Merge branch 'master' into no-arraybuffer-base64
2 parents 2f6c6e8 + f12807a commit fa36f2b

File tree

1 file changed

+128
-72
lines changed

1 file changed

+128
-72
lines changed

scripts/update-docs-rules.js

Lines changed: 128 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function toRuleLink(ruleId) {
3636
return `[es-x/${ruleId}](./${ruleId}.md)`
3737
}
3838

39-
// eslint-disable-next-line complexity
4039
async function main() {
4140
const docsRoot = path.resolve(__dirname, "../docs/rules/")
4241
const configRoot = path.resolve(__dirname, "../lib/configs/flat")
@@ -71,7 +70,6 @@ async function main() {
7170
.replace(/^\n*(?:---[\s\S]*?---\n\n?)?#.+\n>.+\n+(?:- .+\n)*/u, "")
7271
.replace(/## 🚀 Version[\s\S]+/u, "")
7372
.replace(/## 📚 References[\s\S]+/u, "")
74-
.replace(/## 🔧 Options[\s\S]+?(\n## |$)/u, "$1")
7573
.trim()
7674
content = updateCodeBlocks(content, { ruleId, fixable })
7775
content = adjustContents(content)
@@ -122,76 +120,9 @@ async function main() {
122120
)
123121
}
124122

125-
let optionsSection = ""
126123
const optionSchema = schema?.[0]
127-
if (
128-
optionSchema?.type === "object" &&
129-
!content.includes("## 🔧 Options")
130-
) {
131-
const hasAggressive = optionSchema.properties?.aggressive
132-
const hasAllowTestedProperty =
133-
optionSchema.properties?.allowTestedProperty
134-
const hasAllow = optionSchema.properties?.allow
135-
const defaultProperties = [
136-
...(hasAllow ? ['"allow": []'] : []),
137-
...(hasAggressive ? ['"aggressive": false'] : []),
138-
...(hasAllowTestedProperty
139-
? ['"allowTestedProperty": false']
140-
: []),
141-
]
142-
const propertyDescriptions = [
143-
...(hasAllow
144-
? [
145-
`
146-
### allow: string[]
147-
148-
An array of non-standard property names to allow.
149-
`,
150-
]
151-
: []),
152-
...(hasAggressive
153-
? [
154-
`
155-
### aggressive: boolean
156-
157-
Configure the aggressive mode for only this rule.
158-
This is prior to the \`settings['es-x'].aggressive\` setting.
159-
`,
160-
]
161-
: []),
162-
...(hasAllowTestedProperty
163-
? [
164-
`
165-
### allowTestedProperty: boolean
166-
167-
Configure the allowTestedProperty mode for only this rule.
168-
This is prior to the \`settings['es-x'].allowTestedProperty\` setting.
169-
`,
170-
]
171-
: []),
172-
].map((desc) => desc.trim())
173-
optionsSection = `
174-
175-
## 🔧 Options
176-
177-
This rule has an option.
178-
179-
\`\`\`jsonc
180-
{
181-
"rules": {
182-
"es-x/${ruleId}": [
183-
"error",
184-
{
185-
${defaultProperties.join(",\n ")}
186-
}
187-
]
188-
}
189-
}
190-
\`\`\`${
191-
propertyDescriptions.length
192-
? `\n\n${propertyDescriptions.join("\n\n")}`
193-
: ""
194-
}`
124+
if (optionSchema?.type === "object") {
125+
content = processOptions(content, ruleId, optionSchema)
195126
}
196127

197128
const newContent = `${frontmatter.join("\n").trim()}
@@ -200,7 +131,7 @@ ${headerLines.join("\n").trim()}
200131
201132
${content}${
202133
since
203-
? `${optionsSection}
134+
? `
204135
205136
## 🚀 Version
206137
@@ -297,6 +228,131 @@ ${cookeHTMLAttrValue(code.value).trim()}
297228
}
298229
}
299230

231+
function processOptions(content, ruleId, optionSchema) {
232+
let resultContent = content
233+
const hasAggressive = optionSchema.properties?.aggressive
234+
const hasAllowTestedProperty = optionSchema.properties?.allowTestedProperty
235+
const hasAllow = optionSchema.properties?.allow
236+
237+
if (!resultContent.includes("## 🔧 Options")) {
238+
resultContent += `
239+
240+
## 🔧 Options
241+
242+
This rule has an option.
243+
244+
\`\`\`jsonc
245+
{
246+
"rules": {
247+
"es-x/${ruleId}": [
248+
"error",
249+
{
250+
}
251+
]
252+
}
253+
}
254+
\`\`\`
255+
`
256+
}
257+
258+
if (!hasAggressive && !hasAllowTestedProperty && !hasAllow) {
259+
return resultContent
260+
}
261+
262+
writeOptionExample({
263+
...(hasAllow ? { allow: [] } : {}),
264+
...(hasAggressive ? { aggressive: false } : {}),
265+
...(hasAllowTestedProperty ? { allowTestedProperty: false } : {}),
266+
})
267+
268+
if (hasAllow) {
269+
writeOptionSection(
270+
"allow: string[]",
271+
"An array of non-standard property names to allow.",
272+
)
273+
}
274+
275+
if (hasAggressive) {
276+
writeOptionSection(
277+
"aggressive: boolean",
278+
`Configure the aggressive mode for only this rule.
279+
This is prior to the \`settings['es-x'].aggressive\` setting.`,
280+
)
281+
}
282+
283+
if (hasAllowTestedProperty) {
284+
writeOptionSection(
285+
"allowTestedProperty: boolean",
286+
`Configure the allowTestedProperty mode for only this rule.
287+
This is prior to the \`settings['es-x'].allowTestedProperty\` setting.`,
288+
)
289+
}
290+
return resultContent
291+
292+
function writeOptionExample(example) {
293+
writeOptionContent((optionsContent) =>
294+
optionsContent.replace(
295+
/(```json(?:c|5)?\n)([\s\S]+?)(\n```(?:\n|$))/u,
296+
(_, before, json, after) => {
297+
let options = {
298+
rules: {
299+
[`es-x/${ruleId}`]: ["error", {}],
300+
},
301+
}
302+
try {
303+
options = JSON.parse(json)
304+
} catch {
305+
// ignore
306+
}
307+
margeOptionExample(options, example)
308+
return `${before}${JSON.stringify(options, null, 2)}${after}`
309+
},
310+
),
311+
)
312+
313+
function margeOptionExample(options) {
314+
if (!options.rules) {
315+
options.rules = {}
316+
}
317+
let ruleValue = options.rules[`es-x/${ruleId}`]
318+
if (!ruleValue || !Array.isArray(ruleValue)) {
319+
ruleValue = options.rules[`es-x/${ruleId}`] = ["error", {}]
320+
}
321+
let ruleOptions = ruleValue[1]
322+
if (!ruleOptions) {
323+
ruleOptions = ruleValue[1] = {}
324+
}
325+
for (const [key, value] of Object.entries(example)) {
326+
ruleOptions[key] = value
327+
}
328+
}
329+
}
330+
331+
function writeOptionSection(sectionName, sectionContent) {
332+
writeOptionContent((optionsContent) => {
333+
if (!optionsContent.includes(`\n### ${sectionName}`)) {
334+
return `${
335+
optionsContent
336+
}\n\n### ${sectionName}\n\n${sectionContent}\n`
337+
}
338+
return optionsContent.replace(
339+
new RegExp(`\\n+### ${sectionName}[\\s\\S]+?\\n##`, "u"),
340+
`\n\n### ${sectionName}\n\n${sectionContent}\n\n##`,
341+
)
342+
})
343+
}
344+
345+
function writeOptionContent(replacer) {
346+
resultContent = resultContent.replace(
347+
/(\n## 🔧 Options[ \t]*\n)([\s\S]+?)(\n##\s|$)/u,
348+
(_, before, optionsContent, after) =>
349+
`${before}\n${replacer(optionsContent)
350+
.replaceAll(/\n{3,}/gu, "\n\n")
351+
.trim()}${after ? `\n${after}` : after}`,
352+
)
353+
}
354+
}
355+
300356
function adjustContents(content) {
301357
// Adjust the necessary blank lines before and after the code block so that GitHub can recognize `.md`.
302358
let result = content

0 commit comments

Comments
 (0)