1 | /**
|
---|
2 | * @fileoverview Rule to enforce consistent naming of "this" context variables
|
---|
3 | * @author Raphael Pigulla
|
---|
4 | */
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | //------------------------------------------------------------------------------
|
---|
8 | // Rule Definition
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 |
|
---|
11 | /** @type {import('../shared/types').Rule} */
|
---|
12 | module.exports = {
|
---|
13 | meta: {
|
---|
14 | type: "suggestion",
|
---|
15 |
|
---|
16 | docs: {
|
---|
17 | description: "Enforce consistent naming when capturing the current execution context",
|
---|
18 | recommended: false,
|
---|
19 | url: "https://eslint.org/docs/latest/rules/consistent-this"
|
---|
20 | },
|
---|
21 |
|
---|
22 | schema: {
|
---|
23 | type: "array",
|
---|
24 | items: {
|
---|
25 | type: "string",
|
---|
26 | minLength: 1
|
---|
27 | },
|
---|
28 | uniqueItems: true
|
---|
29 | },
|
---|
30 |
|
---|
31 | messages: {
|
---|
32 | aliasNotAssignedToThis: "Designated alias '{{name}}' is not assigned to 'this'.",
|
---|
33 | unexpectedAlias: "Unexpected alias '{{name}}' for 'this'."
|
---|
34 | }
|
---|
35 | },
|
---|
36 |
|
---|
37 | create(context) {
|
---|
38 | let aliases = [];
|
---|
39 | const sourceCode = context.sourceCode;
|
---|
40 |
|
---|
41 | if (context.options.length === 0) {
|
---|
42 | aliases.push("that");
|
---|
43 | } else {
|
---|
44 | aliases = context.options;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Reports that a variable declarator or assignment expression is assigning
|
---|
49 | * a non-'this' value to the specified alias.
|
---|
50 | * @param {ASTNode} node The assigning node.
|
---|
51 | * @param {string} name the name of the alias that was incorrectly used.
|
---|
52 | * @returns {void}
|
---|
53 | */
|
---|
54 | function reportBadAssignment(node, name) {
|
---|
55 | context.report({ node, messageId: "aliasNotAssignedToThis", data: { name } });
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Checks that an assignment to an identifier only assigns 'this' to the
|
---|
60 | * appropriate alias, and the alias is only assigned to 'this'.
|
---|
61 | * @param {ASTNode} node The assigning node.
|
---|
62 | * @param {Identifier} name The name of the variable assigned to.
|
---|
63 | * @param {Expression} value The value of the assignment.
|
---|
64 | * @returns {void}
|
---|
65 | */
|
---|
66 | function checkAssignment(node, name, value) {
|
---|
67 | const isThis = value.type === "ThisExpression";
|
---|
68 |
|
---|
69 | if (aliases.includes(name)) {
|
---|
70 | if (!isThis || node.operator && node.operator !== "=") {
|
---|
71 | reportBadAssignment(node, name);
|
---|
72 | }
|
---|
73 | } else if (isThis) {
|
---|
74 | context.report({ node, messageId: "unexpectedAlias", data: { name } });
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Ensures that a variable declaration of the alias in a program or function
|
---|
80 | * is assigned to the correct value.
|
---|
81 | * @param {string} alias alias the check the assignment of.
|
---|
82 | * @param {Object} scope scope of the current code we are checking.
|
---|
83 | * @private
|
---|
84 | * @returns {void}
|
---|
85 | */
|
---|
86 | function checkWasAssigned(alias, scope) {
|
---|
87 | const variable = scope.set.get(alias);
|
---|
88 |
|
---|
89 | if (!variable) {
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (variable.defs.some(def => def.node.type === "VariableDeclarator" &&
|
---|
94 | def.node.init !== null)) {
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * The alias has been declared and not assigned: check it was
|
---|
100 | * assigned later in the same scope.
|
---|
101 | */
|
---|
102 | if (!variable.references.some(reference => {
|
---|
103 | const write = reference.writeExpr;
|
---|
104 |
|
---|
105 | return (
|
---|
106 | reference.from === scope &&
|
---|
107 | write && write.type === "ThisExpression" &&
|
---|
108 | write.parent.operator === "="
|
---|
109 | );
|
---|
110 | })) {
|
---|
111 | variable.defs.map(def => def.node).forEach(node => {
|
---|
112 | reportBadAssignment(node, alias);
|
---|
113 | });
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Check each alias to ensure that is was assigned to the correct value.
|
---|
119 | * @param {ASTNode} node The node that represents the scope to check.
|
---|
120 | * @returns {void}
|
---|
121 | */
|
---|
122 | function ensureWasAssigned(node) {
|
---|
123 | const scope = sourceCode.getScope(node);
|
---|
124 |
|
---|
125 | aliases.forEach(alias => {
|
---|
126 | checkWasAssigned(alias, scope);
|
---|
127 | });
|
---|
128 | }
|
---|
129 |
|
---|
130 | return {
|
---|
131 | "Program:exit": ensureWasAssigned,
|
---|
132 | "FunctionExpression:exit": ensureWasAssigned,
|
---|
133 | "FunctionDeclaration:exit": ensureWasAssigned,
|
---|
134 |
|
---|
135 | VariableDeclarator(node) {
|
---|
136 | const id = node.id;
|
---|
137 | const isDestructuring =
|
---|
138 | id.type === "ArrayPattern" || id.type === "ObjectPattern";
|
---|
139 |
|
---|
140 | if (node.init !== null && !isDestructuring) {
|
---|
141 | checkAssignment(node, id.name, node.init);
|
---|
142 | }
|
---|
143 | },
|
---|
144 |
|
---|
145 | AssignmentExpression(node) {
|
---|
146 | if (node.left.type === "Identifier") {
|
---|
147 | checkAssignment(node, node.left.name, node.right);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | };
|
---|
151 |
|
---|
152 | }
|
---|
153 | };
|
---|