@@ -36,35 +36,83 @@ export = createRule({
3636 "any" ,
3737 ] ;
3838
39- const hasKeyword = ( name : string ) => keywords . includes ( name ) ;
39+ const isKeyword = ( name : string ) => keywords . includes ( name ) ;
4040
41- const shouldReport = ( node : TSESTree . Identifier ) => {
42- if ( ! node || ! node . parent || ! hasKeyword ( node . name ) ) {
43- return false ;
44- }
45-
46- const parent = node . parent ;
47- if ( parent . type === AST_NODE_TYPES . FunctionDeclaration || parent . type === AST_NODE_TYPES . FunctionExpression ) {
48- return ! ( parent . id && hasKeyword ( parent . id . name ) ) ;
49- }
41+ const report = ( node : TSESTree . Identifier ) => {
42+ context . report ( { messageId : "noKeywordsError" , data : { name : node . name } , node } ) ;
43+ } ;
5044
51- if ( parent . type === AST_NODE_TYPES . TSMethodSignature && parent . key . type === AST_NODE_TYPES . Identifier ) {
52- return ! ( parent . key && hasKeyword ( parent . key . name ) ) ;
53- }
45+ const checkProperties = ( node : TSESTree . ObjectPattern ) : void => {
46+ node . properties . forEach ( property => {
47+ if (
48+ property &&
49+ property . type === AST_NODE_TYPES . Property &&
50+ property . key . type === AST_NODE_TYPES . Identifier &&
51+ isKeyword ( property . key . name )
52+ ) {
53+ report ( property . key ) ;
54+ }
55+ } ) ;
56+ } ;
5457
55- return [
56- AST_NODE_TYPES . ArrowFunctionExpression ,
57- AST_NODE_TYPES . VariableDeclarator ,
58- AST_NODE_TYPES . TSFunctionType ,
59- ] . includes ( node . parent . type ) ;
58+ const checkElements = ( node : TSESTree . ArrayPattern ) : void => {
59+ node . elements . forEach ( element => {
60+ if (
61+ element &&
62+ element . type === AST_NODE_TYPES . Identifier &&
63+ isKeyword ( element . name )
64+ ) {
65+ report ( element ) ;
66+ }
67+ } ) ;
6068 } ;
6169
62- const check = ( node : TSESTree . Identifier ) => {
63- if ( shouldReport ( node ) ) {
64- context . report ( { messageId : "noKeywordsError" , data : { name : node . name } , node } ) ;
70+ const checkParams = (
71+ node :
72+ | TSESTree . ArrowFunctionExpression
73+ | TSESTree . FunctionDeclaration
74+ | TSESTree . FunctionExpression
75+ | TSESTree . TSMethodSignature
76+ | TSESTree . TSFunctionType
77+ ) : void => {
78+ if ( ! node || ! node . params || ! node . params . length ) {
79+ return ;
6580 }
81+
82+ node . params . forEach ( param => {
83+ if (
84+ param &&
85+ param . type === AST_NODE_TYPES . Identifier &&
86+ isKeyword ( param . name )
87+ ) {
88+ report ( param ) ;
89+ }
90+ } ) ;
6691 } ;
6792
68- return { Identifier : check } ;
93+ return {
94+ VariableDeclarator ( node ) {
95+ if ( node . id . type === AST_NODE_TYPES . ObjectPattern ) {
96+ checkProperties ( node . id ) ;
97+ }
98+
99+ if ( node . id . type === AST_NODE_TYPES . ArrayPattern ) {
100+ checkElements ( node . id ) ;
101+ }
102+
103+ if (
104+ node . id . type === AST_NODE_TYPES . Identifier &&
105+ isKeyword ( node . id . name )
106+ ) {
107+ report ( node . id ) ;
108+ }
109+ } ,
110+
111+ ArrowFunctionExpression : checkParams ,
112+ FunctionDeclaration : checkParams ,
113+ FunctionExpression : checkParams ,
114+ TSMethodSignature : checkParams ,
115+ TSFunctionType : checkParams ,
116+ } ;
69117 } ,
70118} ) ;
0 commit comments