@daldalso/eslint-plugin 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/configs/all.js +320 -0
- package/dist/index.js +18 -0
- package/dist/rules/_template.js +17 -0
- package/dist/rules/function-type-annotation-style.js +138 -0
- package/dist/rules/homing-instinct.js +61 -0
- package/dist/rules/iterator-name.js +315 -0
- package/dist/rules/jsx-expression-condition-type.js +76 -0
- package/dist/rules/jsx-indent.js +220 -0
- package/dist/rules/key-quotation-style.js +150 -0
- package/dist/rules/lone-semicolon-indent.js +92 -0
- package/dist/rules/multiline-expression-spacing.js +130 -0
- package/dist/rules/no-class-expression.js +20 -0
- package/dist/rules/no-type-name-affix.js +62 -0
- package/dist/rules/no-unsafe-unquoted-key.js +80 -0
- package/dist/rules/no-useless-template-literal.js +81 -0
- package/dist/rules/one-exported-react-component.js +50 -0
- package/dist/rules/parenthesis-spacing.js +206 -0
- package/dist/rules/prefer-const.js +123 -0
- package/dist/rules/return-type.js +96 -0
- package/dist/rules/semantic-quotes.js +240 -0
- package/dist/rules/sort-keys.js +411 -0
- package/dist/rules/ternary-spacing.js +155 -0
- package/dist/rules/type-colon-spacing.js +128 -0
- package/dist/rules/type-operator-spacing.js +200 -0
- package/dist/rules/variable-name.js +253 -0
- package/dist/utils/code.js +21 -0
- package/dist/utils/patterns.js +16 -0
- package/dist/utils/text.js +15 -0
- package/dist/utils/type.js +82 -0
- package/package.json +40 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
var utils_1 = require("@typescript-eslint/utils");
|
|
13
|
+
var patterns_1 = require("../utils/patterns");
|
|
14
|
+
var text_1 = require("../utils/text");
|
|
15
|
+
var type_1 = require("../utils/type");
|
|
16
|
+
var iterativeMethods = ["map", "reduce", "every", "some", "forEach", "filter", "find", "findIndex"];
|
|
17
|
+
var kindTable = {
|
|
18
|
+
for: ["index"],
|
|
19
|
+
forIn: ["key"],
|
|
20
|
+
forOf: ["value"],
|
|
21
|
+
entries: ["entry", "index"],
|
|
22
|
+
entriesReduce: ["previousValue", "entry", "index"],
|
|
23
|
+
every: ["value", "index"],
|
|
24
|
+
filter: ["value", "index"],
|
|
25
|
+
find: ["value", "index"],
|
|
26
|
+
findIndex: ["value", "index"],
|
|
27
|
+
forEach: ["value", "index"],
|
|
28
|
+
map: ["value", "index"],
|
|
29
|
+
reduce: ["previousValue", "value", "index"],
|
|
30
|
+
some: ["value", "index"]
|
|
31
|
+
};
|
|
32
|
+
exports.default = utils_1.ESLintUtils.RuleCreator.withoutDocs({
|
|
33
|
+
meta: {
|
|
34
|
+
type: "layout",
|
|
35
|
+
messages: {
|
|
36
|
+
'default': "{{index}} iterator name of {{depth}} `{{kind}}` should be `{{criterion}}`."
|
|
37
|
+
},
|
|
38
|
+
schema: [
|
|
39
|
+
{
|
|
40
|
+
type: "object",
|
|
41
|
+
properties: {
|
|
42
|
+
entry: { type: "array", items: { type: "string" } },
|
|
43
|
+
index: { type: "array", items: { type: "string" } },
|
|
44
|
+
key: { type: "array", items: { type: "string" } },
|
|
45
|
+
previousKey: { type: "array", items: { type: "string" } },
|
|
46
|
+
previousValue: { type: "array", items: { type: "string" } },
|
|
47
|
+
value: { type: "array", items: { type: "string" } }
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "object",
|
|
52
|
+
properties: {
|
|
53
|
+
keyListLikeNamePattern: { type: "string" },
|
|
54
|
+
exceptions: { type: "array", items: { type: "string" } }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
defaultOptions: [
|
|
60
|
+
{
|
|
61
|
+
entry: ["e", "f", "g", "h", "i"],
|
|
62
|
+
index: ["i", "j", "k", "l", "m"],
|
|
63
|
+
key: ["k", "l", "m", "n", "o"],
|
|
64
|
+
previousKey: ["pk", "pl", "pm", "pn", "po"],
|
|
65
|
+
previousValue: ["pv", "pw", "px", "py", "pz"],
|
|
66
|
+
value: ["v", "w", "x", "y", "z"]
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
keyListLikeNamePattern: patterns_1.keyListLikeNamePattern.source,
|
|
70
|
+
exceptions: ["_", "__"]
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
create: function (context, _a) {
|
|
74
|
+
var options = _a[0], _b = _a[1], keyListLikeNamePatternString = _b.keyListLikeNamePattern, exceptions = _b.exceptions;
|
|
75
|
+
if (!keyListLikeNamePatternString)
|
|
76
|
+
throw Error("Unhandled keyListLikeNamePatternString: ".concat(keyListLikeNamePatternString));
|
|
77
|
+
var keyListLikeNamePattern = new RegExp(keyListLikeNamePatternString);
|
|
78
|
+
var sourceCode = context.getSourceCode();
|
|
79
|
+
var getIterativeStatementParameters = function (node) {
|
|
80
|
+
var _a;
|
|
81
|
+
var name;
|
|
82
|
+
var list;
|
|
83
|
+
var calleeObject = undefined;
|
|
84
|
+
var keyish = false;
|
|
85
|
+
switch (node.type) {
|
|
86
|
+
case utils_1.AST_NODE_TYPES.ForStatement:
|
|
87
|
+
if (((_a = node.init) === null || _a === void 0 ? void 0 : _a.type) !== utils_1.AST_NODE_TYPES.VariableDeclaration) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (node.init.declarations.length !== 1) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (node.init.declarations[0].id.type !== utils_1.AST_NODE_TYPES.Identifier) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
name = "for";
|
|
97
|
+
list = [node.init.declarations[0].id];
|
|
98
|
+
break;
|
|
99
|
+
case utils_1.AST_NODE_TYPES.ForInStatement:
|
|
100
|
+
case utils_1.AST_NODE_TYPES.ForOfStatement:
|
|
101
|
+
{
|
|
102
|
+
if (node.left.type !== utils_1.AST_NODE_TYPES.VariableDeclaration) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
if (node.left.declarations.length !== 1) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
var leftNode = node.left.declarations[0];
|
|
109
|
+
if (leftNode.id.type !== utils_1.AST_NODE_TYPES.Identifier && leftNode.id.type !== utils_1.AST_NODE_TYPES.ArrayPattern) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
name = node.type === utils_1.AST_NODE_TYPES.ForInStatement ? "forIn" : "forOf";
|
|
113
|
+
if (name === "forOf") {
|
|
114
|
+
if (isKeyListLikeName(node.right)) {
|
|
115
|
+
keyish = true;
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
var iteratorType = (0, type_1.getTSTypeByNode)(context, leftNode);
|
|
119
|
+
if (iteratorType.isUnion() && iteratorType.types.every(function (v) { return v.isStringLiteral(); })) {
|
|
120
|
+
keyish = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
list = [leftNode.id];
|
|
125
|
+
calleeObject = node.right;
|
|
126
|
+
}
|
|
127
|
+
break;
|
|
128
|
+
default: return null;
|
|
129
|
+
}
|
|
130
|
+
return { name: name, keyish: keyish, list: list, calleeObject: calleeObject };
|
|
131
|
+
};
|
|
132
|
+
var getIterativeMethodParameters = function (node) {
|
|
133
|
+
var _a;
|
|
134
|
+
if (node.callee.type !== utils_1.AST_NODE_TYPES.MemberExpression) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
if (node.callee.property.type !== utils_1.AST_NODE_TYPES.Identifier) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
if (((_a = node.arguments[0]) === null || _a === void 0 ? void 0 : _a.type) !== utils_1.AST_NODE_TYPES.ArrowFunctionExpression) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
if (!node.arguments[0].params.every(function (v) { return v.type === utils_1.AST_NODE_TYPES.Identifier
|
|
144
|
+
|| v.type === utils_1.AST_NODE_TYPES.ArrayPattern
|
|
145
|
+
|| v.type === utils_1.AST_NODE_TYPES.ObjectPattern; })) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
if (!(0, type_1.getTSTypeByNode)(context, node.callee.object).getNumberIndexType()) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
if (!iterativeMethods.includes(node.callee.property.name)) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
name: node.callee.property.name,
|
|
156
|
+
keyish: isKeyListLikeName(node.callee.object),
|
|
157
|
+
calleeObject: node.callee.object,
|
|
158
|
+
list: node.arguments[0].params
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
var getCurrentDepth = function (me) {
|
|
162
|
+
var ancestors = context.getAncestors();
|
|
163
|
+
var R = 0;
|
|
164
|
+
for (var i = 0; i < ancestors.length; i++) {
|
|
165
|
+
var v = ancestors[i];
|
|
166
|
+
switch (v.type) {
|
|
167
|
+
case utils_1.AST_NODE_TYPES.CallExpression:
|
|
168
|
+
if (v.callee === (ancestors[i + 1] || me)) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (!getIterativeMethodParameters(v)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
R++;
|
|
175
|
+
break;
|
|
176
|
+
case utils_1.AST_NODE_TYPES.WhileStatement:
|
|
177
|
+
case utils_1.AST_NODE_TYPES.DoWhileStatement:
|
|
178
|
+
if (v.test === (ancestors[i + 1] || me)) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
case utils_1.AST_NODE_TYPES.ForStatement:
|
|
182
|
+
case utils_1.AST_NODE_TYPES.ForInStatement:
|
|
183
|
+
case utils_1.AST_NODE_TYPES.ForOfStatement:
|
|
184
|
+
R++;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return R;
|
|
189
|
+
};
|
|
190
|
+
var getActualName = function (value) {
|
|
191
|
+
if (value.startsWith("$")) {
|
|
192
|
+
return value.slice(1);
|
|
193
|
+
}
|
|
194
|
+
return value;
|
|
195
|
+
};
|
|
196
|
+
var checkParameterNames = function (kind, parameters, depth) {
|
|
197
|
+
var _a, _b;
|
|
198
|
+
var max = Math.min(kind.length, parameters.length);
|
|
199
|
+
for (var i = 0; i < max; i++) {
|
|
200
|
+
var parameter = parameters[i];
|
|
201
|
+
switch (parameter.type) {
|
|
202
|
+
case utils_1.AST_NODE_TYPES.ObjectPattern:
|
|
203
|
+
continue;
|
|
204
|
+
case utils_1.AST_NODE_TYPES.ArrayPattern:
|
|
205
|
+
if (kind[i] !== "entry") {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (((_a = parameter.elements[0]) === null || _a === void 0 ? void 0 : _a.type) !== utils_1.AST_NODE_TYPES.Identifier) {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (((_b = parameter.elements[1]) === null || _b === void 0 ? void 0 : _b.type) !== utils_1.AST_NODE_TYPES.Identifier) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (getActualName(parameter.elements[0].name) === options.key[depth] && getActualName(parameter.elements[1].name) === options.value[depth]) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
context.report({
|
|
218
|
+
node: parameter,
|
|
219
|
+
messageId: "default",
|
|
220
|
+
data: {
|
|
221
|
+
index: "Destructured",
|
|
222
|
+
depth: (0, text_1.toOrdinal)(depth + 1),
|
|
223
|
+
kind: kind[i],
|
|
224
|
+
criterion: "[ ".concat(options.key[depth], ", ").concat(options.value[depth], " ]")
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
break;
|
|
228
|
+
default: {
|
|
229
|
+
var criterion = options[kind[i]][depth];
|
|
230
|
+
if (!criterion) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (exceptions.includes(parameter.name)) {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (getActualName(parameter.name) === criterion) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
context.report({
|
|
240
|
+
node: parameter,
|
|
241
|
+
messageId: "default",
|
|
242
|
+
data: {
|
|
243
|
+
index: (0, text_1.toOrdinal)(i + 1),
|
|
244
|
+
depth: (0, text_1.toOrdinal)(depth + 1),
|
|
245
|
+
kind: kind[i],
|
|
246
|
+
criterion: criterion
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
var isStaticObjectCall = function (node, name) {
|
|
254
|
+
if ((node === null || node === void 0 ? void 0 : node.type) !== utils_1.AST_NODE_TYPES.CallExpression) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (sourceCode.getText(node.callee) !== "Object.".concat(name)) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
};
|
|
262
|
+
var isKeyListLikeName = function (node) {
|
|
263
|
+
switch (node.type) {
|
|
264
|
+
case utils_1.AST_NODE_TYPES.Identifier: return keyListLikeNamePattern.test(node.name);
|
|
265
|
+
case utils_1.AST_NODE_TYPES.CallExpression:
|
|
266
|
+
if (isStaticObjectCall(node, "keys"))
|
|
267
|
+
return true;
|
|
268
|
+
return node.callee.type === utils_1.AST_NODE_TYPES.MemberExpression && isKeyListLikeName(node.callee.object);
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
};
|
|
272
|
+
(0, type_1.useTypeChecker)(context);
|
|
273
|
+
return {
|
|
274
|
+
CallExpression: function (node) {
|
|
275
|
+
var parameters = getIterativeMethodParameters(node);
|
|
276
|
+
if (!parameters) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
var depth = getCurrentDepth(node);
|
|
280
|
+
if (isStaticObjectCall(parameters.calleeObject, "entries")) {
|
|
281
|
+
checkParameterNames(resolveKindTable(parameters.name === "reduce" ? 'entriesReduce' : 'entries', parameters.keyish), parameters.list, depth);
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
checkParameterNames(resolveKindTable(parameters.name, parameters.keyish), parameters.list, depth);
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
'ForStatement, ForInStatement, ForOfStatement': function (node) {
|
|
288
|
+
var parameters = getIterativeStatementParameters(node);
|
|
289
|
+
if (!parameters) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
var depth = getCurrentDepth(node);
|
|
293
|
+
if (parameters.calleeObject && isStaticObjectCall(parameters.calleeObject, "entries")) {
|
|
294
|
+
checkParameterNames(resolveKindTable('entries', parameters.keyish), parameters.list, depth);
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
checkParameterNames(resolveKindTable(parameters.name, parameters.keyish), parameters.list, depth);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
function resolveKindTable(key, keyish) {
|
|
304
|
+
var R = __spreadArray([], kindTable[key], true);
|
|
305
|
+
if (keyish) {
|
|
306
|
+
R = R.map(function (v) {
|
|
307
|
+
switch (v) {
|
|
308
|
+
case "value": return "key";
|
|
309
|
+
case "previousValue": return "previousKey";
|
|
310
|
+
}
|
|
311
|
+
return v;
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
return R;
|
|
315
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
var utils_1 = require("@typescript-eslint/utils");
|
|
31
|
+
var typescript_1 = require("typescript");
|
|
32
|
+
var type_1 = require("../utils/type");
|
|
33
|
+
exports.default = utils_1.ESLintUtils.RuleCreator.withoutDocs({
|
|
34
|
+
meta: {
|
|
35
|
+
type: "suggestion",
|
|
36
|
+
fixable: "code",
|
|
37
|
+
messages: {
|
|
38
|
+
'default': "Left expression of loginal `&&` operations should not be a number or string."
|
|
39
|
+
},
|
|
40
|
+
schema: []
|
|
41
|
+
},
|
|
42
|
+
defaultOptions: [],
|
|
43
|
+
create: function (context) {
|
|
44
|
+
var sourceCode = context.getSourceCode();
|
|
45
|
+
var hasStringOrNumber = function (type) {
|
|
46
|
+
if (type.isUnionOrIntersection()) {
|
|
47
|
+
return type.types.some(function (v) { return hasStringOrNumber(v); });
|
|
48
|
+
}
|
|
49
|
+
var flags = type.getFlags();
|
|
50
|
+
return Boolean(flags & (typescript_1.TypeFlags.StringLike | typescript_1.TypeFlags.NumberLike));
|
|
51
|
+
};
|
|
52
|
+
(0, type_1.useTypeChecker)(context);
|
|
53
|
+
return {
|
|
54
|
+
'JSXExpressionContainer>LogicalExpression[operator="&&"]': function (node) {
|
|
55
|
+
var tsType = (0, type_1.getTSTypeByNode)(context, node.left).getNonNullableType();
|
|
56
|
+
if (!hasStringOrNumber(tsType)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
context.report({
|
|
60
|
+
node: node.left,
|
|
61
|
+
messageId: "default",
|
|
62
|
+
fix: function (fixer) {
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0: return [4 /*yield*/, fixer.replaceText(node.left, "Boolean(".concat(sourceCode.getText(node.left), ")"))];
|
|
66
|
+
case 1:
|
|
67
|
+
_a.sent();
|
|
68
|
+
return [2 /*return*/];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
});
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
4
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
var utils_1 = require("@typescript-eslint/utils");
|
|
31
|
+
var code_1 = require("../utils/code");
|
|
32
|
+
var text_1 = require("../utils/text");
|
|
33
|
+
exports.default = utils_1.ESLintUtils.RuleCreator.withoutDocs({
|
|
34
|
+
meta: {
|
|
35
|
+
type: "layout",
|
|
36
|
+
fixable: "whitespace",
|
|
37
|
+
messages: {
|
|
38
|
+
'in-children-indentation': "Children of a tag should be indented.",
|
|
39
|
+
'in-multiline-tag-closing': "Closing of a tag should not be multiline.",
|
|
40
|
+
'in-tag-closing': "Closing of a single line tag should not be spaced.",
|
|
41
|
+
'in-tag-indentation': "Closing of a multiline tag should appear at the first of its line.",
|
|
42
|
+
'in-tag-opening': "Opening of a tag should not be spaced.",
|
|
43
|
+
'in-tag-self-closing': "Self-closing of a tag should be spaced."
|
|
44
|
+
},
|
|
45
|
+
schema: []
|
|
46
|
+
},
|
|
47
|
+
defaultOptions: [],
|
|
48
|
+
create: function (context) {
|
|
49
|
+
var sourceCode = context.getSourceCode();
|
|
50
|
+
var checkTagOpening = function (tag) {
|
|
51
|
+
var _a, _b;
|
|
52
|
+
var _c;
|
|
53
|
+
var prefix;
|
|
54
|
+
var payload;
|
|
55
|
+
if (tag.type === utils_1.AST_NODE_TYPES.JSXOpeningElement) {
|
|
56
|
+
_a = sourceCode.getFirstTokens(tag, { count: 2 }), prefix = _a[0], payload = _a[1];
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
_b = sourceCode.getFirstTokens(tag, { count: 3 }), prefix = _b[1], payload = _b[2];
|
|
60
|
+
}
|
|
61
|
+
if (!((_c = sourceCode.isSpaceBetween) === null || _c === void 0 ? void 0 : _c.call(sourceCode, prefix, payload))) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
context.report({
|
|
65
|
+
node: prefix,
|
|
66
|
+
messageId: "in-tag-opening",
|
|
67
|
+
fix: function (fixer) {
|
|
68
|
+
return __generator(this, function (_a) {
|
|
69
|
+
switch (_a.label) {
|
|
70
|
+
case 0: return [4 /*yield*/, fixer.removeRange([prefix.range[1], payload.range[0]])];
|
|
71
|
+
case 1:
|
|
72
|
+
_a.sent();
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
var checkTagClosing = function (tag) {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
var selfClosing = tag.type === utils_1.AST_NODE_TYPES.JSXOpeningElement && tag.selfClosing;
|
|
82
|
+
var _c = sourceCode.getLastTokens(tag, {
|
|
83
|
+
count: selfClosing ? 3 : 2
|
|
84
|
+
}), payload = _c[0], next = _c[1];
|
|
85
|
+
if (selfClosing) {
|
|
86
|
+
if ((_a = sourceCode.isSpaceBetween) === null || _a === void 0 ? void 0 : _a.call(sourceCode, payload, next)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
context.report({
|
|
90
|
+
node: next,
|
|
91
|
+
messageId: "in-tag-self-closing",
|
|
92
|
+
fix: function (fixer) {
|
|
93
|
+
return __generator(this, function (_a) {
|
|
94
|
+
switch (_a.label) {
|
|
95
|
+
case 0: return [4 /*yield*/, fixer.insertTextBefore(next, " ")];
|
|
96
|
+
case 1:
|
|
97
|
+
_a.sent();
|
|
98
|
+
return [2 /*return*/];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
if (!((_b = sourceCode.isSpaceBetween) === null || _b === void 0 ? void 0 : _b.call(sourceCode, payload, next))) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
context.report({
|
|
109
|
+
node: next,
|
|
110
|
+
messageId: "in-tag-closing",
|
|
111
|
+
fix: function (fixer) {
|
|
112
|
+
return __generator(this, function (_a) {
|
|
113
|
+
switch (_a.label) {
|
|
114
|
+
case 0: return [4 /*yield*/, fixer.removeRange([payload.range[1], next.range[0]])];
|
|
115
|
+
case 1:
|
|
116
|
+
_a.sent();
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
JSXElement: function (node) {
|
|
126
|
+
if (!node.children.length)
|
|
127
|
+
return;
|
|
128
|
+
var first = sourceCode.getFirstToken(node);
|
|
129
|
+
var openingTagRear = sourceCode.getLastToken(node.openingElement);
|
|
130
|
+
var closingTagFront = node.closingElement && sourceCode.getFirstToken(node.closingElement);
|
|
131
|
+
if (!first || !openingTagRear || !closingTagFront) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
var indentation = (0, code_1.getIndentation)(sourceCode, first.loc.start.line);
|
|
135
|
+
var _loop_1 = function (i) {
|
|
136
|
+
if (!sourceCode.lines[i - 1].trim())
|
|
137
|
+
return "continue";
|
|
138
|
+
var currentIndentation = (0, code_1.getIndentation)(sourceCode, i);
|
|
139
|
+
if (indentation.length < currentIndentation.length)
|
|
140
|
+
return "continue";
|
|
141
|
+
var start = sourceCode.getLocFromIndex(sourceCode.lineStartIndices[i - 1]);
|
|
142
|
+
var end = { line: start.line, column: currentIndentation.length };
|
|
143
|
+
var endIndex = sourceCode.getIndexFromLoc(end);
|
|
144
|
+
context.report({
|
|
145
|
+
loc: { start: start, end: end },
|
|
146
|
+
messageId: "in-children-indentation",
|
|
147
|
+
fix: function (fixer) {
|
|
148
|
+
return __generator(this, function (_a) {
|
|
149
|
+
switch (_a.label) {
|
|
150
|
+
case 0: return [4 /*yield*/, fixer.replaceTextRange([sourceCode.lineStartIndices[i - 1], endIndex], indentation + text_1.INDENTATION_UNIT)];
|
|
151
|
+
case 1:
|
|
152
|
+
_a.sent();
|
|
153
|
+
return [2 /*return*/];
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
for (var i = openingTagRear.loc.end.line + 1; i < closingTagFront.loc.start.line; i++) {
|
|
160
|
+
_loop_1(i);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
JSXOpeningElement: function (node) {
|
|
164
|
+
var isSingleLine = node.loc.start.line === node.loc.end.line;
|
|
165
|
+
checkTagOpening(node);
|
|
166
|
+
if (isSingleLine) {
|
|
167
|
+
checkTagClosing(node);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
var closingBracket_1 = sourceCode.getLastToken(node);
|
|
171
|
+
if (!closingBracket_1)
|
|
172
|
+
return;
|
|
173
|
+
var aIndentation_1 = (0, code_1.getIndentation)(sourceCode, node.loc.start.line);
|
|
174
|
+
var bIndentation = (0, code_1.getIndentation)(sourceCode, closingBracket_1.loc.end.line);
|
|
175
|
+
if (aIndentation_1 !== bIndentation) {
|
|
176
|
+
context.report({
|
|
177
|
+
node: closingBracket_1,
|
|
178
|
+
messageId: "in-tag-indentation",
|
|
179
|
+
fix: function (fixer) {
|
|
180
|
+
var target;
|
|
181
|
+
return __generator(this, function (_a) {
|
|
182
|
+
switch (_a.label) {
|
|
183
|
+
case 0:
|
|
184
|
+
target = node.selfClosing ? sourceCode.getTokenBefore(closingBracket_1) : closingBracket_1;
|
|
185
|
+
return [4 /*yield*/, fixer.insertTextBefore(target, "\n" + aIndentation_1)];
|
|
186
|
+
case 1:
|
|
187
|
+
_a.sent();
|
|
188
|
+
return [2 /*return*/];
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
JSXClosingElement: function (node) {
|
|
197
|
+
var isSingleLine = node.loc.start.line === node.loc.end.line;
|
|
198
|
+
checkTagOpening(node);
|
|
199
|
+
if (isSingleLine) {
|
|
200
|
+
checkTagClosing(node);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
context.report({
|
|
204
|
+
node: node,
|
|
205
|
+
messageId: "in-multiline-tag-closing",
|
|
206
|
+
fix: function (fixer) {
|
|
207
|
+
return __generator(this, function (_a) {
|
|
208
|
+
switch (_a.label) {
|
|
209
|
+
case 0: return [4 /*yield*/, fixer.replaceText(node, sourceCode.getText(node).replace(/\s/g, ""))];
|
|
210
|
+
case 1:
|
|
211
|
+
_a.sent();
|
|
212
|
+
return [2 /*return*/];
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
});
|