eslint-plugin-function 0.2.4 → 0.2.8

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.
Files changed (2) hide show
  1. package/dist/index.js +134 -14
  2. package/package.json +11 -11
package/dist/index.js CHANGED
@@ -4,13 +4,12 @@ import { P, isMatching, match } from "ts-pattern";
4
4
  import ts from "typescript";
5
5
  import * as AST from "@eslint-react/ast";
6
6
  import { toRegExp } from "@eslint-react/shared";
7
- import "@let/eff";
8
7
  import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils";
9
8
  import { AST_NODE_TYPES } from "@typescript-eslint/types";
10
9
 
11
10
  //#region package.json
12
11
  var name = "eslint-plugin-function";
13
- var version = "0.2.3";
12
+ var version = "0.2.7";
14
13
 
15
14
  //#endregion
16
15
  //#region src/utils/create-rule.ts
@@ -142,6 +141,119 @@ function create$1(context) {
142
141
  return {};
143
142
  }
144
143
 
144
+ //#endregion
145
+ //#region ../../node_modules/.pnpm/@jsr+let__eff@0.1.2/node_modules/@jsr/let__eff/src/index.js
146
+ /**
147
+ * Creates a function that can be used in a data-last (aka `pipe`able) or
148
+ * data-first style.
149
+ *
150
+ * The first parameter to `dual` is either the arity of the uncurried function
151
+ * or a predicate that determines if the function is being used in a data-first
152
+ * or data-last style.
153
+ *
154
+ * Using the arity is the most common use case, but there are some cases where
155
+ * you may want to use a predicate. For example, if you have a function that
156
+ * takes an optional argument, you can use a predicate to determine if the
157
+ * function is being used in a data-first or data-last style.
158
+ *
159
+ * You can pass either the arity of the uncurried function or a predicate
160
+ * which determines if the function is being used in a data-first or
161
+ * data-last style.
162
+ *
163
+ * **Example** (Using arity to determine data-first or data-last style)
164
+ *
165
+ * ```ts
166
+ * import { dual, pipe } from "effect/Function"
167
+ *
168
+ * const sum = dual<
169
+ * (that: number) => (self: number) => number,
170
+ * (self: number, that: number) => number
171
+ * >(2, (self, that) => self + that)
172
+ *
173
+ * console.log(sum(2, 3)) // 5
174
+ * console.log(pipe(2, sum(3))) // 5
175
+ * ```
176
+ *
177
+ * **Example** (Using call signatures to define the overloads)
178
+ *
179
+ * ```ts
180
+ * import { dual, pipe } from "effect/Function"
181
+ *
182
+ * const sum: {
183
+ * (that: number): (self: number) => number
184
+ * (self: number, that: number): number
185
+ * } = dual(2, (self: number, that: number): number => self + that)
186
+ *
187
+ * console.log(sum(2, 3)) // 5
188
+ * console.log(pipe(2, sum(3))) // 5
189
+ * ```
190
+ *
191
+ * **Example** (Using a predicate to determine data-first or data-last style)
192
+ *
193
+ * ```ts
194
+ * import { dual, pipe } from "effect/Function"
195
+ *
196
+ * const sum = dual<
197
+ * (that: number) => (self: number) => number,
198
+ * (self: number, that: number) => number
199
+ * >(
200
+ * (args) => args.length === 2,
201
+ * (self, that) => self + that
202
+ * )
203
+ *
204
+ * console.log(sum(2, 3)) // 5
205
+ * console.log(pipe(2, sum(3))) // 5
206
+ * ```
207
+ *
208
+ * @param arity - The arity of the uncurried function or a predicate that determines if the function is being used in a data-first or data-last style.
209
+ * @param body - The function to be curried.
210
+ * @since 1.0.0
211
+ */ const dual = function(arity, body) {
212
+ if (typeof arity === "function") return function() {
213
+ return arity(arguments) ? body.apply(this, arguments) : (self) => body(self, ...arguments);
214
+ };
215
+ switch (arity) {
216
+ case 0:
217
+ case 1: throw new RangeError(`Invalid arity ${arity}`);
218
+ case 2: return function(a, b) {
219
+ if (arguments.length >= 2) return body(a, b);
220
+ return function(self) {
221
+ return body(self, a);
222
+ };
223
+ };
224
+ case 3: return function(a, b, c) {
225
+ if (arguments.length >= 3) return body(a, b, c);
226
+ return function(self) {
227
+ return body(self, a, b);
228
+ };
229
+ };
230
+ default: return function() {
231
+ if (arguments.length >= arity) return body.apply(this, arguments);
232
+ const args = arguments;
233
+ return function(self) {
234
+ return body(self, ...args);
235
+ };
236
+ };
237
+ }
238
+ };
239
+ /**
240
+ * Composes two functions, `ab` and `bc` into a single function that takes in an argument `a` of type `A` and returns a result of type `C`.
241
+ * The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * import * as assert from "node:assert"
246
+ * import { compose } from "effect/Function"
247
+ *
248
+ * const increment = (n: number) => n + 1;
249
+ * const square = (n: number) => n * n;
250
+ *
251
+ * assert.strictEqual(compose(increment, square)(2), 9);
252
+ * ```
253
+ *
254
+ * @since 1.0.0
255
+ */ const compose = dual(2, (ab, bc) => (a) => bc(ab(a)));
256
+
145
257
  //#endregion
146
258
  //#region src/rules/function-return-boolean.ts
147
259
  const RULE_NAME = "function-return-boolean";
@@ -190,20 +302,24 @@ function create(context, [opts]) {
190
302
  }
191
303
  return {
192
304
  [":function"](node) {
193
- const functionName = AST.getFunctionId(node)?.name;
194
- const isMatched = functionName != null && pattern.test(functionName);
305
+ const functionId = AST.getFunctionId(node);
195
306
  functionEntries.push({
196
- functionName,
197
- functionNode: node,
198
- isMatched
307
+ functionId,
308
+ functionNode: node
199
309
  });
200
310
  },
201
311
  [":function:exit"]() {
202
312
  functionEntries.pop();
203
313
  },
204
314
  ["ArrowFunctionExpression"](node) {
205
- const { functionName, isMatched = false } = functionEntries.at(-1) ?? {};
206
- if (functionName == null || !isMatched) return;
315
+ const { functionId, functionNode } = functionEntries.at(-1) ?? {};
316
+ if (functionId == null || functionNode == null) return;
317
+ const functionName = match(functionId).with({ type: AST_NODE_TYPES.Identifier }, (id) => id.name).with({
318
+ type: AST_NODE_TYPES.MemberExpression,
319
+ property: { type: AST_NODE_TYPES.Identifier }
320
+ }, (me) => me.property.name).otherwise(() => null);
321
+ if (functionName == null) return;
322
+ if (!pattern.test(functionName)) return;
207
323
  if (node.body.type === AST_NODE_TYPES.BlockStatement) return;
208
324
  handleReturnExpression(context, node.body, (expr, data) => {
209
325
  context.report({
@@ -217,17 +333,21 @@ function create(context, [opts]) {
217
333
  });
218
334
  },
219
335
  ["ReturnStatement"](node) {
220
- const { functionName, functionNode, isMatched = false } = functionEntries.at(-1) ?? {};
221
- if (functionName == null || functionNode == null || !isMatched) return;
336
+ const { functionId, functionNode } = functionEntries.at(-1) ?? {};
337
+ if (functionId == null || functionNode == null) return;
222
338
  handleReturnExpression(context, node.argument, (expr, data) => {
223
- const functionName$1 = AST.getFunctionId(functionNode)?.name;
224
- if (functionName$1 == null) return;
339
+ const functionName = match(functionId).with({ type: AST_NODE_TYPES.Identifier }, (id) => id.name).with({
340
+ type: AST_NODE_TYPES.MemberExpression,
341
+ property: { type: AST_NODE_TYPES.Identifier }
342
+ }, (me) => me.property.name).otherwise(() => null);
343
+ if (functionName == null) return;
344
+ if (!pattern.test(functionName)) return;
225
345
  context.report({
226
346
  messageId: "functionReturnBoolean",
227
347
  node: expr ?? node.argument ?? node,
228
348
  data: {
229
349
  ...data,
230
- functionName: functionName$1
350
+ functionName
231
351
  }
232
352
  });
233
353
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-function",
3
- "version": "0.2.4",
3
+ "version": "0.2.8",
4
4
  "private": false,
5
5
  "description": "(WIP) An ESLint plugin for function-related rules.",
6
6
  "homepage": "https://github.com/Rel1cx/dx",
@@ -27,23 +27,23 @@
27
27
  "package.json"
28
28
  ],
29
29
  "dependencies": {
30
- "@eslint-react/ast": "^2.5.3",
31
- "@eslint-react/shared": "^2.5.3",
32
- "@let/eff": "npm:@jsr/let__eff@^0.1.2",
33
- "@typescript-eslint/scope-manager": "^8.52.0",
34
- "@typescript-eslint/type-utils": "^8.52.0",
35
- "@typescript-eslint/types": "^8.52.0",
36
- "@typescript-eslint/utils": "^8.52.0",
30
+ "@eslint-react/ast": "^2.7.2",
31
+ "@eslint-react/shared": "^2.7.2",
32
+ "@typescript-eslint/scope-manager": "^8.53.0",
33
+ "@typescript-eslint/type-utils": "^8.53.0",
34
+ "@typescript-eslint/types": "^8.53.0",
35
+ "@typescript-eslint/utils": "^8.53.0",
37
36
  "string-ts": "^2.3.1",
38
37
  "ts-api-utils": "^2.4.0",
39
38
  "ts-pattern": "^5.9.0"
40
39
  },
41
40
  "devDependencies": {
42
- "@types/node": "^25.0.3",
43
- "@typescript-eslint/rule-tester": "^8.52.0",
41
+ "@let/eff": "npm:@jsr/let__eff@^0.1.2",
42
+ "@types/node": "^25.0.9",
43
+ "@typescript-eslint/rule-tester": "^8.53.0",
44
44
  "dedent": "^1.7.1",
45
45
  "eslint": "^9.39.2",
46
- "tsdown": "^0.19.0-beta.5",
46
+ "tsdown": "^0.20.0-beta.3",
47
47
  "tsl": "^1.0.28"
48
48
  },
49
49
  "peerDependencies": {