xshell-lint 1.0.2
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/index.d.ts +3 -0
- package/index.js +1085 -0
- package/package.json +27 -0
- package/tsconfig.json +59 -0
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,1085 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { builtinModules } from 'node:module';
|
|
3
|
+
import { AST_TOKEN_TYPES, AST_NODE_TYPES, ASTUtils } from '@typescript-eslint/utils';
|
|
4
|
+
import TSParser from '@typescript-eslint/parser';
|
|
5
|
+
import ts_plugin from '@typescript-eslint/eslint-plugin';
|
|
6
|
+
import react_plugin from 'eslint-plugin-react';
|
|
7
|
+
import stylistic_plugin from '@stylistic/eslint-plugin';
|
|
8
|
+
import { array_equals, check, path } from 'xshell';
|
|
9
|
+
// 用 ast explorer 选 esprima 来看
|
|
10
|
+
// 或者 https://explorer.eslint.org/
|
|
11
|
+
// node.loc.start.line 下标从 1 开始
|
|
12
|
+
const meta = {
|
|
13
|
+
fixable: 'whitespace',
|
|
14
|
+
type: 'layout'
|
|
15
|
+
};
|
|
16
|
+
export const xlint_plugin = {
|
|
17
|
+
rules: {
|
|
18
|
+
'fold-jsdoc-comments': {
|
|
19
|
+
meta,
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
create: (context) => ({
|
|
22
|
+
Program() {
|
|
23
|
+
for (const comment of context.sourceCode.ast.comments)
|
|
24
|
+
if (comment.type === 'Block') {
|
|
25
|
+
const lines = comment.value.split(line_break_pattern);
|
|
26
|
+
if (lines.length >= 3 &&
|
|
27
|
+
/^\*\s*$/u.test(lines[0]) &&
|
|
28
|
+
lines.slice(1, -1).every(line => /^\s*\*/u.test(line)) &&
|
|
29
|
+
/^[\s\*]*$/u.test(lines.at(-1)))
|
|
30
|
+
context.report({
|
|
31
|
+
loc: {
|
|
32
|
+
start: comment.loc.start,
|
|
33
|
+
end: comment.loc.end
|
|
34
|
+
},
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
message: 'jsdoc 注释格式错误',
|
|
37
|
+
fix(fixer) {
|
|
38
|
+
const indent = ' '.repeat(comment.loc.start.column);
|
|
39
|
+
const indenter = (line) => line.replace(/^\s*\*\s?/u, `${indent} `);
|
|
40
|
+
return fixer.replaceTextRange(comment.range, lines.length === 3 ?
|
|
41
|
+
`/** ${lines[1].replace(/^\s*\*\s*/u, '')} */`
|
|
42
|
+
:
|
|
43
|
+
[
|
|
44
|
+
`/** ${lines[1].replace(/^\s*\*\s?/u, '')}`,
|
|
45
|
+
...lines.slice(2, -2).map(indenter),
|
|
46
|
+
`${indenter(lines.at(-2))} */`
|
|
47
|
+
].join('\n'));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
},
|
|
54
|
+
// 一条语句作为 body 时强制换行缩进
|
|
55
|
+
// if (1) body
|
|
56
|
+
// 更改为
|
|
57
|
+
// if (1)
|
|
58
|
+
// body
|
|
59
|
+
'nonblock-statement-body-position-with-indentation': {
|
|
60
|
+
meta,
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
create(context) {
|
|
63
|
+
const source = context.sourceCode;
|
|
64
|
+
const lines = source.getLines();
|
|
65
|
+
function lint(body, node) {
|
|
66
|
+
if (body.type !== 'BlockStatement') {
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
const token_before = source.getTokenBefore(body);
|
|
69
|
+
let indent = 0;
|
|
70
|
+
const line = lines[node.loc.start.line - 1];
|
|
71
|
+
for (let i = 0; i < line.length; i++)
|
|
72
|
+
if (line[i] === ' ')
|
|
73
|
+
indent++;
|
|
74
|
+
else if (line[i] === '\t')
|
|
75
|
+
indent += 4;
|
|
76
|
+
else
|
|
77
|
+
break;
|
|
78
|
+
if (token_before.loc.end.line === body.loc.start.line ||
|
|
79
|
+
body.loc.start.column !== indent + 4)
|
|
80
|
+
context.report({
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
node: body,
|
|
83
|
+
message: 'Expected a linebreak before this statement.',
|
|
84
|
+
fix: fixer => fixer.replaceTextRange([token_before.range[1], body.range[0]], '\n' + ' '.repeat(indent + 4))
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
IfStatement(node) {
|
|
90
|
+
lint(node.consequent, node);
|
|
91
|
+
// Check the `else` node, but don't check 'else if' statements.
|
|
92
|
+
if (node.alternate && node.alternate.type !== 'IfStatement')
|
|
93
|
+
lint(node.alternate, node);
|
|
94
|
+
},
|
|
95
|
+
WhileStatement: node => { lint(node.body, node); },
|
|
96
|
+
DoWhileStatement: node => { lint(node.body, node); },
|
|
97
|
+
ForStatement: node => { lint(node.body, node); },
|
|
98
|
+
ForInStatement: node => { lint(node.body, node); },
|
|
99
|
+
ForOfStatement: node => { lint(node.body, node); }
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
// let o = { }
|
|
104
|
+
// let a = [ ]
|
|
105
|
+
// function foo () { }
|
|
106
|
+
'empty-bracket-spacing': {
|
|
107
|
+
meta,
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
create(context) {
|
|
110
|
+
const source = context.sourceCode;
|
|
111
|
+
return {
|
|
112
|
+
ObjectExpression(node) {
|
|
113
|
+
// 是 {} 这样的空对象
|
|
114
|
+
if (node.properties.length === 0 && source.getText(node) === '{}')
|
|
115
|
+
context.report({
|
|
116
|
+
node,
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
message: 'Expected a space in this empty object.',
|
|
119
|
+
fix: fixer => fixer.replaceText(node, '{ }')
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
ArrayExpression(node) {
|
|
123
|
+
// 是 [] 这样的空数组
|
|
124
|
+
if (node.elements.length === 0 && source.getText(node) === '[]')
|
|
125
|
+
context.report({
|
|
126
|
+
node,
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
message: 'Expected a space in this empty array.',
|
|
129
|
+
fix: fixer => fixer.replaceText(node, '[ ]')
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
BlockStatement(node) {
|
|
133
|
+
if (node.body.length === 0 && source.getText(node) === '{}')
|
|
134
|
+
context.report({
|
|
135
|
+
node,
|
|
136
|
+
// @ts-ignore
|
|
137
|
+
message: 'Expected a space in this empty block.',
|
|
138
|
+
fix: fixer => fixer.replaceText(node, '{ }')
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
// a + b**c
|
|
145
|
+
'space-infix-ops-except-exponentiation': {
|
|
146
|
+
meta: {
|
|
147
|
+
...meta,
|
|
148
|
+
messages: {
|
|
149
|
+
missingSpace: "Operator '{{operator}}' must be spaced.",
|
|
150
|
+
redundantSpace: "Operator '{{operator}}' must not be spaced."
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
// @ts-ignore
|
|
154
|
+
create(context) {
|
|
155
|
+
const int32Hint = true;
|
|
156
|
+
const { sourceCode } = context;
|
|
157
|
+
/** Returns the first token which violates the rule
|
|
158
|
+
@param {ASTNode} left The left node of the main node
|
|
159
|
+
@param {ASTNode} right The right node of the main node
|
|
160
|
+
@param {string} op The operator of the main node
|
|
161
|
+
@returns {Object} The violator token or null
|
|
162
|
+
@private */
|
|
163
|
+
function getFirstNonSpacedToken(left, right, op) {
|
|
164
|
+
const operator = sourceCode.getFirstTokenBetween(left, right, token => token.value === op);
|
|
165
|
+
const prev = sourceCode.getTokenBefore(operator);
|
|
166
|
+
const next = sourceCode.getTokenAfter(operator);
|
|
167
|
+
if (!sourceCode.isSpaceBetween(prev, operator) || !sourceCode.isSpaceBetween(operator, next))
|
|
168
|
+
return operator;
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
Reports an AST node as a rule violation
|
|
173
|
+
@param {ASTNode} mainNode The node to report
|
|
174
|
+
@param {Object} culpritToken The token which has a problem
|
|
175
|
+
@returns {void}
|
|
176
|
+
@private
|
|
177
|
+
*/
|
|
178
|
+
function report(mainNode, culpritToken) {
|
|
179
|
+
context.report({
|
|
180
|
+
node: mainNode,
|
|
181
|
+
loc: culpritToken.loc,
|
|
182
|
+
messageId: 'missingSpace',
|
|
183
|
+
data: {
|
|
184
|
+
operator: culpritToken.value
|
|
185
|
+
},
|
|
186
|
+
fix(fixer) {
|
|
187
|
+
const previousToken = sourceCode.getTokenBefore(culpritToken);
|
|
188
|
+
const afterToken = sourceCode.getTokenAfter(culpritToken);
|
|
189
|
+
let fixString = '';
|
|
190
|
+
if (culpritToken.range[0] - previousToken.range[1] === 0)
|
|
191
|
+
fixString = ' ';
|
|
192
|
+
fixString += culpritToken.value;
|
|
193
|
+
if (afterToken.range[0] - culpritToken.range[1] === 0)
|
|
194
|
+
fixString += ' ';
|
|
195
|
+
return fixer.replaceText(culpritToken, fixString);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
Check if the node is binary then report
|
|
201
|
+
@param {ASTNode} node node to evaluate
|
|
202
|
+
@returns {void}
|
|
203
|
+
@private
|
|
204
|
+
*/
|
|
205
|
+
function checkBinary(node) {
|
|
206
|
+
const leftNode = node.left.typeAnnotation ? node.left.typeAnnotation : node.left;
|
|
207
|
+
const rightNode = node.right;
|
|
208
|
+
// search for = in AssignmentPattern nodes
|
|
209
|
+
const operator = node.operator || '=';
|
|
210
|
+
// 找到 ** operator
|
|
211
|
+
if (operator === '**') {
|
|
212
|
+
const op = sourceCode.getFirstTokenBetween(leftNode, rightNode, token => token.value === operator);
|
|
213
|
+
const prev = sourceCode.getTokenBefore(op);
|
|
214
|
+
const next = sourceCode.getTokenAfter(op);
|
|
215
|
+
if (sourceCode.isSpaceBetween(prev, op) || sourceCode.isSpaceBetween(op, next))
|
|
216
|
+
context.report({
|
|
217
|
+
node,
|
|
218
|
+
loc: op.loc,
|
|
219
|
+
messageId: 'redundantSpace',
|
|
220
|
+
data: {
|
|
221
|
+
operator: op.value
|
|
222
|
+
},
|
|
223
|
+
fix(fixer) {
|
|
224
|
+
const previousToken = sourceCode.getTokenBefore(op);
|
|
225
|
+
const afterToken = sourceCode.getTokenAfter(op);
|
|
226
|
+
let fixes = [];
|
|
227
|
+
if (op.range[0] > previousToken.range[1])
|
|
228
|
+
fixes.push(
|
|
229
|
+
// @ts-ignore
|
|
230
|
+
fixer.removeRange([previousToken.range[1], op.range[0]]));
|
|
231
|
+
if (afterToken.range[0] > op.range[1])
|
|
232
|
+
fixes.push(
|
|
233
|
+
// @ts-ignore
|
|
234
|
+
fixer.removeRange([op.range[1], afterToken.range[0]]));
|
|
235
|
+
return fixes;
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);
|
|
241
|
+
if (nonSpacedNode)
|
|
242
|
+
if (!(int32Hint && sourceCode.getText(node).endsWith('|0')))
|
|
243
|
+
report(node, nonSpacedNode);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
Check if the node is conditional
|
|
248
|
+
@param {ASTNode} node node to evaluate
|
|
249
|
+
@returns {void}
|
|
250
|
+
@private
|
|
251
|
+
*/
|
|
252
|
+
function checkConditional(node) {
|
|
253
|
+
const nonSpacedConsequentNode = getFirstNonSpacedToken(node.test, node.consequent, '?');
|
|
254
|
+
const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ':');
|
|
255
|
+
if (nonSpacedConsequentNode)
|
|
256
|
+
report(node, nonSpacedConsequentNode);
|
|
257
|
+
if (nonSpacedAlternateNode)
|
|
258
|
+
report(node, nonSpacedAlternateNode);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
Check if the node is a variable
|
|
262
|
+
@param {ASTNode} node node to evaluate
|
|
263
|
+
@returns {void}
|
|
264
|
+
@private
|
|
265
|
+
*/
|
|
266
|
+
function checkVar(node) {
|
|
267
|
+
const leftNode = node.id.typeAnnotation ? node.id.typeAnnotation : node.id;
|
|
268
|
+
const rightNode = node.init;
|
|
269
|
+
if (rightNode) {
|
|
270
|
+
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, '=');
|
|
271
|
+
if (nonSpacedNode)
|
|
272
|
+
report(node, nonSpacedNode);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function ts_report(operator) {
|
|
276
|
+
context.report({
|
|
277
|
+
node: operator,
|
|
278
|
+
messageId: 'missingSpace',
|
|
279
|
+
data: {
|
|
280
|
+
operator: operator.value
|
|
281
|
+
},
|
|
282
|
+
fix(fixer) {
|
|
283
|
+
const previousToken = sourceCode.getTokenBefore(operator);
|
|
284
|
+
const afterToken = sourceCode.getTokenAfter(operator);
|
|
285
|
+
let fixString = '';
|
|
286
|
+
if (operator.range[0] - previousToken.range[1] === 0)
|
|
287
|
+
fixString = ' ';
|
|
288
|
+
fixString += operator.value;
|
|
289
|
+
if (afterToken.range[0] - operator.range[1] === 0)
|
|
290
|
+
fixString += ' ';
|
|
291
|
+
return fixer.replaceText(operator, fixString);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function isSpaceChar(token) {
|
|
296
|
+
return token.type === AST_TOKEN_TYPES.Punctuator && /^[=?:]$/.test(token.value);
|
|
297
|
+
}
|
|
298
|
+
function checkAndReportAssignmentSpace(leftNode, rightNode) {
|
|
299
|
+
if (!rightNode || !leftNode)
|
|
300
|
+
return;
|
|
301
|
+
const operator = sourceCode.getFirstTokenBetween(leftNode, rightNode, isSpaceChar);
|
|
302
|
+
const prev = sourceCode.getTokenBefore(operator);
|
|
303
|
+
const next = sourceCode.getTokenAfter(operator);
|
|
304
|
+
if (!sourceCode.isSpaceBetween(prev, operator) || !sourceCode.isSpaceBetween(operator, next))
|
|
305
|
+
ts_report(operator);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
Check if it has an assignment char and report if it's faulty
|
|
309
|
+
@param node The node to report
|
|
310
|
+
*/
|
|
311
|
+
function checkForEnumAssignmentSpace(node) {
|
|
312
|
+
checkAndReportAssignmentSpace(node.id, node.initializer);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
Check if it has an assignment char and report if it's faulty
|
|
316
|
+
@param node The node to report
|
|
317
|
+
*/
|
|
318
|
+
function checkForPropertyDefinitionAssignmentSpace(node) {
|
|
319
|
+
const leftNode = node.optional && !node.typeAnnotation ? sourceCode.getTokenAfter(node.key) : node.typeAnnotation ?? node.key;
|
|
320
|
+
checkAndReportAssignmentSpace(leftNode, node.value);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
Check if it is missing spaces between type annotations chaining
|
|
324
|
+
@param typeAnnotation TypeAnnotations list
|
|
325
|
+
*/
|
|
326
|
+
function checkForTypeAnnotationSpace(typeAnnotation) {
|
|
327
|
+
const types = typeAnnotation.types;
|
|
328
|
+
types.forEach(type => {
|
|
329
|
+
const skipFunctionParenthesis = type.type === AST_NODE_TYPES.TSFunctionType ? ASTUtils.isNotOpeningBraceToken : 0;
|
|
330
|
+
const operator = sourceCode.getTokenBefore(type, skipFunctionParenthesis);
|
|
331
|
+
if (operator && unions.includes(operator.value)) {
|
|
332
|
+
const prev = sourceCode.getTokenBefore(operator);
|
|
333
|
+
const next = sourceCode.getTokenAfter(operator);
|
|
334
|
+
if (!sourceCode.isSpaceBetween(prev, operator) || !sourceCode.isSpaceBetween(operator, next))
|
|
335
|
+
ts_report(operator);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
Check if it has an assignment char and report if it's faulty
|
|
341
|
+
@param node The node to report
|
|
342
|
+
*/
|
|
343
|
+
function checkForTypeAliasAssignment(node) {
|
|
344
|
+
checkAndReportAssignmentSpace(node.typeParameters ?? node.id, node.typeAnnotation);
|
|
345
|
+
}
|
|
346
|
+
function checkForTypeConditional(node) {
|
|
347
|
+
checkAndReportAssignmentSpace(node.extendsType, node.trueType);
|
|
348
|
+
checkAndReportAssignmentSpace(node.trueType, node.falseType);
|
|
349
|
+
}
|
|
350
|
+
return {
|
|
351
|
+
AssignmentExpression: checkBinary,
|
|
352
|
+
AssignmentPattern: checkBinary,
|
|
353
|
+
BinaryExpression: checkBinary,
|
|
354
|
+
LogicalExpression: checkBinary,
|
|
355
|
+
ConditionalExpression: checkConditional,
|
|
356
|
+
VariableDeclarator: checkVar,
|
|
357
|
+
TSEnumMember: checkForEnumAssignmentSpace,
|
|
358
|
+
PropertyDefinition: checkForPropertyDefinitionAssignmentSpace,
|
|
359
|
+
TSTypeAliasDeclaration: checkForTypeAliasAssignment,
|
|
360
|
+
TSUnionType: checkForTypeAnnotationSpace,
|
|
361
|
+
TSIntersectionType: checkForTypeAnnotationSpace,
|
|
362
|
+
TSConditionalType: checkForTypeConditional
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
},
|
|
366
|
+
// for (;;)
|
|
367
|
+
// 1
|
|
368
|
+
// for (let i = 0; i < 10; i++)
|
|
369
|
+
// for ( ; i < 10; )
|
|
370
|
+
'space-in-for-statement': {
|
|
371
|
+
meta,
|
|
372
|
+
// @ts-ignore
|
|
373
|
+
create(context) {
|
|
374
|
+
const source = context.sourceCode;
|
|
375
|
+
return {
|
|
376
|
+
ForStatement(node) {
|
|
377
|
+
const paren_left = source.getFirstToken(node, token => token.value === '(');
|
|
378
|
+
const semi0 = source.getTokenAfter(node.init || paren_left);
|
|
379
|
+
const semi1 = source.getTokenAfter(node.test || semi0);
|
|
380
|
+
const semi0_prev = source.getTokenBefore(semi0, { includeComments: true });
|
|
381
|
+
const semi0_next = source.getTokenAfter(semi0, { includeComments: true });
|
|
382
|
+
const semi1_next = source.getTokenAfter(semi1, { includeComments: true });
|
|
383
|
+
if (node.init || node.test || node.update) {
|
|
384
|
+
// for (let i = 0; i < 10; i++)
|
|
385
|
+
// for ( ; i < 10; )
|
|
386
|
+
// for ( ; ; i++)
|
|
387
|
+
// for (let i = 0;)
|
|
388
|
+
if (node.init) {
|
|
389
|
+
if (semi0.loc.start.line === semi0_prev.loc.end.line &&
|
|
390
|
+
semi0.loc.start.column !== semi0_prev.loc.end.column)
|
|
391
|
+
context.report({
|
|
392
|
+
loc: {
|
|
393
|
+
start: semi0_prev.loc.end,
|
|
394
|
+
end: semi0.loc.start
|
|
395
|
+
},
|
|
396
|
+
// @ts-ignore
|
|
397
|
+
message: 'for 语句第一个分号前面应该没有多余空格',
|
|
398
|
+
node,
|
|
399
|
+
fix: fixer => fixer.removeRange([semi0_prev.range[1], semi0.range[0]])
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
else // for ( ;)
|
|
403
|
+
if (semi0.loc.start.line === semi0_prev.loc.end.line &&
|
|
404
|
+
semi0.loc.start.column - semi0_prev.loc.end.column !== 2)
|
|
405
|
+
context.report({
|
|
406
|
+
loc: {
|
|
407
|
+
start: semi0_prev.loc.end,
|
|
408
|
+
end: semi0.loc.start
|
|
409
|
+
},
|
|
410
|
+
// @ts-ignore
|
|
411
|
+
message: 'for 语句第一个分号前面应该为 2 个空格',
|
|
412
|
+
node,
|
|
413
|
+
fix: fixer => fixer.replaceTextRange([semi0_prev.range[1], semi0.range[0]], ' ')
|
|
414
|
+
});
|
|
415
|
+
// for ( ; ;)
|
|
416
|
+
if (semi0_next.loc.start.line === semi0.loc.end.line &&
|
|
417
|
+
semi0_next.loc.start.column - semi0.loc.end.column !== 2)
|
|
418
|
+
context.report({
|
|
419
|
+
loc: {
|
|
420
|
+
start: semi0.loc.end,
|
|
421
|
+
end: semi0_next.loc.start
|
|
422
|
+
},
|
|
423
|
+
// @ts-ignore
|
|
424
|
+
message: 'for 语句第一个分号后面应为 2 个空格',
|
|
425
|
+
node,
|
|
426
|
+
fix: fixer => fixer.replaceTextRange([semi0.range[1], semi0_next.range[0]], ' ')
|
|
427
|
+
});
|
|
428
|
+
// // for ( ; i > 0;)
|
|
429
|
+
if (node.test) {
|
|
430
|
+
const test_end = source.getLastToken(node.test);
|
|
431
|
+
if (semi1.loc.start.line === test_end.loc.end.line &&
|
|
432
|
+
semi1.loc.start.column !== test_end.loc.end.column)
|
|
433
|
+
context.report({
|
|
434
|
+
loc: {
|
|
435
|
+
start: test_end.loc.end,
|
|
436
|
+
end: semi1.loc.start,
|
|
437
|
+
},
|
|
438
|
+
// @ts-ignore
|
|
439
|
+
message: 'for 语句第二个分号前面没有多余的空格',
|
|
440
|
+
node,
|
|
441
|
+
fix: fixer => fixer.removeRange([test_end.range[1], semi1.range[0]])
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
if (semi1_next.loc.start.line === semi1.loc.end.line &&
|
|
445
|
+
semi1_next.loc.start.column - semi1.loc.end.column !== 2)
|
|
446
|
+
context.report({
|
|
447
|
+
loc: {
|
|
448
|
+
start: semi1.loc.end,
|
|
449
|
+
end: semi1_next.loc.start
|
|
450
|
+
},
|
|
451
|
+
// @ts-ignore
|
|
452
|
+
message: 'for 语句第二个分号后面应为 2 个空格',
|
|
453
|
+
node,
|
|
454
|
+
fix: fixer => fixer.replaceTextRange([semi1.range[1], semi1_next.range[0]], ' ')
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
else { // for (;;)
|
|
458
|
+
if (semi0.loc.start.line === semi0_prev.loc.end.line &&
|
|
459
|
+
semi0.loc.start.column !== semi0_prev.loc.end.column)
|
|
460
|
+
context.report({
|
|
461
|
+
loc: {
|
|
462
|
+
start: semi0_prev.loc.end,
|
|
463
|
+
end: semi0.loc.start
|
|
464
|
+
},
|
|
465
|
+
// @ts-ignore
|
|
466
|
+
message: '无条件 for 语句第一个分号前面应该没有空格',
|
|
467
|
+
node,
|
|
468
|
+
fix: fixer => fixer.removeRange([semi0_prev.range[1], semi0.range[0]])
|
|
469
|
+
});
|
|
470
|
+
if (semi0_next.loc.start.line === semi0.loc.end.line &&
|
|
471
|
+
semi0_next.loc.start.column !== semi0.loc.end.column)
|
|
472
|
+
context.report({
|
|
473
|
+
loc: {
|
|
474
|
+
start: semi0.loc.end,
|
|
475
|
+
end: semi0_next.loc.start
|
|
476
|
+
},
|
|
477
|
+
// @ts-ignore
|
|
478
|
+
message: '无条件 for 语句第一个分号后面应该没有空格',
|
|
479
|
+
node,
|
|
480
|
+
fix: fixer => fixer.removeRange([semi0.range[1], semi0_next.range[0]])
|
|
481
|
+
});
|
|
482
|
+
if (semi1_next.loc.start.line === semi1.loc.end.line &&
|
|
483
|
+
semi1_next.loc.start.column !== semi1.loc.end.column)
|
|
484
|
+
context.report({
|
|
485
|
+
loc: {
|
|
486
|
+
start: semi1.loc.end,
|
|
487
|
+
end: semi1_next.loc.start
|
|
488
|
+
},
|
|
489
|
+
// @ts-ignore
|
|
490
|
+
message: '无条件 for 语句第二个分号后面应该没有空格',
|
|
491
|
+
node,
|
|
492
|
+
fix: fixer => fixer.removeRange([semi1.range[1], semi1_next.range[0]])
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
// return (
|
|
500
|
+
// <div>1234</div>
|
|
501
|
+
// )
|
|
502
|
+
// 改为
|
|
503
|
+
// return <div>1234</div>
|
|
504
|
+
'jsx-no-redundant-parenthesis-in-return': {
|
|
505
|
+
meta,
|
|
506
|
+
// @ts-ignore
|
|
507
|
+
create(context) {
|
|
508
|
+
const source = context.sourceCode;
|
|
509
|
+
return {
|
|
510
|
+
ReturnStatement(node) {
|
|
511
|
+
if (node.argument?.type === 'JSXElement' || node.argument?.type === 'JSXFragment') {
|
|
512
|
+
const paren0 = source.getTokenBefore(node.argument);
|
|
513
|
+
const paren1 = source.getTokenAfter(node.argument);
|
|
514
|
+
if (paren0.type === 'Punctuator' && paren0.value === '(' &&
|
|
515
|
+
paren1.type === 'Punctuator' && paren1.value === ')') {
|
|
516
|
+
const paren0_next = source.getTokenAfter(paren0);
|
|
517
|
+
const paren1_prev = source.getTokenBefore(paren1);
|
|
518
|
+
context.report({
|
|
519
|
+
loc: {
|
|
520
|
+
start: paren0.loc.start,
|
|
521
|
+
end: paren1.loc.end
|
|
522
|
+
},
|
|
523
|
+
// @ts-ignore
|
|
524
|
+
message: 'return jsx 表达式不应该有多余的括号',
|
|
525
|
+
node,
|
|
526
|
+
fix: fixer => ([
|
|
527
|
+
fixer.removeRange([paren0.range[0], paren0_next.range[0]]),
|
|
528
|
+
fixer.removeRange([paren1_prev.range[1], paren1.range[1]])
|
|
529
|
+
])
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
},
|
|
534
|
+
ArrowFunctionExpression(node) {
|
|
535
|
+
if (node.body?.type === 'JSXElement' || node.body?.type === 'JSXFragment') {
|
|
536
|
+
const paren0 = source.getTokenBefore(node.body);
|
|
537
|
+
const paren1 = source.getTokenAfter(node.body);
|
|
538
|
+
if (paren0.type === 'Punctuator' && paren0.value === '(' &&
|
|
539
|
+
paren1.type === 'Punctuator' && paren1.value === ')') {
|
|
540
|
+
const paren0_next = source.getTokenAfter(paren0);
|
|
541
|
+
const paren1_prev = source.getTokenBefore(paren1);
|
|
542
|
+
context.report({
|
|
543
|
+
loc: {
|
|
544
|
+
start: paren0.loc.start,
|
|
545
|
+
end: paren1.loc.end
|
|
546
|
+
},
|
|
547
|
+
// @ts-ignore
|
|
548
|
+
message: 'return jsx 表达式不应该有多余的括号',
|
|
549
|
+
node,
|
|
550
|
+
fix: fixer => ([
|
|
551
|
+
fixer.removeRange([paren0.range[0], paren0_next.range[0]]),
|
|
552
|
+
fixer.removeRange([paren1_prev.range[1], paren1.range[1]])
|
|
553
|
+
])
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
},
|
|
561
|
+
'keep-indent': {
|
|
562
|
+
meta,
|
|
563
|
+
// @ts-ignore
|
|
564
|
+
create(context) {
|
|
565
|
+
const source = context.sourceCode;
|
|
566
|
+
const lines = source.getLines();
|
|
567
|
+
return {
|
|
568
|
+
Program() {
|
|
569
|
+
// 参考 keep_indent 逻辑
|
|
570
|
+
if (lines.length >= 2) {
|
|
571
|
+
let last_line = { indent: 0, text: '' };
|
|
572
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
573
|
+
const _line = split_indent(lines[i]);
|
|
574
|
+
if (!_line.indent && !_line.text && last_line.indent) {
|
|
575
|
+
const pos = { column: 0, line: i + 1 };
|
|
576
|
+
const index = source.getIndexFromLoc(pos);
|
|
577
|
+
const node = source.getNodeByRangeIndex(index);
|
|
578
|
+
if (node && node.type !== 'Program')
|
|
579
|
+
context.report({
|
|
580
|
+
// @ts-ignore
|
|
581
|
+
message: '空行应保留与上一个区块相同的缩进',
|
|
582
|
+
loc: { start: pos, end: pos },
|
|
583
|
+
fix: fixer => fixer.replaceTextRange([index, index], ' '.repeat(last_line.indent))
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
last_line = _line;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
'func-style': {
|
|
594
|
+
meta,
|
|
595
|
+
// @ts-ignore
|
|
596
|
+
create(context) {
|
|
597
|
+
const source = context.sourceCode;
|
|
598
|
+
let stack = [];
|
|
599
|
+
const fix = (fixer, node) => fixer.replaceText(
|
|
600
|
+
// @ts-ignore
|
|
601
|
+
node.parent.parent, `${node.async ? 'async ' : ''}function ${node.generator ? '* ' : ''}` +
|
|
602
|
+
// todo: 丢失了泛型参数 const foo = <D = Zippable> (xxx: D) => { },如何加回来?
|
|
603
|
+
node.parent.id.name +
|
|
604
|
+
' (' +
|
|
605
|
+
// @ts-ignore
|
|
606
|
+
node.params.map(param => source.getText(param)).join(', ') +
|
|
607
|
+
') ' +
|
|
608
|
+
// @ts-ignore
|
|
609
|
+
source.getText(node.body));
|
|
610
|
+
return {
|
|
611
|
+
FunctionDeclaration(node) {
|
|
612
|
+
stack.push(false);
|
|
613
|
+
},
|
|
614
|
+
'FunctionDeclaration:exit'() {
|
|
615
|
+
stack.pop();
|
|
616
|
+
},
|
|
617
|
+
FunctionExpression(node) {
|
|
618
|
+
stack.push(false);
|
|
619
|
+
if (node.parent.type === 'VariableDeclarator' &&
|
|
620
|
+
// 忽略 let a = 1, b = function () { } 这样的情况
|
|
621
|
+
node.parent.parent.declarations.length === 1)
|
|
622
|
+
context.report({
|
|
623
|
+
node: node.parent.parent,
|
|
624
|
+
// @ts-ignore
|
|
625
|
+
message: '函数应该使用 function foo { } 这样来声明而不是 const foo = function () { }',
|
|
626
|
+
loc: node.parent.parent.loc,
|
|
627
|
+
fix: fixer => fix(fixer, node)
|
|
628
|
+
});
|
|
629
|
+
},
|
|
630
|
+
'FunctionExpression:exit'() {
|
|
631
|
+
stack.pop();
|
|
632
|
+
},
|
|
633
|
+
ThisExpression() {
|
|
634
|
+
if (stack.length > 0)
|
|
635
|
+
stack[stack.length - 1] = true;
|
|
636
|
+
},
|
|
637
|
+
ArrowFunctionExpression() {
|
|
638
|
+
stack.push(false);
|
|
639
|
+
},
|
|
640
|
+
'ArrowFunctionExpression:exit'(node) {
|
|
641
|
+
if (!stack.pop() && // not has this expr
|
|
642
|
+
(node.parent.type === 'VariableDeclarator' &&
|
|
643
|
+
// 忽略 let a = 1, b = () => { } 这样的情况
|
|
644
|
+
node.parent.parent.declarations.length === 1) &&
|
|
645
|
+
// 多条语句才转换
|
|
646
|
+
node.body.type === 'BlockStatement' && node.body.body.length)
|
|
647
|
+
context.report({
|
|
648
|
+
node: node.parent.parent,
|
|
649
|
+
// @ts-ignore
|
|
650
|
+
message: '多条语句的函数应该使用 function foo { } 这样来声明而不是 const foo = () => { }',
|
|
651
|
+
loc: node.parent.parent.loc,
|
|
652
|
+
fix: fixer => fix(fixer, node)
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
},
|
|
658
|
+
// 读取文件中所有的 ImportDeclaration,每个 decl 包括前面的注释,收集起来
|
|
659
|
+
// 检查这些 decl 是连续的,即中间没有其他语句(中间有语句分割的,需要手动修复)
|
|
660
|
+
// 检查、解析 decl 中的 source (from '...' 中的 ...) 做解析和替换:
|
|
661
|
+
// 处理 alias, 相对路径,区分外部包,解析 .ts, .tsx
|
|
662
|
+
// 排序 decl
|
|
663
|
+
// 在 Program:exit 的时候,检查是否有顺序不一致的,一旦有,就通过 replaceTextRange 用已排序的 decls 重新生成替换
|
|
664
|
+
import: {
|
|
665
|
+
meta,
|
|
666
|
+
// @ts-ignore
|
|
667
|
+
create(context) {
|
|
668
|
+
let imports = [];
|
|
669
|
+
// 检查、解析 decl 中的 source (from '...' 中的 ...) 做解析和替换
|
|
670
|
+
// 这里不能用异步操作,很恶心
|
|
671
|
+
const cwd = context.cwd.fpd;
|
|
672
|
+
const pkgs = get_packages(cwd);
|
|
673
|
+
const aliases = get_tsconfig_aliases(cwd);
|
|
674
|
+
// console.log('pkgs:', pkgs)
|
|
675
|
+
// console.log('aliases:', aliases)
|
|
676
|
+
let need_fix_source = false;
|
|
677
|
+
return {
|
|
678
|
+
// 格式化 source, 收集 decl
|
|
679
|
+
ImportDeclaration(node) {
|
|
680
|
+
const source = node.source.value;
|
|
681
|
+
const [type, source_, ialias] = format_source(pkgs, aliases, cwd, context.filename.fp, source);
|
|
682
|
+
if (!source_ || source_ === source) {
|
|
683
|
+
imports.push({
|
|
684
|
+
node,
|
|
685
|
+
type,
|
|
686
|
+
style: style_fexts.has(source.fext),
|
|
687
|
+
ialias
|
|
688
|
+
});
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
need_fix_source = true;
|
|
692
|
+
// console.log(`自动修复 ${source} -> ${source_}`)
|
|
693
|
+
context.report({
|
|
694
|
+
// @ts-ignore
|
|
695
|
+
message: `import source ${source} 应该改为 ${source_}`,
|
|
696
|
+
node,
|
|
697
|
+
loc: node.source.loc,
|
|
698
|
+
fix: fixer => fixer.replaceTextRange(node.source.range, source_.quote())
|
|
699
|
+
});
|
|
700
|
+
},
|
|
701
|
+
// 排序 decl 整体替换修复
|
|
702
|
+
'Program:exit'() {
|
|
703
|
+
if (need_fix_source || imports.length <= 1)
|
|
704
|
+
return;
|
|
705
|
+
// 检查顺序并修复: 样式 xxx.sass < nodejs builtin < package < alias < subpath
|
|
706
|
+
// 如果为样式,则放前面,其他的就按照数组来
|
|
707
|
+
let imports_ = imports.toSorted((a, b) => {
|
|
708
|
+
if (a.style !== b.style)
|
|
709
|
+
return a.style ? -1 : 1;
|
|
710
|
+
if (a.type === 'alias' && b.type === 'alias')
|
|
711
|
+
return a.ialias - b.ialias;
|
|
712
|
+
return imtypes.indexOf(a.type) - imtypes.indexOf(b.type);
|
|
713
|
+
});
|
|
714
|
+
if (array_equals(imports, imports_))
|
|
715
|
+
return;
|
|
716
|
+
const { sourceCode: source } = context;
|
|
717
|
+
const { body } = source.ast;
|
|
718
|
+
const first = imports[0].node;
|
|
719
|
+
const ifirst = body.indexOf(first);
|
|
720
|
+
if (body.indexOf(imports.last.node, ifirst + 1) - ifirst !== imports.length - 1) {
|
|
721
|
+
context.report({
|
|
722
|
+
// @ts-ignore
|
|
723
|
+
message: 'import 顺序不对,且 import 语句不连续,中间有其他语句,无法自动排序修复,需手动修复',
|
|
724
|
+
node: first
|
|
725
|
+
});
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
context.report({
|
|
729
|
+
// @ts-ignore
|
|
730
|
+
message: 'import 顺序不对',
|
|
731
|
+
node: imports[0].node,
|
|
732
|
+
fix(fixer) {
|
|
733
|
+
let _start = Number.MAX_SAFE_INTEGER, _end = 0;
|
|
734
|
+
let type;
|
|
735
|
+
// 收集所有排序后的 import 文本
|
|
736
|
+
const texts = imports_.map((im, i) => {
|
|
737
|
+
const [start, end] = get_node_range_with_comments(source, im.node, true);
|
|
738
|
+
if (start < _start)
|
|
739
|
+
_start = start;
|
|
740
|
+
if (end > _end)
|
|
741
|
+
_end = end;
|
|
742
|
+
let text = source.text.slice(start, end);
|
|
743
|
+
// 分组之间空一行
|
|
744
|
+
if (im.type !== type) {
|
|
745
|
+
if (type)
|
|
746
|
+
text = '\n' + text;
|
|
747
|
+
type = im.type;
|
|
748
|
+
}
|
|
749
|
+
return text;
|
|
750
|
+
});
|
|
751
|
+
// 一次性替换所有 import
|
|
752
|
+
return fixer.replaceTextRange([_start, _end], texts.join_lines(false));
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
export const xlint_config = {
|
|
762
|
+
files: ['**/*.{js,mjs,cjs,ts,tsx,mts,cts}'],
|
|
763
|
+
ignores: [
|
|
764
|
+
'*.d.ts',
|
|
765
|
+
'node_modules/',
|
|
766
|
+
'.git/',
|
|
767
|
+
],
|
|
768
|
+
languageOptions: {
|
|
769
|
+
ecmaVersion: 'latest',
|
|
770
|
+
parser: TSParser,
|
|
771
|
+
parserOptions: {
|
|
772
|
+
ecmaVersion: 'latest',
|
|
773
|
+
sourceType: 'module',
|
|
774
|
+
projectService: true,
|
|
775
|
+
ecmaFeatures: {
|
|
776
|
+
jsx: true
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
},
|
|
780
|
+
plugins: {
|
|
781
|
+
'@typescript-eslint': ts_plugin,
|
|
782
|
+
react: react_plugin,
|
|
783
|
+
'@stylistic': stylistic_plugin,
|
|
784
|
+
xlint: xlint_plugin,
|
|
785
|
+
},
|
|
786
|
+
settings: {
|
|
787
|
+
react: {
|
|
788
|
+
version: 'detect'
|
|
789
|
+
}
|
|
790
|
+
},
|
|
791
|
+
rules: {
|
|
792
|
+
'xlint/fold-jsdoc-comments': 'error',
|
|
793
|
+
// 取代 nonblock-statement-body-position
|
|
794
|
+
'xlint/nonblock-statement-body-position-with-indentation': 'error',
|
|
795
|
+
'xlint/empty-bracket-spacing': 'error',
|
|
796
|
+
// a + b**c
|
|
797
|
+
'xlint/space-infix-ops-except-exponentiation': 'error',
|
|
798
|
+
'xlint/space-in-for-statement': 'error',
|
|
799
|
+
'xlint/jsx-no-redundant-parenthesis-in-return': 'error',
|
|
800
|
+
'xlint/keep-indent': 'error',
|
|
801
|
+
// 函数使用 function 来声明而不是 const foo = () => { }
|
|
802
|
+
'xlint/func-style': 'error',
|
|
803
|
+
'xlint/import': 'error',
|
|
804
|
+
'@stylistic/semi': ['error', 'never'],
|
|
805
|
+
'@stylistic/no-extra-semi': 'error',
|
|
806
|
+
'@stylistic/semi-style': ['error', 'first'],
|
|
807
|
+
// 使用 ===
|
|
808
|
+
eqeqeq: 'error',
|
|
809
|
+
// 父类尽量返回 this 类型
|
|
810
|
+
'@typescript-eslint/prefer-return-this-type': 'error',
|
|
811
|
+
// 尽量使用 . 访问属性而不是 []
|
|
812
|
+
'@typescript-eslint/dot-notation': 'error',
|
|
813
|
+
// 必须 throw Error
|
|
814
|
+
'@typescript-eslint/only-throw-error': 'error',
|
|
815
|
+
// 不要 return await promise, 直接 return promise, 除非外面有 try catch
|
|
816
|
+
'@typescript-eslint/return-await': 'error',
|
|
817
|
+
// ------------ 括号
|
|
818
|
+
// a => { } 而不是 (a) => { }
|
|
819
|
+
'@stylistic/arrow-parens': ['error', 'as-needed', { requireForBlockBody: false }],
|
|
820
|
+
// arrow function 在 return 单条表达式时去掉后面的大括号, a => 1
|
|
821
|
+
'arrow-body-style': 'error',
|
|
822
|
+
// 不要多余的大括号
|
|
823
|
+
// if (true)
|
|
824
|
+
// console.log()
|
|
825
|
+
curly: ['error', 'multi'],
|
|
826
|
+
// 简单属性不要冗余的大括号 <Component prop='simple-value' />
|
|
827
|
+
'@stylistic/jsx-curly-brace-presence': ['error', 'never'],
|
|
828
|
+
// ------------ 空格
|
|
829
|
+
// { a, b } 这样的对象,大括号里面要有空格
|
|
830
|
+
'@stylistic/object-curly-spacing': ['error', 'always'],
|
|
831
|
+
// [a, b, c]
|
|
832
|
+
'@stylistic/comma-spacing': 'error',
|
|
833
|
+
// foo()
|
|
834
|
+
'@stylistic/function-call-spacing': 'error',
|
|
835
|
+
// a => { } 中箭头左右两边空格
|
|
836
|
+
'@stylistic/arrow-spacing': ['error'],
|
|
837
|
+
// 注释双斜杠后面要有空格
|
|
838
|
+
'@stylistic/spaced-comment': ['error', 'always', { markers: ['/'] }],
|
|
839
|
+
// 函数声明中,名称后面要有空格
|
|
840
|
+
'@stylistic/space-before-function-paren': 'error',
|
|
841
|
+
// { return true } 这样的 block 大括号里面要有空格
|
|
842
|
+
'@stylistic/block-spacing': ['error', 'always'],
|
|
843
|
+
// aaa: 123
|
|
844
|
+
'@stylistic/key-spacing': ['error', { beforeColon: false, afterColon: true, mode: 'minimum' }],
|
|
845
|
+
// aaa: string
|
|
846
|
+
'@stylistic/type-annotation-spacing': 'error',
|
|
847
|
+
// if ()
|
|
848
|
+
'@stylistic/keyword-spacing': ['error', { before: true, after: true }],
|
|
849
|
+
// if (1) { }
|
|
850
|
+
'@stylistic/space-before-blocks': 'error',
|
|
851
|
+
// case 1: ...
|
|
852
|
+
'@stylistic/switch-colon-spacing': 'error',
|
|
853
|
+
// <Hello name={firstname} />
|
|
854
|
+
'@stylistic/jsx-equals-spacing': ['error', 'never'],
|
|
855
|
+
// 不允许使用 tab
|
|
856
|
+
'@stylistic/no-tabs': 'error',
|
|
857
|
+
// 使用 \n 换行
|
|
858
|
+
'@stylistic/linebreak-style': ['error', 'unix'],
|
|
859
|
+
// 文件以 \n 结尾
|
|
860
|
+
'@stylistic/eol-last': ['error', 'always'],
|
|
861
|
+
// ------------ 引号
|
|
862
|
+
// 用单引号
|
|
863
|
+
'@stylistic/jsx-quotes': ['error', 'prefer-single'],
|
|
864
|
+
// 用单引号
|
|
865
|
+
'@stylistic/quotes': ['error', 'single', { avoidEscape: true, allowTemplateLiterals: 'avoidEscape' }],
|
|
866
|
+
// 不要冗余的引号包裹属性
|
|
867
|
+
'@stylistic/quote-props': ['error', 'as-needed', { keywords: false, unnecessary: true }],
|
|
868
|
+
// ------------ 其它
|
|
869
|
+
// boolean 属性不要冗余的 ={true} <Component boolprop />
|
|
870
|
+
'react/jsx-boolean-value': ['error', 'never'],
|
|
871
|
+
// 没有 children 的 Component 写成闭合标签 <Component />
|
|
872
|
+
'react/self-closing-comp': 'error',
|
|
873
|
+
// 单行类型声明用 `,` 分割,多行类型声明结尾不要加分号
|
|
874
|
+
'@stylistic/member-delimiter-style': [
|
|
875
|
+
'error',
|
|
876
|
+
{
|
|
877
|
+
multiline: {
|
|
878
|
+
delimiter: 'none',
|
|
879
|
+
requireLast: false
|
|
880
|
+
},
|
|
881
|
+
singleline: {
|
|
882
|
+
delimiter: 'comma',
|
|
883
|
+
requireLast: false
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
],
|
|
887
|
+
'@typescript-eslint/prefer-includes': 'error',
|
|
888
|
+
'@typescript-eslint/prefer-regexp-exec': 'error',
|
|
889
|
+
// 禁止使用 export default
|
|
890
|
+
'no-restricted-exports': [
|
|
891
|
+
'error',
|
|
892
|
+
{ restrictDefaultExports: { direct: true, named: true, defaultFrom: true, namedFrom: true, namespaceFrom: true } }
|
|
893
|
+
],
|
|
894
|
+
'@typescript-eslint/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports', disallowTypeAnnotations: false }],
|
|
895
|
+
// () => { 返回 void 的表达式 }
|
|
896
|
+
'@typescript-eslint/no-confusing-void-expression': 'error'
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
const line_break_pattern = /\r\n|[\r\n\u2028\u2029]/u;
|
|
900
|
+
const style_fexts = new Set(['sass', 'css', 'scss', 'less', 'styl']);
|
|
901
|
+
const unions = ['|', '&'];
|
|
902
|
+
function split_indent(line) {
|
|
903
|
+
let i = 0;
|
|
904
|
+
let indent = 0;
|
|
905
|
+
for (; i < line.length; i++)
|
|
906
|
+
if (line[i] === ' ')
|
|
907
|
+
indent++;
|
|
908
|
+
else if (line[i] === '\t')
|
|
909
|
+
indent += 4;
|
|
910
|
+
else
|
|
911
|
+
break;
|
|
912
|
+
return {
|
|
913
|
+
indent,
|
|
914
|
+
text: line.slice(i)
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
function not_opening_paren(token) {
|
|
918
|
+
return !(['(', '[', '{'].includes(token.value) && token.type === 'Punctuator');
|
|
919
|
+
}
|
|
920
|
+
function not_closing_paren(token) {
|
|
921
|
+
return !([')', ']', '}'].includes(token.value) && token.type === 'Punctuator');
|
|
922
|
+
}
|
|
923
|
+
function all_space(str, len = str.length) {
|
|
924
|
+
for (let i = 0; i < len; ++i)
|
|
925
|
+
if (str[i] !== ' ')
|
|
926
|
+
return false;
|
|
927
|
+
return true;
|
|
928
|
+
}
|
|
929
|
+
/** 获取节点及其注释的范围(排除前一条语句的行尾注释)
|
|
930
|
+
返回 `[start, end]`,其中 start 是第一个独立注释的位置(若无则为节点起始位置)
|
|
931
|
+
- source
|
|
932
|
+
- node
|
|
933
|
+
- include_after_comment?: `true` 是否包含节点后面的行尾注释 */
|
|
934
|
+
function get_node_range_with_comments(source, node, include_after_comment) {
|
|
935
|
+
let [start, end] = node.range;
|
|
936
|
+
// 获取节点前的所有注释
|
|
937
|
+
const comments = source.getCommentsBefore(node);
|
|
938
|
+
if (comments.length !== 0) {
|
|
939
|
+
// 从最后一个注释往前找,遇到前一条语句行尾注释时退出
|
|
940
|
+
// 判断标准:独立注释以多个空格 + // 或者 /* 开头
|
|
941
|
+
// 行尾注释:// 前面含有非空格
|
|
942
|
+
let valid_comment;
|
|
943
|
+
for (let i = comments.length - 1; i >= 0; --i) {
|
|
944
|
+
const comment = comments[i];
|
|
945
|
+
// 获取 comment 所在行的内容
|
|
946
|
+
const line = source.lines[comment.loc.start.line - 1];
|
|
947
|
+
// 找到注释开始标记 // 或 /* 在行中的位置
|
|
948
|
+
const istart = comment.type === 'Line' ? line.indexOf('//') : line.indexOf('/*');
|
|
949
|
+
// 注释标记之前是否全是空格
|
|
950
|
+
if (!all_space(line.slice(0, istart)))
|
|
951
|
+
break;
|
|
952
|
+
valid_comment = comment;
|
|
953
|
+
}
|
|
954
|
+
// 如果有有效的注释,更新 start
|
|
955
|
+
if (valid_comment)
|
|
956
|
+
start = valid_comment.range[0];
|
|
957
|
+
}
|
|
958
|
+
if (include_after_comment) {
|
|
959
|
+
const comments_after = source.getCommentsAfter(node);
|
|
960
|
+
// 和节点在同一行,在尾部的注释
|
|
961
|
+
if (comments_after[0]?.loc.start.line === node.loc.end.line)
|
|
962
|
+
end = comments_after[0].range[1];
|
|
963
|
+
}
|
|
964
|
+
return [start, end];
|
|
965
|
+
}
|
|
966
|
+
let cache_alias_cwd;
|
|
967
|
+
let cache_alias;
|
|
968
|
+
/** 解析 tsconfig.json 中的 paths 配置
|
|
969
|
+
返回 `{ alias: string, fp: string }[]`
|
|
970
|
+
- alias: 导入别名前缀(如 `@lib/`)
|
|
971
|
+
- fp: 对应的路径前缀(如 `./lib/`)
|
|
972
|
+
|
|
973
|
+
处理规则:
|
|
974
|
+
- value 数组必须只有一项
|
|
975
|
+
- 如果 key 和 value 都以 `/*` 结尾,去掉 `*`
|
|
976
|
+
- 否则 value 就是具体文件路径 */
|
|
977
|
+
function get_tsconfig_aliases(cwd) {
|
|
978
|
+
if (cwd === cache_alias_cwd)
|
|
979
|
+
return cache_alias;
|
|
980
|
+
// 读取文件内容,移除 // 行注释
|
|
981
|
+
const { compilerOptions } = JSON.parse(fs.readFileSync(`${cwd}tsconfig.json`, 'utf-8')
|
|
982
|
+
.split_lines()
|
|
983
|
+
.filter(line => !line.trimStart().startsWith('//'))
|
|
984
|
+
.join_lines());
|
|
985
|
+
const paths = compilerOptions?.paths;
|
|
986
|
+
if (!paths)
|
|
987
|
+
return [];
|
|
988
|
+
const aliases = Object.entries(paths).map(([key, values]) => {
|
|
989
|
+
// 检查 value 数组只有一项
|
|
990
|
+
check(values?.length === 1, `paths 的 value 数组必须只有一项`);
|
|
991
|
+
const value = values[0];
|
|
992
|
+
// key 和 value 都以 /* 结尾,去掉 *
|
|
993
|
+
if (key.endsWith('/*') && value.endsWith('/*'))
|
|
994
|
+
return {
|
|
995
|
+
alias: key.slice(0, -1), // @lib/* -> @lib/
|
|
996
|
+
fp: value.slice(0, -1).strip_if_start('./') // ./lib/* -> lib/
|
|
997
|
+
};
|
|
998
|
+
// 具体文件路径的情况
|
|
999
|
+
return {
|
|
1000
|
+
alias: key,
|
|
1001
|
+
fp: value.strip_if_start('./')
|
|
1002
|
+
};
|
|
1003
|
+
});
|
|
1004
|
+
cache_alias_cwd = cwd;
|
|
1005
|
+
return cache_alias = aliases;
|
|
1006
|
+
}
|
|
1007
|
+
let cache_pkgs_cwd;
|
|
1008
|
+
let cache_pkgs;
|
|
1009
|
+
function get_packages(cwd) {
|
|
1010
|
+
if (cache_pkgs_cwd === cwd)
|
|
1011
|
+
return cache_pkgs;
|
|
1012
|
+
const { dependencies = {}, devDependencies = {} } = JSON.parse(fs.readFileSync(`${cwd}package.json`, 'utf-8'));
|
|
1013
|
+
const pkgs = [
|
|
1014
|
+
...Object.keys(dependencies),
|
|
1015
|
+
...Object.keys(devDependencies)
|
|
1016
|
+
.map(pkg => pkg.startsWith('@types/') ? pkg.strip_start('@types/') : pkg),
|
|
1017
|
+
].unique();
|
|
1018
|
+
cache_pkgs_cwd = cwd;
|
|
1019
|
+
return cache_pkgs = pkgs;
|
|
1020
|
+
}
|
|
1021
|
+
/** import types */
|
|
1022
|
+
const imtypes = ['builtin', 'package', 'alias', 'subpath'];
|
|
1023
|
+
/** 将一个有后缀或无后缀的文件,解析到实际文件,并通过 alias 简化 */
|
|
1024
|
+
function format_source(pkgs, aliases, cwd, fp, source) {
|
|
1025
|
+
check(path.isAbsolute(fp), 'fp 应该是绝对路径');
|
|
1026
|
+
if (source.startsWith('node:'))
|
|
1027
|
+
return ['builtin'];
|
|
1028
|
+
if (builtins.has(source))
|
|
1029
|
+
return ['builtin', `node:${source}`];
|
|
1030
|
+
const pkg = pkgs.find(p => source === p || source.startsWith(p) && source[p.length] === '/');
|
|
1031
|
+
if (pkg) {
|
|
1032
|
+
if (pkgs.includes(pkg) || source.fext)
|
|
1033
|
+
return ['package'];
|
|
1034
|
+
// package.json 中有 exports 的,也允许无后缀(可能 .js 没有导出)
|
|
1035
|
+
const pkg_json = JSON.parse(fs.readFileSync(`${cwd}node_modules/${pkg}/package.json`, 'utf-8'));
|
|
1036
|
+
if (pkg_json.exports)
|
|
1037
|
+
return ['package'];
|
|
1038
|
+
return ['package', `${source}.js`];
|
|
1039
|
+
}
|
|
1040
|
+
// 剩下的全是内部路径
|
|
1041
|
+
// 相对子路径
|
|
1042
|
+
if (source.startsWith('./')) {
|
|
1043
|
+
const { fext } = source;
|
|
1044
|
+
// 已经完美了
|
|
1045
|
+
if (fext && fext !== 'js')
|
|
1046
|
+
return ['subpath'];
|
|
1047
|
+
source = source.strip_if_end('.js');
|
|
1048
|
+
return ['subpath', `${source}${resolve_suffix(path.resolve(fp.fdir, source))}`];
|
|
1049
|
+
}
|
|
1050
|
+
// ../ 父级路径,或者 @/... 等 alias 路径
|
|
1051
|
+
// 先 resolve 到完整路径,再修复后缀,最后通过 alias 简化
|
|
1052
|
+
const alias = aliases.find(({ alias }) => source.startsWith(alias));
|
|
1053
|
+
if (alias)
|
|
1054
|
+
source = cwd + alias.fp + source.strip_start(alias.alias);
|
|
1055
|
+
// 是完整路径了
|
|
1056
|
+
source = path.resolve(fp.fdir, source);
|
|
1057
|
+
const { fext } = source;
|
|
1058
|
+
if (!fext || fext === 'js') {
|
|
1059
|
+
source = source.strip_if_end('.js');
|
|
1060
|
+
source = `${source}${resolve_suffix(source)}`;
|
|
1061
|
+
}
|
|
1062
|
+
const relative = source.strip_start(cwd, true);
|
|
1063
|
+
const ialias = aliases.findIndex(({ fp }) => relative.startsWith(fp));
|
|
1064
|
+
check(ialias !== -1, '一定能找到匹配的 alias');
|
|
1065
|
+
const alias_ = aliases[ialias];
|
|
1066
|
+
return ['alias', alias_.alias + relative.strip_start(alias_.fp), ialias];
|
|
1067
|
+
}
|
|
1068
|
+
let cache_exists = new Map();
|
|
1069
|
+
function resolve_suffix(fp_base) {
|
|
1070
|
+
for (const suffix of ['.ts', '.tsx', '/index.ts', '/index.tsx']) {
|
|
1071
|
+
const fp = `${fp_base}${suffix}`;
|
|
1072
|
+
const c = cache_exists.get(fp);
|
|
1073
|
+
if (c !== undefined)
|
|
1074
|
+
if (c)
|
|
1075
|
+
return suffix;
|
|
1076
|
+
else
|
|
1077
|
+
continue;
|
|
1078
|
+
const e = fs.existsSync(fp);
|
|
1079
|
+
cache_exists.set(fp, e);
|
|
1080
|
+
if (e)
|
|
1081
|
+
return suffix;
|
|
1082
|
+
}
|
|
1083
|
+
throw new Error(`无法解析 ${fp_base} 到文件`);
|
|
1084
|
+
}
|
|
1085
|
+
const builtins = new Set(builtinModules);
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "xshell-lint",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"publisher": "ShenHongFei",
|
|
7
|
+
"homepage": "https://www.npmjs.com/package/xshell-lint",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@stylistic/eslint-plugin": "^5.10.0",
|
|
10
|
+
"@typescript-eslint/eslint-plugin": "^8.70.0",
|
|
11
|
+
"@typescript-eslint/parser": "^8.70.0",
|
|
12
|
+
"@typescript-eslint/utils": "^8.70.0",
|
|
13
|
+
"eslint": "^10.10.0",
|
|
14
|
+
"eslint-plugin-react": "^7.37.5",
|
|
15
|
+
"xshell": "^1.4.51"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/eslint": "^9.6.1",
|
|
19
|
+
"@types/estree": "^1.0.9",
|
|
20
|
+
"@types/node": "^26.6.1"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"publish:npm": "npm publish --access=public"
|
|
26
|
+
}
|
|
27
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// --- module
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"allowSyntheticDefaultImports": true,
|
|
7
|
+
"resolveJsonModule": true,
|
|
8
|
+
"isolatedModules": true,
|
|
9
|
+
// files without `import` or `export` are treated as modules.
|
|
10
|
+
"moduleDetection": "force",
|
|
11
|
+
|
|
12
|
+
// --- build
|
|
13
|
+
"target": "esnext",
|
|
14
|
+
"allowJs": false,
|
|
15
|
+
"checkJs": false,
|
|
16
|
+
"pretty": true,
|
|
17
|
+
"newLine": "lf",
|
|
18
|
+
"lib": ["ESNext", "DOM"],
|
|
19
|
+
"importHelpers": false,
|
|
20
|
+
|
|
21
|
+
"incremental": true,
|
|
22
|
+
"tsBuildInfoFile": "T:/1/xlint/.tsbuildinfo",
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// --- emit
|
|
26
|
+
"noEmit": false,
|
|
27
|
+
"declaration": true,
|
|
28
|
+
"emitDeclarationOnly": false,
|
|
29
|
+
"noEmitOnError": false,
|
|
30
|
+
"listEmittedFiles": true,
|
|
31
|
+
"rewriteRelativeImportExtensions": true,
|
|
32
|
+
|
|
33
|
+
// --- source maps
|
|
34
|
+
"sourceMap": false,
|
|
35
|
+
"inlineSourceMap": false,
|
|
36
|
+
"inlineSources": false,
|
|
37
|
+
|
|
38
|
+
// --- features
|
|
39
|
+
"erasableSyntaxOnly": true,
|
|
40
|
+
"preserveSymlinks": false,
|
|
41
|
+
"jsx": "react-jsx",
|
|
42
|
+
"removeComments": false,
|
|
43
|
+
"preserveConstEnums": true,
|
|
44
|
+
"forceConsistentCasingInFileNames": true,
|
|
45
|
+
"allowImportingTsExtensions": true,
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
// --- type checking
|
|
49
|
+
"strict": false,
|
|
50
|
+
"noImplicitAny": false,
|
|
51
|
+
"noImplicitReturns": false,
|
|
52
|
+
"noImplicitThis": true,
|
|
53
|
+
"noImplicitOverride": true,
|
|
54
|
+
"noUnusedLocals": false,
|
|
55
|
+
"noUnusedParameters": false,
|
|
56
|
+
"skipLibCheck": true
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|