@@ -3,14 +3,18 @@ import { deepmerge } from "deepmerge-ts";
33import type { JSONSchema4 } from "json-schema" ;
44import type { ReadonlyDeep } from "type-fest" ;
55
6- import type { IgnorePatternOption } from "~/common/ignore-options" ;
6+ import type {
7+ IgnorePatternOption ,
8+ IgnorePrefixSelectorOption ,
9+ } from "~/common/ignore-options" ;
710import {
811 shouldIgnorePattern ,
912 ignorePatternOptionSchema ,
13+ ignorePrefixSelectorOptionSchema ,
1014} from "~/common/ignore-options" ;
1115import type { ESFunction } from "~/src/util/node-types" ;
1216import type { RuleResult } from "~/util/rule" ;
13- import { createRule } from "~/util/rule" ;
17+ import { createRuleUsingFunction } from "~/util/rule" ;
1418import { isIIFE , isPropertyAccess , isPropertyName } from "~/util/tree" ;
1519import { isRestElement } from "~/util/typeguard" ;
1620
@@ -29,6 +33,7 @@ type ParameterCountOptions = "atLeastOne" | "exactlyOne";
2933 */
3034type Options = readonly [
3135 IgnorePatternOption &
36+ IgnorePrefixSelectorOption &
3237 Readonly < {
3338 allowRestParameter : boolean ;
3439 allowArgumentsKeyword : boolean ;
@@ -48,39 +53,43 @@ type Options = readonly [
4853const schema : JSONSchema4 = [
4954 {
5055 type : "object" ,
51- properties : deepmerge ( ignorePatternOptionSchema , {
52- allowRestParameter : {
53- type : "boolean" ,
54- } ,
55- allowArgumentsKeyword : {
56- type : "boolean" ,
57- } ,
58- enforceParameterCount : {
59- oneOf : [
60- {
61- type : "boolean" ,
62- enum : [ false ] ,
63- } ,
64- {
65- type : "string" ,
66- enum : [ "atLeastOne" , "exactlyOne" ] ,
67- } ,
68- {
69- type : "object" ,
70- properties : {
71- count : {
72- type : "string" ,
73- enum : [ "atLeastOne" , "exactlyOne" ] ,
74- } ,
75- ignoreIIFE : {
76- type : "boolean" ,
56+ properties : deepmerge (
57+ ignorePatternOptionSchema ,
58+ ignorePrefixSelectorOptionSchema ,
59+ {
60+ allowRestParameter : {
61+ type : "boolean" ,
62+ } ,
63+ allowArgumentsKeyword : {
64+ type : "boolean" ,
65+ } ,
66+ enforceParameterCount : {
67+ oneOf : [
68+ {
69+ type : "boolean" ,
70+ enum : [ false ] ,
71+ } ,
72+ {
73+ type : "string" ,
74+ enum : [ "atLeastOne" , "exactlyOne" ] ,
75+ } ,
76+ {
77+ type : "object" ,
78+ properties : {
79+ count : {
80+ type : "string" ,
81+ enum : [ "atLeastOne" , "exactlyOne" ] ,
82+ } ,
83+ ignoreIIFE : {
84+ type : "boolean" ,
85+ } ,
7786 } ,
87+ additionalProperties : false ,
7888 } ,
79- additionalProperties : false ,
80- } ,
81- ] ,
82- } ,
83- } ) ,
89+ ] ,
90+ } ,
91+ }
92+ ) ,
8493 additionalProperties : false ,
8594 } ,
8695] ;
@@ -255,14 +264,36 @@ function checkIdentifier(
255264}
256265
257266// Create the rule.
258- export const rule = createRule < keyof typeof errorMessages , Options > (
259- name ,
260- meta ,
261- defaultOptions ,
262- {
263- FunctionDeclaration : checkFunction ,
264- FunctionExpression : checkFunction ,
265- ArrowFunctionExpression : checkFunction ,
267+ export const rule = createRuleUsingFunction <
268+ keyof typeof errorMessages ,
269+ Options
270+ > ( name , meta , defaultOptions , ( context , options ) => {
271+ const [ optionsObject ] = options ;
272+ const { ignorePrefixSelector } = optionsObject ;
273+
274+ const baseFunctionSelectors = [
275+ "ArrowFunctionExpression" ,
276+ "FunctionDeclaration" ,
277+ "FunctionExpression" ,
278+ ] ;
279+
280+ const ignoreSelectors : ReadonlyArray < string > | undefined =
281+ ignorePrefixSelector === undefined
282+ ? undefined
283+ : Array . isArray ( ignorePrefixSelector )
284+ ? ignorePrefixSelector
285+ : [ ignorePrefixSelector ] ;
286+
287+ const fullFunctionSelectors = baseFunctionSelectors . flatMap ( ( baseSelector ) =>
288+ ignoreSelectors === undefined
289+ ? [ baseSelector ]
290+ : `:not(:matches(${ ignoreSelectors . join ( "," ) } )) > ${ baseSelector } `
291+ ) ;
292+
293+ return {
294+ ...Object . fromEntries (
295+ fullFunctionSelectors . map ( ( selector ) => [ selector , checkFunction ] )
296+ ) ,
266297 Identifier : checkIdentifier ,
267- }
268- ) ;
298+ } ;
299+ } ) ;
0 commit comments