source: imaps-frontend/node_modules/eslint/lib/rules/comma-style.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 11.7 KB
Line 
1/**
2 * @fileoverview Comma style - enforces comma styles of two types: last and first
3 * @author Vignesh Anand aka vegetableman
4 * @deprecated in ESLint v8.53.0
5 */
6
7"use strict";
8
9const astUtils = require("./utils/ast-utils");
10
11//------------------------------------------------------------------------------
12// Rule Definition
13//------------------------------------------------------------------------------
14
15/** @type {import('../shared/types').Rule} */
16module.exports = {
17 meta: {
18 deprecated: true,
19 replacedBy: [],
20 type: "layout",
21
22 docs: {
23 description: "Enforce consistent comma style",
24 recommended: false,
25 url: "https://eslint.org/docs/latest/rules/comma-style"
26 },
27
28 fixable: "code",
29
30 schema: [
31 {
32 enum: ["first", "last"]
33 },
34 {
35 type: "object",
36 properties: {
37 exceptions: {
38 type: "object",
39 additionalProperties: {
40 type: "boolean"
41 }
42 }
43 },
44 additionalProperties: false
45 }
46 ],
47
48 messages: {
49 unexpectedLineBeforeAndAfterComma: "Bad line breaking before and after ','.",
50 expectedCommaFirst: "',' should be placed first.",
51 expectedCommaLast: "',' should be placed last."
52 }
53 },
54
55 create(context) {
56 const style = context.options[0] || "last",
57 sourceCode = context.sourceCode;
58 const exceptions = {
59 ArrayPattern: true,
60 ArrowFunctionExpression: true,
61 CallExpression: true,
62 FunctionDeclaration: true,
63 FunctionExpression: true,
64 ImportDeclaration: true,
65 ObjectPattern: true,
66 NewExpression: true
67 };
68
69 if (context.options.length === 2 && Object.prototype.hasOwnProperty.call(context.options[1], "exceptions")) {
70 const keys = Object.keys(context.options[1].exceptions);
71
72 for (let i = 0; i < keys.length; i++) {
73 exceptions[keys[i]] = context.options[1].exceptions[keys[i]];
74 }
75 }
76
77 //--------------------------------------------------------------------------
78 // Helpers
79 //--------------------------------------------------------------------------
80
81 /**
82 * Modified text based on the style
83 * @param {string} styleType Style type
84 * @param {string} text Source code text
85 * @returns {string} modified text
86 * @private
87 */
88 function getReplacedText(styleType, text) {
89 switch (styleType) {
90 case "between":
91 return `,${text.replace(astUtils.LINEBREAK_MATCHER, "")}`;
92
93 case "first":
94 return `${text},`;
95
96 case "last":
97 return `,${text}`;
98
99 default:
100 return "";
101 }
102 }
103
104 /**
105 * Determines the fixer function for a given style.
106 * @param {string} styleType comma style
107 * @param {ASTNode} previousItemToken The token to check.
108 * @param {ASTNode} commaToken The token to check.
109 * @param {ASTNode} currentItemToken The token to check.
110 * @returns {Function} Fixer function
111 * @private
112 */
113 function getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken) {
114 const text =
115 sourceCode.text.slice(previousItemToken.range[1], commaToken.range[0]) +
116 sourceCode.text.slice(commaToken.range[1], currentItemToken.range[0]);
117 const range = [previousItemToken.range[1], currentItemToken.range[0]];
118
119 return function(fixer) {
120 return fixer.replaceTextRange(range, getReplacedText(styleType, text));
121 };
122 }
123
124 /**
125 * Validates the spacing around single items in lists.
126 * @param {Token} previousItemToken The last token from the previous item.
127 * @param {Token} commaToken The token representing the comma.
128 * @param {Token} currentItemToken The first token of the current item.
129 * @param {Token} reportItem The item to use when reporting an error.
130 * @returns {void}
131 * @private
132 */
133 function validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem) {
134
135 // if single line
136 if (astUtils.isTokenOnSameLine(commaToken, currentItemToken) &&
137 astUtils.isTokenOnSameLine(previousItemToken, commaToken)) {
138
139 // do nothing.
140
141 } else if (!astUtils.isTokenOnSameLine(commaToken, currentItemToken) &&
142 !astUtils.isTokenOnSameLine(previousItemToken, commaToken)) {
143
144 const comment = sourceCode.getCommentsAfter(commaToken)[0];
145 const styleType = comment && comment.type === "Block" && astUtils.isTokenOnSameLine(commaToken, comment)
146 ? style
147 : "between";
148
149 // lone comma
150 context.report({
151 node: reportItem,
152 loc: commaToken.loc,
153 messageId: "unexpectedLineBeforeAndAfterComma",
154 fix: getFixerFunction(styleType, previousItemToken, commaToken, currentItemToken)
155 });
156
157 } else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {
158
159 context.report({
160 node: reportItem,
161 loc: commaToken.loc,
162 messageId: "expectedCommaFirst",
163 fix: getFixerFunction(style, previousItemToken, commaToken, currentItemToken)
164 });
165
166 } else if (style === "last" && astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {
167
168 context.report({
169 node: reportItem,
170 loc: commaToken.loc,
171 messageId: "expectedCommaLast",
172 fix: getFixerFunction(style, previousItemToken, commaToken, currentItemToken)
173 });
174 }
175 }
176
177 /**
178 * Checks the comma placement with regards to a declaration/property/element
179 * @param {ASTNode} node The binary expression node to check
180 * @param {string} property The property of the node containing child nodes.
181 * @private
182 * @returns {void}
183 */
184 function validateComma(node, property) {
185 const items = node[property],
186 arrayLiteral = (node.type === "ArrayExpression" || node.type === "ArrayPattern");
187
188 if (items.length > 1 || arrayLiteral) {
189
190 // seed as opening [
191 let previousItemToken = sourceCode.getFirstToken(node);
192
193 items.forEach(item => {
194 const commaToken = item ? sourceCode.getTokenBefore(item) : previousItemToken,
195 currentItemToken = item ? sourceCode.getFirstToken(item) : sourceCode.getTokenAfter(commaToken),
196 reportItem = item || currentItemToken;
197
198 /*
199 * This works by comparing three token locations:
200 * - previousItemToken is the last token of the previous item
201 * - commaToken is the location of the comma before the current item
202 * - currentItemToken is the first token of the current item
203 *
204 * These values get switched around if item is undefined.
205 * previousItemToken will refer to the last token not belonging
206 * to the current item, which could be a comma or an opening
207 * square bracket. currentItemToken could be a comma.
208 *
209 * All comparisons are done based on these tokens directly, so
210 * they are always valid regardless of an undefined item.
211 */
212 if (astUtils.isCommaToken(commaToken)) {
213 validateCommaItemSpacing(previousItemToken, commaToken, currentItemToken, reportItem);
214 }
215
216 if (item) {
217 const tokenAfterItem = sourceCode.getTokenAfter(item, astUtils.isNotClosingParenToken);
218
219 previousItemToken = tokenAfterItem
220 ? sourceCode.getTokenBefore(tokenAfterItem)
221 : sourceCode.ast.tokens[sourceCode.ast.tokens.length - 1];
222 } else {
223 previousItemToken = currentItemToken;
224 }
225 });
226
227 /*
228 * Special case for array literals that have empty last items, such
229 * as [ 1, 2, ]. These arrays only have two items show up in the
230 * AST, so we need to look at the token to verify that there's no
231 * dangling comma.
232 */
233 if (arrayLiteral) {
234
235 const lastToken = sourceCode.getLastToken(node),
236 nextToLastToken = sourceCode.getTokenBefore(lastToken);
237
238 if (astUtils.isCommaToken(nextToLastToken)) {
239 validateCommaItemSpacing(
240 sourceCode.getTokenBefore(nextToLastToken),
241 nextToLastToken,
242 lastToken,
243 lastToken
244 );
245 }
246 }
247 }
248 }
249
250 //--------------------------------------------------------------------------
251 // Public
252 //--------------------------------------------------------------------------
253
254 const nodes = {};
255
256 if (!exceptions.VariableDeclaration) {
257 nodes.VariableDeclaration = function(node) {
258 validateComma(node, "declarations");
259 };
260 }
261 if (!exceptions.ObjectExpression) {
262 nodes.ObjectExpression = function(node) {
263 validateComma(node, "properties");
264 };
265 }
266 if (!exceptions.ObjectPattern) {
267 nodes.ObjectPattern = function(node) {
268 validateComma(node, "properties");
269 };
270 }
271 if (!exceptions.ArrayExpression) {
272 nodes.ArrayExpression = function(node) {
273 validateComma(node, "elements");
274 };
275 }
276 if (!exceptions.ArrayPattern) {
277 nodes.ArrayPattern = function(node) {
278 validateComma(node, "elements");
279 };
280 }
281 if (!exceptions.FunctionDeclaration) {
282 nodes.FunctionDeclaration = function(node) {
283 validateComma(node, "params");
284 };
285 }
286 if (!exceptions.FunctionExpression) {
287 nodes.FunctionExpression = function(node) {
288 validateComma(node, "params");
289 };
290 }
291 if (!exceptions.ArrowFunctionExpression) {
292 nodes.ArrowFunctionExpression = function(node) {
293 validateComma(node, "params");
294 };
295 }
296 if (!exceptions.CallExpression) {
297 nodes.CallExpression = function(node) {
298 validateComma(node, "arguments");
299 };
300 }
301 if (!exceptions.ImportDeclaration) {
302 nodes.ImportDeclaration = function(node) {
303 validateComma(node, "specifiers");
304 };
305 }
306 if (!exceptions.NewExpression) {
307 nodes.NewExpression = function(node) {
308 validateComma(node, "arguments");
309 };
310 }
311
312 return nodes;
313 }
314};
Note: See TracBrowser for help on using the repository browser.