1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | module.exports = {
|
12 | meta: {
|
13 | type: "layout",
|
14 |
|
15 | docs: {
|
16 | description: "require spacing around infix operators",
|
17 | category: "Stylistic Issues",
|
18 | recommended: false,
|
19 | url: "https://eslint.org/docs/rules/space-infix-ops"
|
20 | },
|
21 |
|
22 | fixable: "whitespace",
|
23 |
|
24 | schema: [
|
25 | {
|
26 | type: "object",
|
27 | properties: {
|
28 | int32Hint: {
|
29 | type: "boolean",
|
30 | default: false
|
31 | }
|
32 | },
|
33 | additionalProperties: false
|
34 | }
|
35 | ],
|
36 |
|
37 | messages: {
|
38 | missingSpace: "Operator '{{operator}}' must be spaced."
|
39 | }
|
40 | },
|
41 |
|
42 | create(context) {
|
43 | const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
|
44 | const sourceCode = context.getSourceCode();
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
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);
|
58 |
|
59 | if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
|
60 | return operator;
|
61 | }
|
62 |
|
63 | return null;
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function report(mainNode, culpritToken) {
|
74 | context.report({
|
75 | node: mainNode,
|
76 | loc: culpritToken.loc,
|
77 | messageId: "missingSpace",
|
78 | data: {
|
79 | operator: culpritToken.value
|
80 | },
|
81 | fix(fixer) {
|
82 | const previousToken = sourceCode.getTokenBefore(culpritToken);
|
83 | const afterToken = sourceCode.getTokenAfter(culpritToken);
|
84 | let fixString = "";
|
85 |
|
86 | if (culpritToken.range[0] - previousToken.range[1] === 0) {
|
87 | fixString = " ";
|
88 | }
|
89 |
|
90 | fixString += culpritToken.value;
|
91 |
|
92 | if (afterToken.range[0] - culpritToken.range[1] === 0) {
|
93 | fixString += " ";
|
94 | }
|
95 |
|
96 | return fixer.replaceText(culpritToken, fixString);
|
97 | }
|
98 | });
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | function checkBinary(node) {
|
108 | const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
|
109 | const rightNode = node.right;
|
110 |
|
111 |
|
112 | const operator = node.operator || "=";
|
113 |
|
114 | const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);
|
115 |
|
116 | if (nonSpacedNode) {
|
117 | if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
|
118 | report(node, nonSpacedNode);
|
119 | }
|
120 | }
|
121 | }
|
122 |
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function checkConditional(node) {
|
130 | const nonSpacedConsequentNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
|
131 | const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");
|
132 |
|
133 | if (nonSpacedConsequentNode) {
|
134 | report(node, nonSpacedConsequentNode);
|
135 | }
|
136 |
|
137 | if (nonSpacedAlternateNode) {
|
138 | report(node, nonSpacedAlternateNode);
|
139 | }
|
140 | }
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function checkVar(node) {
|
149 | const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
|
150 | const rightNode = node.init;
|
151 |
|
152 | if (rightNode) {
|
153 | const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");
|
154 |
|
155 | if (nonSpacedNode) {
|
156 | report(node, nonSpacedNode);
|
157 | }
|
158 | }
|
159 | }
|
160 |
|
161 | return {
|
162 | AssignmentExpression: checkBinary,
|
163 | AssignmentPattern: checkBinary,
|
164 | BinaryExpression: checkBinary,
|
165 | LogicalExpression: checkBinary,
|
166 | ConditionalExpression: checkConditional,
|
167 | VariableDeclarator: checkVar
|
168 | };
|
169 |
|
170 | }
|
171 | };
|