| |
| |
| |
| |
| |
| "use strict"; |
|
|
| |
| |
| |
|
|
| const astUtils = require("./utils/ast-utils"); |
|
|
| |
| |
| |
|
|
| |
| module.exports = { |
| meta: { |
| dialects: ["javascript", "typescript"], |
| language: "javascript", |
| type: "suggestion", |
|
|
| defaultOptions: [ |
| { |
| enforceForClassFields: true, |
| exceptMethods: [], |
| ignoreOverrideMethods: false, |
| }, |
| ], |
|
|
| docs: { |
| description: "Enforce that class methods utilize `this`", |
| recommended: false, |
| url: "https://eslint.org/docs/latest/rules/class-methods-use-this", |
| }, |
|
|
| schema: [ |
| { |
| type: "object", |
| properties: { |
| exceptMethods: { |
| type: "array", |
| items: { |
| type: "string", |
| }, |
| }, |
| enforceForClassFields: { |
| type: "boolean", |
| }, |
| ignoreOverrideMethods: { |
| type: "boolean", |
| }, |
| ignoreClassesWithImplements: { |
| enum: ["all", "public-fields"], |
| }, |
| }, |
| additionalProperties: false, |
| }, |
| ], |
|
|
| messages: { |
| missingThis: "Expected 'this' to be used by class {{name}}.", |
| }, |
| }, |
| create(context) { |
| const [options] = context.options; |
| const { |
| enforceForClassFields, |
| ignoreOverrideMethods, |
| ignoreClassesWithImplements, |
| } = options; |
| const exceptMethods = new Set(options.exceptMethods); |
|
|
| const stack = []; |
|
|
| |
| |
| |
| |
| function pushContext() { |
| stack.push(false); |
| } |
|
|
| |
| |
| |
| |
| function popContext() { |
| return stack.pop(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function enterFunction() { |
| pushContext(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function isInstanceMethod(node) { |
| switch (node.type) { |
| case "MethodDefinition": |
| return !node.static && node.kind !== "constructor"; |
| case "AccessorProperty": |
| case "PropertyDefinition": |
| return !node.static && enforceForClassFields; |
| default: |
| return false; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function hasImplements(node) { |
| const classNode = node.parent.parent; |
| return ( |
| classNode?.type === "ClassDeclaration" && |
| classNode.implements?.length > 0 |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function isIncludedInstanceMethod(node) { |
| if (isInstanceMethod(node)) { |
| if (node.computed) { |
| return true; |
| } |
|
|
| if (ignoreOverrideMethods && node.override) { |
| return false; |
| } |
|
|
| if (ignoreClassesWithImplements) { |
| const implementsInterfaces = hasImplements(node); |
| if (implementsInterfaces) { |
| if ( |
| ignoreClassesWithImplements === "all" || |
| (ignoreClassesWithImplements === "public-fields" && |
| node.key.type !== "PrivateIdentifier" && |
| (!node.accessibility || |
| node.accessibility === "public")) |
| ) { |
| return false; |
| } |
| } |
| } |
|
|
| const hashIfNeeded = |
| node.key.type === "PrivateIdentifier" ? "#" : ""; |
| const name = |
| node.key.type === "Literal" |
| ? astUtils.getStaticStringValue(node.key) |
| : node.key.name || ""; |
|
|
| return !exceptMethods.has(hashIfNeeded + name); |
| } |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function exitFunction(node) { |
| const methodUsesThis = popContext(); |
|
|
| if (isIncludedInstanceMethod(node.parent) && !methodUsesThis) { |
| context.report({ |
| node, |
| loc: astUtils.getFunctionHeadLoc(node, context.sourceCode), |
| messageId: "missingThis", |
| data: { |
| name: astUtils.getFunctionNameWithKind(node), |
| }, |
| }); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| function markThisUsed() { |
| if (stack.length) { |
| stack[stack.length - 1] = true; |
| } |
| } |
|
|
| return { |
| FunctionDeclaration: enterFunction, |
| "FunctionDeclaration:exit": exitFunction, |
| FunctionExpression: enterFunction, |
| "FunctionExpression:exit": exitFunction, |
|
|
| |
| |
| |
| "AccessorProperty > *.key:exit": pushContext, |
| "AccessorProperty:exit": popContext, |
| "PropertyDefinition > *.key:exit": pushContext, |
| "PropertyDefinition:exit": popContext, |
|
|
| |
| |
| |
| |
| |
| |
| StaticBlock: pushContext, |
| "StaticBlock:exit": popContext, |
|
|
| ThisExpression: markThisUsed, |
| Super: markThisUsed, |
| ...(enforceForClassFields && { |
| "AccessorProperty > ArrowFunctionExpression.value": |
| enterFunction, |
| "AccessorProperty > ArrowFunctionExpression.value:exit": |
| exitFunction, |
| "PropertyDefinition > ArrowFunctionExpression.value": |
| enterFunction, |
| "PropertyDefinition > ArrowFunctionExpression.value:exit": |
| exitFunction, |
| }), |
| }; |
| }, |
| }; |
|
|