1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const astUtils = require("../ast-utils");
|
13 | const esutils = require("esutils");
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function isModuleExports(pattern) {
|
25 | if (pattern.type === "MemberExpression" && pattern.object.type === "Identifier" && pattern.object.name === "module") {
|
26 |
|
27 |
|
28 | if (pattern.property.type === "Identifier" && pattern.property.name === "exports") {
|
29 | return true;
|
30 | }
|
31 |
|
32 |
|
33 | if (pattern.property.type === "Literal" && pattern.property.value === "exports") {
|
34 | return true;
|
35 | }
|
36 | }
|
37 | return false;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function isIdentifier(name, ecmaVersion) {
|
47 | if (ecmaVersion >= 6) {
|
48 | return esutils.keyword.isIdentifierES6(name);
|
49 | }
|
50 | return esutils.keyword.isIdentifierES5(name);
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | const alwaysOrNever = { enum: ["always", "never"] };
|
58 | const optionsObject = {
|
59 | type: "object",
|
60 | properties: {
|
61 | includeCommonJSModuleExports: {
|
62 | type: "boolean"
|
63 | }
|
64 | },
|
65 | additionalProperties: false
|
66 | };
|
67 |
|
68 | module.exports = {
|
69 | meta: {
|
70 | docs: {
|
71 | description: "require function names to match the name of the variable or property to which they are assigned",
|
72 | category: "Stylistic Issues",
|
73 | recommended: false,
|
74 | url: "https://eslint.org/docs/rules/func-name-matching"
|
75 | },
|
76 |
|
77 | schema: {
|
78 | anyOf: [{
|
79 | type: "array",
|
80 | additionalItems: false,
|
81 | items: [alwaysOrNever, optionsObject]
|
82 | }, {
|
83 | type: "array",
|
84 | additionalItems: false,
|
85 | items: [optionsObject]
|
86 | }]
|
87 | }
|
88 | },
|
89 |
|
90 | create(context) {
|
91 | const options = (typeof context.options[0] === "object" ? context.options[0] : context.options[1]) || {};
|
92 | const nameMatches = typeof context.options[0] === "string" ? context.options[0] : "always";
|
93 | const includeModuleExports = options.includeCommonJSModuleExports;
|
94 | const ecmaVersion = context.parserOptions && context.parserOptions.ecmaVersion ? context.parserOptions.ecmaVersion : 5;
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | function shouldWarn(x, y) {
|
103 | return (nameMatches === "always" && x !== y) || (nameMatches === "never" && x === y);
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function report(node, name, funcName, isProp) {
|
115 | let message;
|
116 |
|
117 | if (nameMatches === "always" && isProp) {
|
118 | message = "Function name `{{funcName}}` should match property name `{{name}}`";
|
119 | } else if (nameMatches === "always") {
|
120 | message = "Function name `{{funcName}}` should match variable name `{{name}}`";
|
121 | } else if (isProp) {
|
122 | message = "Function name `{{funcName}}` should not match property name `{{name}}`";
|
123 | } else {
|
124 | message = "Function name `{{funcName}}` should not match variable name `{{name}}`";
|
125 | }
|
126 | context.report({
|
127 | node,
|
128 | message,
|
129 | data: {
|
130 | name,
|
131 | funcName
|
132 | }
|
133 | });
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 | function isStringLiteral(node) {
|
142 | return node.type === "Literal" && typeof node.value === "string";
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | return {
|
150 |
|
151 | VariableDeclarator(node) {
|
152 | if (!node.init || node.init.type !== "FunctionExpression" || node.id.type !== "Identifier") {
|
153 | return;
|
154 | }
|
155 | if (node.init.id && shouldWarn(node.id.name, node.init.id.name)) {
|
156 | report(node, node.id.name, node.init.id.name, false);
|
157 | }
|
158 | },
|
159 |
|
160 | AssignmentExpression(node) {
|
161 | if (
|
162 | node.right.type !== "FunctionExpression" ||
|
163 | (node.left.computed && node.left.property.type !== "Literal") ||
|
164 | (!includeModuleExports && isModuleExports(node.left)) ||
|
165 | (node.left.type !== "Identifier" && node.left.type !== "MemberExpression")
|
166 | ) {
|
167 | return;
|
168 | }
|
169 |
|
170 | const isProp = node.left.type === "MemberExpression";
|
171 | const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name;
|
172 |
|
173 | if (node.right.id && isIdentifier(name) && shouldWarn(name, node.right.id.name)) {
|
174 | report(node, name, node.right.id.name, isProp);
|
175 | }
|
176 | },
|
177 |
|
178 | Property(node) {
|
179 | if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && !isStringLiteral(node.key)) {
|
180 | return;
|
181 | }
|
182 | if (node.key.type === "Identifier" && shouldWarn(node.key.name, node.value.id.name)) {
|
183 | report(node, node.key.name, node.value.id.name, true);
|
184 | } else if (
|
185 | isStringLiteral(node.key) &&
|
186 | isIdentifier(node.key.value, ecmaVersion) &&
|
187 | shouldWarn(node.key.value, node.value.id.name)
|
188 | ) {
|
189 | report(node, node.key.value, node.value.id.name, true);
|
190 | }
|
191 | }
|
192 | };
|
193 | }
|
194 | };
|