|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
const astUtils = require("./utils/ast-utils"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
meta: { |
|
type: "suggestion", |
|
|
|
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", |
|
default: true |
|
} |
|
}, |
|
additionalProperties: false |
|
}], |
|
|
|
messages: { |
|
missingThis: "Expected 'this' to be used by class {{name}}." |
|
} |
|
}, |
|
create(context) { |
|
const config = Object.assign({}, context.options[0]); |
|
const enforceForClassFields = config.enforceForClassFields !== false; |
|
const exceptMethods = new Set(config.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 "PropertyDefinition": |
|
return !node.static && enforceForClassFields; |
|
default: |
|
return false; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isIncludedInstanceMethod(node) { |
|
if (isInstanceMethod(node)) { |
|
if (node.computed) { |
|
return true; |
|
} |
|
|
|
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, |
|
|
|
|
|
|
|
|
|
"PropertyDefinition > *.key:exit": pushContext, |
|
"PropertyDefinition:exit": popContext, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StaticBlock: pushContext, |
|
"StaticBlock:exit": popContext, |
|
|
|
ThisExpression: markThisUsed, |
|
Super: markThisUsed, |
|
...( |
|
enforceForClassFields && { |
|
"PropertyDefinition > ArrowFunctionExpression.value": enterFunction, |
|
"PropertyDefinition > ArrowFunctionExpression.value:exit": exitFunction |
|
} |
|
) |
|
}; |
|
} |
|
}; |
|
|