source: imaps-frontend/node_modules/eslint/lib/rules/space-infix-ops.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: 6.4 KB
RevLine 
[d565449]1/**
2 * @fileoverview Require spaces around infix operators
3 * @author Michael Ficarra
4 * @deprecated in ESLint v8.53.0
5 */
6"use strict";
7
8const { isEqToken } = require("./utils/ast-utils");
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13
14/** @type {import('../shared/types').Rule} */
15module.exports = {
16 meta: {
17 deprecated: true,
18 replacedBy: [],
19 type: "layout",
20
21 docs: {
22 description: "Require spacing around infix operators",
23 recommended: false,
24 url: "https://eslint.org/docs/latest/rules/space-infix-ops"
25 },
26
27 fixable: "whitespace",
28
29 schema: [
30 {
31 type: "object",
32 properties: {
33 int32Hint: {
34 type: "boolean",
35 default: false
36 }
37 },
38 additionalProperties: false
39 }
40 ],
41
42 messages: {
43 missingSpace: "Operator '{{operator}}' must be spaced."
44 }
45 },
46
47 create(context) {
48 const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
49 const sourceCode = context.sourceCode;
50
51 /**
52 * Returns the first token which violates the rule
53 * @param {ASTNode} left The left node of the main node
54 * @param {ASTNode} right The right node of the main node
55 * @param {string} op The operator of the main node
56 * @returns {Object} The violator token or null
57 * @private
58 */
59 function getFirstNonSpacedToken(left, right, op) {
60 const operator = sourceCode.getFirstTokenBetween(left, right, token => token.value === op);
61 const prev = sourceCode.getTokenBefore(operator);
62 const next = sourceCode.getTokenAfter(operator);
63
64 if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
65 return operator;
66 }
67
68 return null;
69 }
70
71 /**
72 * Reports an AST node as a rule violation
73 * @param {ASTNode} mainNode The node to report
74 * @param {Object} culpritToken The token which has a problem
75 * @returns {void}
76 * @private
77 */
78 function report(mainNode, culpritToken) {
79 context.report({
80 node: mainNode,
81 loc: culpritToken.loc,
82 messageId: "missingSpace",
83 data: {
84 operator: culpritToken.value
85 },
86 fix(fixer) {
87 const previousToken = sourceCode.getTokenBefore(culpritToken);
88 const afterToken = sourceCode.getTokenAfter(culpritToken);
89 let fixString = "";
90
91 if (culpritToken.range[0] - previousToken.range[1] === 0) {
92 fixString = " ";
93 }
94
95 fixString += culpritToken.value;
96
97 if (afterToken.range[0] - culpritToken.range[1] === 0) {
98 fixString += " ";
99 }
100
101 return fixer.replaceText(culpritToken, fixString);
102 }
103 });
104 }
105
106 /**
107 * Check if the node is binary then report
108 * @param {ASTNode} node node to evaluate
109 * @returns {void}
110 * @private
111 */
112 function checkBinary(node) {
113 const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
114 const rightNode = node.right;
115
116 // search for = in AssignmentPattern nodes
117 const operator = node.operator || "=";
118
119 const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);
120
121 if (nonSpacedNode) {
122 if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
123 report(node, nonSpacedNode);
124 }
125 }
126 }
127
128 /**
129 * Check if the node is conditional
130 * @param {ASTNode} node node to evaluate
131 * @returns {void}
132 * @private
133 */
134 function checkConditional(node) {
135 const nonSpacedConsequentNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
136 const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");
137
138 if (nonSpacedConsequentNode) {
139 report(node, nonSpacedConsequentNode);
140 }
141
142 if (nonSpacedAlternateNode) {
143 report(node, nonSpacedAlternateNode);
144 }
145 }
146
147 /**
148 * Check if the node is a variable
149 * @param {ASTNode} node node to evaluate
150 * @returns {void}
151 * @private
152 */
153 function checkVar(node) {
154 const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
155 const rightNode = node.init;
156
157 if (rightNode) {
158 const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");
159
160 if (nonSpacedNode) {
161 report(node, nonSpacedNode);
162 }
163 }
164 }
165
166 return {
167 AssignmentExpression: checkBinary,
168 AssignmentPattern: checkBinary,
169 BinaryExpression: checkBinary,
170 LogicalExpression: checkBinary,
171 ConditionalExpression: checkConditional,
172 VariableDeclarator: checkVar,
173
174 PropertyDefinition(node) {
175 if (!node.value) {
176 return;
177 }
178
179 /*
180 * Because of computed properties and type annotations, some
181 * tokens may exist between `node.key` and `=`.
182 * Therefore, find the `=` from the right.
183 */
184 const operatorToken = sourceCode.getTokenBefore(node.value, isEqToken);
185 const leftToken = sourceCode.getTokenBefore(operatorToken);
186 const rightToken = sourceCode.getTokenAfter(operatorToken);
187
188 if (
189 !sourceCode.isSpaceBetweenTokens(leftToken, operatorToken) ||
190 !sourceCode.isSpaceBetweenTokens(operatorToken, rightToken)
191 ) {
192 report(node, operatorToken);
193 }
194 }
195 };
196
197 }
198};
Note: See TracBrowser for help on using the repository browser.