2 * @fileoverview Require spaces around infix operators
3 * @author Michael Ficarra
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "require spacing around infix operators",
17 category: "Stylistic Issues",
19 url: "https://eslint.org/docs/rules/space-infix-ops"
22 fixable: "whitespace",
33 additionalProperties: false
38 missingSpace: "Operator '{{operator}}' must be spaced."
43 const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
44 const sourceCode = context.getSourceCode();
47 * Returns the first token which violates the rule
48 * @param {ASTNode} left The left node of the main node
49 * @param {ASTNode} right The right node of the main node
50 * @param {string} op The operator of the main node
51 * @returns {Object} The violator token or null
54 function getFirstNonSpacedToken(left, right, op) {
55 const operator = sourceCode.getFirstTokenBetween(left, right, token => token.value === op);
56 const prev = sourceCode.getTokenBefore(operator);
57 const next = sourceCode.getTokenAfter(operator);
59 if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
67 * Reports an AST node as a rule violation
68 * @param {ASTNode} mainNode The node to report
69 * @param {Object} culpritToken The token which has a problem
73 function report(mainNode, culpritToken) {
76 loc: culpritToken.loc,
77 messageId: "missingSpace",
79 operator: culpritToken.value
82 const previousToken = sourceCode.getTokenBefore(culpritToken);
83 const afterToken = sourceCode.getTokenAfter(culpritToken);
86 if (culpritToken.range[0] - previousToken.range[1] === 0) {
90 fixString += culpritToken.value;
92 if (afterToken.range[0] - culpritToken.range[1] === 0) {
96 return fixer.replaceText(culpritToken, fixString);
102 * Check if the node is binary then report
103 * @param {ASTNode} node node to evaluate
107 function checkBinary(node) {
108 const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
109 const rightNode = node.right;
111 // search for = in AssignmentPattern nodes
112 const operator = node.operator || "=";
114 const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);
117 if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
118 report(node, nonSpacedNode);
124 * Check if the node is conditional
125 * @param {ASTNode} node node to evaluate
129 function checkConditional(node) {
130 const nonSpacedConsequentNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
131 const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");
133 if (nonSpacedConsequentNode) {
134 report(node, nonSpacedConsequentNode);
137 if (nonSpacedAlternateNode) {
138 report(node, nonSpacedAlternateNode);
143 * Check if the node is a variable
144 * @param {ASTNode} node node to evaluate
148 function checkVar(node) {
149 const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
150 const rightNode = node.init;
153 const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");
156 report(node, nonSpacedNode);
162 AssignmentExpression: checkBinary,
163 AssignmentPattern: checkBinary,
164 BinaryExpression: checkBinary,
165 LogicalExpression: checkBinary,
166 ConditionalExpression: checkConditional,
167 VariableDeclarator: checkVar