β¬ οΈ Back to Table of Contents
π no-empty-function¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 6 |
| π¦ Imports | 8 |
| π Variables & Constants | 2 |
| π Type Aliases | 2 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/no-empty-function.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'no-empty-function' |
meta.type |
'suggestion' |
meta.docs.description |
'Disallow empty functions' |
meta.docs.extendsBaseRule |
true |
meta.docs.recommended |
'stylistic' |
meta.hasSuggestions |
baseRule.meta.hasSuggestions |
meta.messages |
baseRule.meta.messages |
meta.schema |
[schema] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
JSONSchema4 |
@typescript-eslint/utils/json-schema |
AST_NODE_TYPES |
@typescript-eslint/utils |
InferMessageIdsTypeFromRule |
../util |
InferOptionsTypeFromRule |
../util |
createRule |
../util |
deepMerge |
../util |
getESLintCoreRule |
../util/getESLintCoreRule |
Variables & Constants¶
| Name | Type | Kind | Value | Exported |
|---|---|---|---|---|
defaultOptions |
Options |
const | [ { allow: [], }, ] |
β |
schema |
JSONSchema4 |
const | deepMerge( // eslint-disable-next-line @typescript-eslint/no-unsafe-argument ... |
β |
Functions¶
create(context: any, [{ allow = [] }]: any): any¶
Parameters:
contextany[{ allow = [] }]any
Returns: any
Calls:
baseRule.createallow.includesnode.params.someisBodyEmptyhasParameterPropertiesisAllowedEmptyConstructorisAllowedEmptyDecoratedFunctionsisAllowedEmptyOverrideMethodrules.FunctionExpression
Internal Comments:
/**
* Check if the method body is empty
* @param node the node to be validated
* @returns true if the body is empty
* @private
*/
/**
* Check if method has parameter properties
* @param node the node to be validated
* @returns true if the body has parameter properties
* @private
*/
/**
* @param node the node to be validated
* @returns true if the constructor is allowed to be empty
* @private
*/
// allow protected constructors (x3)
// allow private constructors
// allow constructors which have parameter properties (x2)
/**
* @param node the node to be validated
* @returns true if a function has decorators
* @private
*/
Code
create(context, [{ allow = [] }]) {
const rules = baseRule.create(context);
const isAllowedProtectedConstructors = allow.includes(
'protected-constructors',
);
const isAllowedPrivateConstructors = allow.includes('private-constructors');
const isAllowedDecoratedFunctions = allow.includes('decoratedFunctions');
const isAllowedOverrideMethods = allow.includes('overrideMethods');
/**
* Check if the method body is empty
* @param node the node to be validated
* @returns true if the body is empty
* @private
*/
function isBodyEmpty(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
return node.body.body.length === 0;
}
/**
* Check if method has parameter properties
* @param node the node to be validated
* @returns true if the body has parameter properties
* @private
*/
function hasParameterProperties(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
return node.params.some(
param => param.type === AST_NODE_TYPES.TSParameterProperty,
);
}
/**
* @param node the node to be validated
* @returns true if the constructor is allowed to be empty
* @private
*/
function isAllowedEmptyConstructor(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
const parent = node.parent;
if (
isBodyEmpty(node) &&
parent.type === AST_NODE_TYPES.MethodDefinition &&
parent.kind === 'constructor'
) {
const { accessibility } = parent;
return (
// allow protected constructors
(accessibility === 'protected' && isAllowedProtectedConstructors) ||
// allow private constructors
(accessibility === 'private' && isAllowedPrivateConstructors) ||
// allow constructors which have parameter properties
hasParameterProperties(node)
);
}
return false;
}
/**
* @param node the node to be validated
* @returns true if a function has decorators
* @private
*/
function isAllowedEmptyDecoratedFunctions(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
const decorators =
node.parent.type === AST_NODE_TYPES.MethodDefinition
? node.parent.decorators
: undefined;
return !!decorators && !!decorators.length;
}
return false;
}
function isAllowedEmptyOverrideMethod(
node: TSESTree.FunctionExpression,
): boolean {
return (
isAllowedOverrideMethods &&
isBodyEmpty(node) &&
node.parent.type === AST_NODE_TYPES.MethodDefinition &&
node.parent.override
);
}
return {
...rules,
FunctionExpression(node): void {
if (
isAllowedEmptyConstructor(node) ||
isAllowedEmptyDecoratedFunctions(node) ||
isAllowedEmptyOverrideMethod(node)
) {
return;
}
rules.FunctionExpression(node);
},
};
}
Internal helpers¶
Declared inside another function in this file.
isBodyEmpty(node: TSESTree.FunctionDeclaration | TSESTreeβ¦): boolean¶
Check if the method body is empty
Parameters:
nodeany: the node to be validated
Returns: undefined
true if the body is empty
Tags: @private
Raw JSDoc
Code
hasParameterProperties(node: TSESTree.FunctionDeclaration | TSESTreeβ¦): boolean¶
Check if method has parameter properties
Parameters:
nodeany: the node to be validated
Returns: undefined
true if the body has parameter properties
Tags: @private
Raw JSDoc
Calls:
node.params.some
Code
isAllowedEmptyConstructor(node: TSESTree.FunctionDeclaration | TSESTreeβ¦): boolean¶
Parameters:
nodeany: the node to be validated
Returns: undefined
true if the constructor is allowed to be empty
Tags: @private
Raw JSDoc
Calls:
isBodyEmptyhasParameterProperties
Internal Comments:
// allow protected constructors (x3)
// allow private constructors
// allow constructors which have parameter properties (x2)
Code
function isAllowedEmptyConstructor(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
const parent = node.parent;
if (
isBodyEmpty(node) &&
parent.type === AST_NODE_TYPES.MethodDefinition &&
parent.kind === 'constructor'
) {
const { accessibility } = parent;
return (
// allow protected constructors
(accessibility === 'protected' && isAllowedProtectedConstructors) ||
// allow private constructors
(accessibility === 'private' && isAllowedPrivateConstructors) ||
// allow constructors which have parameter properties
hasParameterProperties(node)
);
}
return false;
}
isAllowedEmptyDecoratedFunctions(node: TSESTree.FunctionDeclaration | TSESTreeβ¦): boolean¶
Parameters:
nodeany: the node to be validated
Returns: undefined
true if a function has decorators
Tags: @private
Raw JSDoc
Calls:
isBodyEmpty
Code
function isAllowedEmptyDecoratedFunctions(
node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
): boolean {
if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
const decorators =
node.parent.type === AST_NODE_TYPES.MethodDefinition
? node.parent.decorators
: undefined;
return !!decorators && !!decorators.length;
}
return false;
}
isAllowedEmptyOverrideMethod(node: TSESTree.FunctionExpression): boolean¶
Parameters:
nodeTSESTree.FunctionExpression
Returns: boolean
Calls:
isBodyEmpty
Code
Type Aliases¶
Options¶
MessageIds¶
Generated by Syntax Scribe