eslint-plugin-function 0.0.22 → 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/README.md CHANGED
@@ -19,8 +19,8 @@ npm install --save-dev eslint-plugin-function
19
19
 
20
20
  // @ts-check
21
21
  import eslintJs from "@eslint/js";
22
- import tseslint from "typescript-eslint";
23
22
  import pluginFunction from "eslint-plugin-function";
23
+ import tseslint from "typescript-eslint";
24
24
 
25
25
  export default tseslint.config({
26
26
  files: ["**/*.ts", "**/*.tsx"],
package/dist/index.d.ts CHANGED
@@ -1,17 +1,18 @@
1
- import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
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
- 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-name": _typescript_eslint_utils_ts_eslint.RuleModule<"functionName", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
11
- readonly "function-return-boolean": _typescript_eslint_utils_ts_eslint.RuleModule<"functionReturnBoolean", readonly [{
12
- readonly pattern?: string;
13
- } | undefined], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
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,262 +1,174 @@
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';
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
- // package.json
10
+ //#region package.json
11
11
  var name = "eslint-plugin-function";
12
- var version = "0.0.22";
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
- }
12
+ var version = "0.0.24";
97
13
 
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 };
14
+ //#endregion
15
+ //#region src/utils/create-rule.ts
16
+ function getDocsUrl() {
17
+ return "TODO: add docs for local ESLint rules";
104
18
  }
19
+ const createRule = ESLintUtils.RuleCreator(getDocsUrl);
105
20
 
106
- // src/rules/function-definition.ts
107
- var RULE_NAME = "function-definition";
108
- var RULE_FEATURES = [];
21
+ //#endregion
22
+ //#region src/rules/function-definition.ts
23
+ const RULE_NAME$2 = "function-definition";
24
+ const RULE_FEATURES$2 = [];
109
25
  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: []
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: []
124
38
  });
125
- function create(context) {
126
- return {};
39
+ function create$2(context) {
40
+ return {};
127
41
  }
128
42
 
129
- // src/rules/function-name.ts
130
- var RULE_NAME2 = "function-name";
131
- var RULE_FEATURES2 = [];
43
+ //#endregion
44
+ //#region src/rules/function-name.ts
45
+ const RULE_NAME$1 = "function-name";
46
+ const RULE_FEATURES$1 = [];
132
47
  var function_name_default = createRule({
133
- meta: {
134
- type: "problem",
135
- docs: {
136
- description: "Enforce a consistent function naming style.",
137
- [Symbol.for("rule_features")]: RULE_FEATURES2
138
- },
139
- messages: {
140
- functionName: ""
141
- },
142
- schema: []
143
- },
144
- name: RULE_NAME2,
145
- create: create2,
146
- 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: []
147
60
  });
148
- function create2(context) {
149
- return {};
61
+ function create$1(context) {
62
+ return {};
150
63
  }
151
- var RULE_NAME3 = "function-return-boolean";
152
- var RULE_FEATURES3 = [];
153
- var defaultPattern = "/^(is|has|should)/u";
154
- var defaultOptions = [
155
- {
156
- pattern: defaultPattern
157
- }
158
- ];
159
- var allowedVariants = [
160
- "never",
161
- "boolean",
162
- "falsy boolean",
163
- "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"
164
76
  ];
165
77
  var function_return_boolean_default = createRule({
166
- meta: {
167
- type: "problem",
168
- docs: {
169
- description: `Enforce functions that match the pattern \`${defaultPattern}\` return a boolean.`,
170
- [Symbol.for("rule_features")]: RULE_FEATURES3
171
- },
172
- messages: {
173
- functionReturnBoolean: "The function '{{functionName}}' should return a boolean value (got {{variants}})."
174
- },
175
- schema: [{
176
- type: "object",
177
- additionalProperties: false,
178
- properties: {
179
- pattern: {
180
- type: "string",
181
- description: "The pattern to match function names against."
182
- }
183
- }
184
- }]
185
- },
186
- name: RULE_NAME3,
187
- create: create3,
188
- 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
189
97
  });
190
- function create3(context, [opts]) {
191
- const services = ESLintUtils.getParserServices(context, false);
192
- const pattern = toRegExp(opts?.pattern ?? defaultPattern);
193
- const functionEntries = [];
194
- function handleReturnExpression(context2, returnExpression, onViolation) {
195
- if (returnExpression == null) {
196
- onViolation(returnExpression, { variants: "nullish" });
197
- return;
198
- }
199
- const returnType = getConstrainedTypeAtLocation(services, returnExpression);
200
- const parts = [...inspectVariantTypes(unionConstituents(returnType))];
201
- if (parts.every((part) => allowedVariants.some((allowed) => part === allowed))) return;
202
- onViolation(returnExpression, {
203
- variants: [...parts].map((part) => `'${part}'`).join(", ")
204
- });
205
- }
206
- return {
207
- [":function"](node) {
208
- const functionName = AST.getFunctionId(node)?.name;
209
- const isMatched = functionName != null && pattern.test(functionName);
210
- functionEntries.push({ functionName, functionNode: node, isMatched });
211
- },
212
- [":function:exit"]() {
213
- functionEntries.pop();
214
- },
215
- ["ArrowFunctionExpression"](node) {
216
- const { functionName, isMatched = false } = functionEntries.at(-1) ?? {};
217
- if (functionName == null || !isMatched) return;
218
- if (node.body.type === AST_NODE_TYPES.BlockStatement) return;
219
- handleReturnExpression(context, node.body, (expr, data) => {
220
- context.report({
221
- messageId: "functionReturnBoolean",
222
- node: expr ?? node,
223
- data: {
224
- ...data,
225
- functionName
226
- }
227
- });
228
- });
229
- },
230
- ["ReturnStatement"](node) {
231
- const { functionName, functionNode, isMatched = false } = functionEntries.at(-1) ?? {};
232
- if (functionName == null || functionNode == null || !isMatched) return;
233
- handleReturnExpression(context, node.argument, (expr, data) => {
234
- const functionName2 = AST.getFunctionId(functionNode)?.name;
235
- if (functionName2 == null) return;
236
- context.report({
237
- messageId: "functionReturnBoolean",
238
- node: expr ?? node.argument ?? node,
239
- data: {
240
- ...data,
241
- functionName: functionName2
242
- }
243
- });
244
- });
245
- }
246
- };
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
+ };
247
157
  }
248
158
 
249
- // src/index.ts
250
- var index_default = {
251
- meta: {
252
- name,
253
- version
254
- },
255
- rules: {
256
- "function-definition": function_definition_default,
257
- "function-name": function_name_default,
258
- "function-return-boolean": function_return_boolean_default
259
- }
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
+ }
260
171
  };
261
172
 
262
- export { index_default as default };
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.22",
3
+ "version": "0.0.24",
4
4
  "private": false,
5
5
  "description": "(WIP) The ESLint plugin for function-related rules.",
6
6
  "license": "MIT",
@@ -18,32 +18,33 @@
18
18
  "package.json"
19
19
  ],
20
20
  "dependencies": {
21
- "@eslint-react/ast": "^2.0.0-next.4",
22
- "@eslint-react/eff": "^2.0.0-next.4",
23
- "@eslint-react/kit": "^2.0.0-next.4",
24
- "@typescript-eslint/scope-manager": "^8.33.1",
25
- "@typescript-eslint/type-utils": "^8.33.1",
26
- "@typescript-eslint/types": "^8.33.1",
27
- "@typescript-eslint/utils": "^8.33.1",
21
+ "@eslint-react/ast": "^2.0.0-beta.34",
22
+ "@eslint-react/core": "^2.0.0-beta.34",
23
+ "@eslint-react/eff": "^2.0.0-beta.34",
24
+ "@eslint-react/kit": "^2.0.0-beta.34",
25
+ "@typescript-eslint/scope-manager": "^8.40.0",
26
+ "@typescript-eslint/type-utils": "^8.40.0",
27
+ "@typescript-eslint/types": "^8.40.0",
28
+ "@typescript-eslint/utils": "^8.40.0",
28
29
  "string-ts": "^2.2.1",
29
- "ts-pattern": "^5.7.1"
30
+ "ts-pattern": "^5.8.0"
30
31
  },
31
32
  "devDependencies": {
32
- "@eslint/markdown": "^6.5.0",
33
+ "@eslint/markdown": "^7.1.0",
33
34
  "@tsconfig/node22": "^22.0.2",
34
35
  "@tsconfig/strictest": "^2.0.5",
35
- "@types/node": "^22.15.30",
36
- "@typescript-eslint/rule-tester": "^8.33.1",
36
+ "@types/node": "^24.3.0",
37
+ "@typescript-eslint/rule-tester": "^8.40.0",
37
38
  "dedent": "^1.6.0",
38
- "eslint": "^9.28.0",
39
+ "eslint": "^9.33.0",
39
40
  "eslint-config-flat-gitignore": "^2.1.0",
40
41
  "eslint-plugin-vitest": "^0.5.4",
41
- "jiti": "^2.4.2",
42
+ "jiti": "^2.5.1",
42
43
  "publint": "^0.3.12",
43
44
  "skott": "^0.35.4",
44
- "tsup": "^8.5.0",
45
- "typescript-eslint": "^8.33.1",
46
- "vitest": "^3.2.2",
45
+ "tsdown": "^0.14.1",
46
+ "typescript-eslint": "^8.40.0",
47
+ "vitest": "^3.2.4",
47
48
  "@local/configs": "0.0.0"
48
49
  },
49
50
  "peerDependencies": {
@@ -55,7 +56,7 @@
55
56
  "node": ">=20.10.0"
56
57
  },
57
58
  "scripts": {
58
- "build": "tsup",
59
+ "build": "tsdown",
59
60
  "build:pkgs": "pnpm -F \"./.pkgs/*\" run build",
60
61
  "format:check": "dprint check",
61
62
  "format:package-json": "pnpm m exec sort-package-json -q package.json && dprint fmt",