@@ -26,6 +26,10 @@ const ALLOW_OPTIONS = Object.freeze([
2626 "constructors" ,
2727 "asyncFunctions" ,
2828 "asyncMethods" ,
29+ "privateConstructors" ,
30+ "protectedConstructors" ,
31+ "decoratedFunctions" ,
32+ "overrideMethods" ,
2933] ) ;
3034
3135/**
@@ -83,13 +87,24 @@ function getKind(node) {
8387 return prefix + kind [ 0 ] . toUpperCase ( ) + kind . slice ( 1 ) ;
8488}
8589
90+ /**
91+ * Checks if a constructor function has parameter properties.
92+ * @param {ASTNode } node The function node to examine.
93+ * @returns {boolean } True if the constructor has parameter properties, false otherwise.
94+ */
95+ function isParameterPropertiesConstructor ( node ) {
96+ return node . params . some ( param => param . type === "TSParameterProperty" ) ;
97+ }
98+
8699//------------------------------------------------------------------------------
87100// Rule Definition
88101//------------------------------------------------------------------------------
89102
90103/** @type {import('../types').Rule.RuleModule } */
91104module . exports = {
92105 meta : {
106+ dialects : [ "javascript" , "typescript" ] ,
107+ language : "javascript" ,
93108 type : "suggestion" ,
94109
95110 defaultOptions : [ { allow : [ ] } ] ,
@@ -123,6 +138,43 @@ module.exports = {
123138 const [ { allow } ] = context . options ;
124139 const sourceCode = context . sourceCode ;
125140
141+ /**
142+ * Checks if the given function node is allowed to be empty.
143+ * @param {ASTNode } node The function node to check.
144+ * @returns {boolean } True if the function is allowed to be empty, false otherwise.
145+ */
146+ function isAllowedEmptyFunction ( node ) {
147+ const kind = getKind ( node ) ;
148+
149+ if ( allow . includes ( kind ) ) {
150+ return true ;
151+ }
152+
153+ if ( kind === "constructors" ) {
154+ if (
155+ ( node . parent . accessibility === "private" &&
156+ allow . includes ( "privateConstructors" ) ) ||
157+ ( node . parent . accessibility === "protected" &&
158+ allow . includes ( "protectedConstructors" ) ) ||
159+ isParameterPropertiesConstructor ( node )
160+ ) {
161+ return true ;
162+ }
163+ }
164+
165+ if ( / ( g | s ) e t t e r s | m e t h o d s $ / iu. test ( kind ) ) {
166+ if (
167+ ( node . parent . decorators ?. length &&
168+ allow . includes ( "decoratedFunctions" ) ) ||
169+ ( node . parent . override && allow . includes ( "overrideMethods" ) )
170+ ) {
171+ return true ;
172+ }
173+ }
174+
175+ return false ;
176+ }
177+
126178 /**
127179 * Reports a given function node if the node matches the following patterns.
128180 *
@@ -135,15 +187,14 @@ module.exports = {
135187 * @returns {void }
136188 */
137189 function reportIfEmpty ( node ) {
138- const kind = getKind ( node ) ;
139190 const name = astUtils . getFunctionNameWithKind ( node ) ;
140191 const innerComments = sourceCode . getTokens ( node . body , {
141192 includeComments : true ,
142193 filter : astUtils . isCommentToken ,
143194 } ) ;
144195
145196 if (
146- ! allow . includes ( kind ) &&
197+ ! isAllowedEmptyFunction ( node ) &&
147198 node . body . type === "BlockStatement" &&
148199 node . body . body . length === 0 &&
149200 innerComments . length === 0
0 commit comments