@so1ve/eslint-plugin 0.93.1 → 0.93.3
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.cjs +25 -5
- package/dist/index.mjs +25 -5
- package/package.json +2 -7
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,22 @@ const types = require('@typescript-eslint/types');
|
|
|
4
4
|
const utils = require('@typescript-eslint/utils');
|
|
5
5
|
|
|
6
6
|
const createEslintRule = utils.ESLintUtils.RuleCreator((ruleName) => ruleName);
|
|
7
|
+
function getPreviousNode(node) {
|
|
8
|
+
if (!node) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const { parent } = node;
|
|
12
|
+
if (parent && "body" in parent) {
|
|
13
|
+
const { body } = parent;
|
|
14
|
+
if (!Array.isArray(body)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const index = body.indexOf(node);
|
|
18
|
+
if (index > 0) {
|
|
19
|
+
return body[index - 1];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
7
23
|
|
|
8
24
|
const RULE_NAME$6 = "function-style";
|
|
9
25
|
const START_RETURN = /^return /;
|
|
@@ -46,7 +62,7 @@ const functionStyle = createEslintRule({
|
|
|
46
62
|
const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
|
|
47
63
|
const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
|
|
48
64
|
const body = sourceCode.getText(node.body);
|
|
49
|
-
const variableDeclaration = asVariable ? `const ${name} = ` : "";
|
|
65
|
+
const variableDeclaration = asVariable && name ? `const ${name} = ` : "";
|
|
50
66
|
return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
|
|
51
67
|
}
|
|
52
68
|
let currentScope = null;
|
|
@@ -85,12 +101,16 @@ const functionStyle = createEslintRule({
|
|
|
85
101
|
if (haveThisAccess) {
|
|
86
102
|
return;
|
|
87
103
|
}
|
|
104
|
+
const previousNode = getPreviousNode(node.parent);
|
|
105
|
+
if (previousNode?.type === types.AST_NODE_TYPES.ExportNamedDeclaration && previousNode.declaration.type === types.AST_NODE_TYPES.TSDeclareFunction) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
88
108
|
const statement = getLoneReturnStatement(node);
|
|
89
|
-
|
|
109
|
+
const isExportDefault = node.parent?.type === types.AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
110
|
+
if (!statement || !node.id?.name && !isExportDefault || node.generator) {
|
|
90
111
|
clearThisAccess();
|
|
91
112
|
return;
|
|
92
113
|
}
|
|
93
|
-
const asVariable = node.parent?.type !== types.AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
94
114
|
context.report({
|
|
95
115
|
node,
|
|
96
116
|
messageId: "arrow",
|
|
@@ -98,10 +118,10 @@ const functionStyle = createEslintRule({
|
|
|
98
118
|
node.range,
|
|
99
119
|
generateFunction(
|
|
100
120
|
"arrow",
|
|
101
|
-
node.id
|
|
121
|
+
node.id?.name ?? null,
|
|
102
122
|
node,
|
|
103
123
|
getStatementRaw(statement),
|
|
104
|
-
|
|
124
|
+
!isExportDefault
|
|
105
125
|
)
|
|
106
126
|
)
|
|
107
127
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -2,6 +2,22 @@ import { AST_NODE_TYPES } from '@typescript-eslint/types';
|
|
|
2
2
|
import { ESLintUtils } from '@typescript-eslint/utils';
|
|
3
3
|
|
|
4
4
|
const createEslintRule = ESLintUtils.RuleCreator((ruleName) => ruleName);
|
|
5
|
+
function getPreviousNode(node) {
|
|
6
|
+
if (!node) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const { parent } = node;
|
|
10
|
+
if (parent && "body" in parent) {
|
|
11
|
+
const { body } = parent;
|
|
12
|
+
if (!Array.isArray(body)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const index = body.indexOf(node);
|
|
16
|
+
if (index > 0) {
|
|
17
|
+
return body[index - 1];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
5
21
|
|
|
6
22
|
const RULE_NAME$6 = "function-style";
|
|
7
23
|
const START_RETURN = /^return /;
|
|
@@ -44,7 +60,7 @@ const functionStyle = createEslintRule({
|
|
|
44
60
|
const params = node.params.map((param) => sourceCode.getText(param)).join(", ");
|
|
45
61
|
const returnType = node.returnType ? sourceCode.getText(node.returnType) : "";
|
|
46
62
|
const body = sourceCode.getText(node.body);
|
|
47
|
-
const variableDeclaration = asVariable ? `const ${name} = ` : "";
|
|
63
|
+
const variableDeclaration = asVariable && name ? `const ${name} = ` : "";
|
|
48
64
|
return type === "arrow" ? `${variableDeclaration}${async}${generics}(${params})${returnType} => ${rawStatement};` : `${async}function ${name}${generics}(${params})${returnType} ${body}`;
|
|
49
65
|
}
|
|
50
66
|
let currentScope = null;
|
|
@@ -83,12 +99,16 @@ const functionStyle = createEslintRule({
|
|
|
83
99
|
if (haveThisAccess) {
|
|
84
100
|
return;
|
|
85
101
|
}
|
|
102
|
+
const previousNode = getPreviousNode(node.parent);
|
|
103
|
+
if (previousNode?.type === AST_NODE_TYPES.ExportNamedDeclaration && previousNode.declaration.type === AST_NODE_TYPES.TSDeclareFunction) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
86
106
|
const statement = getLoneReturnStatement(node);
|
|
87
|
-
|
|
107
|
+
const isExportDefault = node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
108
|
+
if (!statement || !node.id?.name && !isExportDefault || node.generator) {
|
|
88
109
|
clearThisAccess();
|
|
89
110
|
return;
|
|
90
111
|
}
|
|
91
|
-
const asVariable = node.parent?.type !== AST_NODE_TYPES.ExportDefaultDeclaration;
|
|
92
112
|
context.report({
|
|
93
113
|
node,
|
|
94
114
|
messageId: "arrow",
|
|
@@ -96,10 +116,10 @@ const functionStyle = createEslintRule({
|
|
|
96
116
|
node.range,
|
|
97
117
|
generateFunction(
|
|
98
118
|
"arrow",
|
|
99
|
-
node.id
|
|
119
|
+
node.id?.name ?? null,
|
|
100
120
|
node,
|
|
101
121
|
getStatementRaw(statement),
|
|
102
|
-
|
|
122
|
+
!isExportDefault
|
|
103
123
|
)
|
|
104
124
|
)
|
|
105
125
|
});
|
package/package.json
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@so1ve/eslint-plugin",
|
|
3
|
-
"version": "0.93.
|
|
4
|
-
"author": "
|
|
5
|
-
"contributors": [
|
|
6
|
-
{
|
|
7
|
-
"name": "Ray <i@mk1.io> (https://github.com/so1ve/)"
|
|
8
|
-
}
|
|
9
|
-
],
|
|
3
|
+
"version": "0.93.3",
|
|
4
|
+
"author": "Ray <i@mk1.io> (https://github.com/so1ve/)",
|
|
10
5
|
"keywords": [
|
|
11
6
|
"eslint",
|
|
12
7
|
"eslint-config",
|