@eslint-react/var 4.2.4-beta.0 → 5.0.0-beta.0
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 +14 -1
- package/dist/index.js +171 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Scope } from "@typescript-eslint/scope-manager";
|
|
1
2
|
import { TSESTree } from "@typescript-eslint/types";
|
|
2
3
|
import { RuleContext } from "@eslint-react/shared";
|
|
3
4
|
|
|
@@ -52,6 +53,15 @@ declare function findEnclosingAssignmentTarget(node: TSESTree.Node): TSESTree.Ar
|
|
|
52
53
|
*/
|
|
53
54
|
type AssignmentTarget = ReturnType<typeof findEnclosingAssignmentTarget>;
|
|
54
55
|
//#endregion
|
|
56
|
+
//#region src/get-require-expression-arguments.d.ts
|
|
57
|
+
/**
|
|
58
|
+
* Get the arguments of a require expression
|
|
59
|
+
* @param node The node to match
|
|
60
|
+
* @returns The require expression arguments or null if the node is not a require expression
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
declare function getRequireExpressionArguments(node: TSESTree.Node): TSESTree.CallExpressionArgument[] | null;
|
|
64
|
+
//#endregion
|
|
55
65
|
//#region src/is-assignment-target-equal.d.ts
|
|
56
66
|
/**
|
|
57
67
|
* Check if two assignment targets are equal
|
|
@@ -110,4 +120,7 @@ declare function resolve(context: RuleContext, node: TSESTree.Identifier, option
|
|
|
110
120
|
localOnly: boolean;
|
|
111
121
|
}>): TSESTree.Node | null;
|
|
112
122
|
//#endregion
|
|
113
|
-
|
|
123
|
+
//#region src/resolve-import-source.d.ts
|
|
124
|
+
declare function resolveImportSource(name: string, initialScope: Scope, visited?: Set<string>): string | null;
|
|
125
|
+
//#endregion
|
|
126
|
+
export { AssignmentTarget, ObjectType, computeObjectType, findEnclosingAssignmentTarget, getRequireExpressionArguments, isAssignmentTargetEqual, isValueEqual, resolve, resolveImportSource };
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import { isIdentifier } from "@eslint-react/ast";
|
|
|
3
3
|
import { DefinitionType } from "@typescript-eslint/scope-manager";
|
|
4
4
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
5
5
|
import { findVariable, getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
6
|
+
import { P, match } from "ts-pattern";
|
|
6
7
|
|
|
7
8
|
//#region src/resolve.ts
|
|
8
9
|
/**
|
|
@@ -191,6 +192,155 @@ function findEnclosingAssignmentTarget(node) {
|
|
|
191
192
|
}
|
|
192
193
|
}
|
|
193
194
|
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region ../../../.pkgs/eff/dist/index.js
|
|
197
|
+
/**
|
|
198
|
+
* Returns its argument.
|
|
199
|
+
*
|
|
200
|
+
* @param x - The value to return.
|
|
201
|
+
* @returns The input value unchanged.
|
|
202
|
+
*/
|
|
203
|
+
function identity(x) {
|
|
204
|
+
return x;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Creates a function that can be used in a data-last (aka `pipe`able) or
|
|
208
|
+
* data-first style.
|
|
209
|
+
*
|
|
210
|
+
* The first parameter to `dual` is either the arity of the uncurried function
|
|
211
|
+
* or a predicate that determines if the function is being used in a data-first
|
|
212
|
+
* or data-last style.
|
|
213
|
+
*
|
|
214
|
+
* Using the arity is the most common use case, but there are some cases where
|
|
215
|
+
* you may want to use a predicate. For example, if you have a function that
|
|
216
|
+
* takes an optional argument, you can use a predicate to determine if the
|
|
217
|
+
* function is being used in a data-first or data-last style.
|
|
218
|
+
*
|
|
219
|
+
* You can pass either the arity of the uncurried function or a predicate
|
|
220
|
+
* which determines if the function is being used in a data-first or
|
|
221
|
+
* data-last style.
|
|
222
|
+
*
|
|
223
|
+
* **Example** (Using arity to determine data-first or data-last style)
|
|
224
|
+
*
|
|
225
|
+
* ```ts
|
|
226
|
+
* import { dual, pipe } from "effect/Function"
|
|
227
|
+
*
|
|
228
|
+
* const sum = dual<
|
|
229
|
+
* (that: number) => (self: number) => number,
|
|
230
|
+
* (self: number, that: number) => number
|
|
231
|
+
* >(2, (self, that) => self + that)
|
|
232
|
+
*
|
|
233
|
+
* console.log(sum(2, 3)) // 5
|
|
234
|
+
* console.log(pipe(2, sum(3))) // 5
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* **Example** (Using call signatures to define the overloads)
|
|
238
|
+
*
|
|
239
|
+
* ```ts
|
|
240
|
+
* import { dual, pipe } from "effect/Function"
|
|
241
|
+
*
|
|
242
|
+
* const sum: {
|
|
243
|
+
* (that: number): (self: number) => number
|
|
244
|
+
* (self: number, that: number): number
|
|
245
|
+
* } = dual(2, (self: number, that: number): number => self + that)
|
|
246
|
+
*
|
|
247
|
+
* console.log(sum(2, 3)) // 5
|
|
248
|
+
* console.log(pipe(2, sum(3))) // 5
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* **Example** (Using a predicate to determine data-first or data-last style)
|
|
252
|
+
*
|
|
253
|
+
* ```ts
|
|
254
|
+
* import { dual, pipe } from "effect/Function"
|
|
255
|
+
*
|
|
256
|
+
* const sum = dual<
|
|
257
|
+
* (that: number) => (self: number) => number,
|
|
258
|
+
* (self: number, that: number) => number
|
|
259
|
+
* >(
|
|
260
|
+
* (args) => args.length === 2,
|
|
261
|
+
* (self, that) => self + that
|
|
262
|
+
* )
|
|
263
|
+
*
|
|
264
|
+
* console.log(sum(2, 3)) // 5
|
|
265
|
+
* console.log(pipe(2, sum(3))) // 5
|
|
266
|
+
* ```
|
|
267
|
+
*
|
|
268
|
+
* @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.
|
|
269
|
+
* @param body - The function to be curried.
|
|
270
|
+
* @since 1.0.0
|
|
271
|
+
*/
|
|
272
|
+
const dual = function(arity, body) {
|
|
273
|
+
if (typeof arity === "function") return function() {
|
|
274
|
+
return arity(arguments) ? body.apply(this, arguments) : ((self) => body(self, ...arguments));
|
|
275
|
+
};
|
|
276
|
+
switch (arity) {
|
|
277
|
+
case 0:
|
|
278
|
+
case 1: throw new RangeError(`Invalid arity ${arity}`);
|
|
279
|
+
case 2: return function(a, b) {
|
|
280
|
+
if (arguments.length >= 2) return body(a, b);
|
|
281
|
+
return function(self) {
|
|
282
|
+
return body(self, a);
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
case 3: return function(a, b, c) {
|
|
286
|
+
if (arguments.length >= 3) return body(a, b, c);
|
|
287
|
+
return function(self) {
|
|
288
|
+
return body(self, a, b);
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
default: return function() {
|
|
292
|
+
if (arguments.length >= arity) return body.apply(this, arguments);
|
|
293
|
+
const args = arguments;
|
|
294
|
+
return function(self) {
|
|
295
|
+
return body(self, ...args);
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* 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`.
|
|
302
|
+
* The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
|
|
303
|
+
*
|
|
304
|
+
* @param self - The first function to apply (or the composed function in data-last style).
|
|
305
|
+
* @param bc - The second function to apply.
|
|
306
|
+
* @returns A composed function that applies both functions in sequence.
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* import * as assert from "node:assert"
|
|
310
|
+
* import { compose } from "effect/Function"
|
|
311
|
+
*
|
|
312
|
+
* const increment = (n: number) => n + 1;
|
|
313
|
+
* const square = (n: number) => n * n;
|
|
314
|
+
*
|
|
315
|
+
* assert.strictEqual(compose(increment, square)(2), 9);
|
|
316
|
+
* ```
|
|
317
|
+
*
|
|
318
|
+
* @since 1.0.0
|
|
319
|
+
*/
|
|
320
|
+
const compose = dual(2, (ab, bc) => (a) => bc(ab(a)));
|
|
321
|
+
|
|
322
|
+
//#endregion
|
|
323
|
+
//#region src/get-require-expression-arguments.ts
|
|
324
|
+
/**
|
|
325
|
+
* Get the arguments of a require expression
|
|
326
|
+
* @param node The node to match
|
|
327
|
+
* @returns The require expression arguments or null if the node is not a require expression
|
|
328
|
+
* @internal
|
|
329
|
+
*/
|
|
330
|
+
function getRequireExpressionArguments(node) {
|
|
331
|
+
return match(node).with({
|
|
332
|
+
type: AST_NODE_TYPES.CallExpression,
|
|
333
|
+
arguments: P.select(),
|
|
334
|
+
callee: {
|
|
335
|
+
type: AST_NODE_TYPES.Identifier,
|
|
336
|
+
name: "require"
|
|
337
|
+
}
|
|
338
|
+
}, identity).with({
|
|
339
|
+
type: AST_NODE_TYPES.MemberExpression,
|
|
340
|
+
object: P.select()
|
|
341
|
+
}, getRequireExpressionArguments).otherwise(() => null);
|
|
342
|
+
}
|
|
343
|
+
|
|
194
344
|
//#endregion
|
|
195
345
|
//#region src/is-value-equal.ts
|
|
196
346
|
const thisBlockTypes = [
|
|
@@ -275,4 +425,24 @@ function isAssignmentTargetEqual(context, a, b) {
|
|
|
275
425
|
}
|
|
276
426
|
|
|
277
427
|
//#endregion
|
|
278
|
-
|
|
428
|
+
//#region src/resolve-import-source.ts
|
|
429
|
+
function resolveImportSource(name, initialScope, visited = /* @__PURE__ */ new Set()) {
|
|
430
|
+
if (visited.has(name)) return null;
|
|
431
|
+
visited.add(name);
|
|
432
|
+
const latestDef = findVariable(initialScope, name)?.defs.at(-1);
|
|
433
|
+
if (latestDef == null) return null;
|
|
434
|
+
const { node, parent } = latestDef;
|
|
435
|
+
if (node.type === AST_NODE_TYPES.VariableDeclarator && node.init != null) {
|
|
436
|
+
const { init } = node;
|
|
437
|
+
if (init.type === AST_NODE_TYPES.MemberExpression && init.object.type === AST_NODE_TYPES.Identifier) return resolveImportSource(init.object.name, initialScope, visited);
|
|
438
|
+
if (init.type === AST_NODE_TYPES.Identifier) return resolveImportSource(init.name, initialScope, visited);
|
|
439
|
+
const arg0 = getRequireExpressionArguments(init)?.[0];
|
|
440
|
+
if (arg0 == null || !ast.isLiteral(arg0, "string")) return null;
|
|
441
|
+
return arg0.value;
|
|
442
|
+
}
|
|
443
|
+
if (parent?.type === AST_NODE_TYPES.ImportDeclaration) return parent.source.value;
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
//#endregion
|
|
448
|
+
export { computeObjectType, findEnclosingAssignmentTarget, getRequireExpressionArguments, isAssignmentTargetEqual, isValueEqual, resolve, resolveImportSource };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/var",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-beta.0",
|
|
4
4
|
"description": "ESLint React's TSESTree AST utility module for static analysis of variables.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"@typescript-eslint/types": "^8.58.0",
|
|
35
35
|
"@typescript-eslint/utils": "^8.58.0",
|
|
36
36
|
"ts-pattern": "^5.9.0",
|
|
37
|
-
"@eslint-react/ast": "
|
|
38
|
-
"@eslint-react/shared": "
|
|
37
|
+
"@eslint-react/ast": "5.0.0-beta.0",
|
|
38
|
+
"@eslint-react/shared": "5.0.0-beta.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@typescript-eslint/typescript-estree": "^8.58.0",
|