eslint-plugin-function 0.0.23 → 0.0.24
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.d.ts +15 -14
- package/dist/index.js +155 -155
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _typescript_eslint_utils_ts_eslint0 from "@typescript-eslint/utils/ts-eslint";
|
|
2
2
|
|
|
3
|
+
//#region src/index.d.ts
|
|
3
4
|
declare const _default: {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
5
|
+
readonly meta: {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly version: string;
|
|
8
|
+
};
|
|
9
|
+
readonly rules: {
|
|
10
|
+
readonly "function-definition": _typescript_eslint_utils_ts_eslint0.RuleModule<"functionDefinition", [], unknown, _typescript_eslint_utils_ts_eslint0.RuleListener>;
|
|
11
|
+
readonly "function-name": _typescript_eslint_utils_ts_eslint0.RuleModule<"functionName", [], unknown, _typescript_eslint_utils_ts_eslint0.RuleListener>;
|
|
12
|
+
readonly "function-return-boolean": _typescript_eslint_utils_ts_eslint0.RuleModule<"functionReturnBoolean", readonly [{
|
|
13
|
+
readonly pattern?: string;
|
|
14
|
+
} | undefined], unknown, _typescript_eslint_utils_ts_eslint0.RuleListener>;
|
|
15
|
+
};
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
-
export { _default as default };
|
|
17
|
+
//#endregion
|
|
18
|
+
export { _default as default };
|
package/dist/index.js
CHANGED
|
@@ -1,174 +1,174 @@
|
|
|
1
|
-
import { ESLintUtils } from
|
|
2
|
-
import * as AST from
|
|
3
|
-
import * as ER from
|
|
4
|
-
import
|
|
5
|
-
import { RegExp } from
|
|
6
|
-
import { getConstrainedTypeAtLocation } from
|
|
7
|
-
import { AST_NODE_TYPES } from
|
|
8
|
-
import { unionConstituents } from
|
|
1
|
+
import { ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
import * as AST from "@eslint-react/ast";
|
|
3
|
+
import * as ER from "@eslint-react/core";
|
|
4
|
+
import "@eslint-react/eff";
|
|
5
|
+
import { RegExp } from "@eslint-react/kit";
|
|
6
|
+
import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils";
|
|
7
|
+
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
8
|
+
import { unionConstituents } from "ts-api-utils";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
//#region package.json
|
|
11
11
|
var name = "eslint-plugin-function";
|
|
12
|
-
var version = "0.0.
|
|
12
|
+
var version = "0.0.24";
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/utils/create-rule.ts
|
|
13
16
|
function getDocsUrl() {
|
|
14
|
-
|
|
17
|
+
return "TODO: add docs for local ESLint rules";
|
|
15
18
|
}
|
|
16
|
-
|
|
19
|
+
const createRule = ESLintUtils.RuleCreator(getDocsUrl);
|
|
17
20
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/rules/function-definition.ts
|
|
23
|
+
const RULE_NAME$2 = "function-definition";
|
|
24
|
+
const RULE_FEATURES$2 = [];
|
|
21
25
|
var function_definition_default = createRule({
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
create,
|
|
35
|
-
defaultOptions: []
|
|
26
|
+
meta: {
|
|
27
|
+
type: "problem",
|
|
28
|
+
docs: {
|
|
29
|
+
description: "Enforce a consistent function definition style.",
|
|
30
|
+
[Symbol.for("rule_features")]: RULE_FEATURES$2
|
|
31
|
+
},
|
|
32
|
+
messages: { functionDefinition: "" },
|
|
33
|
+
schema: []
|
|
34
|
+
},
|
|
35
|
+
name: RULE_NAME$2,
|
|
36
|
+
create: create$2,
|
|
37
|
+
defaultOptions: []
|
|
36
38
|
});
|
|
37
|
-
function create(context) {
|
|
38
|
-
|
|
39
|
+
function create$2(context) {
|
|
40
|
+
return {};
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/rules/function-name.ts
|
|
45
|
+
const RULE_NAME$1 = "function-name";
|
|
46
|
+
const RULE_FEATURES$1 = [];
|
|
44
47
|
var function_name_default = createRule({
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
create: create2,
|
|
58
|
-
defaultOptions: []
|
|
48
|
+
meta: {
|
|
49
|
+
type: "problem",
|
|
50
|
+
docs: {
|
|
51
|
+
description: "Enforce a consistent function naming style.",
|
|
52
|
+
[Symbol.for("rule_features")]: RULE_FEATURES$1
|
|
53
|
+
},
|
|
54
|
+
messages: { functionName: "" },
|
|
55
|
+
schema: []
|
|
56
|
+
},
|
|
57
|
+
name: RULE_NAME$1,
|
|
58
|
+
create: create$1,
|
|
59
|
+
defaultOptions: []
|
|
59
60
|
});
|
|
60
|
-
function
|
|
61
|
-
|
|
61
|
+
function create$1(context) {
|
|
62
|
+
return {};
|
|
62
63
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"truthy boolean"
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/rules/function-return-boolean.ts
|
|
67
|
+
const RULE_NAME = "function-return-boolean";
|
|
68
|
+
const RULE_FEATURES = [];
|
|
69
|
+
const defaultPattern = "/^(is|has|should)/u";
|
|
70
|
+
const defaultOptions = [{ pattern: defaultPattern }];
|
|
71
|
+
const allowedVariants = [
|
|
72
|
+
"never",
|
|
73
|
+
"boolean",
|
|
74
|
+
"falsy boolean",
|
|
75
|
+
"truthy boolean"
|
|
76
76
|
];
|
|
77
77
|
var function_return_boolean_default = createRule({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
},
|
|
98
|
-
name: RULE_NAME3,
|
|
99
|
-
create: create3,
|
|
100
|
-
defaultOptions
|
|
78
|
+
meta: {
|
|
79
|
+
type: "problem",
|
|
80
|
+
docs: {
|
|
81
|
+
description: `Enforce functions that match the pattern \`${defaultPattern}\` return a boolean.`,
|
|
82
|
+
[Symbol.for("rule_features")]: RULE_FEATURES
|
|
83
|
+
},
|
|
84
|
+
messages: { functionReturnBoolean: "The function '{{functionName}}' should return a boolean value (got {{variants}})." },
|
|
85
|
+
schema: [{
|
|
86
|
+
type: "object",
|
|
87
|
+
additionalProperties: false,
|
|
88
|
+
properties: { pattern: {
|
|
89
|
+
type: "string",
|
|
90
|
+
description: "The pattern to match function names against."
|
|
91
|
+
} }
|
|
92
|
+
}]
|
|
93
|
+
},
|
|
94
|
+
name: RULE_NAME,
|
|
95
|
+
create,
|
|
96
|
+
defaultOptions
|
|
101
97
|
});
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
98
|
+
function create(context, [opts]) {
|
|
99
|
+
const services = ESLintUtils.getParserServices(context, false);
|
|
100
|
+
const pattern = RegExp.toRegExp(opts?.pattern ?? defaultPattern);
|
|
101
|
+
const functionEntries = [];
|
|
102
|
+
function handleReturnExpression(context$1, returnExpression, onViolation) {
|
|
103
|
+
if (returnExpression == null) {
|
|
104
|
+
onViolation(returnExpression, { variants: "nullish" });
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const returnType = getConstrainedTypeAtLocation(services, returnExpression);
|
|
108
|
+
const parts = [...ER.getTypeVariants(unionConstituents(returnType))];
|
|
109
|
+
if (parts.every((part) => allowedVariants.some((allowed) => part === allowed))) return;
|
|
110
|
+
onViolation(returnExpression, { variants: [...parts].map((part) => `'${part}'`).join(", ") });
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
[":function"](node) {
|
|
114
|
+
const functionName = AST.getFunctionId(node)?.name;
|
|
115
|
+
const isMatched = functionName != null && pattern.test(functionName);
|
|
116
|
+
functionEntries.push({
|
|
117
|
+
functionName,
|
|
118
|
+
functionNode: node,
|
|
119
|
+
isMatched
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
[":function:exit"]() {
|
|
123
|
+
functionEntries.pop();
|
|
124
|
+
},
|
|
125
|
+
["ArrowFunctionExpression"](node) {
|
|
126
|
+
const { functionName, isMatched = false } = functionEntries.at(-1) ?? {};
|
|
127
|
+
if (functionName == null || !isMatched) return;
|
|
128
|
+
if (node.body.type === AST_NODE_TYPES.BlockStatement) return;
|
|
129
|
+
handleReturnExpression(context, node.body, (expr, data) => {
|
|
130
|
+
context.report({
|
|
131
|
+
messageId: "functionReturnBoolean",
|
|
132
|
+
node: expr ?? node,
|
|
133
|
+
data: {
|
|
134
|
+
...data,
|
|
135
|
+
functionName
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
},
|
|
140
|
+
["ReturnStatement"](node) {
|
|
141
|
+
const { functionName, functionNode, isMatched = false } = functionEntries.at(-1) ?? {};
|
|
142
|
+
if (functionName == null || functionNode == null || !isMatched) return;
|
|
143
|
+
handleReturnExpression(context, node.argument, (expr, data) => {
|
|
144
|
+
const functionName$1 = AST.getFunctionId(functionNode)?.name;
|
|
145
|
+
if (functionName$1 == null) return;
|
|
146
|
+
context.report({
|
|
147
|
+
messageId: "functionReturnBoolean",
|
|
148
|
+
node: expr ?? node.argument ?? node,
|
|
149
|
+
data: {
|
|
150
|
+
...data,
|
|
151
|
+
functionName: functionName$1
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
159
157
|
}
|
|
160
158
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/index.ts
|
|
161
|
+
var src_default = {
|
|
162
|
+
meta: {
|
|
163
|
+
name,
|
|
164
|
+
version
|
|
165
|
+
},
|
|
166
|
+
rules: {
|
|
167
|
+
"function-definition": function_definition_default,
|
|
168
|
+
"function-name": function_name_default,
|
|
169
|
+
"function-return-boolean": function_return_boolean_default
|
|
170
|
+
}
|
|
172
171
|
};
|
|
173
172
|
|
|
174
|
-
|
|
173
|
+
//#endregion
|
|
174
|
+
export { src_default as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-function",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.24",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "(WIP) The ESLint plugin for function-related rules.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"jiti": "^2.5.1",
|
|
43
43
|
"publint": "^0.3.12",
|
|
44
44
|
"skott": "^0.35.4",
|
|
45
|
-
"
|
|
45
|
+
"tsdown": "^0.14.1",
|
|
46
46
|
"typescript-eslint": "^8.40.0",
|
|
47
47
|
"vitest": "^3.2.4",
|
|
48
48
|
"@local/configs": "0.0.0"
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"node": ">=20.10.0"
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
|
-
"build": "
|
|
59
|
+
"build": "tsdown",
|
|
60
60
|
"build:pkgs": "pnpm -F \"./.pkgs/*\" run build",
|
|
61
61
|
"format:check": "dprint check",
|
|
62
62
|
"format:package-json": "pnpm m exec sort-package-json -q package.json && dprint fmt",
|