eslint-plugin-function 0.0.0 → 0.0.4

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 REL1CX
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -0,0 +1,16 @@
1
+ import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
2
+
3
+ declare const _default: {
4
+ readonly meta: {
5
+ readonly name: string;
6
+ readonly version: string;
7
+ };
8
+ readonly rules: {
9
+ readonly "function-definition": _typescript_eslint_utils_ts_eslint.RuleModule<"functionDefinition", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
10
+ readonly "function-return-boolean": _typescript_eslint_utils_ts_eslint.RuleModule<"functionReturnBoolean", readonly [{
11
+ readonly pattern?: string;
12
+ } | undefined], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
13
+ };
14
+ };
15
+
16
+ export { _default as default };
package/dist/index.js CHANGED
@@ -1,2 +1,237 @@
1
- export {}
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ import { unionConstituents, isTypeFlagSet, isTrueLiteralType, isFalseLiteralType } from 'ts-api-utils';
3
+ import { match, isMatching, P } from 'ts-pattern';
4
+ import ts from 'typescript';
5
+ import * as AST from '@eslint-react/ast';
6
+ import '@eslint-react/eff';
7
+ import { getConstrainedTypeAtLocation } from '@typescript-eslint/type-utils';
8
+ import { AST_NODE_TYPES } from '@typescript-eslint/types';
2
9
 
10
+ // package.json
11
+ var name = "eslint-plugin-function";
12
+ var version = "0.0.4";
13
+ function getDocsUrl() {
14
+ return "TODO: add docs for local ESLint rules";
15
+ }
16
+ var createRule = ESLintUtils.RuleCreator(getDocsUrl);
17
+ var tsHelpers = {
18
+ isAnyType: (type) => isTypeFlagSet(type, ts.TypeFlags.TypeParameter | ts.TypeFlags.Any),
19
+ isBigIntType: (type) => isTypeFlagSet(type, ts.TypeFlags.BigIntLike),
20
+ isBooleanType: (type) => isTypeFlagSet(type, ts.TypeFlags.BooleanLike),
21
+ isEnumType: (type) => isTypeFlagSet(type, ts.TypeFlags.EnumLike),
22
+ isFalsyBigIntType: (type) => type.isLiteral() && isMatching({ value: { base10Value: "0" } }, type),
23
+ isFalsyNumberType: (type) => type.isNumberLiteral() && type.value === 0,
24
+ isFalsyStringType: (type) => type.isStringLiteral() && type.value === "",
25
+ isNeverType: (type) => isTypeFlagSet(type, ts.TypeFlags.Never),
26
+ isNullishType: (type) => isTypeFlagSet(
27
+ type,
28
+ ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.VoidLike
29
+ ),
30
+ isNumberType: (type) => isTypeFlagSet(type, ts.TypeFlags.NumberLike),
31
+ isObjectType: (type) => !isTypeFlagSet(
32
+ type,
33
+ ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.VoidLike | ts.TypeFlags.BooleanLike | ts.TypeFlags.StringLike | ts.TypeFlags.NumberLike | ts.TypeFlags.BigIntLike | ts.TypeFlags.TypeParameter | ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never
34
+ ),
35
+ isStringType: (type) => isTypeFlagSet(type, ts.TypeFlags.StringLike),
36
+ isTruthyBigIntType: (type) => type.isLiteral() && isMatching({ value: { base10Value: P.not("0") } }, type),
37
+ isTruthyNumberType: (type) => type.isNumberLiteral() && type.value !== 0,
38
+ isTruthyStringType: (type) => type.isStringLiteral() && type.value !== "",
39
+ isUnknownType: (type) => isTypeFlagSet(type, ts.TypeFlags.Unknown)
40
+ };
41
+
42
+ // src/utils/inspect-variant-types.ts
43
+ function inspectVariantTypes(types) {
44
+ const variantTypes = /* @__PURE__ */ new Set();
45
+ if (types.some(tsHelpers.isUnknownType)) {
46
+ variantTypes.add("unknown");
47
+ return variantTypes;
48
+ }
49
+ if (types.some(tsHelpers.isNullishType)) {
50
+ variantTypes.add("nullish");
51
+ }
52
+ const booleans = types.filter(tsHelpers.isBooleanType);
53
+ switch (true) {
54
+ case (booleans.length === 1 && booleans[0] != null): {
55
+ const first = booleans[0];
56
+ if (isTrueLiteralType(first)) {
57
+ variantTypes.add("truthy boolean");
58
+ } else if (isFalseLiteralType(first)) {
59
+ variantTypes.add("falsy boolean");
60
+ }
61
+ break;
62
+ }
63
+ case booleans.length === 2: {
64
+ variantTypes.add("boolean");
65
+ break;
66
+ }
67
+ }
68
+ const strings = types.filter(tsHelpers.isStringType);
69
+ if (strings.length > 0) {
70
+ const evaluated = match(strings).when((types2) => types2.every(tsHelpers.isTruthyStringType), () => "truthy string").when((types2) => types2.every(tsHelpers.isFalsyStringType), () => "falsy string").otherwise(() => "string");
71
+ variantTypes.add(evaluated);
72
+ }
73
+ const bigints = types.filter(tsHelpers.isBigIntType);
74
+ if (bigints.length > 0) {
75
+ const evaluated = match(bigints).when((types2) => types2.every(tsHelpers.isTruthyBigIntType), () => "truthy bigint").when((types2) => types2.every(tsHelpers.isFalsyBigIntType), () => "falsy bigint").otherwise(() => "bigint");
76
+ variantTypes.add(evaluated);
77
+ }
78
+ const numbers = types.filter(tsHelpers.isNumberType);
79
+ if (numbers.length > 0) {
80
+ const evaluated = match(numbers).when((types2) => types2.every(tsHelpers.isTruthyNumberType), () => "truthy number").when((types2) => types2.every(tsHelpers.isFalsyNumberType), () => "falsy number").otherwise(() => "number");
81
+ variantTypes.add(evaluated);
82
+ }
83
+ if (types.some(tsHelpers.isEnumType)) {
84
+ variantTypes.add("enum");
85
+ }
86
+ if (types.some(tsHelpers.isObjectType)) {
87
+ variantTypes.add("object");
88
+ }
89
+ if (types.some(tsHelpers.isAnyType)) {
90
+ variantTypes.add("any");
91
+ }
92
+ if (types.some(tsHelpers.isNeverType)) {
93
+ variantTypes.add("never");
94
+ }
95
+ return variantTypes;
96
+ }
97
+
98
+ // src/utils/regexp.ts
99
+ var RE_REGEXP_STR = /^\/(.+)\/([A-Za-z]*)$/u;
100
+ function toRegExp(string) {
101
+ const [, pattern, flags = "u"] = RE_REGEXP_STR.exec(string) ?? [];
102
+ if (pattern) return new RegExp(pattern, flags);
103
+ return { test: (s) => s === string };
104
+ }
105
+
106
+ // src/rules/function-definition.ts
107
+ var RULE_NAME = "function-definition";
108
+ var RULE_FEATURES = [];
109
+ var function_definition_default = createRule({
110
+ meta: {
111
+ type: "problem",
112
+ docs: {
113
+ description: "Enforce a consistent function definition style.",
114
+ [Symbol.for("rule_features")]: RULE_FEATURES
115
+ },
116
+ messages: {
117
+ functionDefinition: ""
118
+ },
119
+ schema: []
120
+ },
121
+ name: RULE_NAME,
122
+ create,
123
+ defaultOptions: []
124
+ });
125
+ function create(context) {
126
+ return {};
127
+ }
128
+ var RULE_NAME2 = "function-return-boolean";
129
+ var RULE_FEATURES2 = [];
130
+ var defaultPattern = "/^(is|should)/u";
131
+ var defaultOptions = [
132
+ {
133
+ pattern: defaultPattern
134
+ }
135
+ ];
136
+ var allowedVariants = [
137
+ "boolean",
138
+ "falsy boolean",
139
+ "truthy boolean"
140
+ ];
141
+ var function_return_boolean_default = createRule({
142
+ meta: {
143
+ type: "problem",
144
+ docs: {
145
+ description: "Enforce functions that match the pattern `/^(is|should)/u` return a boolean.",
146
+ [Symbol.for("rule_features")]: RULE_FEATURES2
147
+ },
148
+ messages: {
149
+ functionReturnBoolean: "The function '{{functionName}}' should return a boolean value (got {{variants}})."
150
+ },
151
+ schema: [{
152
+ type: "object",
153
+ additionalProperties: false,
154
+ properties: {
155
+ pattern: {
156
+ type: "string",
157
+ description: "The pattern to match function names against."
158
+ }
159
+ }
160
+ }]
161
+ },
162
+ name: RULE_NAME2,
163
+ create: create2,
164
+ defaultOptions
165
+ });
166
+ function create2(context, [opts]) {
167
+ const services = ESLintUtils.getParserServices(context, false);
168
+ const pattern = toRegExp(opts?.pattern ?? defaultPattern);
169
+ const functionEntries = [];
170
+ function handleReturnExpression(context2, returnExpression, onViolation) {
171
+ if (returnExpression == null) {
172
+ onViolation(returnExpression, { variants: "nullish" });
173
+ return;
174
+ }
175
+ const returnType = getConstrainedTypeAtLocation(services, returnExpression);
176
+ const parts = inspectVariantTypes(unionConstituents(returnType));
177
+ if (allowedVariants.some((variant) => parts.has(variant))) return;
178
+ onViolation(returnExpression, {
179
+ variants: [...parts].map((part) => `'${part}'`).join(", ")
180
+ });
181
+ }
182
+ return {
183
+ [":function"](node) {
184
+ const functionName = AST.getFunctionIdentifier(node)?.name;
185
+ const isMatched = functionName != null && pattern.test(functionName);
186
+ functionEntries.push({ functionName, functionNode: node, isMatched });
187
+ },
188
+ [":function:exit"]() {
189
+ functionEntries.pop();
190
+ },
191
+ ["ArrowFunctionExpression"](node) {
192
+ const { functionName, isMatched = false } = functionEntries.at(-1) ?? {};
193
+ if (functionName == null || !isMatched) return;
194
+ if (node.body.type === AST_NODE_TYPES.BlockStatement) return;
195
+ handleReturnExpression(context, node.body, (expr, data) => {
196
+ context.report({
197
+ messageId: "functionReturnBoolean",
198
+ node: expr ?? node,
199
+ data: {
200
+ ...data,
201
+ functionName
202
+ }
203
+ });
204
+ });
205
+ },
206
+ ["ReturnStatement"](node) {
207
+ const { functionName, functionNode, isMatched = false } = functionEntries.at(-1) ?? {};
208
+ if (functionName == null || functionNode == null || !isMatched) return;
209
+ handleReturnExpression(context, node.argument, (expr, data) => {
210
+ const functionName2 = AST.getFunctionIdentifier(functionNode)?.name;
211
+ if (functionName2 == null) return;
212
+ context.report({
213
+ messageId: "functionReturnBoolean",
214
+ node: expr ?? node.argument ?? node,
215
+ data: {
216
+ ...data,
217
+ functionName: functionName2
218
+ }
219
+ });
220
+ });
221
+ }
222
+ };
223
+ }
224
+
225
+ // src/index.ts
226
+ var index_default = {
227
+ meta: {
228
+ name,
229
+ version
230
+ },
231
+ rules: {
232
+ "function-definition": function_definition_default,
233
+ "function-return-boolean": function_return_boolean_default
234
+ }
235
+ };
236
+
237
+ export { index_default as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-function",
3
- "version": "0.0.0",
3
+ "version": "0.0.4",
4
4
  "description": "ESLint plugin for function",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -14,12 +14,36 @@
14
14
  "dist",
15
15
  "package.json"
16
16
  ],
17
- "dependencies": {},
17
+ "dependencies": {
18
+ "@eslint-react/ast": "^1.38.0",
19
+ "@eslint-react/eff": "^1.38.0",
20
+ "@eslint-react/kit": "^1.38.0",
21
+ "@typescript-eslint/scope-manager": "^8.28.0",
22
+ "@typescript-eslint/type-utils": "^8.28.0",
23
+ "@typescript-eslint/types": "^8.28.0",
24
+ "@typescript-eslint/utils": "^8.28.0",
25
+ "string-ts": "^2.2.1",
26
+ "ts-pattern": "^5.6.2"
27
+ },
18
28
  "devDependencies": {
19
- "tsup": "^8.4.0"
29
+ "@eslint/markdown": "^6.3.0",
30
+ "@tsconfig/node22": "^22.0.1",
31
+ "@tsconfig/strictest": "^2.0.5",
32
+ "@types/node": "^22.13.13",
33
+ "@typescript-eslint/rule-tester": "^8.28.0",
34
+ "dedent": "^1.5.3",
35
+ "eslint": "^9.23.0",
36
+ "eslint-config-flat-gitignore": "^2.1.0",
37
+ "eslint-plugin-vitest": "^0.5.4",
38
+ "jiti": "^2.4.2",
39
+ "tsup": "^8.4.0",
40
+ "typescript-eslint": "^8.28.0",
41
+ "vitest": "^3.0.9",
42
+ "@local/configs": "0.0.0"
20
43
  },
21
44
  "peerDependencies": {
22
45
  "eslint": "^8.57.0 || ^9.0.0",
46
+ "ts-api-utils": "^2.1.0",
23
47
  "typescript": "^4.9.5 || ^5.3.3"
24
48
  },
25
49
  "engines": {