66} from "@typescript-eslint/experimental-utils/dist/ts-eslint" ;
77
88type Banned = {
9- snippets : string [ ] ;
9+ snippets ?: string [ ] ;
10+ regexSnippets ?: string [ ] ;
1011 message : string ;
1112 // TODO includePaths?: string
1213 excludePaths ?: string [ ] ;
@@ -33,6 +34,24 @@ function isFileInPaths(filePath: string, paths: string[]): boolean {
3334 return paths . some ( ( path ) => filePath . indexOf ( path ) >= 0 ) ;
3435}
3536
37+ const isRegexMatch = ( regexRaw : string , text : string ) : boolean => {
38+ const regex = new RegExp ( regexRaw ) ;
39+ return regex . test ( text ) ;
40+ } ;
41+
42+ const filterSnippets = (
43+ bannedSnippetsRaw : string [ ] ,
44+ isRegex : boolean ,
45+ text : string
46+ ) : string [ ] => {
47+ const filterFun = ( s : string ) : boolean => {
48+ if ( isRegex ) return isRegexMatch ( s , text ) ;
49+ return text . startsWith ( s ) ;
50+ } ;
51+
52+ return bannedSnippetsRaw . filter ( filterFun ) ;
53+ } ;
54+
3655const analyzeNodeFor = (
3756 node : Node ,
3857 banned : Banned ,
@@ -51,7 +70,16 @@ const analyzeNodeFor = (
5170 node . getEnd ( ) ;
5271 node . getSourceFile ( ) . getLineAndCharacterOfPosition ( node . pos ) ;
5372
54- const bannedSnippets = banned . snippets . filter ( ( s ) => text . startsWith ( s ) ) ;
73+ if ( ! ! banned . snippets && ! ! banned . regexSnippets ) {
74+ throw new Error (
75+ "Invalid config: 'snippets' and 'regexSnippets' are mutually exclusive."
76+ ) ;
77+ }
78+
79+ const bannedSnippetsRaw = banned . snippets || banned . regexSnippets || [ ] ;
80+ const isRegex = ! ! banned . regexSnippets ;
81+
82+ const bannedSnippets = filterSnippets ( bannedSnippetsRaw , isRegex , text ) ;
5583
5684 if ( bannedSnippets . length > 0 ) {
5785 const pos = node . getSourceFile ( ) . getLineAndCharacterOfPosition ( node . pos ) ;
@@ -95,6 +123,12 @@ export default createRule<Options, MessageIds>({
95123 type : "string" ,
96124 } ,
97125 } ,
126+ regexSnippets : {
127+ type : "array" ,
128+ items : {
129+ type : "string" ,
130+ } ,
131+ } ,
98132 message : {
99133 type : "string" ,
100134 } ,
0 commit comments