UNPKG

@addon24/eslint-config

Version:

ESLint configuration rules for WorldOfTextcraft projects - Centralized configuration for all project types

160 lines (141 loc) 6.25 kB
export default { meta: { type: "problem", docs: { description: "Enforce no explicit visibility modifiers or readonly in DTO classes", category: "Best Practices", recommended: true, }, fixable: "code", schema: [], messages: { removePublicModifier: "Remove unnecessary 'public' modifier from DTO member '{{memberName}}'", removePrivateModifier: "Remove 'private' modifier from DTO member '{{memberName}}' - DTO members should be accessible", removeProtectedModifier: "Remove 'protected' modifier from DTO member '{{memberName}}' - DTO members should be accessible", removeReadonlyModifier: "Remove 'readonly' modifier from DTO member '{{memberName}}' - DTO members should be mutable", }, }, create (context) { return { PropertyDefinition (node) { // Check if this property is in a DTO class const parent = node.parent; if (parent && parent.type === "ClassBody") { const classNode = parent.parent; if (classNode && classNode.type === "ClassDeclaration") { const className = classNode.id?.name; if (className && className.endsWith("Dto")) { // This is a DTO class property // Check for visibility modifiers if (node.accessibility) { const memberName = node.key?.name || "unknown"; const modifier = node.accessibility; context.report({ node, messageId: `remove${modifier.charAt(0).toUpperCase() + modifier.slice(1)}Modifier`, data: { memberName, }, fix (fixer) { const sourceCode = context.getSourceCode(); const tokens = sourceCode.getTokens(node); // Find the visibility modifier token for (const token of tokens) { if (token.type === "Keyword" && (token.value === "public" || token.value === "private" || token.value === "protected")) { // Find the next token after the modifier const nextToken = sourceCode.getTokenAfter(token); if (nextToken) { // Remove from start of modifier to start of next token (includes whitespace) return fixer.removeRange([token.range[0], nextToken.range[0]]); } else { // Just remove the modifier token return fixer.remove(token); } } } return null; }, }); } // Check for readonly modifier if (node.readonly) { const memberName = node.key?.name || "unknown"; context.report({ node, messageId: "removeReadonlyModifier", data: { memberName, }, fix (fixer) { const sourceCode = context.getSourceCode(); const tokens = sourceCode.getTokens(node); // Find the readonly modifier token for (const token of tokens) { if (token.type === "Keyword" && token.value === "readonly") { // Find the next token after the modifier const nextToken = sourceCode.getTokenAfter(token); if (nextToken) { // Remove from start of modifier to start of next token (includes whitespace) return fixer.removeRange([token.range[0], nextToken.range[0]]); } else { // Just remove the modifier token return fixer.remove(token); } } } return null; }, }); } } } } }, MethodDefinition (node) { // Check if this method is in a DTO class const parent = node.parent; if (parent && parent.type === "ClassBody") { const classNode = parent.parent; if (classNode && classNode.type === "ClassDeclaration") { const className = classNode.id?.name; if (className && className.endsWith("Dto")) { // This is a DTO class method if (node.accessibility) { const memberName = node.key?.name || "unknown"; const modifier = node.accessibility; context.report({ node, messageId: `remove${modifier.charAt(0).toUpperCase() + modifier.slice(1)}Modifier`, data: { memberName, }, fix (fixer) { const sourceCode = context.getSourceCode(); const tokens = sourceCode.getTokens(node); // Find the visibility modifier token for (const token of tokens) { if (token.type === "Keyword" && (token.value === "public" || token.value === "private" || token.value === "protected")) { // Find the next token after the modifier const nextToken = sourceCode.getTokenAfter(token); if (nextToken) { // Remove from start of modifier to start of next token (includes whitespace) return fixer.removeRange([token.range[0], nextToken.range[0]]); } else { // Just remove the modifier token return fixer.remove(token); } } } return null; }, }); } } } } }, }; }, };