4
4
*/
5
5
'use strict' ;
6
6
7
+ const micromatch = require ( 'micromatch' ) ;
8
+ const { getFilename, getBasename } = require ( '../utils/filename' ) ;
9
+ const NAMING_CONVENTION = require ( '../constants/naming-convention' ) ;
10
+
11
+ /**
12
+ * @type {string } invalid naming pattern
13
+ * @param {string } rules naming pattern configurated by user
14
+ */
15
+ const checkNamingConfig = ( rules ) => {
16
+ const buildInPatterns = Object . keys ( NAMING_CONVENTION ) ;
17
+ const isValidPattern = ( pattern ) =>
18
+ ( typeof pattern === 'string' && buildInPatterns . includes ( pattern ) ) ||
19
+ pattern instanceof RegExp ;
20
+
21
+ return Object . values ( rules ) . find ( ( pattern ) => ! isValidPattern ( pattern ) ) ;
22
+ } ;
23
+
7
24
/**
8
25
* @type {import('eslint').Rule.RuleModule }
9
26
*/
@@ -17,10 +34,57 @@ module.exports = {
17
34
url : null , // TODO: URL to the documentation page for this rule
18
35
} ,
19
36
fixable : null , // Or `code` or `whitespace`
20
- schema : [ ] , // Add a schema if the rule has options
37
+ schema : [
38
+ {
39
+ additionalProperties : {
40
+ type : 'string' ,
41
+ } ,
42
+ } ,
43
+ ] ,
21
44
} ,
22
45
23
- create ( ) {
24
- return { } ;
46
+ create ( context ) {
47
+ return {
48
+ Program : ( node ) => {
49
+ const rules = context . options [ 0 ] ;
50
+ const invalidPatter = checkNamingConfig ( rules ) ;
51
+
52
+ if ( invalidPatter ) {
53
+ context . report ( {
54
+ node,
55
+ message :
56
+ 'There is an unsupported naming pattern "{{invalidPatter}}", please check it.' ,
57
+ data : {
58
+ invalidPatter,
59
+ } ,
60
+ } ) ;
61
+ return ;
62
+ }
63
+
64
+ const filenameWithPath = context . getFilename ( ) ;
65
+ const filename = getFilename ( filenameWithPath ) ;
66
+
67
+ for ( const [ fexPattern , pattern ] of Object . entries ( rules ) ) {
68
+ if ( ! micromatch . isMatch ( filename , fexPattern ) ) {
69
+ continue ;
70
+ } else if (
71
+ ( NAMING_CONVENTION [ pattern ] || pattern ) . test ( getBasename ( filename ) )
72
+ ) {
73
+ return ;
74
+ } else {
75
+ context . report ( {
76
+ node,
77
+ message :
78
+ 'The filename "{{filename}}" does not match the "{{pattern}}" style' ,
79
+ data : {
80
+ filename,
81
+ pattern,
82
+ } ,
83
+ } ) ;
84
+ return ;
85
+ }
86
+ }
87
+ } ,
88
+ } ;
25
89
} ,
26
90
} ;
0 commit comments