β¬ οΈ Back to Table of Contents
π no-misused-new¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 3 |
| π¦ Imports | 3 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/no-misused-new.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'no-misused-new' |
meta.type |
'problem' |
meta.docs.description |
'Enforce valid definition of new and constructor' |
meta.docs.recommended |
'recommended' |
meta.messages.errorMessageClass |
'Class cannot have method named new.' |
meta.messages.errorMessageInterface |
'Interfaces cannot be constructed, only classes.' |
meta.schema |
[] |
defaultOptions |
[] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
createRule |
../util |
Functions¶
create(context: any): { "ClassBody > MethodDefinition[key.name='new']"(node: TSES⦶
Parameters:
contextany
Returns: { "ClassBody > MethodDefinition[key.name='new']"(node: TSESTree.MethodDefinition): void; 'TSInterfaceBody > TSConstructSignatureDeclaration'(node: TSESTree.TSConstructSignatureDeclaration): void; "TSMethodSignature[key.name='constructor']"(node: TSESTree.TSMethodSignature): void; }
Calls:
getTypeReferenceNameisMatchingParentTypecontext.report
Internal Comments:
/**
* @param node type to be inspected.
* @returns name of simple type or null
*/
/**
* @param parent parent node.
* @param returnType type to be compared
*/
// constructor (x4)
Code
create(context) {
/**
* @param node type to be inspected.
* @returns name of simple type or null
*/
function getTypeReferenceName(
node:
| TSESTree.EntityName
| TSESTree.TSTypeAnnotation
| TSESTree.TypeNode
| undefined,
): string | null {
if (node) {
switch (node.type) {
case AST_NODE_TYPES.TSTypeAnnotation:
return getTypeReferenceName(node.typeAnnotation);
case AST_NODE_TYPES.TSTypeReference:
return getTypeReferenceName(node.typeName);
case AST_NODE_TYPES.Identifier:
return node.name;
default:
break;
}
}
return null;
}
/**
* @param parent parent node.
* @param returnType type to be compared
*/
function isMatchingParentType(
parent:
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression
| TSESTree.Identifier
| TSESTree.TSInterfaceDeclaration
| undefined,
returnType: TSESTree.TSTypeAnnotation | undefined,
): boolean {
if (
parent &&
(parent.type === AST_NODE_TYPES.ClassDeclaration ||
parent.type === AST_NODE_TYPES.ClassExpression ||
parent.type === AST_NODE_TYPES.TSInterfaceDeclaration) &&
parent.id
) {
return getTypeReferenceName(returnType) === parent.id.name;
}
return false;
}
return {
"ClassBody > MethodDefinition[key.name='new']"(
node: TSESTree.MethodDefinition,
): void {
if (
node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression &&
isMatchingParentType(node.parent.parent, node.value.returnType)
) {
context.report({
node,
messageId: 'errorMessageClass',
});
}
},
'TSInterfaceBody > TSConstructSignatureDeclaration'(
node: TSESTree.TSConstructSignatureDeclaration,
): void {
if (
isMatchingParentType(
node.parent.parent as TSESTree.TSInterfaceDeclaration,
node.returnType,
)
) {
// constructor
context.report({
node,
messageId: 'errorMessageInterface',
});
}
},
"TSMethodSignature[key.name='constructor']"(
node: TSESTree.TSMethodSignature,
): void {
context.report({
node,
messageId: 'errorMessageInterface',
});
},
};
}
Internal helpers¶
Declared inside another function in this file.
getTypeReferenceName(node: | TSESTree.EntityName | TSESTree.TSTypeβ¦): string | null¶
Parameters:
nodeany: type to be inspected.
Returns: undefined
name of simple type or null
Calls:
getTypeReferenceName
Code
function getTypeReferenceName(
node:
| TSESTree.EntityName
| TSESTree.TSTypeAnnotation
| TSESTree.TypeNode
| undefined,
): string | null {
if (node) {
switch (node.type) {
case AST_NODE_TYPES.TSTypeAnnotation:
return getTypeReferenceName(node.typeAnnotation);
case AST_NODE_TYPES.TSTypeReference:
return getTypeReferenceName(node.typeName);
case AST_NODE_TYPES.Identifier:
return node.name;
default:
break;
}
}
return null;
}
isMatchingParentType(parent: | TSESTree.ClassDeclaration | TSESTree.β¦, returnType: TSESTree.TSTypeAnnotation | undefined): boolean¶
Parameters:
parentany: parent node.returnTypeany: type to be compared
Calls:
getTypeReferenceName
Code
function isMatchingParentType(
parent:
| TSESTree.ClassDeclaration
| TSESTree.ClassExpression
| TSESTree.Identifier
| TSESTree.TSInterfaceDeclaration
| undefined,
returnType: TSESTree.TSTypeAnnotation | undefined,
): boolean {
if (
parent &&
(parent.type === AST_NODE_TYPES.ClassDeclaration ||
parent.type === AST_NODE_TYPES.ClassExpression ||
parent.type === AST_NODE_TYPES.TSInterfaceDeclaration) &&
parent.id
) {
return getTypeReferenceName(returnType) === parent.id.name;
}
return false;
}
Generated by Syntax Scribe