]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/space-infix-ops.js
8065b5240b9ffac0e43c5e882de0bc86e4bb9db2
2 * @fileoverview Require spaces around infix operators
3 * @author Michael Ficarra
7 const { isEqToken
} = require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description
: "require spacing around infix operators",
20 url
: "https://eslint.org/docs/rules/space-infix-ops"
23 fixable
: "whitespace",
34 additionalProperties
: false
39 missingSpace
: "Operator '{{operator}}' must be spaced."
44 const int32Hint
= context
.options
[0] ? context
.options
[0].int32Hint
=== true : false;
45 const sourceCode
= context
.getSourceCode();
48 * Returns the first token which violates the rule
49 * @param {ASTNode} left The left node of the main node
50 * @param {ASTNode} right The right node of the main node
51 * @param {string} op The operator of the main node
52 * @returns {Object} The violator token or null
55 function getFirstNonSpacedToken(left
, right
, op
) {
56 const operator
= sourceCode
.getFirstTokenBetween(left
, right
, token
=> token
.value
=== op
);
57 const prev
= sourceCode
.getTokenBefore(operator
);
58 const next
= sourceCode
.getTokenAfter(operator
);
60 if (!sourceCode
.isSpaceBetweenTokens(prev
, operator
) || !sourceCode
.isSpaceBetweenTokens(operator
, next
)) {
68 * Reports an AST node as a rule violation
69 * @param {ASTNode} mainNode The node to report
70 * @param {Object} culpritToken The token which has a problem
74 function report(mainNode
, culpritToken
) {
77 loc
: culpritToken
.loc
,
78 messageId
: "missingSpace",
80 operator
: culpritToken
.value
83 const previousToken
= sourceCode
.getTokenBefore(culpritToken
);
84 const afterToken
= sourceCode
.getTokenAfter(culpritToken
);
87 if (culpritToken
.range
[0] - previousToken
.range
[1] === 0) {
91 fixString
+= culpritToken
.value
;
93 if (afterToken
.range
[0] - culpritToken
.range
[1] === 0) {
97 return fixer
.replaceText(culpritToken
, fixString
);
103 * Check if the node is binary then report
104 * @param {ASTNode} node node to evaluate
108 function checkBinary(node
) {
109 const leftNode
= (node
.left
.typeAnnotation
) ? node
.left
.typeAnnotation
: node
.left
;
110 const rightNode
= node
.right
;
112 // search for = in AssignmentPattern nodes
113 const operator
= node
.operator
|| "=";
115 const nonSpacedNode
= getFirstNonSpacedToken(leftNode
, rightNode
, operator
);
118 if (!(int32Hint
&& sourceCode
.getText(node
).endsWith("|0"))) {
119 report(node
, nonSpacedNode
);
125 * Check if the node is conditional
126 * @param {ASTNode} node node to evaluate
130 function checkConditional(node
) {
131 const nonSpacedConsequentNode
= getFirstNonSpacedToken(node
.test
, node
.consequent
, "?");
132 const nonSpacedAlternateNode
= getFirstNonSpacedToken(node
.consequent
, node
.alternate
, ":");
134 if (nonSpacedConsequentNode
) {
135 report(node
, nonSpacedConsequentNode
);
138 if (nonSpacedAlternateNode
) {
139 report(node
, nonSpacedAlternateNode
);
144 * Check if the node is a variable
145 * @param {ASTNode} node node to evaluate
149 function checkVar(node
) {
150 const leftNode
= (node
.id
.typeAnnotation
) ? node
.id
.typeAnnotation
: node
.id
;
151 const rightNode
= node
.init
;
154 const nonSpacedNode
= getFirstNonSpacedToken(leftNode
, rightNode
, "=");
157 report(node
, nonSpacedNode
);
163 AssignmentExpression
: checkBinary
,
164 AssignmentPattern
: checkBinary
,
165 BinaryExpression
: checkBinary
,
166 LogicalExpression
: checkBinary
,
167 ConditionalExpression
: checkConditional
,
168 VariableDeclarator
: checkVar
,
170 PropertyDefinition(node
) {
176 * Because of computed properties and type annotations, some
177 * tokens may exist between `node.key` and `=`.
178 * Therefore, find the `=` from the right.
180 const operatorToken
= sourceCode
.getTokenBefore(node
.value
, isEqToken
);
181 const leftToken
= sourceCode
.getTokenBefore(operatorToken
);
182 const rightToken
= sourceCode
.getTokenAfter(operatorToken
);
185 !sourceCode
.isSpaceBetweenTokens(leftToken
, operatorToken
) ||
186 !sourceCode
.isSpaceBetweenTokens(operatorToken
, rightToken
)
188 report(node
, operatorToken
);