@so1ve/eslint-plugin 3.3.2 → 3.5.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/dist/index.js +64 -35
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -32,28 +32,36 @@ var function_style_default = createEslintRule({
|
|
|
32
32
|
defaultOptions: [],
|
|
33
33
|
create: (context) => {
|
|
34
34
|
const sourceCode = context.sourceCode;
|
|
35
|
-
|
|
35
|
+
const scopeStack = [];
|
|
36
|
+
let haveThisAccess = false;
|
|
37
|
+
function getSingleReturnStatement(node) {
|
|
36
38
|
const { body } = node;
|
|
37
39
|
if (body.type !== AST_NODE_TYPES.BlockStatement) return;
|
|
38
40
|
const { body: blockBody } = body;
|
|
39
|
-
const allComments = sourceCode.getCommentsInside(node);
|
|
40
41
|
if (blockBody.length !== 1) return;
|
|
41
42
|
const [statement] = blockBody;
|
|
43
|
+
if (statement?.type !== AST_NODE_TYPES.ReturnStatement) return;
|
|
44
|
+
const allComments = sourceCode.getCommentsInside(node);
|
|
42
45
|
const statementComments = sourceCode.getCommentsInside(statement);
|
|
43
46
|
if (allComments.length !== statementComments.length) return;
|
|
44
|
-
|
|
47
|
+
return statement;
|
|
45
48
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
const extractFunctionInfo = (node) => ({
|
|
50
|
+
async: node.async,
|
|
51
|
+
generics: node.typeParameters ? sourceCode.getText(node.typeParameters) : "",
|
|
52
|
+
params: node.params.map((param) => sourceCode.getText(param)).join(", "),
|
|
53
|
+
returnType: node.returnType ? sourceCode.getText(node.returnType) : "",
|
|
54
|
+
body: sourceCode.getText(node.body)
|
|
55
|
+
});
|
|
56
|
+
function generateArrowFunction(name, info, returnValue, asVariable = true) {
|
|
57
|
+
const asyncKeyword = info.async ? "async " : "";
|
|
52
58
|
const variableDeclaration = asVariable && name ? `const ${name} = ` : "";
|
|
53
|
-
return
|
|
59
|
+
return `${variableDeclaration}${asyncKeyword}${info.generics}(${info.params})${info.returnType} => ${returnValue};`;
|
|
60
|
+
}
|
|
61
|
+
function generateFunctionDeclaration(name, info) {
|
|
62
|
+
const asyncKeyword = info.async ? "async " : "";
|
|
63
|
+
return `${asyncKeyword}function ${name}${info.generics}(${info.params})${info.returnType} ${info.body}`;
|
|
54
64
|
}
|
|
55
|
-
const scopeStack = [];
|
|
56
|
-
let haveThisAccess = false;
|
|
57
65
|
function setupScope(node) {
|
|
58
66
|
scopeStack.push(sourceCode.getScope(node));
|
|
59
67
|
}
|
|
@@ -61,37 +69,56 @@ var function_style_default = createEslintRule({
|
|
|
61
69
|
scopeStack.pop();
|
|
62
70
|
haveThisAccess = false;
|
|
63
71
|
}
|
|
72
|
+
function getFunctionExpressionParentNameString(node) {
|
|
73
|
+
const parent = node.parent;
|
|
74
|
+
if (parent?.id?.typeAnnotation || parent?.type !== AST_NODE_TYPES.VariableDeclarator || haveThisAccess) return null;
|
|
75
|
+
return parent.id.name;
|
|
76
|
+
}
|
|
77
|
+
function shouldConvertFunctionDeclarationToArrow(node) {
|
|
78
|
+
if (haveThisAccess) return { shouldConvert: false };
|
|
79
|
+
const previousNode = getPreviousNode(node.parent);
|
|
80
|
+
if (previousNode?.type === AST_NODE_TYPES.ExportNamedDeclaration && previousNode.declaration?.type === AST_NODE_TYPES.TSDeclareFunction) return { shouldConvert: false };
|
|
81
|
+
const returnStatement = getSingleReturnStatement(node);
|
|
82
|
+
const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
83
|
+
if (!returnStatement?.argument || !node.id?.name && !isExportDefault || node.generator) return { shouldConvert: false };
|
|
84
|
+
return {
|
|
85
|
+
shouldConvert: true,
|
|
86
|
+
returnStatement
|
|
87
|
+
};
|
|
88
|
+
}
|
|
64
89
|
return {
|
|
65
90
|
"FunctionExpression": setupScope,
|
|
66
91
|
"FunctionExpression:exit"(node) {
|
|
67
|
-
|
|
92
|
+
const name = getFunctionExpressionParentNameString(node);
|
|
93
|
+
if (!name) {
|
|
68
94
|
clearThisAccess();
|
|
69
95
|
return;
|
|
70
96
|
}
|
|
71
|
-
const
|
|
97
|
+
const info = extractFunctionInfo(node);
|
|
98
|
+
const grandParent = node.parent?.parent;
|
|
99
|
+
if (!grandParent) return;
|
|
72
100
|
context.report({
|
|
73
101
|
node,
|
|
74
102
|
messageId: "declaration",
|
|
75
|
-
fix: (fixer) => fixer.replaceText(
|
|
103
|
+
fix: (fixer) => fixer.replaceText(grandParent, generateFunctionDeclaration(name, info))
|
|
76
104
|
});
|
|
77
105
|
clearThisAccess();
|
|
78
106
|
},
|
|
79
107
|
"FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration)": setupScope,
|
|
80
108
|
"FunctionDeclaration:not(TSDeclareFunction + FunctionDeclaration):exit"(node) {
|
|
81
109
|
if (haveThisAccess) return;
|
|
82
|
-
const
|
|
83
|
-
if (
|
|
84
|
-
const statement = getLoneReturnStatement(node);
|
|
85
|
-
const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
86
|
-
if (!statement?.argument || !node.id?.name && !isExportDefault || node.generator) {
|
|
110
|
+
const { shouldConvert, returnStatement } = shouldConvertFunctionDeclarationToArrow(node);
|
|
111
|
+
if (!shouldConvert || !returnStatement?.argument) {
|
|
87
112
|
clearThisAccess();
|
|
88
113
|
return;
|
|
89
114
|
}
|
|
90
|
-
const
|
|
115
|
+
const info = extractFunctionInfo(node);
|
|
116
|
+
const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
117
|
+
const returnValue = `(${sourceCode.getText(returnStatement.argument)})`;
|
|
91
118
|
context.report({
|
|
92
119
|
node,
|
|
93
120
|
messageId: "arrow",
|
|
94
|
-
fix: (fixer) => fixer.replaceText(node,
|
|
121
|
+
fix: (fixer) => fixer.replaceText(node, generateArrowFunction(node.id?.name ?? null, info, returnValue, !isExportDefault))
|
|
95
122
|
});
|
|
96
123
|
clearThisAccess();
|
|
97
124
|
},
|
|
@@ -99,22 +126,24 @@ var function_style_default = createEslintRule({
|
|
|
99
126
|
"ArrowFunctionExpression:exit"(node) {
|
|
100
127
|
if (haveThisAccess) return;
|
|
101
128
|
const { body, parent } = node;
|
|
102
|
-
const
|
|
103
|
-
if (
|
|
104
|
-
const
|
|
129
|
+
const returnStatement = getSingleReturnStatement(node);
|
|
130
|
+
if (returnStatement?.argument) {
|
|
131
|
+
const returnValue = `(${sourceCode.getText(returnStatement.argument)})`;
|
|
105
132
|
context.report({
|
|
106
133
|
node,
|
|
107
134
|
messageId: "arrow",
|
|
108
|
-
fix: (fixer) => fixer.replaceText(node.body,
|
|
135
|
+
fix: (fixer) => fixer.replaceText(node.body, returnValue)
|
|
109
136
|
});
|
|
110
137
|
} else if (body.type === AST_NODE_TYPES.BlockStatement && !parent?.id?.typeAnnotation) {
|
|
111
138
|
const { body: blockBody } = body;
|
|
112
139
|
if (blockBody.length > 0 && node.parent?.parent?.type === AST_NODE_TYPES.VariableDeclaration) {
|
|
113
|
-
const
|
|
140
|
+
const grandParent = node.parent.parent;
|
|
141
|
+
const name = node.parent.id.name;
|
|
142
|
+
const info = extractFunctionInfo(node);
|
|
114
143
|
context.report({
|
|
115
144
|
node: grandParent,
|
|
116
145
|
messageId: "declaration",
|
|
117
|
-
fix: (fixer) => fixer.replaceText(grandParent,
|
|
146
|
+
fix: (fixer) => fixer.replaceText(grandParent, generateFunctionDeclaration(name, info))
|
|
118
147
|
});
|
|
119
148
|
}
|
|
120
149
|
}
|
|
@@ -168,13 +197,12 @@ var import_dedupe_default = createEslintRule({
|
|
|
168
197
|
//#endregion
|
|
169
198
|
//#region src/rules/no-import-promises-as.ts
|
|
170
199
|
const RULE_NAME$6 = "no-import-promises-as";
|
|
171
|
-
const
|
|
200
|
+
const POSSIBLE_IMPORT_SOURCES = [
|
|
172
201
|
"dns",
|
|
173
202
|
"fs",
|
|
174
203
|
"readline",
|
|
175
204
|
"stream"
|
|
176
|
-
];
|
|
177
|
-
const POSSIBLE_IMPORT_SOURCES = [..._POSSIBLE_IMPORT_SOURCES, ..._POSSIBLE_IMPORT_SOURCES.map((s) => `node:${s}`)];
|
|
205
|
+
].flatMap((s) => [s, `node:${s}`]);
|
|
178
206
|
var no_import_promises_as_default = createEslintRule({
|
|
179
207
|
name: RULE_NAME$6,
|
|
180
208
|
meta: {
|
|
@@ -452,10 +480,11 @@ var vue_root_element_sort_attributes_default = createEslintRule({
|
|
|
452
480
|
node: element.startTag,
|
|
453
481
|
messageId: "wrongOrder",
|
|
454
482
|
*fix(fixer) {
|
|
455
|
-
const sortedAttributes = attributesToCheck.sort((a, b) => expectedOrder.indexOf(a.key.name) - expectedOrder.indexOf(b.key.name));
|
|
456
|
-
const
|
|
457
|
-
|
|
458
|
-
|
|
483
|
+
const sortedAttributes = [...attributesToCheck].sort((a, b) => expectedOrder.indexOf(a.key.name) - expectedOrder.indexOf(b.key.name));
|
|
484
|
+
for (const [i, originalAttr] of attributesToCheck.entries()) {
|
|
485
|
+
const sortedAttr = sortedAttributes[i];
|
|
486
|
+
if (originalAttr.key.name !== sortedAttr.key.name) yield fixer.replaceText(originalAttr, sourceCode.getText(sortedAttr));
|
|
487
|
+
}
|
|
459
488
|
}
|
|
460
489
|
});
|
|
461
490
|
}
|