File tree 3 files changed +81
-0
lines changed
3 files changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* eslint documented-errors: "error" */
1
2
/* eslint alphabetize-errors: "error" */
2
3
3
4
'use strict' ;
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ require ( '../common' ) ;
4
+
5
+ const RuleTester = require ( '../../tools/eslint' ) . RuleTester ;
6
+ const rule = require ( '../../tools/eslint-rules/documented-errors' ) ;
7
+
8
+ const invalidCode = 'UNDOCUMENTED ERROR CODE' ;
9
+
10
+ new RuleTester ( ) . run ( 'documented-errors' , rule , {
11
+ valid : [
12
+ `
13
+ E('ERR_ASSERTION', 'foo');
14
+ `
15
+ ] ,
16
+ invalid : [
17
+ {
18
+ code : `
19
+ E('${ invalidCode } ', 'bar');
20
+ ` ,
21
+ errors : [
22
+ {
23
+ message : `"${ invalidCode } " is not documented in doc/api/errors.md` ,
24
+ line : 2
25
+ } ,
26
+ {
27
+ message :
28
+ `doc/api/errors.md does not have an anchor for "${ invalidCode } "` ,
29
+ line : 2
30
+ }
31
+ ]
32
+ }
33
+ ]
34
+ } ) ;
Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+
3
+ const fs = require ( 'fs' ) ;
4
+ const path = require ( 'path' ) ;
5
+
6
+ const doc = fs . readFileSync ( path . resolve ( __dirname , '../../doc/api/errors.md' ) ,
7
+ 'utf8' ) ;
8
+
9
+ function isInDoc ( code ) {
10
+ return doc . match ( `### ${ code } ` ) != null ;
11
+ }
12
+
13
+ function includesAnchor ( code ) {
14
+ return doc . match ( `<a id="${ code } "></a>` ) != null ;
15
+ }
16
+
17
+ function errorForNode ( node ) {
18
+ return node . expression . arguments [ 0 ] . value ;
19
+ }
20
+
21
+ function isDefiningError ( node ) {
22
+ return node . expression &&
23
+ node . expression . type === 'CallExpression' &&
24
+ node . expression . callee &&
25
+ node . expression . callee . name === 'E' ;
26
+ }
27
+
28
+ module . exports = {
29
+ create : function ( context ) {
30
+ return {
31
+ ExpressionStatement : function ( node ) {
32
+ if ( ! isDefiningError ( node ) ) return ;
33
+ const code = errorForNode ( node ) ;
34
+ if ( ! isInDoc ( code ) ) {
35
+ const message = `"${ code } " is not documented in doc/api/errors.md` ;
36
+ context . report ( { node, message } ) ;
37
+ }
38
+ if ( ! includesAnchor ( code ) ) {
39
+ const message =
40
+ `doc/api/errors.md does not have an anchor for "${ code } "` ;
41
+ context . report ( { node, message } ) ;
42
+ }
43
+ }
44
+ } ;
45
+ }
46
+ } ;
You can’t perform that action at this time.
0 commit comments