@addon24/eslint-config
Version:
ESLint configuration rules for WorldOfTextcraft projects - Centralized configuration for all project types
175 lines (153 loc) • 5.85 kB
JavaScript
export default {
rules: {
"no-type-assertion": {
meta: {
type: "problem",
docs: {
description: "Verbietet Type Assertions mit 'as'",
category: "Best Practices",
recommended: true,
},
schema: [],
messages: {
noTypeAssertion: "Type Assertions mit 'as' sind verboten. Verwende stattdessen Typ-Guards oder Validierung.",
},
},
create(context) {
// Prüfe, ob wir in einer Validierungsfunktion sind
const isInValidationFunction = (node) => {
let current = node;
while (current) {
if (current.type === 'MethodDefinition' && current.key.name && current.key.name.includes('Valid')) {
return true;
}
if (current.type === 'FunctionDeclaration' && current.id.name && current.id.name.includes('Valid')) {
return true;
}
current = current.parent;
}
return false;
};
// Prüfe, ob wir in einer DTO-Entity-Mapping-Methode sind
const isInDtoEntityMappingMethod = (node) => {
let current = node;
while (current) {
if (current.type === 'MethodDefinition' && current.key.name &&
(current.key.name === 'fromEntity' || current.key.name === 'toEntity' || current.key.name === 'fromEntityArray')) {
return true;
}
if (current.type === 'FunctionDeclaration' && current.id.name &&
(current.id.name === 'fromEntity' || current.id.name === 'toEntity' || current.id.name === 'fromEntityArray')) {
return true;
}
current = current.parent;
}
return false;
};
// Prüfe, ob es sich um eine Conditional Type Assertion handelt
const isConditionalTypeAssertion = (node) => {
if (node.typeAnnotation && node.typeAnnotation.type === 'TSConditionalType') {
return true;
}
return false;
};
// Prüfe, ob es sich um eine Generic Type Assertion handelt
const isGenericTypeAssertion = (node) => {
if (node.typeAnnotation && node.typeAnnotation.type === 'TSTypeReference') {
// Prüfe auf generische Parameter
if (node.typeAnnotation.typeParameters && node.typeAnnotation.typeParameters.params) {
return true;
}
}
return false;
};
// Prüfe, ob es sich um eine DTO-Entity-Mapping Type Assertion handelt
const isDtoEntityMappingAssertion = (node) => {
if (node.typeAnnotation) {
const typeAnnotation = node.typeAnnotation;
// Prüfe auf Conditional Types mit "extends"
if (typeAnnotation.type === 'TSConditionalType') {
return true;
}
// Prüfe auf Type References mit generischen Parametern
if (typeAnnotation.type === 'TSTypeReference') {
// Prüfe auf "T extends" Pattern
if (typeAnnotation.typeName && typeAnnotation.typeName.name === 'T') {
return true;
}
// Prüfe auf EntityDto oder EntityDto<...> Pattern
if (typeAnnotation.typeName &&
(typeAnnotation.typeName.name.includes('EntityDto') ||
typeAnnotation.typeName.name.includes('Entity'))) {
return true;
}
}
}
return false;
};
return {
TSAsExpression(node) {
const filename = context.getFilename();
// Erlaube Type Assertions in bestimmten Dateien
if (filename.includes('BaseController.ts') ||
filename.includes('openapi.ts') ||
filename.includes('/setup/') ||
filename.includes('/config/')) {
return;
}
// Erlaube Type Assertions in Validierungsfunktionen
if (isInValidationFunction(node)) {
return;
}
// Erlaube Type Assertions in DTO-Entity-Mapping-Methoden
if (isInDtoEntityMappingMethod(node)) {
return;
}
// Erlaube Conditional Type Assertions (für Generics)
if (isConditionalTypeAssertion(node)) {
return;
}
// Erlaube Generic Type Assertions (für Generics)
if (isGenericTypeAssertion(node)) {
return;
}
// Erlaube DTO-Entity-Mapping Type Assertions (für Generics)
if (isDtoEntityMappingAssertion(node)) {
return;
}
context.report({
node,
messageId: "noTypeAssertion",
});
},
TSTypeAssertion(node) {
// Erlaube Type Assertions in Validierungsfunktionen
if (isInValidationFunction(node)) {
return;
}
// Erlaube Type Assertions in DTO-Entity-Mapping-Methoden
if (isInDtoEntityMappingMethod(node)) {
return;
}
// Erlaube Conditional Type Assertions (für Generics)
if (isConditionalTypeAssertion(node)) {
return;
}
// Erlaube Generic Type Assertions (für Generics)
if (isGenericTypeAssertion(node)) {
return;
}
// Erlaube DTO-Entity-Mapping Type Assertions (für Generics)
if (isDtoEntityMappingAssertion(node)) {
return;
}
context.report({
node,
messageId: "noTypeAssertion",
});
},
};
},
},
},
};