@@ -36,7 +36,6 @@ function toRuleLink(ruleId) {
36
36
return `[es-x/${ ruleId } ](./${ ruleId } .md)`
37
37
}
38
38
39
- // eslint-disable-next-line complexity
40
39
async function main ( ) {
41
40
const docsRoot = path . resolve ( __dirname , "../docs/rules/" )
42
41
const configRoot = path . resolve ( __dirname , "../lib/configs/flat" )
@@ -71,7 +70,6 @@ async function main() {
71
70
. replace ( / ^ \n * (?: - - - [ \s \S ] * ?- - - \n \n ? ) ? # .+ \n > .+ \n + (?: - .+ \n ) * / u, "" )
72
71
. replace ( / # # 🚀 V e r s i o n [ \s \S ] + / u, "" )
73
72
. replace ( / # # 📚 R e f e r e n c e s [ \s \S ] + / u, "" )
74
- . replace ( / # # 🔧 O p t i o n s [ \s \S ] + ?( \n # # | $ ) / u, "$1" )
75
73
. trim ( )
76
74
content = updateCodeBlocks ( content , { ruleId, fixable } )
77
75
content = adjustContents ( content )
@@ -122,76 +120,9 @@ async function main() {
122
120
)
123
121
}
124
122
125
- let optionsSection = ""
126
123
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 )
195
126
}
196
127
197
128
const newContent = `${ frontmatter . join ( "\n" ) . trim ( ) }
@@ -200,7 +131,7 @@ ${headerLines.join("\n").trim()}
200
131
201
132
${ content } ${
202
133
since
203
- ? `${ optionsSection }
134
+ ? `
204
135
205
136
## 🚀 Version
206
137
@@ -297,6 +228,131 @@ ${cookeHTMLAttrValue(code.value).trim()}
297
228
}
298
229
}
299
230
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
+ / ( ` ` ` j s o n (?: 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 # # 🔧 O p t i o n s [ \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
+
300
356
function adjustContents ( content ) {
301
357
// Adjust the necessary blank lines before and after the code block so that GitHub can recognize `.md`.
302
358
let result = content
0 commit comments