eslint-plugin-function 0.0.1 → 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
@@ -1,11 +1,5 @@
1
1
  import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
2
2
 
3
- type Options = [
4
- undefined | {
5
- pattern: string;
6
- }
7
- ];
8
-
9
3
  declare const _default: {
10
4
  readonly meta: {
11
5
  readonly name: string;
@@ -13,7 +7,9 @@ declare const _default: {
13
7
  };
14
8
  readonly rules: {
15
9
  readonly "function-definition": _typescript_eslint_utils_ts_eslint.RuleModule<"functionDefinition", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
16
- readonly "function-return-boolean": _typescript_eslint_utils_ts_eslint.RuleModule<"functionReturnBoolean", Options, 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>;
17
13
  };
18
14
  };
19
15
 
package/dist/index.js CHANGED
@@ -1,12 +1,107 @@
1
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
 
3
10
  // package.json
4
11
  var name = "eslint-plugin-function";
5
- var version = "0.0.0";
12
+ var version = "0.0.4";
6
13
  function getDocsUrl() {
7
14
  return "TODO: add docs for local ESLint rules";
8
15
  }
9
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
+ }
10
105
 
11
106
  // src/rules/function-definition.ts
12
107
  var RULE_NAME = "function-definition";
@@ -30,42 +125,101 @@ var function_definition_default = createRule({
30
125
  function create(context) {
31
126
  return {};
32
127
  }
33
-
34
- // src/rules/function-return-boolean.ts
35
128
  var RULE_NAME2 = "function-return-boolean";
36
129
  var RULE_FEATURES2 = [];
130
+ var defaultPattern = "/^(is|should)/u";
37
131
  var defaultOptions = [
38
132
  {
39
- pattern: "is[A-Z].*"
133
+ pattern: defaultPattern
40
134
  }
41
135
  ];
136
+ var allowedVariants = [
137
+ "boolean",
138
+ "falsy boolean",
139
+ "truthy boolean"
140
+ ];
42
141
  var function_return_boolean_default = createRule({
43
142
  meta: {
44
143
  type: "problem",
45
144
  docs: {
46
- description: "Enforce functions that match the pattern `is[A-Z].*` return a boolean.",
145
+ description: "Enforce functions that match the pattern `/^(is|should)/u` return a boolean.",
47
146
  [Symbol.for("rule_features")]: RULE_FEATURES2
48
147
  },
49
148
  messages: {
50
- functionReturnBoolean: "This function should return a boolean."
149
+ functionReturnBoolean: "The function '{{functionName}}' should return a boolean value (got {{variants}})."
51
150
  },
52
151
  schema: [{
53
152
  type: "object",
153
+ additionalProperties: false,
54
154
  properties: {
55
155
  pattern: {
56
156
  type: "string",
57
157
  description: "The pattern to match function names against."
58
158
  }
59
- },
60
- additionalProperties: false
159
+ }
61
160
  }]
62
161
  },
63
162
  name: RULE_NAME2,
64
163
  create: create2,
65
164
  defaultOptions
66
165
  });
67
- function create2(context) {
68
- return {};
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
+ };
69
223
  }
70
224
 
71
225
  // src/index.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-function",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "ESLint plugin for function",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -16,6 +16,7 @@
16
16
  ],
17
17
  "dependencies": {
18
18
  "@eslint-react/ast": "^1.38.0",
19
+ "@eslint-react/eff": "^1.38.0",
19
20
  "@eslint-react/kit": "^1.38.0",
20
21
  "@typescript-eslint/scope-manager": "^8.28.0",
21
22
  "@typescript-eslint/type-utils": "^8.28.0",
@@ -25,12 +26,24 @@
25
26
  "ts-pattern": "^5.6.2"
26
27
  },
27
28
  "devDependencies": {
29
+ "@eslint/markdown": "^6.3.0",
28
30
  "@tsconfig/node22": "^22.0.1",
29
31
  "@tsconfig/strictest": "^2.0.5",
30
- "tsup": "^8.4.0"
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"
31
43
  },
32
44
  "peerDependencies": {
33
45
  "eslint": "^8.57.0 || ^9.0.0",
46
+ "ts-api-utils": "^2.1.0",
34
47
  "typescript": "^4.9.5 || ^5.3.3"
35
48
  },
36
49
  "engines": {