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 |
|
38 | create(context) {
|
39 | const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
|
40 | const sourceCode = context.getSourceCode();
|
41 |
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function getFirstNonSpacedToken(left, right, op) {
|
51 | const operator = sourceCode.getFirstTokenBetween(left, right, token => token.value === op);
|
52 | const prev = sourceCode.getTokenBefore(operator);
|
53 | const next = sourceCode.getTokenAfter(operator);
|
54 |
|
55 | if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
|
56 | return operator;
|
57 | }
|
58 |
|
59 | return null;
|
60 | }
|
61 |
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function report(mainNode, culpritToken) {
|
70 | context.report({
|
71 | node: mainNode,
|
72 | loc: culpritToken.loc.start,
|
73 | message: "Operator '{{operator}}' must be spaced.",
|
74 | data: {
|
75 | operator: culpritToken.value
|
76 | },
|
77 | fix(fixer) {
|
78 | const previousToken = sourceCode.getTokenBefore(culpritToken);
|
79 | const afterToken = sourceCode.getTokenAfter(culpritToken);
|
80 | let fixString = "";
|
81 |
|
82 | if (culpritToken.range[0] - previousToken.range[1] === 0) {
|
83 | fixString = " ";
|
84 | }
|
85 |
|
86 | fixString += culpritToken.value;
|
87 |
|
88 | if (afterToken.range[0] - culpritToken.range[1] === 0) {
|
89 | fixString += " ";
|
90 | }
|
91 |
|
92 | return fixer.replaceText(culpritToken, fixString);
|
93 | }
|
94 | });
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function checkBinary(node) {
|
104 | const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
|
105 | const rightNode = node.right;
|
106 |
|
107 |
|
108 | const operator = node.operator || "=";
|
109 |
|
110 | const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);
|
111 |
|
112 | if (nonSpacedNode) {
|
113 | if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
|
114 | report(node, nonSpacedNode);
|
115 | }
|
116 | }
|
117 | }
|
118 |
|
119 | |
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function checkConditional(node) {
|
126 | const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
|
127 | const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");
|
128 |
|
129 | if (nonSpacedConsequesntNode) {
|
130 | report(node, nonSpacedConsequesntNode);
|
131 | } else if (nonSpacedAlternateNode) {
|
132 | report(node, nonSpacedAlternateNode);
|
133 | }
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | function checkVar(node) {
|
143 | const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
|
144 | const rightNode = node.init;
|
145 |
|
146 | if (rightNode) {
|
147 | const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");
|
148 |
|
149 | if (nonSpacedNode) {
|
150 | report(node, nonSpacedNode);
|
151 | }
|
152 | }
|
153 | }
|
154 |
|
155 | return {
|
156 | AssignmentExpression: checkBinary,
|
157 | AssignmentPattern: checkBinary,
|
158 | BinaryExpression: checkBinary,
|
159 | LogicalExpression: checkBinary,
|
160 | ConditionalExpression: checkConditional,
|
161 | VariableDeclarator: checkVar
|
162 | };
|
163 |
|
164 | }
|
165 | };
|