source: imaps-frontend/node_modules/eslint/lib/rules/space-unary-ops.js

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[d565449]1/**
2 * @fileoverview This rule should require or disallow spaces before or after unary operations.
3 * @author Marcin Kumorek
4 * @deprecated in ESLint v8.53.0
5 */
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const astUtils = require("./utils/ast-utils");
13
14//------------------------------------------------------------------------------
15// Rule Definition
16//------------------------------------------------------------------------------
17
18/** @type {import('../shared/types').Rule} */
19module.exports = {
20 meta: {
21 deprecated: true,
22 replacedBy: [],
23 type: "layout",
24
25 docs: {
26 description: "Enforce consistent spacing before or after unary operators",
27 recommended: false,
28 url: "https://eslint.org/docs/latest/rules/space-unary-ops"
29 },
30
31 fixable: "whitespace",
32
33 schema: [
34 {
35 type: "object",
36 properties: {
37 words: {
38 type: "boolean",
39 default: true
40 },
41 nonwords: {
42 type: "boolean",
43 default: false
44 },
45 overrides: {
46 type: "object",
47 additionalProperties: {
48 type: "boolean"
49 }
50 }
51 },
52 additionalProperties: false
53 }
54 ],
55 messages: {
56 unexpectedBefore: "Unexpected space before unary operator '{{operator}}'.",
57 unexpectedAfter: "Unexpected space after unary operator '{{operator}}'.",
58 unexpectedAfterWord: "Unexpected space after unary word operator '{{word}}'.",
59 wordOperator: "Unary word operator '{{word}}' must be followed by whitespace.",
60 operator: "Unary operator '{{operator}}' must be followed by whitespace.",
61 beforeUnaryExpressions: "Space is required before unary expressions '{{token}}'."
62 }
63 },
64
65 create(context) {
66 const options = context.options[0] || { words: true, nonwords: false };
67
68 const sourceCode = context.sourceCode;
69
70 //--------------------------------------------------------------------------
71 // Helpers
72 //--------------------------------------------------------------------------
73
74 /**
75 * Check if the node is the first "!" in a "!!" convert to Boolean expression
76 * @param {ASTnode} node AST node
77 * @returns {boolean} Whether or not the node is first "!" in "!!"
78 */
79 function isFirstBangInBangBangExpression(node) {
80 return node && node.type === "UnaryExpression" && node.argument.operator === "!" &&
81 node.argument && node.argument.type === "UnaryExpression" && node.argument.operator === "!";
82 }
83
84 /**
85 * Checks if an override exists for a given operator.
86 * @param {string} operator Operator
87 * @returns {boolean} Whether or not an override has been provided for the operator
88 */
89 function overrideExistsForOperator(operator) {
90 return options.overrides && Object.prototype.hasOwnProperty.call(options.overrides, operator);
91 }
92
93 /**
94 * Gets the value that the override was set to for this operator
95 * @param {string} operator Operator
96 * @returns {boolean} Whether or not an override enforces a space with this operator
97 */
98 function overrideEnforcesSpaces(operator) {
99 return options.overrides[operator];
100 }
101
102 /**
103 * Verify Unary Word Operator has spaces after the word operator
104 * @param {ASTnode} node AST node
105 * @param {Object} firstToken first token from the AST node
106 * @param {Object} secondToken second token from the AST node
107 * @param {string} word The word to be used for reporting
108 * @returns {void}
109 */
110 function verifyWordHasSpaces(node, firstToken, secondToken, word) {
111 if (secondToken.range[0] === firstToken.range[1]) {
112 context.report({
113 node,
114 messageId: "wordOperator",
115 data: {
116 word
117 },
118 fix(fixer) {
119 return fixer.insertTextAfter(firstToken, " ");
120 }
121 });
122 }
123 }
124
125 /**
126 * Verify Unary Word Operator doesn't have spaces after the word operator
127 * @param {ASTnode} node AST node
128 * @param {Object} firstToken first token from the AST node
129 * @param {Object} secondToken second token from the AST node
130 * @param {string} word The word to be used for reporting
131 * @returns {void}
132 */
133 function verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word) {
134 if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) {
135 if (secondToken.range[0] > firstToken.range[1]) {
136 context.report({
137 node,
138 messageId: "unexpectedAfterWord",
139 data: {
140 word
141 },
142 fix(fixer) {
143 return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
144 }
145 });
146 }
147 }
148 }
149
150 /**
151 * Check Unary Word Operators for spaces after the word operator
152 * @param {ASTnode} node AST node
153 * @param {Object} firstToken first token from the AST node
154 * @param {Object} secondToken second token from the AST node
155 * @param {string} word The word to be used for reporting
156 * @returns {void}
157 */
158 function checkUnaryWordOperatorForSpaces(node, firstToken, secondToken, word) {
159 if (overrideExistsForOperator(word)) {
160 if (overrideEnforcesSpaces(word)) {
161 verifyWordHasSpaces(node, firstToken, secondToken, word);
162 } else {
163 verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word);
164 }
165 } else if (options.words) {
166 verifyWordHasSpaces(node, firstToken, secondToken, word);
167 } else {
168 verifyWordDoesntHaveSpaces(node, firstToken, secondToken, word);
169 }
170 }
171
172 /**
173 * Verifies YieldExpressions satisfy spacing requirements
174 * @param {ASTnode} node AST node
175 * @returns {void}
176 */
177 function checkForSpacesAfterYield(node) {
178 const tokens = sourceCode.getFirstTokens(node, 3),
179 word = "yield";
180
181 if (!node.argument || node.delegate) {
182 return;
183 }
184
185 checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], word);
186 }
187
188 /**
189 * Verifies AwaitExpressions satisfy spacing requirements
190 * @param {ASTNode} node AwaitExpression AST node
191 * @returns {void}
192 */
193 function checkForSpacesAfterAwait(node) {
194 const tokens = sourceCode.getFirstTokens(node, 3);
195
196 checkUnaryWordOperatorForSpaces(node, tokens[0], tokens[1], "await");
197 }
198
199 /**
200 * Verifies UnaryExpression, UpdateExpression and NewExpression have spaces before or after the operator
201 * @param {ASTnode} node AST node
202 * @param {Object} firstToken First token in the expression
203 * @param {Object} secondToken Second token in the expression
204 * @returns {void}
205 */
206 function verifyNonWordsHaveSpaces(node, firstToken, secondToken) {
207 if (node.prefix) {
208 if (isFirstBangInBangBangExpression(node)) {
209 return;
210 }
211 if (firstToken.range[1] === secondToken.range[0]) {
212 context.report({
213 node,
214 messageId: "operator",
215 data: {
216 operator: firstToken.value
217 },
218 fix(fixer) {
219 return fixer.insertTextAfter(firstToken, " ");
220 }
221 });
222 }
223 } else {
224 if (firstToken.range[1] === secondToken.range[0]) {
225 context.report({
226 node,
227 messageId: "beforeUnaryExpressions",
228 data: {
229 token: secondToken.value
230 },
231 fix(fixer) {
232 return fixer.insertTextBefore(secondToken, " ");
233 }
234 });
235 }
236 }
237 }
238
239 /**
240 * Verifies UnaryExpression, UpdateExpression and NewExpression don't have spaces before or after the operator
241 * @param {ASTnode} node AST node
242 * @param {Object} firstToken First token in the expression
243 * @param {Object} secondToken Second token in the expression
244 * @returns {void}
245 */
246 function verifyNonWordsDontHaveSpaces(node, firstToken, secondToken) {
247 if (node.prefix) {
248 if (secondToken.range[0] > firstToken.range[1]) {
249 context.report({
250 node,
251 messageId: "unexpectedAfter",
252 data: {
253 operator: firstToken.value
254 },
255 fix(fixer) {
256 if (astUtils.canTokensBeAdjacent(firstToken, secondToken)) {
257 return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
258 }
259 return null;
260 }
261 });
262 }
263 } else {
264 if (secondToken.range[0] > firstToken.range[1]) {
265 context.report({
266 node,
267 messageId: "unexpectedBefore",
268 data: {
269 operator: secondToken.value
270 },
271 fix(fixer) {
272 return fixer.removeRange([firstToken.range[1], secondToken.range[0]]);
273 }
274 });
275 }
276 }
277 }
278
279 /**
280 * Verifies UnaryExpression, UpdateExpression and NewExpression satisfy spacing requirements
281 * @param {ASTnode} node AST node
282 * @returns {void}
283 */
284 function checkForSpaces(node) {
285 const tokens = node.type === "UpdateExpression" && !node.prefix
286 ? sourceCode.getLastTokens(node, 2)
287 : sourceCode.getFirstTokens(node, 2);
288 const firstToken = tokens[0];
289 const secondToken = tokens[1];
290
291 if ((node.type === "NewExpression" || node.prefix) && firstToken.type === "Keyword") {
292 checkUnaryWordOperatorForSpaces(node, firstToken, secondToken, firstToken.value);
293 return;
294 }
295
296 const operator = node.prefix ? tokens[0].value : tokens[1].value;
297
298 if (overrideExistsForOperator(operator)) {
299 if (overrideEnforcesSpaces(operator)) {
300 verifyNonWordsHaveSpaces(node, firstToken, secondToken);
301 } else {
302 verifyNonWordsDontHaveSpaces(node, firstToken, secondToken);
303 }
304 } else if (options.nonwords) {
305 verifyNonWordsHaveSpaces(node, firstToken, secondToken);
306 } else {
307 verifyNonWordsDontHaveSpaces(node, firstToken, secondToken);
308 }
309 }
310
311 //--------------------------------------------------------------------------
312 // Public
313 //--------------------------------------------------------------------------
314
315 return {
316 UnaryExpression: checkForSpaces,
317 UpdateExpression: checkForSpaces,
318 NewExpression: checkForSpaces,
319 YieldExpression: checkForSpacesAfterYield,
320 AwaitExpression: checkForSpacesAfterAwait
321 };
322
323 }
324};
Note: See TracBrowser for help on using the repository browser.