|
| 1 | +import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema"; |
| 2 | +import type { RuleContext } from "@typescript-eslint/utils/ts-eslint"; |
| 3 | +import { deepmerge } from "deepmerge-ts"; |
| 4 | + |
| 5 | +import { |
| 6 | + type IgnoreCodePatternOption, |
| 7 | + type IgnoreIdentifierPatternOption, |
| 8 | + ignoreCodePatternOptionSchema, |
| 9 | + ignoreIdentifierPatternOptionSchema, |
| 10 | + shouldIgnorePattern, |
| 11 | +} from "#/options"; |
| 12 | +import { ruleNameScope } from "#/utils/misc"; |
| 13 | +import type { ESClass } from "#/utils/node-types"; |
| 14 | +import { type NamedCreateRuleCustomMeta, type Rule, type RuleResult, createRule } from "#/utils/rule"; |
| 15 | + |
| 16 | +/** |
| 17 | + * The name of this rule. |
| 18 | + */ |
| 19 | +export const name = "no-class-inheritance"; |
| 20 | + |
| 21 | +/** |
| 22 | + * The full name of this rule. |
| 23 | + */ |
| 24 | +export const fullName: `${typeof ruleNameScope}/${typeof name}` = `${ruleNameScope}/${name}`; |
| 25 | + |
| 26 | +/** |
| 27 | + * The options this rule can take. |
| 28 | + */ |
| 29 | +type Options = [IgnoreIdentifierPatternOption & IgnoreCodePatternOption]; |
| 30 | + |
| 31 | +/** |
| 32 | + * The schema for the rule options. |
| 33 | + */ |
| 34 | +const schema: JSONSchema4[] = [ |
| 35 | + { |
| 36 | + type: "object", |
| 37 | + properties: deepmerge(ignoreIdentifierPatternOptionSchema, ignoreCodePatternOptionSchema), |
| 38 | + additionalProperties: false, |
| 39 | + }, |
| 40 | +]; |
| 41 | + |
| 42 | +/** |
| 43 | + * The default options for the rule. |
| 44 | + */ |
| 45 | +const defaultOptions: Options = [{}]; |
| 46 | + |
| 47 | +/** |
| 48 | + * The possible error messages. |
| 49 | + */ |
| 50 | +const errorMessages = { |
| 51 | + abstract: "Unexpected abstract class.", |
| 52 | + extends: "Unexpected inheritance, use composition instead.", |
| 53 | +} as const; |
| 54 | + |
| 55 | +/** |
| 56 | + * The meta data for this rule. |
| 57 | + */ |
| 58 | +const meta: NamedCreateRuleCustomMeta<keyof typeof errorMessages> = { |
| 59 | + type: "suggestion", |
| 60 | + docs: { |
| 61 | + category: "No Other Paradigms", |
| 62 | + description: "Disallow inheritance in classes.", |
| 63 | + recommended: "recommended", |
| 64 | + recommendedSeverity: "error", |
| 65 | + requiresTypeChecking: false, |
| 66 | + }, |
| 67 | + messages: errorMessages, |
| 68 | + schema, |
| 69 | +}; |
| 70 | + |
| 71 | +/** |
| 72 | + * Check if the given class node violates this rule. |
| 73 | + */ |
| 74 | +function checkClass( |
| 75 | + node: ESClass, |
| 76 | + context: Readonly<RuleContext<keyof typeof errorMessages, Options>>, |
| 77 | + options: Readonly<Options>, |
| 78 | +): RuleResult<keyof typeof errorMessages, Options> { |
| 79 | + const [optionsObject] = options; |
| 80 | + const { ignoreIdentifierPattern, ignoreCodePattern } = optionsObject; |
| 81 | + |
| 82 | + const mut_descriptors: Array<RuleResult<keyof typeof errorMessages, Options>["descriptors"][number]> = []; |
| 83 | + |
| 84 | + if (!shouldIgnorePattern(node, context, ignoreIdentifierPattern, undefined, ignoreCodePattern)) { |
| 85 | + if (node.abstract) { |
| 86 | + const nodeText = context.sourceCode.getText(node); |
| 87 | + const abstractRelativeIndex = nodeText.indexOf("abstract"); |
| 88 | + const abstractIndex = context.sourceCode.getIndexFromLoc(node.loc.start) + abstractRelativeIndex; |
| 89 | + const start = context.sourceCode.getLocFromIndex(abstractIndex); |
| 90 | + const end = context.sourceCode.getLocFromIndex(abstractIndex + "abstract".length); |
| 91 | + |
| 92 | + mut_descriptors.push({ |
| 93 | + node, |
| 94 | + loc: { |
| 95 | + start, |
| 96 | + end, |
| 97 | + }, |
| 98 | + messageId: "abstract", |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + if (node.superClass !== null) { |
| 103 | + const nodeText = context.sourceCode.getText(node); |
| 104 | + const extendsRelativeIndex = nodeText.indexOf("extends"); |
| 105 | + const extendsIndex = context.sourceCode.getIndexFromLoc(node.loc.start) + extendsRelativeIndex; |
| 106 | + const start = context.sourceCode.getLocFromIndex(extendsIndex); |
| 107 | + const { end } = node.superClass.loc; |
| 108 | + |
| 109 | + mut_descriptors.push({ |
| 110 | + node, |
| 111 | + loc: { |
| 112 | + start, |
| 113 | + end, |
| 114 | + }, |
| 115 | + messageId: "extends", |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + context, |
| 122 | + descriptors: mut_descriptors, |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +// Create the rule. |
| 127 | +export const rule: Rule<keyof typeof errorMessages, Options> = createRule<keyof typeof errorMessages, Options>( |
| 128 | + name, |
| 129 | + meta, |
| 130 | + defaultOptions, |
| 131 | + { |
| 132 | + ClassDeclaration: checkClass, |
| 133 | + ClassExpression: checkClass, |
| 134 | + }, |
| 135 | +); |
0 commit comments