1
+ import * as fs from 'fs' ;
1
2
import type * as unist from 'unist' ;
2
3
import * as mdast from 'mdast' ;
4
+ import * as path from 'path' ;
3
5
import { format } from 'prettier' ;
4
6
import type { Plugin } from 'unified' ;
5
7
import { compile } from 'json-schema-to-typescript' ;
@@ -19,6 +21,13 @@ const COMPLICATED_RULE_OPTIONS = new Set([
19
21
'naming-convention' ,
20
22
] ) ;
21
23
24
+ const sourceUrlPrefix =
25
+ 'https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/' ;
26
+
27
+ const eslintPluginDirectory = path . resolve (
28
+ path . join ( __dirname , '../../eslint-plugin' ) ,
29
+ ) ;
30
+
22
31
export const generatedRuleDocs : Plugin = ( ) => {
23
32
return async ( root , file ) => {
24
33
if ( file . stem == null ) {
@@ -288,6 +297,65 @@ export const generatedRuleDocs: Plugin = () => {
288
297
type : 'paragraph' ,
289
298
} as mdast . Paragraph ) ;
290
299
}
300
+
301
+ // 7. Also add a link to view the rule's source and test code
302
+ parent . children . push (
303
+ {
304
+ children : [
305
+ {
306
+ type : 'text' ,
307
+ value : 'Resources' ,
308
+ } ,
309
+ ] ,
310
+ depth : 2 ,
311
+ type : 'heading' ,
312
+ } as mdast . Heading ,
313
+ {
314
+ children : [
315
+ {
316
+ children : [
317
+ {
318
+ children : [
319
+ {
320
+ type : 'link' ,
321
+ url : `${ sourceUrlPrefix } src/rules/${ file . stem } .ts` ,
322
+ children : [
323
+ {
324
+ type : 'text' ,
325
+ value : 'Rule source' ,
326
+ } ,
327
+ ] ,
328
+ } ,
329
+ ] ,
330
+ type : 'paragraph' ,
331
+ } ,
332
+ ] ,
333
+ type : 'listItem' ,
334
+ } ,
335
+ {
336
+ children : [
337
+ {
338
+ children : [
339
+ {
340
+ type : 'link' ,
341
+ url : getUrlForRuleTest ( file . stem ) ,
342
+ children : [
343
+ {
344
+ type : 'text' ,
345
+ value : 'Test source' ,
346
+ } ,
347
+ ] ,
348
+ } ,
349
+ ] ,
350
+ type : 'paragraph' ,
351
+ } ,
352
+ ] ,
353
+ type : 'listItem' ,
354
+ } ,
355
+ ] ,
356
+ type : 'list' ,
357
+ } as mdast . List ,
358
+ ) ;
291
359
} ;
292
360
} ;
293
361
@@ -305,3 +373,16 @@ function createH2TextFilter(
305
373
node . children [ 0 ] . type === 'text' &&
306
374
node . children [ 0 ] . value === text ;
307
375
}
376
+
377
+ function getUrlForRuleTest ( ruleName : string ) : string {
378
+ for ( const localPath of [
379
+ `tests/rules/${ ruleName } .test.ts` ,
380
+ `tests/rules/${ ruleName } /` ,
381
+ ] ) {
382
+ if ( fs . existsSync ( `${ eslintPluginDirectory } /${ localPath } ` ) ) {
383
+ return `${ sourceUrlPrefix } ${ localPath } ` ;
384
+ }
385
+ }
386
+
387
+ throw new Error ( `Could not find test file for ${ ruleName } .` ) ;
388
+ }
0 commit comments