@eslint-react/jsx 5.24.1 → 5.24.3
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.js +0 -907
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -5,44 +5,6 @@ import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
|
5
5
|
import { P, match } from "ts-pattern";
|
|
6
6
|
|
|
7
7
|
//#region ../../.pkgs/eff/dist/index.js
|
|
8
|
-
/**
|
|
9
|
-
* Applies a `pipe` method's variadic arguments to an initial value from left
|
|
10
|
-
* to right.
|
|
11
|
-
*
|
|
12
|
-
* **When to use**
|
|
13
|
-
*
|
|
14
|
-
* Use to implement a custom `.pipe(...)` method from JavaScript's `arguments`
|
|
15
|
-
* object.
|
|
16
|
-
*
|
|
17
|
-
* **Details**
|
|
18
|
-
*
|
|
19
|
-
* This helper is intended for implementing `Pipeable.pipe` methods that
|
|
20
|
-
* receive JavaScript's `arguments` object. With no functions it returns the
|
|
21
|
-
* original value; otherwise it feeds each result into the next function.
|
|
22
|
-
*
|
|
23
|
-
* **Example** (Implementing a pipe method)
|
|
24
|
-
*
|
|
25
|
-
* ```ts
|
|
26
|
-
* import { Pipeable } from "effect"
|
|
27
|
-
*
|
|
28
|
-
* class NumberBox {
|
|
29
|
-
* constructor(readonly value: number) {}
|
|
30
|
-
*
|
|
31
|
-
* pipe(..._fns: ReadonlyArray<(value: number) => number>): number {
|
|
32
|
-
* return Pipeable.pipeArguments(this.value, arguments) as number
|
|
33
|
-
* }
|
|
34
|
-
* }
|
|
35
|
-
*
|
|
36
|
-
* const result = new NumberBox(5).pipe(
|
|
37
|
-
* (n) => n + 2,
|
|
38
|
-
* (n) => n * 3
|
|
39
|
-
* )
|
|
40
|
-
* result // => 21
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* @category combinators
|
|
44
|
-
* @since 2.0.0
|
|
45
|
-
*/
|
|
46
8
|
const pipeArguments = (self, args) => {
|
|
47
9
|
switch (args.length) {
|
|
48
10
|
case 0: return self;
|
|
@@ -62,100 +24,14 @@ const pipeArguments = (self, args) => {
|
|
|
62
24
|
}
|
|
63
25
|
}
|
|
64
26
|
};
|
|
65
|
-
/**
|
|
66
|
-
* Reusable prototype that implements `Pipeable.pipe`.
|
|
67
|
-
*
|
|
68
|
-
* **When to use**
|
|
69
|
-
*
|
|
70
|
-
* Use when classes or object prototypes can reuse this value when they need the
|
|
71
|
-
* standard pipe implementation backed by `pipeArguments`.
|
|
72
|
-
*
|
|
73
|
-
* @category prototypes
|
|
74
|
-
* @since 3.15.0
|
|
75
|
-
*/
|
|
76
27
|
const Prototype = { pipe() {
|
|
77
28
|
return pipeArguments(this, arguments);
|
|
78
29
|
} };
|
|
79
|
-
/**
|
|
80
|
-
* Provides a base constructor whose instances implement the standard `Pipeable.pipe`
|
|
81
|
-
* method.
|
|
82
|
-
*
|
|
83
|
-
* **When to use**
|
|
84
|
-
*
|
|
85
|
-
* Use when you need to define a class that supports Effect-style method
|
|
86
|
-
* chaining through `.pipe(...)`.
|
|
87
|
-
*
|
|
88
|
-
* @category constructors
|
|
89
|
-
* @since 3.15.0
|
|
90
|
-
*/
|
|
91
30
|
const Class = (function() {
|
|
92
31
|
function PipeableBase() {}
|
|
93
32
|
PipeableBase.prototype = Prototype;
|
|
94
33
|
return PipeableBase;
|
|
95
34
|
})();
|
|
96
|
-
/**
|
|
97
|
-
* Creates a function that can be called in data-first style or data-last
|
|
98
|
-
* (`pipe`-friendly) style.
|
|
99
|
-
*
|
|
100
|
-
* **When to use**
|
|
101
|
-
*
|
|
102
|
-
* Use to expose one implementation through both direct and `pipe`-friendly
|
|
103
|
-
* call styles.
|
|
104
|
-
*
|
|
105
|
-
* **Details**
|
|
106
|
-
*
|
|
107
|
-
* Pass either the arity of the uncurried function or a predicate that decides
|
|
108
|
-
* whether the current call is data-first. Arity is the common case. Use a
|
|
109
|
-
* predicate when optional arguments make arity ambiguous.
|
|
110
|
-
*
|
|
111
|
-
* **Example** (Selecting data-first or data-last style by arity)
|
|
112
|
-
*
|
|
113
|
-
* ```ts
|
|
114
|
-
* import { Function, pipe } from "effect"
|
|
115
|
-
*
|
|
116
|
-
* const sum = Function.dual<
|
|
117
|
-
* (that: number) => (self: number) => number,
|
|
118
|
-
* (self: number, that: number) => number
|
|
119
|
-
* >(2, (self, that) => self + that)
|
|
120
|
-
*
|
|
121
|
-
* sum(2, 3) // => 5
|
|
122
|
-
* pipe(2, sum(3)) // => 5
|
|
123
|
-
* ```
|
|
124
|
-
*
|
|
125
|
-
* **Example** (Defining overloads with call signatures)
|
|
126
|
-
*
|
|
127
|
-
* ```ts
|
|
128
|
-
* import { Function, pipe } from "effect"
|
|
129
|
-
*
|
|
130
|
-
* const sum: {
|
|
131
|
-
* (that: number): (self: number) => number
|
|
132
|
-
* (self: number, that: number): number
|
|
133
|
-
* } = Function.dual(2, (self: number, that: number): number => self + that)
|
|
134
|
-
*
|
|
135
|
-
* sum(2, 3) // => 5
|
|
136
|
-
* pipe(2, sum(3)) // => 5
|
|
137
|
-
* ```
|
|
138
|
-
*
|
|
139
|
-
* **Example** (Selecting data-first or data-last style with a predicate)
|
|
140
|
-
*
|
|
141
|
-
* ```ts
|
|
142
|
-
* import { Function, pipe } from "effect"
|
|
143
|
-
*
|
|
144
|
-
* const sum = Function.dual<
|
|
145
|
-
* (that: number) => (self: number) => number,
|
|
146
|
-
* (self: number, that: number) => number
|
|
147
|
-
* >(
|
|
148
|
-
* (args) => args.length === 2,
|
|
149
|
-
* (self, that) => self + that
|
|
150
|
-
* )
|
|
151
|
-
*
|
|
152
|
-
* sum(2, 3) // => 5
|
|
153
|
-
* pipe(2, sum(3)) // => 5
|
|
154
|
-
* ```
|
|
155
|
-
*
|
|
156
|
-
* @category combinators
|
|
157
|
-
* @since 2.0.0
|
|
158
|
-
*/
|
|
159
35
|
const dual = function(arity, body) {
|
|
160
36
|
if (typeof arity === "function") return function() {
|
|
161
37
|
return arity(arguments) ? body.apply(this, arguments) : ((self) => body(self, ...arguments));
|
|
@@ -184,555 +60,32 @@ const dual = function(arity, body) {
|
|
|
184
60
|
};
|
|
185
61
|
}
|
|
186
62
|
};
|
|
187
|
-
/**
|
|
188
|
-
* Returns its input argument unchanged.
|
|
189
|
-
*
|
|
190
|
-
* **When to use**
|
|
191
|
-
*
|
|
192
|
-
* Use to return a value unchanged where a function is required.
|
|
193
|
-
*
|
|
194
|
-
* **Example** (Returning the same value)
|
|
195
|
-
*
|
|
196
|
-
* ```ts
|
|
197
|
-
* import { identity } from "effect"
|
|
198
|
-
*
|
|
199
|
-
* identity(5) // => 5
|
|
200
|
-
* ```
|
|
201
|
-
*
|
|
202
|
-
* @category combinators
|
|
203
|
-
* @since 2.0.0
|
|
204
|
-
*/
|
|
205
63
|
const identity = (a) => a;
|
|
206
|
-
/**
|
|
207
|
-
* Returns the input value with a different static type.
|
|
208
|
-
*
|
|
209
|
-
* **When to use**
|
|
210
|
-
*
|
|
211
|
-
* Use when you need an explicit type-level cast and accept that the value is
|
|
212
|
-
* returned unchanged at runtime.
|
|
213
|
-
*
|
|
214
|
-
* **Gotchas**
|
|
215
|
-
*
|
|
216
|
-
* This is a type-level cast only; it performs no runtime validation or
|
|
217
|
-
* conversion.
|
|
218
|
-
*
|
|
219
|
-
* @see {@link satisfies} for checking assignability without changing the resulting type
|
|
220
|
-
*
|
|
221
|
-
* @category utility types
|
|
222
|
-
* @since 4.0.0
|
|
223
|
-
*/
|
|
224
64
|
const cast = identity;
|
|
225
|
-
/**
|
|
226
|
-
* Creates a zero-argument function that always returns the provided value.
|
|
227
|
-
*
|
|
228
|
-
* **When to use**
|
|
229
|
-
*
|
|
230
|
-
* Use when you need a thunk or callback that returns the same value on every
|
|
231
|
-
* invocation.
|
|
232
|
-
*
|
|
233
|
-
* **Example** (Creating a constant thunk)
|
|
234
|
-
*
|
|
235
|
-
* ```ts
|
|
236
|
-
* import { Function } from "effect"
|
|
237
|
-
*
|
|
238
|
-
* const constNull = Function.constant(null)
|
|
239
|
-
*
|
|
240
|
-
* constNull() // => null
|
|
241
|
-
* constNull() // => null
|
|
242
|
-
* ```
|
|
243
|
-
*
|
|
244
|
-
* @category constructors
|
|
245
|
-
* @since 2.0.0
|
|
246
|
-
*/
|
|
247
65
|
const constant = (value) => () => value;
|
|
248
|
-
/**
|
|
249
|
-
* Returns `true` when called.
|
|
250
|
-
*
|
|
251
|
-
* **When to use**
|
|
252
|
-
*
|
|
253
|
-
* Use when you need a thunk that returns `true` on every invocation.
|
|
254
|
-
*
|
|
255
|
-
* **Example** (Returning true from a thunk)
|
|
256
|
-
*
|
|
257
|
-
* ```ts
|
|
258
|
-
* import { Function } from "effect"
|
|
259
|
-
*
|
|
260
|
-
* Function.constTrue() // => true
|
|
261
|
-
* ```
|
|
262
|
-
*
|
|
263
|
-
* @category constants
|
|
264
|
-
* @since 2.0.0
|
|
265
|
-
*/
|
|
266
66
|
const constTrue = constant(true);
|
|
267
|
-
/**
|
|
268
|
-
* Returns `false` when called.
|
|
269
|
-
*
|
|
270
|
-
* **When to use**
|
|
271
|
-
*
|
|
272
|
-
* Use when you need a thunk that returns `false` on every invocation.
|
|
273
|
-
*
|
|
274
|
-
* **Example** (Returning false from a thunk)
|
|
275
|
-
*
|
|
276
|
-
* ```ts
|
|
277
|
-
* import { Function } from "effect"
|
|
278
|
-
*
|
|
279
|
-
* Function.constFalse() // => false
|
|
280
|
-
* ```
|
|
281
|
-
*
|
|
282
|
-
* @category constants
|
|
283
|
-
* @since 2.0.0
|
|
284
|
-
*/
|
|
285
67
|
const constFalse = constant(false);
|
|
286
|
-
/**
|
|
287
|
-
* Returns `null` when called.
|
|
288
|
-
*
|
|
289
|
-
* **When to use**
|
|
290
|
-
*
|
|
291
|
-
* Use when you need a thunk that returns `null` on every invocation.
|
|
292
|
-
*
|
|
293
|
-
* **Example** (Returning null from a thunk)
|
|
294
|
-
*
|
|
295
|
-
* ```ts
|
|
296
|
-
* import { Function } from "effect"
|
|
297
|
-
*
|
|
298
|
-
* Function.constNull() // => null
|
|
299
|
-
* ```
|
|
300
|
-
*
|
|
301
|
-
* @category constants
|
|
302
|
-
* @since 2.0.0
|
|
303
|
-
*/
|
|
304
68
|
const constNull = constant(null);
|
|
305
|
-
/**
|
|
306
|
-
* Returns `undefined` when called.
|
|
307
|
-
*
|
|
308
|
-
* **When to use**
|
|
309
|
-
*
|
|
310
|
-
* Use when you need a thunk that returns `undefined` on every invocation.
|
|
311
|
-
*
|
|
312
|
-
* **Example** (Returning undefined from a thunk)
|
|
313
|
-
*
|
|
314
|
-
* ```ts
|
|
315
|
-
* import { Function } from "effect"
|
|
316
|
-
*
|
|
317
|
-
* Function.constUndefined() // => undefined
|
|
318
|
-
* ```
|
|
319
|
-
*
|
|
320
|
-
* @category constants
|
|
321
|
-
* @since 2.0.0
|
|
322
|
-
*/
|
|
323
69
|
const constUndefined = constant(void 0);
|
|
324
|
-
/**
|
|
325
|
-
* 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`.
|
|
326
|
-
* The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
|
|
327
|
-
*
|
|
328
|
-
* **When to use**
|
|
329
|
-
*
|
|
330
|
-
* Use to compose exactly two unary functions into a reusable unary function.
|
|
331
|
-
*
|
|
332
|
-
* **Example** (Composing two functions)
|
|
333
|
-
*
|
|
334
|
-
* ```ts
|
|
335
|
-
* import { Function } from "effect"
|
|
336
|
-
*
|
|
337
|
-
* const increment = (n: number) => n + 1
|
|
338
|
-
* const square = (n: number) => n * n
|
|
339
|
-
*
|
|
340
|
-
* Function.compose(increment, square)(2) // => 9
|
|
341
|
-
* ```
|
|
342
|
-
*
|
|
343
|
-
* @see {@link flow} for composing a left-to-right sequence of functions
|
|
344
|
-
* @see {@link pipe} for applying a value through a left-to-right sequence immediately
|
|
345
|
-
*
|
|
346
|
-
* @category combinators
|
|
347
|
-
* @since 2.0.0
|
|
348
|
-
*/
|
|
349
70
|
const compose = dual(2, (ab, bc) => (a) => bc(ab(a)));
|
|
350
|
-
/**
|
|
351
|
-
* Marks an impossible branch by accepting a `never` value and returning any
|
|
352
|
-
* type.
|
|
353
|
-
*
|
|
354
|
-
* **When to use**
|
|
355
|
-
*
|
|
356
|
-
* Use when you need a return value in a branch that exhaustive checks prove
|
|
357
|
-
* cannot be reached.
|
|
358
|
-
*
|
|
359
|
-
* **Gotchas**
|
|
360
|
-
*
|
|
361
|
-
* Calling `absurd` throws, because a value of type `never` should be
|
|
362
|
-
* impossible at runtime.
|
|
363
|
-
*
|
|
364
|
-
* **Example** (Handling impossible values)
|
|
365
|
-
*
|
|
366
|
-
* ```ts
|
|
367
|
-
* import { absurd } from "effect"
|
|
368
|
-
*
|
|
369
|
-
* const handleNever = (value: never) => {
|
|
370
|
-
* return absurd(value) // This will throw an error if called
|
|
371
|
-
* }
|
|
372
|
-
* ```
|
|
373
|
-
*
|
|
374
|
-
* @category utility types
|
|
375
|
-
* @since 2.0.0
|
|
376
|
-
*/
|
|
377
71
|
const absurd = (_) => {
|
|
378
72
|
throw new Error("Called `absurd` function which should be uncallable");
|
|
379
73
|
};
|
|
380
|
-
/**
|
|
381
|
-
* Creates a compile-time placeholder for a value of any type.
|
|
382
|
-
*
|
|
383
|
-
* **When to use**
|
|
384
|
-
*
|
|
385
|
-
* Use as a temporary typed placeholder while developing incomplete code.
|
|
386
|
-
*
|
|
387
|
-
* **Gotchas**
|
|
388
|
-
*
|
|
389
|
-
* `hole` is intended for temporary development use. If the placeholder is
|
|
390
|
-
* evaluated at runtime, it throws.
|
|
391
|
-
*
|
|
392
|
-
* **Example** (Creating a development placeholder)
|
|
393
|
-
*
|
|
394
|
-
* ```ts
|
|
395
|
-
* import { hole } from "effect"
|
|
396
|
-
*
|
|
397
|
-
* // Intentionally not called: `hole` throws if the placeholder is evaluated.
|
|
398
|
-
* const buildUser = (id: number): { readonly id: number; readonly name: string } => ({
|
|
399
|
-
* id,
|
|
400
|
-
* name: hole<string>()
|
|
401
|
-
* })
|
|
402
|
-
*
|
|
403
|
-
* ```
|
|
404
|
-
*
|
|
405
|
-
* @category utility types
|
|
406
|
-
* @since 2.0.0
|
|
407
|
-
*/
|
|
408
74
|
const hole = cast(absurd);
|
|
409
|
-
/**
|
|
410
|
-
* Creates a predicate that returns `true` only if both predicates are `true`.
|
|
411
|
-
*
|
|
412
|
-
* **When to use**
|
|
413
|
-
*
|
|
414
|
-
* Use when you want to combine `Predicate`s with AND, accepting values that
|
|
415
|
-
* satisfy multiple conditions, including refinements that narrow to an
|
|
416
|
-
* intersection.
|
|
417
|
-
*
|
|
418
|
-
* **Details**
|
|
419
|
-
*
|
|
420
|
-
* Evaluation short-circuits on the first `false`. For refinements, the output
|
|
421
|
-
* type is an intersection.
|
|
422
|
-
*
|
|
423
|
-
* **Example** (Checking both conditions)
|
|
424
|
-
*
|
|
425
|
-
* ```ts
|
|
426
|
-
* import { Predicate } from "effect"
|
|
427
|
-
*
|
|
428
|
-
* const hasAAndB = Predicate.and(
|
|
429
|
-
* Predicate.hasProperty("a"),
|
|
430
|
-
* Predicate.hasProperty("b")
|
|
431
|
-
* )
|
|
432
|
-
*
|
|
433
|
-
* const input: unknown = JSON.parse(`{"a":1,"b":"ok"}`)
|
|
434
|
-
* if (hasAAndB(input)) {
|
|
435
|
-
* // input has both properties at this point
|
|
436
|
-
* const a = input.a
|
|
437
|
-
* const b = input.b
|
|
438
|
-
*
|
|
439
|
-
* const values = [a, b] // => [1, "ok"]
|
|
440
|
-
* }
|
|
441
|
-
* ```
|
|
442
|
-
*
|
|
443
|
-
* @see {@link or}
|
|
444
|
-
* @see {@link not}
|
|
445
|
-
* @category combinators
|
|
446
|
-
* @since 2.0.0
|
|
447
|
-
*/
|
|
448
75
|
const and = dual(2, (a, b) => (data) => a(data) && b(data));
|
|
449
|
-
/**
|
|
450
|
-
* Creates a predicate that returns `true` if either predicate is `true`.
|
|
451
|
-
*
|
|
452
|
-
* **When to use**
|
|
453
|
-
*
|
|
454
|
-
* Use when you want to combine `Predicate`s with OR, accepting values that
|
|
455
|
-
* satisfy at least one condition, including refinements that narrow to a union.
|
|
456
|
-
*
|
|
457
|
-
* **Details**
|
|
458
|
-
*
|
|
459
|
-
* Evaluation short-circuits on the first `true`. For refinements, the output
|
|
460
|
-
* type is a union.
|
|
461
|
-
*
|
|
462
|
-
* **Example** (Checking either condition)
|
|
463
|
-
*
|
|
464
|
-
* ```ts
|
|
465
|
-
* import { Predicate } from "effect"
|
|
466
|
-
*
|
|
467
|
-
* const isStringOrNumber = Predicate.or(Predicate.isString, Predicate.isNumber)
|
|
468
|
-
*
|
|
469
|
-
* isStringOrNumber("a") // => true
|
|
470
|
-
* ```
|
|
471
|
-
*
|
|
472
|
-
* @see {@link and}
|
|
473
|
-
* @see {@link xor}
|
|
474
|
-
* @category combinators
|
|
475
|
-
* @since 2.0.0
|
|
476
|
-
*/
|
|
477
76
|
const or = dual(2, (a, b) => (data) => a(data) || b(data));
|
|
478
|
-
/**
|
|
479
|
-
* Creates a predicate that returns `true` if exactly one predicate is `true`.
|
|
480
|
-
*
|
|
481
|
-
* **When to use**
|
|
482
|
-
*
|
|
483
|
-
* Use when you want to combine two `Predicate`s with exclusive-or semantics.
|
|
484
|
-
*
|
|
485
|
-
* **Details**
|
|
486
|
-
*
|
|
487
|
-
* Returns `true` when results differ.
|
|
488
|
-
*
|
|
489
|
-
* **Example** (Checking exclusive-or conditions)
|
|
490
|
-
*
|
|
491
|
-
* ```ts
|
|
492
|
-
* import { Predicate } from "effect"
|
|
493
|
-
*
|
|
494
|
-
* const isEven = (n: number) => n % 2 === 0
|
|
495
|
-
* const isPositive = (n: number) => n > 0
|
|
496
|
-
* const either = Predicate.xor(isEven, isPositive)
|
|
497
|
-
*
|
|
498
|
-
* either(-2) // => true
|
|
499
|
-
* ```
|
|
500
|
-
*
|
|
501
|
-
* @see {@link or}
|
|
502
|
-
* @see {@link and}
|
|
503
|
-
* @category combinators
|
|
504
|
-
* @since 2.0.0
|
|
505
|
-
*/
|
|
506
77
|
const xor = dual(2, (a, b) => (data) => a(data) !== b(data));
|
|
507
|
-
/**
|
|
508
|
-
* Creates a predicate that returns `true` when both predicates agree.
|
|
509
|
-
*
|
|
510
|
-
* **When to use**
|
|
511
|
-
*
|
|
512
|
-
* Use when you want to check equivalence of two `Predicate`s.
|
|
513
|
-
*
|
|
514
|
-
* **Details**
|
|
515
|
-
*
|
|
516
|
-
* Returns `true` when both results are equal.
|
|
517
|
-
*
|
|
518
|
-
* **Example** (Defining equivalence)
|
|
519
|
-
*
|
|
520
|
-
* ```ts
|
|
521
|
-
* import { Predicate } from "effect"
|
|
522
|
-
*
|
|
523
|
-
* const isEven = (n: number) => n % 2 === 0
|
|
524
|
-
* const same = Predicate.eqv(isEven, isEven)
|
|
525
|
-
*
|
|
526
|
-
* same(3) // => true
|
|
527
|
-
* ```
|
|
528
|
-
*
|
|
529
|
-
* @see {@link xor}
|
|
530
|
-
* @category combinators
|
|
531
|
-
* @since 2.0.0
|
|
532
|
-
*/
|
|
533
78
|
const eqv = dual(2, (a, b) => (data) => a(data) === b(data));
|
|
534
|
-
/**
|
|
535
|
-
* Creates a predicate representing logical implication: if `antecedent`, then `consequent`.
|
|
536
|
-
*
|
|
537
|
-
* **When to use**
|
|
538
|
-
*
|
|
539
|
-
* Use when you need to encode logical implication between `Predicate` rules,
|
|
540
|
-
* where one rule only applies when a precondition holds.
|
|
541
|
-
*
|
|
542
|
-
* **Details**
|
|
543
|
-
*
|
|
544
|
-
* Models constraints like "if A then B" and returns `true` when the antecedent
|
|
545
|
-
* is `false`.
|
|
546
|
-
*
|
|
547
|
-
* **Example** (Checking implication)
|
|
548
|
-
*
|
|
549
|
-
* ```ts
|
|
550
|
-
* import { Predicate } from "effect"
|
|
551
|
-
*
|
|
552
|
-
* const isAdult = (age: number) => age >= 18
|
|
553
|
-
* const canVote = (age: number) => age >= 18
|
|
554
|
-
* const implies = Predicate.implies(isAdult, canVote)
|
|
555
|
-
*
|
|
556
|
-
* implies(16) // => true
|
|
557
|
-
* ```
|
|
558
|
-
*
|
|
559
|
-
* @see {@link and}
|
|
560
|
-
* @see {@link or}
|
|
561
|
-
* @category combinators
|
|
562
|
-
* @since 2.0.0
|
|
563
|
-
*/
|
|
564
79
|
const implies = dual(2, (antecedent, consequent) => (data) => !antecedent(data) || consequent(data));
|
|
565
|
-
/**
|
|
566
|
-
* Creates a predicate that returns `true` when neither predicate is `true`.
|
|
567
|
-
*
|
|
568
|
-
* **When to use**
|
|
569
|
-
*
|
|
570
|
-
* Use when you want to combine two `Predicate`s with logical NOR semantics.
|
|
571
|
-
*
|
|
572
|
-
* **Details**
|
|
573
|
-
*
|
|
574
|
-
* Returns the negation of `or`.
|
|
575
|
-
*
|
|
576
|
-
* **Example** (Checking NOR conditions)
|
|
577
|
-
*
|
|
578
|
-
* ```ts
|
|
579
|
-
* import { Predicate } from "effect"
|
|
580
|
-
*
|
|
581
|
-
* const neither = Predicate.nor(Predicate.isString, Predicate.isNumber)
|
|
582
|
-
*
|
|
583
|
-
* neither(true) // => true
|
|
584
|
-
* ```
|
|
585
|
-
*
|
|
586
|
-
* @see {@link or}
|
|
587
|
-
* @see {@link not}
|
|
588
|
-
* @category combinators
|
|
589
|
-
* @since 2.0.0
|
|
590
|
-
*/
|
|
591
80
|
const nor = dual(2, (a, b) => (data) => !a(data) && !b(data));
|
|
592
|
-
/**
|
|
593
|
-
* Creates a predicate that returns `true` unless both predicates are `true`.
|
|
594
|
-
*
|
|
595
|
-
* **When to use**
|
|
596
|
-
*
|
|
597
|
-
* Use when you want to combine two `Predicate`s with logical NAND semantics.
|
|
598
|
-
*
|
|
599
|
-
* **Details**
|
|
600
|
-
*
|
|
601
|
-
* Returns the negation of `and`.
|
|
602
|
-
*
|
|
603
|
-
* **Example** (Checking NAND conditions)
|
|
604
|
-
*
|
|
605
|
-
* ```ts
|
|
606
|
-
* import { Predicate } from "effect"
|
|
607
|
-
*
|
|
608
|
-
* const notBoth = Predicate.nand(Predicate.isString, Predicate.isNumber)
|
|
609
|
-
*
|
|
610
|
-
* notBoth("a") // => true
|
|
611
|
-
* ```
|
|
612
|
-
*
|
|
613
|
-
* @see {@link and}
|
|
614
|
-
* @see {@link not}
|
|
615
|
-
* @category combinators
|
|
616
|
-
* @since 2.0.0
|
|
617
|
-
*/
|
|
618
81
|
const nand = dual(2, (a, b) => (data) => !a(data) || !b(data));
|
|
619
|
-
/**
|
|
620
|
-
* Checks whether a value is a `function`.
|
|
621
|
-
*
|
|
622
|
-
* **When to use**
|
|
623
|
-
*
|
|
624
|
-
* Use when you need a `Predicate` guard to narrow an `unknown` value to a
|
|
625
|
-
* callable function.
|
|
626
|
-
*
|
|
627
|
-
* **Details**
|
|
628
|
-
*
|
|
629
|
-
* Uses `typeof input === "function"`.
|
|
630
|
-
*
|
|
631
|
-
* **Example** (Guarding functions)
|
|
632
|
-
*
|
|
633
|
-
* ```ts
|
|
634
|
-
* import { Predicate } from "effect"
|
|
635
|
-
*
|
|
636
|
-
* const data: unknown = () => 1
|
|
637
|
-
*
|
|
638
|
-
* if (Predicate.isFunction(data)) {
|
|
639
|
-
* data() // => 1
|
|
640
|
-
* }
|
|
641
|
-
* ```
|
|
642
|
-
*
|
|
643
|
-
* @see {@link isObjectKeyword}
|
|
644
|
-
* @category guards
|
|
645
|
-
* @since 2.0.0
|
|
646
|
-
*/
|
|
647
82
|
function isFunction(input) {
|
|
648
83
|
return typeof input === "function";
|
|
649
84
|
}
|
|
650
|
-
/**
|
|
651
|
-
* Checks whether a value is an `object` in the JavaScript sense (objects, arrays, functions).
|
|
652
|
-
*
|
|
653
|
-
* **When to use**
|
|
654
|
-
*
|
|
655
|
-
* Use when you need a `Predicate` guard that accepts arrays and functions as
|
|
656
|
-
* well as objects.
|
|
657
|
-
*
|
|
658
|
-
* **Details**
|
|
659
|
-
*
|
|
660
|
-
* Returns `true` for arrays and functions, and `false` for `null`.
|
|
661
|
-
*
|
|
662
|
-
* **Example** (Checking object keywords)
|
|
663
|
-
*
|
|
664
|
-
* ```ts
|
|
665
|
-
* import { Predicate } from "effect"
|
|
666
|
-
*
|
|
667
|
-
* Predicate.isObjectKeyword(() => 1) // => true
|
|
668
|
-
* Predicate.isObjectKeyword(null) // => false
|
|
669
|
-
* ```
|
|
670
|
-
*
|
|
671
|
-
* @see {@link isObject}
|
|
672
|
-
* @see {@link isObjectOrArray}
|
|
673
|
-
* @category guards
|
|
674
|
-
* @since 4.0.0
|
|
675
|
-
*/
|
|
676
85
|
function isObjectKeyword(input) {
|
|
677
86
|
return typeof input === "object" && input !== null || isFunction(input);
|
|
678
87
|
}
|
|
679
|
-
/**
|
|
680
|
-
* Checks whether a value has a given property key.
|
|
681
|
-
*
|
|
682
|
-
* **When to use**
|
|
683
|
-
*
|
|
684
|
-
* Use when you need a `Predicate` guard for property access on `unknown`
|
|
685
|
-
* values with a simple structural object check.
|
|
686
|
-
*
|
|
687
|
-
* **Details**
|
|
688
|
-
*
|
|
689
|
-
* Uses the `in` operator and `isObjectKeyword`. This does not check property
|
|
690
|
-
* value types.
|
|
691
|
-
*
|
|
692
|
-
* **Example** (Guarding object properties)
|
|
693
|
-
*
|
|
694
|
-
* ```ts
|
|
695
|
-
* import { Predicate } from "effect"
|
|
696
|
-
*
|
|
697
|
-
* const hasName = Predicate.hasProperty("name")
|
|
698
|
-
* const data: unknown = { name: "Ada" }
|
|
699
|
-
*
|
|
700
|
-
* if (hasName(data)) {
|
|
701
|
-
* data.name // => "Ada"
|
|
702
|
-
* }
|
|
703
|
-
* ```
|
|
704
|
-
*
|
|
705
|
-
* @see {@link isTagged}
|
|
706
|
-
* @see {@link isObjectKeyword}
|
|
707
|
-
* @category guards
|
|
708
|
-
* @since 2.0.0
|
|
709
|
-
*/
|
|
710
88
|
const hasProperty = dual(2, (data, property) => isObjectKeyword(data) && property in data);
|
|
711
|
-
/**
|
|
712
|
-
* Drops elements from the start while the predicate holds, returning the rest.
|
|
713
|
-
*
|
|
714
|
-
* **When to use**
|
|
715
|
-
*
|
|
716
|
-
* Use to remove a leading prefix of elements that satisfy a predicate.
|
|
717
|
-
*
|
|
718
|
-
* **Details**
|
|
719
|
-
*
|
|
720
|
-
* The predicate receives `(element, index)`.
|
|
721
|
-
*
|
|
722
|
-
* **Example** (Dropping while condition holds)
|
|
723
|
-
*
|
|
724
|
-
* ```ts
|
|
725
|
-
* import { Array } from "effect"
|
|
726
|
-
*
|
|
727
|
-
* Array.dropWhile([1, 2, 3, 4, 5], (x) => x < 4) // => [4, 5]
|
|
728
|
-
* ```
|
|
729
|
-
*
|
|
730
|
-
* @see {@link takeWhile} — keep the matching prefix instead
|
|
731
|
-
* @see {@link drop} — drop a fixed count
|
|
732
|
-
*
|
|
733
|
-
* @category getters
|
|
734
|
-
* @since 2.0.0
|
|
735
|
-
*/
|
|
736
89
|
const dropWhile = dual(2, (self, predicate) => {
|
|
737
90
|
const input = Array.isArray(self) ? self : Array.from(self);
|
|
738
91
|
const len = input.length;
|
|
@@ -740,35 +93,6 @@ const dropWhile = dual(2, (self, predicate) => {
|
|
|
740
93
|
while (idx < len && predicate(input[idx], idx)) idx++;
|
|
741
94
|
return input.slice(idx);
|
|
742
95
|
});
|
|
743
|
-
/**
|
|
744
|
-
* Takes elements from the start while the predicate holds, stopping at the
|
|
745
|
-
* first element that fails.
|
|
746
|
-
*
|
|
747
|
-
* **When to use**
|
|
748
|
-
*
|
|
749
|
-
* Use to keep the leading elements of an iterable while each element satisfies
|
|
750
|
-
* a predicate, returning the retained prefix as an array.
|
|
751
|
-
*
|
|
752
|
-
* **Details**
|
|
753
|
-
*
|
|
754
|
-
* Supports refinements for type narrowing. The predicate receives
|
|
755
|
-
* `(element, index)`.
|
|
756
|
-
*
|
|
757
|
-
* **Example** (Taking while condition holds)
|
|
758
|
-
*
|
|
759
|
-
* ```ts
|
|
760
|
-
* import { Array } from "effect"
|
|
761
|
-
*
|
|
762
|
-
* Array.takeWhile([1, 3, 2, 4, 1, 2], (x) => x < 4) // => [1, 3, 2]
|
|
763
|
-
* ```
|
|
764
|
-
*
|
|
765
|
-
* @see {@link take} for keeping a fixed number of leading elements
|
|
766
|
-
* @see {@link dropWhile} for removing the matching prefix and keeping the rest
|
|
767
|
-
* @see {@link span} for splitting the matching prefix from the remaining elements
|
|
768
|
-
*
|
|
769
|
-
* @category getters
|
|
770
|
-
* @since 2.0.0
|
|
771
|
-
*/
|
|
772
96
|
const takeWhile = dual(2, (self, predicate) => {
|
|
773
97
|
const input = Array.isArray(self) ? self : Array.from(self);
|
|
774
98
|
const len = input.length;
|
|
@@ -779,90 +103,28 @@ const takeWhile = dual(2, (self, predicate) => {
|
|
|
779
103
|
|
|
780
104
|
//#endregion
|
|
781
105
|
//#region src/attribute-name.ts
|
|
782
|
-
/**
|
|
783
|
-
* Get the stringified name of a `JSXAttribute` node.
|
|
784
|
-
*
|
|
785
|
-
* - `className` -> `"className"`
|
|
786
|
-
* - `aria-label` -> `"aria-label"`
|
|
787
|
-
* - `xml:space` -> `"xml:space"`.
|
|
788
|
-
* @param node The `JSXAttribute` node to get the name from.
|
|
789
|
-
* @returns The attribute name as a plain string.
|
|
790
|
-
*/
|
|
791
106
|
function getAttributeName(node) {
|
|
792
107
|
if (node.name.type === AST_NODE_TYPES.JSXIdentifier) return node.name.name;
|
|
793
108
|
return node.name.namespace.name + ":" + node.name.name.name;
|
|
794
109
|
}
|
|
795
|
-
/**
|
|
796
|
-
* Check if the node is a `JSXAttribute` with the given name.
|
|
797
|
-
*
|
|
798
|
-
* Only plain identifier names are matched (ex: `className`); namespaced
|
|
799
|
-
* attributes (ex: `xml:space`) do not match.
|
|
800
|
-
*
|
|
801
|
-
* Supports both data-first and data-last (curried) call styles:
|
|
802
|
-
* - `isAttribute(node, "className")`
|
|
803
|
-
* - `isAttribute("className")(node)`.
|
|
804
|
-
* @param node The node to check.
|
|
805
|
-
* @param name The attribute name to match (ex: "className").
|
|
806
|
-
* @returns `true` if the node is a `JSXAttribute` named `name`.
|
|
807
|
-
*/
|
|
808
110
|
const isAttribute = dual(2, (node, name) => {
|
|
809
111
|
return node.type === AST_NODE_TYPES.JSXAttribute && node.name.type === AST_NODE_TYPES.JSXIdentifier && node.name.name === name;
|
|
810
112
|
});
|
|
811
113
|
|
|
812
114
|
//#endregion
|
|
813
115
|
//#region src/attribute-find.ts
|
|
814
|
-
/**
|
|
815
|
-
* Find a JSX attribute (or a spread attribute containing the property) by name.
|
|
816
|
-
*
|
|
817
|
-
* Returns the last matching attribute to mirror React's behavior where later props win.
|
|
818
|
-
* Spread attributes are resolved when possible: if the spread argument is an identifier
|
|
819
|
-
* that resolves to an object expression, the object's properties are searched for a
|
|
820
|
-
* matching key (see {@link findSpreadProperty}).
|
|
821
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
822
|
-
* @param element The `JSXElement` node to search.
|
|
823
|
-
* @param name The attribute name to look for (ex: "className").
|
|
824
|
-
* @returns The matching `JSXAttribute` or `JSXSpreadAttribute`, or `undefined` when not found.
|
|
825
|
-
*/
|
|
826
116
|
function findAttribute(context, element, name) {
|
|
827
117
|
return element.openingElement.attributes.findLast((attr) => {
|
|
828
118
|
if (attr.type === AST_NODE_TYPES.JSXAttribute) return getAttributeName(attr) === name;
|
|
829
119
|
return findSpreadProperty(context, attr.argument, name) != null;
|
|
830
120
|
});
|
|
831
121
|
}
|
|
832
|
-
/**
|
|
833
|
-
* Walk up the AST from `node` to find the nearest `JSXAttribute` ancestor, optionally matching a predicate.
|
|
834
|
-
* @param node The starting node for the upward search.
|
|
835
|
-
* @param test Optional predicate to filter candidate `JSXAttribute` nodes.
|
|
836
|
-
* @returns The first matching `JSXAttribute` ancestor, or `undefined` when none is found.
|
|
837
|
-
*/
|
|
838
122
|
function findParentAttribute(node, test = () => true) {
|
|
839
123
|
const guard = (n) => {
|
|
840
124
|
return n.type === AST_NODE_TYPES.JSXAttribute && test(n);
|
|
841
125
|
};
|
|
842
126
|
return Traverse.findParent(node, guard) ?? void 0;
|
|
843
127
|
}
|
|
844
|
-
/**
|
|
845
|
-
* Find the `Property` node that provides a given key inside a spread argument.
|
|
846
|
-
*
|
|
847
|
-
* This is the single resolution routine shared by {@link findAttribute} (existence
|
|
848
|
-
* checks) and the `spreadProps` variant of `resolveAttributeValue` (value extraction):
|
|
849
|
-
*
|
|
850
|
-
* - An `Identifier` argument is resolved to its initializer via variable
|
|
851
|
-
* resolution, following alias chains (`const b = a`) like `getStaticValue`
|
|
852
|
-
* does; an `ObjectExpression` argument is searched directly.
|
|
853
|
-
* - Properties are walked **in reverse** so that later entries win, matching
|
|
854
|
-
* JavaScript object semantics (`{ ...a, k: 1 }` -> the literal `k`).
|
|
855
|
-
* - Nested `SpreadElement`s (identifiers or inline object expressions) are
|
|
856
|
-
* searched recursively; a `seen` set guards against circular references.
|
|
857
|
-
* - Plain identifier keys and string literal keys are matched directly;
|
|
858
|
-
* computed keys are matched when they are statically evaluable
|
|
859
|
-
* (ex: `{ ["class" + "Name"]: 1 }`).
|
|
860
|
-
* @param context The ESLint rule context (needed for variable resolution).
|
|
861
|
-
* @param argument The spread argument expression to search.
|
|
862
|
-
* @param name The property name to look for.
|
|
863
|
-
* @param seen Internal set of already-visited nodes (cycle guard).
|
|
864
|
-
* @returns The matching `Property` node, or `undefined` when the key is not found.
|
|
865
|
-
*/
|
|
866
128
|
function findSpreadProperty(context, argument, name, seen = /* @__PURE__ */ new Set()) {
|
|
867
129
|
let objectExpression;
|
|
868
130
|
if (Check.isIdentifier(argument)) {
|
|
@@ -897,81 +159,27 @@ function findSpreadProperty(context, argument, name, seen = /* @__PURE__ */ new
|
|
|
897
159
|
|
|
898
160
|
//#endregion
|
|
899
161
|
//#region src/attribute-has.ts
|
|
900
|
-
/**
|
|
901
|
-
* Check if the element has an attribute with the given name.
|
|
902
|
-
*
|
|
903
|
-
* Spread attributes are taken into account: `<Comp {...{ disabled: true }} />`
|
|
904
|
-
* reports `true` for `"disabled"` (see {@link findAttribute}).
|
|
905
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
906
|
-
* @param element The `JSXElement` node to check.
|
|
907
|
-
* @param name The attribute name to look for (ex: "className").
|
|
908
|
-
* @returns `true` if the attribute is present on the element.
|
|
909
|
-
*/
|
|
910
162
|
function hasAttribute(context, element, name) {
|
|
911
163
|
return findAttribute(context, element, name) != null;
|
|
912
164
|
}
|
|
913
|
-
/**
|
|
914
|
-
* Check if the element has at least one of the given attributes.
|
|
915
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
916
|
-
* @param element The `JSXElement` node to check.
|
|
917
|
-
* @param names The attribute names to look for.
|
|
918
|
-
* @returns `true` if at least one of the attributes is present.
|
|
919
|
-
*/
|
|
920
165
|
function hasAnyAttribute(context, element, names) {
|
|
921
166
|
return names.some((name) => findAttribute(context, element, name) != null);
|
|
922
167
|
}
|
|
923
|
-
/**
|
|
924
|
-
* Check if the element has all of the given attributes.
|
|
925
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
926
|
-
* @param element The `JSXElement` node to check.
|
|
927
|
-
* @param names The attribute names to look for.
|
|
928
|
-
* @returns `true` if every attribute is present on the element.
|
|
929
|
-
*/
|
|
930
168
|
function hasEveryAttribute(context, element, names) {
|
|
931
169
|
return names.every((name) => findAttribute(context, element, name) != null);
|
|
932
170
|
}
|
|
933
171
|
|
|
934
172
|
//#endregion
|
|
935
173
|
//#region src/attribute-value.ts
|
|
936
|
-
/**
|
|
937
|
-
* Resolve the value of a JSX attribute (or spread attribute) into an {@link AttributeValue} descriptor.
|
|
938
|
-
*
|
|
939
|
-
* When the attribute is a `JSXSpreadAttribute`, passing `name` (typically the name
|
|
940
|
-
* the attribute was found by) makes `toStatic()` return the static value of that
|
|
941
|
-
* named property. For the higher-level "find by name and resolve" combo, see {@link getAttributeValue}.
|
|
942
|
-
* @param context The ESLint rule context (needed for scope look-ups).
|
|
943
|
-
* @param attribute A `JSXAttribute` or `JSXSpreadAttribute` node.
|
|
944
|
-
* @param name Optional property name used to resolve `toStatic()` for spread attributes.
|
|
945
|
-
* @returns A discriminated-union descriptor of the attribute's value.
|
|
946
|
-
*/
|
|
947
174
|
function resolveAttributeValue(context, attribute, name) {
|
|
948
175
|
if (attribute.type === AST_NODE_TYPES.JSXAttribute) return resolveJsxAttribute(context, attribute);
|
|
949
176
|
return resolveJsxSpreadAttribute(context, attribute, name);
|
|
950
177
|
}
|
|
951
|
-
/**
|
|
952
|
-
* Find an attribute by name on a JSX element and resolve its value in a single call.
|
|
953
|
-
*
|
|
954
|
-
* Convenience composition of {@link findAttribute} and {@link resolveAttributeValue}.
|
|
955
|
-
* @param context The ESLint rule context.
|
|
956
|
-
* @param element The `JSXElement` node to search.
|
|
957
|
-
* @param name The attribute name to look up (ex: "className").
|
|
958
|
-
* @returns An {@link AttributeValue} descriptor, or `undefined` when the attribute is not present.
|
|
959
|
-
*/
|
|
960
178
|
function getAttributeValue(context, element, name) {
|
|
961
179
|
const attr = findAttribute(context, element, name);
|
|
962
180
|
if (attr == null) return void 0;
|
|
963
181
|
return resolveAttributeValue(context, attr, name);
|
|
964
182
|
}
|
|
965
|
-
/**
|
|
966
|
-
* Find an attribute by name on a JSX element and collapse its value to a plain JavaScript value.
|
|
967
|
-
*
|
|
968
|
-
* Returns `undefined` both when the attribute is absent and when its value cannot
|
|
969
|
-
* be statically determined; use {@link hasAttribute} when presence itself matters.
|
|
970
|
-
* @param context The ESLint rule context.
|
|
971
|
-
* @param element The `JSXElement` node to check.
|
|
972
|
-
* @param name The attribute name to look up (ex: "className").
|
|
973
|
-
* @returns The static value of the attribute, or `undefined` when absent or indeterminate.
|
|
974
|
-
*/
|
|
975
183
|
function getAttributeStaticValue(context, element, name) {
|
|
976
184
|
return getAttributeValue(context, element, name)?.toStatic();
|
|
977
185
|
}
|
|
@@ -1041,19 +249,6 @@ function resolveJsxSpreadAttribute(context, node, name) {
|
|
|
1041
249
|
|
|
1042
250
|
//#endregion
|
|
1043
251
|
//#region src/text.ts
|
|
1044
|
-
/**
|
|
1045
|
-
* Collapse a multiline JSX text string following React's whitespace rules.
|
|
1046
|
-
*
|
|
1047
|
-
* Mirrors Babel's `cleanJSXElementLiteralChild` algorithm:
|
|
1048
|
-
* 1. Split the raw text into lines.
|
|
1049
|
-
* 2. Find the last non-empty line.
|
|
1050
|
-
* 3. Trim leading spaces on non-first lines and trailing spaces on non-last lines.
|
|
1051
|
-
* 4. Collapse tabs into spaces.
|
|
1052
|
-
* 5. Append a single space after each non-last non-empty line.
|
|
1053
|
-
* @param text The raw JSX text string to collapse.
|
|
1054
|
-
* @returns The collapsed string, or `null` when the text contains only whitespace.
|
|
1055
|
-
* @see https://github.com/babel/babel/blob/main/packages/babel-types/src/utils/react/cleanJSXElementLiteralChild.ts
|
|
1056
|
-
*/
|
|
1057
252
|
function collapseMultilineText(text) {
|
|
1058
253
|
const lines = text.split(/\r\n|\n|\r/);
|
|
1059
254
|
let lastNonEmptyLine = 0;
|
|
@@ -1074,40 +269,14 @@ function collapseMultilineText(text) {
|
|
|
1074
269
|
}
|
|
1075
270
|
return str === "" ? null : str;
|
|
1076
271
|
}
|
|
1077
|
-
/**
|
|
1078
|
-
* Check if the node is whitespace padding that React would trim away during
|
|
1079
|
-
* rendering, that is, a `JSXText` node that cleans to nothing (see
|
|
1080
|
-
* {@link collapseMultilineText}) and contains a newline.
|
|
1081
|
-
*
|
|
1082
|
-
* For the looser "any whitespace-only text" check, see {@link isWhitespaceText}.
|
|
1083
|
-
* @param node The JSX child node to check.
|
|
1084
|
-
* @returns `true` if the node is purely formatting whitespace.
|
|
1085
|
-
*/
|
|
1086
272
|
function isPaddingWhitespace(node) {
|
|
1087
273
|
if (node.type !== AST_NODE_TYPES.JSXText) return false;
|
|
1088
274
|
return collapseMultilineText(node.value) == null && node.value.includes("\n");
|
|
1089
275
|
}
|
|
1090
|
-
/**
|
|
1091
|
-
* Check if the node is whitespace-only text.
|
|
1092
|
-
*
|
|
1093
|
-
* Looser variant of {@link isPaddingWhitespace}; matches every `JSXText` node
|
|
1094
|
-
* whose raw content is empty after trimming, regardless of newlines.
|
|
1095
|
-
* @param node The JSX child node to check.
|
|
1096
|
-
* @returns `true` if the node is a whitespace-only `JSXText`.
|
|
1097
|
-
*/
|
|
1098
276
|
function isWhitespaceText(node) {
|
|
1099
277
|
if (node.type !== AST_NODE_TYPES.JSXText) return false;
|
|
1100
278
|
return node.raw.trim() === "";
|
|
1101
279
|
}
|
|
1102
|
-
/**
|
|
1103
|
-
* Check if the node is an empty string expression (`{""}`).
|
|
1104
|
-
*
|
|
1105
|
-
* React's reconciler and SSR renderer explicitly skip empty strings, producing no
|
|
1106
|
-
* DOM node, so such expressions are treated as non-rendered children, same as
|
|
1107
|
-
* whitespace padding.
|
|
1108
|
-
* @param node The JSX child node to check.
|
|
1109
|
-
* @returns `true` if the node is a `{""}` expression container.
|
|
1110
|
-
*/
|
|
1111
280
|
function isEmptyStringExpression(node) {
|
|
1112
281
|
if (node.type !== AST_NODE_TYPES.JSXExpressionContainer) return false;
|
|
1113
282
|
const expr = node.expression;
|
|
@@ -1117,18 +286,6 @@ function isEmptyStringExpression(node) {
|
|
|
1117
286
|
|
|
1118
287
|
//#endregion
|
|
1119
288
|
//#region src/children.ts
|
|
1120
|
-
/**
|
|
1121
|
-
* Get the meaningful children of a JSX element or fragment.
|
|
1122
|
-
*
|
|
1123
|
-
* Mirrors Babel's `buildChildren` helper:
|
|
1124
|
-
* 1. Iterate over `element.children`.
|
|
1125
|
-
* 2. Skip `JSXText` nodes that clean to nothing (padding whitespace).
|
|
1126
|
-
* 3. Skip `JSXExpressionContainer` nodes whose expression is empty.
|
|
1127
|
-
* 4. Skip empty string expressions (`{""}`), which produce no DOM node.
|
|
1128
|
-
* 5. Collect everything else.
|
|
1129
|
-
* @param element A `JSXElement` or `JSXFragment` node.
|
|
1130
|
-
* @returns The children nodes that contribute to rendered output.
|
|
1131
|
-
*/
|
|
1132
289
|
function getChildren(element) {
|
|
1133
290
|
const children = [];
|
|
1134
291
|
for (const child of element.children) {
|
|
@@ -1141,16 +298,6 @@ function getChildren(element) {
|
|
|
1141
298
|
}
|
|
1142
299
|
return children;
|
|
1143
300
|
}
|
|
1144
|
-
/**
|
|
1145
|
-
* Check if the element has at least one meaningful child, that is, a child that
|
|
1146
|
-
* is not purely whitespace text or an empty string expression (`{""}`).
|
|
1147
|
-
*
|
|
1148
|
-
* Unlike {@link getChildren} (which only filters whitespace containing a newline),
|
|
1149
|
-
* this check treats any whitespace-only text as non-meaningful, so `hasChildren(node)`
|
|
1150
|
-
* is not always equal to `getChildren(node).length > 0` (ex: `<div> </div>`).
|
|
1151
|
-
* @param element A `JSXElement` or `JSXFragment` node.
|
|
1152
|
-
* @returns `true` if the element has at least one meaningful child.
|
|
1153
|
-
*/
|
|
1154
301
|
function hasChildren(element) {
|
|
1155
302
|
if (element.children.length === 0) return false;
|
|
1156
303
|
return !element.children.every(or(isWhitespaceText, isEmptyStringExpression));
|
|
@@ -1158,17 +305,6 @@ function hasChildren(element) {
|
|
|
1158
305
|
|
|
1159
306
|
//#endregion
|
|
1160
307
|
//#region src/element-type.ts
|
|
1161
|
-
/**
|
|
1162
|
-
* Get the string representation of a JSX element's type.
|
|
1163
|
-
*
|
|
1164
|
-
* - `<div>` -> `"div"`
|
|
1165
|
-
* - `<Foo.Bar>` -> `"Foo.Bar"`
|
|
1166
|
-
* - `<React.Fragment>` -> `"React.Fragment"`
|
|
1167
|
-
* - `<xml:space>` -> `"xml:space"`
|
|
1168
|
-
* - `<></>` -> `""`.
|
|
1169
|
-
* @param node The `JSXElement` or `JSXFragment` node.
|
|
1170
|
-
* @returns The fully qualified element type string.
|
|
1171
|
-
*/
|
|
1172
308
|
function getElementFullType(node) {
|
|
1173
309
|
if (node.type === AST_NODE_TYPES.JSXFragment) return "";
|
|
1174
310
|
function getQualifiedName(node) {
|
|
@@ -1180,31 +316,12 @@ function getElementFullType(node) {
|
|
|
1180
316
|
}
|
|
1181
317
|
return getQualifiedName(node.openingElement.name);
|
|
1182
318
|
}
|
|
1183
|
-
/**
|
|
1184
|
-
* Get the self name (last dot-separated segment) of a JSX element type.
|
|
1185
|
-
*
|
|
1186
|
-
* - `<Foo.Bar.Baz>` -> `"Baz"`
|
|
1187
|
-
* - `<div>` -> `"div"`
|
|
1188
|
-
* - `<></>` -> `""`
|
|
1189
|
-
* @param node The `JSXElement` or `JSXFragment` node.
|
|
1190
|
-
* @returns The last segment of the element type, or `""` for fragments.
|
|
1191
|
-
*/
|
|
1192
319
|
function getElementSelfType(node) {
|
|
1193
320
|
return getElementFullType(node).split(".").at(-1) ?? "";
|
|
1194
321
|
}
|
|
1195
322
|
|
|
1196
323
|
//#endregion
|
|
1197
324
|
//#region src/element-is.ts
|
|
1198
|
-
/**
|
|
1199
|
-
* Check if the node is a `JSXElement` (or `JSXFragment`), optionally matching a given test.
|
|
1200
|
-
*
|
|
1201
|
-
* Modelled after [`hast-util-is-element`](https://github.com/syntax-tree/hast-util-is-element):
|
|
1202
|
-
* the `test` parameter controls what counts as a match. When called without a test,
|
|
1203
|
-
* the function acts as a simple type guard for `JSXElement | JSXFragment`.
|
|
1204
|
-
* @param node The node to check.
|
|
1205
|
-
* @param test Optional test to match the element type against.
|
|
1206
|
-
* @returns `true` if the node is a matching JSX element.
|
|
1207
|
-
*/
|
|
1208
325
|
function isElement(node, test) {
|
|
1209
326
|
if (node == null) return false;
|
|
1210
327
|
if (node.type !== AST_NODE_TYPES.JSXElement && node.type !== AST_NODE_TYPES.JSXFragment) return false;
|
|
@@ -1212,36 +329,12 @@ function isElement(node, test) {
|
|
|
1212
329
|
const type = getElementFullType(node);
|
|
1213
330
|
return match(test).with(P.string, (t) => type === t).with(P.array(), (t) => t.includes(type)).with(P.instanceOf(Function), (t) => t(type, node)).exhaustive();
|
|
1214
331
|
}
|
|
1215
|
-
/**
|
|
1216
|
-
* Check if the node is a React Fragment element.
|
|
1217
|
-
*
|
|
1218
|
-
* Recognizes both the shorthand `<>...</>` syntax (`JSXFragment`) and the explicit
|
|
1219
|
-
* `<Fragment>` / `<React.Fragment>` form (`JSXElement`). The comparison is performed
|
|
1220
|
-
* against the self name (last dot-separated segment) of both the node and the
|
|
1221
|
-
* configured factory, so `<React.Fragment>` matches `"React.Fragment"` and
|
|
1222
|
-
* `<Fragment>` matches `"Fragment"`.
|
|
1223
|
-
*
|
|
1224
|
-
* The factory is a required parameter (no implicit `"React.Fragment"` default) so
|
|
1225
|
-
* that callers always pass the project-configured pragma — see `getJsxConfig` in
|
|
1226
|
-
* `@eslint-react/core` — and cannot silently use the wrong factory under a custom
|
|
1227
|
-
* JSX runtime (ex: Preact). Note the self-name comparison is a heuristic: any
|
|
1228
|
-
* `<*.Fragment>` member expression matches, regardless of the qualifier.
|
|
1229
|
-
* @param node The node to check.
|
|
1230
|
-
* @param jsxFragmentFactory The configured fragment factory string (ex: "React.Fragment").
|
|
1231
|
-
* @returns `true` if the node represents a React Fragment.
|
|
1232
|
-
*/
|
|
1233
332
|
function isFragmentElement(node, jsxFragmentFactory) {
|
|
1234
333
|
if (node.type === AST_NODE_TYPES.JSXFragment) return true;
|
|
1235
334
|
if (node.type !== AST_NODE_TYPES.JSXElement) return false;
|
|
1236
335
|
const fragment = jsxFragmentFactory.split(".").at(-1) ?? "Fragment";
|
|
1237
336
|
return getElementFullType(node).split(".").at(-1) === fragment;
|
|
1238
337
|
}
|
|
1239
|
-
/**
|
|
1240
|
-
* Check if the node is a host (intrinsic / DOM) element, that is, a `JSXElement`
|
|
1241
|
-
* whose tag name starts with a lowercase letter (ex: `<div>` vs `<MyComponent>`).
|
|
1242
|
-
* @param node The node to check.
|
|
1243
|
-
* @returns `true` if the node is a `JSXElement` with a lowercase tag name.
|
|
1244
|
-
*/
|
|
1245
338
|
function isHostElement(node) {
|
|
1246
339
|
if (node.type !== AST_NODE_TYPES.JSXElement) return false;
|
|
1247
340
|
const name = node.openingElement.name;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/jsx",
|
|
3
|
-
"version": "5.24.
|
|
3
|
+
"version": "5.24.3",
|
|
4
4
|
"description": "ESLint React's TSESTree JSX utility module for static analysis of JSX patterns.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eslint-react/ast": "5.24.
|
|
33
|
-
"@eslint-react/eslint": "5.24.
|
|
34
|
-
"@eslint-react/var": "5.24.
|
|
32
|
+
"@eslint-react/ast": "5.24.3",
|
|
33
|
+
"@eslint-react/eslint": "5.24.3",
|
|
34
|
+
"@eslint-react/var": "5.24.3",
|
|
35
35
|
"@typescript-eslint/types": "^8.71.0",
|
|
36
36
|
"@typescript-eslint/utils": "^8.71.0",
|
|
37
37
|
"ts-pattern": "^5.9.0"
|