Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * @fileoverview Rule to enforce that all class methods use 'this'. |
| 3 | * @author Patrick Williams |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | // Rule Definition |
| 10 | //------------------------------------------------------------------------------ |
| 11 | |
| 12 | module.exports = { |
| 13 | meta: { |
| 14 | type: "suggestion", |
| 15 | |
| 16 | docs: { |
| 17 | description: "enforce that class methods utilize `this`", |
| 18 | category: "Best Practices", |
| 19 | recommended: false, |
| 20 | url: "https://eslint.org/docs/rules/class-methods-use-this" |
| 21 | }, |
| 22 | |
| 23 | schema: [{ |
| 24 | type: "object", |
| 25 | properties: { |
| 26 | exceptMethods: { |
| 27 | type: "array", |
| 28 | items: { |
| 29 | type: "string" |
| 30 | } |
| 31 | } |
| 32 | }, |
| 33 | additionalProperties: false |
| 34 | }], |
| 35 | |
| 36 | messages: { |
| 37 | missingThis: "Expected 'this' to be used by class method '{{name}}'." |
| 38 | } |
| 39 | }, |
| 40 | create(context) { |
| 41 | const config = Object.assign({}, context.options[0]); |
| 42 | const exceptMethods = new Set(config.exceptMethods || []); |
| 43 | |
| 44 | const stack = []; |
| 45 | |
| 46 | /** |
| 47 | * Initializes the current context to false and pushes it onto the stack. |
| 48 | * These booleans represent whether 'this' has been used in the context. |
| 49 | * @returns {void} |
| 50 | * @private |
| 51 | */ |
| 52 | function enterFunction() { |
| 53 | stack.push(false); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Check if the node is an instance method |
| 58 | * @param {ASTNode} node - node to check |
| 59 | * @returns {boolean} True if its an instance method |
| 60 | * @private |
| 61 | */ |
| 62 | function isInstanceMethod(node) { |
| 63 | return !node.static && node.kind !== "constructor" && node.type === "MethodDefinition"; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Check if the node is an instance method not excluded by config |
| 68 | * @param {ASTNode} node - node to check |
| 69 | * @returns {boolean} True if it is an instance method, and not excluded by config |
| 70 | * @private |
| 71 | */ |
| 72 | function isIncludedInstanceMethod(node) { |
| 73 | return isInstanceMethod(node) && !exceptMethods.has(node.key.name); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks if we are leaving a function that is a method, and reports if 'this' has not been used. |
| 78 | * Static methods and the constructor are exempt. |
| 79 | * Then pops the context off the stack. |
| 80 | * @param {ASTNode} node - A function node that was entered. |
| 81 | * @returns {void} |
| 82 | * @private |
| 83 | */ |
| 84 | function exitFunction(node) { |
| 85 | const methodUsesThis = stack.pop(); |
| 86 | |
| 87 | if (isIncludedInstanceMethod(node.parent) && !methodUsesThis) { |
| 88 | context.report({ |
| 89 | node, |
| 90 | messageId: "missingThis", |
| 91 | data: { |
| 92 | name: node.parent.key.name |
| 93 | } |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Mark the current context as having used 'this'. |
| 100 | * @returns {void} |
| 101 | * @private |
| 102 | */ |
| 103 | function markThisUsed() { |
| 104 | if (stack.length) { |
| 105 | stack[stack.length - 1] = true; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return { |
| 110 | FunctionDeclaration: enterFunction, |
| 111 | "FunctionDeclaration:exit": exitFunction, |
| 112 | FunctionExpression: enterFunction, |
| 113 | "FunctionExpression:exit": exitFunction, |
| 114 | ThisExpression: markThisUsed, |
| 115 | Super: markThisUsed |
| 116 | }; |
| 117 | } |
| 118 | }; |