@eslint-react/jsx 1.5.3-next.4 → 1.5.4-next.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.js +1366 -1228
- package/dist/index.mjs +1363 -1229
- package/package.json +7 -7
- package/dist/index.cjs +0 -1656
package/dist/index.cjs
DELETED
|
@@ -1,1656 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var ast = require('@eslint-react/ast');
|
|
4
|
-
var shared = require('@eslint-react/shared');
|
|
5
|
-
var memo = require('micro-memoize');
|
|
6
|
-
var _var = require('@eslint-react/var');
|
|
7
|
-
var types = require('@typescript-eslint/types');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Tests if a value is a `function`.
|
|
11
|
-
*
|
|
12
|
-
* @param input - The value to test.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* import { isFunction } from 'effect/Predicate'
|
|
16
|
-
*
|
|
17
|
-
* assert.deepStrictEqual(isFunction(isFunction), true)
|
|
18
|
-
* assert.deepStrictEqual(isFunction("function"), false)
|
|
19
|
-
*
|
|
20
|
-
* @category guards
|
|
21
|
-
* @since 2.0.0
|
|
22
|
-
*/
|
|
23
|
-
const isFunction$1 = input => typeof input === "function";
|
|
24
|
-
/**
|
|
25
|
-
* Creates a function that can be used in a data-last (aka `pipe`able) or
|
|
26
|
-
* data-first style.
|
|
27
|
-
*
|
|
28
|
-
* The first parameter to `dual` is either the arity of the uncurried function
|
|
29
|
-
* or a predicate that determines if the function is being used in a data-first
|
|
30
|
-
* or data-last style.
|
|
31
|
-
*
|
|
32
|
-
* Using the arity is the most common use case, but there are some cases where
|
|
33
|
-
* you may want to use a predicate. For example, if you have a function that
|
|
34
|
-
* takes an optional argument, you can use a predicate to determine if the
|
|
35
|
-
* function is being used in a data-first or data-last style.
|
|
36
|
-
*
|
|
37
|
-
* @param arity - Either the arity of the uncurried function or a predicate
|
|
38
|
-
* which determines if the function is being used in a data-first
|
|
39
|
-
* or data-last style.
|
|
40
|
-
* @param body - The definition of the uncurried function.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* import { dual, pipe } from "effect/Function"
|
|
44
|
-
*
|
|
45
|
-
* // Exampe using arity to determine data-first or data-last style
|
|
46
|
-
* const sum: {
|
|
47
|
-
* (that: number): (self: number) => number
|
|
48
|
-
* (self: number, that: number): number
|
|
49
|
-
* } = dual(2, (self: number, that: number): number => self + that)
|
|
50
|
-
*
|
|
51
|
-
* assert.deepStrictEqual(sum(2, 3), 5)
|
|
52
|
-
* assert.deepStrictEqual(pipe(2, sum(3)), 5)
|
|
53
|
-
*
|
|
54
|
-
* // Example using a predicate to determine data-first or data-last style
|
|
55
|
-
* const sum2: {
|
|
56
|
-
* (that: number): (self: number) => number
|
|
57
|
-
* (self: number, that: number): number
|
|
58
|
-
* } = dual((args) => args.length === 1, (self: number, that: number): number => self + that)
|
|
59
|
-
*
|
|
60
|
-
* assert.deepStrictEqual(sum(2, 3), 5)
|
|
61
|
-
* assert.deepStrictEqual(pipe(2, sum(3)), 5)
|
|
62
|
-
*
|
|
63
|
-
* @since 2.0.0
|
|
64
|
-
*/
|
|
65
|
-
const dual = function (arity, body) {
|
|
66
|
-
if (typeof arity === "function") {
|
|
67
|
-
return function () {
|
|
68
|
-
if (arity(arguments)) {
|
|
69
|
-
// @ts-expect-error
|
|
70
|
-
return body.apply(this, arguments);
|
|
71
|
-
}
|
|
72
|
-
return self => body(self, ...arguments);
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
switch (arity) {
|
|
76
|
-
case 0:
|
|
77
|
-
case 1:
|
|
78
|
-
throw new RangeError(`Invalid arity ${arity}`);
|
|
79
|
-
case 2:
|
|
80
|
-
return function (a, b) {
|
|
81
|
-
if (arguments.length >= 2) {
|
|
82
|
-
return body(a, b);
|
|
83
|
-
}
|
|
84
|
-
return function (self) {
|
|
85
|
-
return body(self, a);
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
case 3:
|
|
89
|
-
return function (a, b, c) {
|
|
90
|
-
if (arguments.length >= 3) {
|
|
91
|
-
return body(a, b, c);
|
|
92
|
-
}
|
|
93
|
-
return function (self) {
|
|
94
|
-
return body(self, a, b);
|
|
95
|
-
};
|
|
96
|
-
};
|
|
97
|
-
case 4:
|
|
98
|
-
return function (a, b, c, d) {
|
|
99
|
-
if (arguments.length >= 4) {
|
|
100
|
-
return body(a, b, c, d);
|
|
101
|
-
}
|
|
102
|
-
return function (self) {
|
|
103
|
-
return body(self, a, b, c);
|
|
104
|
-
};
|
|
105
|
-
};
|
|
106
|
-
case 5:
|
|
107
|
-
return function (a, b, c, d, e) {
|
|
108
|
-
if (arguments.length >= 5) {
|
|
109
|
-
return body(a, b, c, d, e);
|
|
110
|
-
}
|
|
111
|
-
return function (self) {
|
|
112
|
-
return body(self, a, b, c, d);
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
default:
|
|
116
|
-
return function () {
|
|
117
|
-
if (arguments.length >= arity) {
|
|
118
|
-
// @ts-expect-error
|
|
119
|
-
return body.apply(this, arguments);
|
|
120
|
-
}
|
|
121
|
-
const args = arguments;
|
|
122
|
-
return function (self) {
|
|
123
|
-
return body(self, ...args);
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
};
|
|
128
|
-
/**
|
|
129
|
-
* Creates a constant value that never changes.
|
|
130
|
-
*
|
|
131
|
-
* This is useful when you want to pass a value to a higher-order function (a function that takes another function as its argument)
|
|
132
|
-
* and want that inner function to always use the same value, no matter how many times it is called.
|
|
133
|
-
*
|
|
134
|
-
* @param value - The constant value to be returned.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* import { constant } from "effect/Function"
|
|
138
|
-
*
|
|
139
|
-
* const constNull = constant(null)
|
|
140
|
-
*
|
|
141
|
-
* assert.deepStrictEqual(constNull(), null)
|
|
142
|
-
* assert.deepStrictEqual(constNull(), null)
|
|
143
|
-
*
|
|
144
|
-
* @since 2.0.0
|
|
145
|
-
*/
|
|
146
|
-
const constant = value => () => value;
|
|
147
|
-
/**
|
|
148
|
-
* A thunk that returns always `true`.
|
|
149
|
-
*
|
|
150
|
-
* @example
|
|
151
|
-
* import { constTrue } from "effect/Function"
|
|
152
|
-
*
|
|
153
|
-
* assert.deepStrictEqual(constTrue(), true)
|
|
154
|
-
*
|
|
155
|
-
* @since 2.0.0
|
|
156
|
-
*/
|
|
157
|
-
const constTrue = /*#__PURE__*/constant(true);
|
|
158
|
-
/**
|
|
159
|
-
* A thunk that returns always `false`.
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* import { constFalse } from "effect/Function"
|
|
163
|
-
*
|
|
164
|
-
* assert.deepStrictEqual(constFalse(), false)
|
|
165
|
-
*
|
|
166
|
-
* @since 2.0.0
|
|
167
|
-
*/
|
|
168
|
-
const constFalse = /*#__PURE__*/constant(false);
|
|
169
|
-
function pipe(a, ab, bc, cd, de, ef, fg, gh, hi) {
|
|
170
|
-
switch (arguments.length) {
|
|
171
|
-
case 1:
|
|
172
|
-
return a;
|
|
173
|
-
case 2:
|
|
174
|
-
return ab(a);
|
|
175
|
-
case 3:
|
|
176
|
-
return bc(ab(a));
|
|
177
|
-
case 4:
|
|
178
|
-
return cd(bc(ab(a)));
|
|
179
|
-
case 5:
|
|
180
|
-
return de(cd(bc(ab(a))));
|
|
181
|
-
case 6:
|
|
182
|
-
return ef(de(cd(bc(ab(a)))));
|
|
183
|
-
case 7:
|
|
184
|
-
return fg(ef(de(cd(bc(ab(a))))));
|
|
185
|
-
case 8:
|
|
186
|
-
return gh(fg(ef(de(cd(bc(ab(a)))))));
|
|
187
|
-
case 9:
|
|
188
|
-
return hi(gh(fg(ef(de(cd(bc(ab(a))))))));
|
|
189
|
-
default:
|
|
190
|
-
{
|
|
191
|
-
let ret = arguments[0];
|
|
192
|
-
for (let i = 1; i < arguments.length; i++) {
|
|
193
|
-
ret = arguments[i](ret);
|
|
194
|
-
}
|
|
195
|
-
return ret;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const moduleVersion = "2.3.1";
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* @since 2.0.0
|
|
204
|
-
*/
|
|
205
|
-
const globalStoreId = /*#__PURE__*/Symbol.for(`effect/GlobalValue/globalStoreId/${moduleVersion}`);
|
|
206
|
-
if (!(globalStoreId in globalThis)) {
|
|
207
|
-
globalThis[globalStoreId] = /*#__PURE__*/new Map();
|
|
208
|
-
}
|
|
209
|
-
const globalStore = globalThis[globalStoreId];
|
|
210
|
-
/**
|
|
211
|
-
* @since 2.0.0
|
|
212
|
-
*/
|
|
213
|
-
const globalValue = (id, compute) => {
|
|
214
|
-
if (!globalStore.has(id)) {
|
|
215
|
-
globalStore.set(id, compute());
|
|
216
|
-
}
|
|
217
|
-
return globalStore.get(id);
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* @since 2.0.0
|
|
222
|
-
*/
|
|
223
|
-
/**
|
|
224
|
-
* Tests if a value is a `string`.
|
|
225
|
-
*
|
|
226
|
-
* @param input - The value to test.
|
|
227
|
-
*
|
|
228
|
-
* @example
|
|
229
|
-
* import { isString } from "effect/Predicate"
|
|
230
|
-
*
|
|
231
|
-
* assert.deepStrictEqual(isString("a"), true)
|
|
232
|
-
*
|
|
233
|
-
* assert.deepStrictEqual(isString(1), false)
|
|
234
|
-
*
|
|
235
|
-
* @category guards
|
|
236
|
-
* @since 2.0.0
|
|
237
|
-
*/
|
|
238
|
-
const isString = input => typeof input === "string";
|
|
239
|
-
/**
|
|
240
|
-
* Tests if a value is a `function`.
|
|
241
|
-
*
|
|
242
|
-
* @param input - The value to test.
|
|
243
|
-
*
|
|
244
|
-
* @example
|
|
245
|
-
* import { isFunction } from "effect/Predicate"
|
|
246
|
-
*
|
|
247
|
-
* assert.deepStrictEqual(isFunction(isFunction), true)
|
|
248
|
-
*
|
|
249
|
-
* assert.deepStrictEqual(isFunction("function"), false)
|
|
250
|
-
*
|
|
251
|
-
* @category guards
|
|
252
|
-
* @since 2.0.0
|
|
253
|
-
*/
|
|
254
|
-
const isFunction = isFunction$1;
|
|
255
|
-
const isRecordOrArray = input => typeof input === "object" && input !== null;
|
|
256
|
-
/**
|
|
257
|
-
* Tests if a value is an `object`.
|
|
258
|
-
*
|
|
259
|
-
* @param input - The value to test.
|
|
260
|
-
*
|
|
261
|
-
* @example
|
|
262
|
-
* import { isObject } from "effect/Predicate"
|
|
263
|
-
*
|
|
264
|
-
* assert.deepStrictEqual(isObject({}), true)
|
|
265
|
-
* assert.deepStrictEqual(isObject([]), true)
|
|
266
|
-
*
|
|
267
|
-
* assert.deepStrictEqual(isObject(null), false)
|
|
268
|
-
* assert.deepStrictEqual(isObject(undefined), false)
|
|
269
|
-
*
|
|
270
|
-
* @category guards
|
|
271
|
-
* @since 2.0.0
|
|
272
|
-
*/
|
|
273
|
-
const isObject = input => isRecordOrArray(input) || isFunction(input);
|
|
274
|
-
/**
|
|
275
|
-
* Checks whether a value is an `object` containing a specified property key.
|
|
276
|
-
*
|
|
277
|
-
* @param property - The field to check within the object.
|
|
278
|
-
* @param self - The value to examine.
|
|
279
|
-
*
|
|
280
|
-
* @category guards
|
|
281
|
-
* @since 2.0.0
|
|
282
|
-
*/
|
|
283
|
-
const hasProperty = /*#__PURE__*/dual(2, (self, property) => isObject(self) && property in self);
|
|
284
|
-
/**
|
|
285
|
-
* A guard that succeeds when the input is `null` or `undefined`.
|
|
286
|
-
*
|
|
287
|
-
* @param input - The value to test.
|
|
288
|
-
*
|
|
289
|
-
* @example
|
|
290
|
-
* import { isNullable } from "effect/Predicate"
|
|
291
|
-
*
|
|
292
|
-
* assert.deepStrictEqual(isNullable(null), true)
|
|
293
|
-
* assert.deepStrictEqual(isNullable(undefined), true)
|
|
294
|
-
*
|
|
295
|
-
* assert.deepStrictEqual(isNullable({}), false)
|
|
296
|
-
* assert.deepStrictEqual(isNullable([]), false)
|
|
297
|
-
*
|
|
298
|
-
* @category guards
|
|
299
|
-
* @since 2.0.0
|
|
300
|
-
*/
|
|
301
|
-
const isNullable = input => input === null || input === undefined;
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* @since 2.0.0
|
|
305
|
-
*/
|
|
306
|
-
const defaultIncHi = 0x14057b7e;
|
|
307
|
-
const defaultIncLo = 0xf767814f;
|
|
308
|
-
const MUL_HI = 0x5851f42d >>> 0;
|
|
309
|
-
const MUL_LO = 0x4c957f2d >>> 0;
|
|
310
|
-
const BIT_53 = 9007199254740992.0;
|
|
311
|
-
const BIT_27 = 134217728.0;
|
|
312
|
-
/**
|
|
313
|
-
* PCG is a family of simple fast space-efficient statistically good algorithms
|
|
314
|
-
* for random number generation. Unlike many general-purpose RNGs, they are also
|
|
315
|
-
* hard to predict.
|
|
316
|
-
*
|
|
317
|
-
* @category model
|
|
318
|
-
* @since 2.0.0
|
|
319
|
-
*/
|
|
320
|
-
class PCGRandom {
|
|
321
|
-
_state;
|
|
322
|
-
constructor(seedHi, seedLo, incHi, incLo) {
|
|
323
|
-
if (isNullable(seedLo) && isNullable(seedHi)) {
|
|
324
|
-
seedLo = Math.random() * 0xffffffff >>> 0;
|
|
325
|
-
seedHi = 0;
|
|
326
|
-
} else if (isNullable(seedLo)) {
|
|
327
|
-
seedLo = seedHi;
|
|
328
|
-
seedHi = 0;
|
|
329
|
-
}
|
|
330
|
-
if (isNullable(incLo) && isNullable(incHi)) {
|
|
331
|
-
incLo = this._state ? this._state[3] : defaultIncLo;
|
|
332
|
-
incHi = this._state ? this._state[2] : defaultIncHi;
|
|
333
|
-
} else if (isNullable(incLo)) {
|
|
334
|
-
incLo = incHi;
|
|
335
|
-
incHi = 0;
|
|
336
|
-
}
|
|
337
|
-
this._state = new Int32Array([0, 0, incHi >>> 0, ((incLo || 0) | 1) >>> 0]);
|
|
338
|
-
this._next();
|
|
339
|
-
add64(this._state, this._state[0], this._state[1], seedHi >>> 0, seedLo >>> 0);
|
|
340
|
-
this._next();
|
|
341
|
-
return this;
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* Returns a copy of the internal state of this random number generator as a
|
|
345
|
-
* JavaScript Array.
|
|
346
|
-
*
|
|
347
|
-
* @category getters
|
|
348
|
-
* @since 2.0.0
|
|
349
|
-
*/
|
|
350
|
-
getState() {
|
|
351
|
-
return [this._state[0], this._state[1], this._state[2], this._state[3]];
|
|
352
|
-
}
|
|
353
|
-
/**
|
|
354
|
-
* Restore state previously retrieved using `getState()`.
|
|
355
|
-
*
|
|
356
|
-
* @since 2.0.0
|
|
357
|
-
*/
|
|
358
|
-
setState(state) {
|
|
359
|
-
this._state[0] = state[0];
|
|
360
|
-
this._state[1] = state[1];
|
|
361
|
-
this._state[2] = state[2];
|
|
362
|
-
this._state[3] = state[3] | 1;
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Get a uniformly distributed 32 bit integer between [0, max).
|
|
366
|
-
*
|
|
367
|
-
* @category getter
|
|
368
|
-
* @since 2.0.0
|
|
369
|
-
*/
|
|
370
|
-
integer(max) {
|
|
371
|
-
if (!max) {
|
|
372
|
-
return this._next();
|
|
373
|
-
}
|
|
374
|
-
max = max >>> 0;
|
|
375
|
-
if ((max & max - 1) === 0) {
|
|
376
|
-
return this._next() & max - 1; // fast path for power of 2
|
|
377
|
-
}
|
|
378
|
-
let num = 0;
|
|
379
|
-
const skew = (-max >>> 0) % max >>> 0;
|
|
380
|
-
for (num = this._next(); num < skew; num = this._next()) {
|
|
381
|
-
// this loop will rarely execute more than twice,
|
|
382
|
-
// and is intentionally empty
|
|
383
|
-
}
|
|
384
|
-
return num % max;
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Get a uniformly distributed IEEE-754 double between 0.0 and 1.0, with
|
|
388
|
-
* 53 bits of precision (every bit of the mantissa is randomized).
|
|
389
|
-
*
|
|
390
|
-
* @category getters
|
|
391
|
-
* @since 2.0.0
|
|
392
|
-
*/
|
|
393
|
-
number() {
|
|
394
|
-
const hi = (this._next() & 0x03ffffff) * 1.0;
|
|
395
|
-
const lo = (this._next() & 0x07ffffff) * 1.0;
|
|
396
|
-
return (hi * BIT_27 + lo) / BIT_53;
|
|
397
|
-
}
|
|
398
|
-
/** @internal */
|
|
399
|
-
_next() {
|
|
400
|
-
// save current state (what we'll use for this number)
|
|
401
|
-
const oldHi = this._state[0] >>> 0;
|
|
402
|
-
const oldLo = this._state[1] >>> 0;
|
|
403
|
-
// churn LCG.
|
|
404
|
-
mul64(this._state, oldHi, oldLo, MUL_HI, MUL_LO);
|
|
405
|
-
add64(this._state, this._state[0], this._state[1], this._state[2], this._state[3]);
|
|
406
|
-
// get least sig. 32 bits of ((oldstate >> 18) ^ oldstate) >> 27
|
|
407
|
-
let xsHi = oldHi >>> 18;
|
|
408
|
-
let xsLo = (oldLo >>> 18 | oldHi << 14) >>> 0;
|
|
409
|
-
xsHi = (xsHi ^ oldHi) >>> 0;
|
|
410
|
-
xsLo = (xsLo ^ oldLo) >>> 0;
|
|
411
|
-
const xorshifted = (xsLo >>> 27 | xsHi << 5) >>> 0;
|
|
412
|
-
// rotate xorshifted right a random amount, based on the most sig. 5 bits
|
|
413
|
-
// bits of the old state.
|
|
414
|
-
const rot = oldHi >>> 27;
|
|
415
|
-
const rot2 = (-rot >>> 0 & 31) >>> 0;
|
|
416
|
-
return (xorshifted >>> rot | xorshifted << rot2) >>> 0;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
function mul64(out, aHi, aLo, bHi, bLo) {
|
|
420
|
-
let c1 = (aLo >>> 16) * (bLo & 0xffff) >>> 0;
|
|
421
|
-
let c0 = (aLo & 0xffff) * (bLo >>> 16) >>> 0;
|
|
422
|
-
let lo = (aLo & 0xffff) * (bLo & 0xffff) >>> 0;
|
|
423
|
-
let hi = (aLo >>> 16) * (bLo >>> 16) + ((c0 >>> 16) + (c1 >>> 16)) >>> 0;
|
|
424
|
-
c0 = c0 << 16 >>> 0;
|
|
425
|
-
lo = lo + c0 >>> 0;
|
|
426
|
-
if (lo >>> 0 < c0 >>> 0) {
|
|
427
|
-
hi = hi + 1 >>> 0;
|
|
428
|
-
}
|
|
429
|
-
c1 = c1 << 16 >>> 0;
|
|
430
|
-
lo = lo + c1 >>> 0;
|
|
431
|
-
if (lo >>> 0 < c1 >>> 0) {
|
|
432
|
-
hi = hi + 1 >>> 0;
|
|
433
|
-
}
|
|
434
|
-
hi = hi + Math.imul(aLo, bHi) >>> 0;
|
|
435
|
-
hi = hi + Math.imul(aHi, bLo) >>> 0;
|
|
436
|
-
out[0] = hi;
|
|
437
|
-
out[1] = lo;
|
|
438
|
-
}
|
|
439
|
-
// add two 64 bit numbers (given in parts), and store the result in `out`.
|
|
440
|
-
function add64(out, aHi, aLo, bHi, bLo) {
|
|
441
|
-
let hi = aHi + bHi >>> 0;
|
|
442
|
-
const lo = aLo + bLo >>> 0;
|
|
443
|
-
if (lo >>> 0 < aLo >>> 0) {
|
|
444
|
-
hi = hi + 1 | 0;
|
|
445
|
-
}
|
|
446
|
-
out[0] = hi;
|
|
447
|
-
out[1] = lo;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* @since 2.0.0
|
|
452
|
-
*/
|
|
453
|
-
/** @internal */
|
|
454
|
-
const randomHashCache = /*#__PURE__*/globalValue( /*#__PURE__*/Symbol.for("effect/Hash/randomHashCache"), () => new WeakMap());
|
|
455
|
-
/** @internal */
|
|
456
|
-
const pcgr = /*#__PURE__*/globalValue( /*#__PURE__*/Symbol.for("effect/Hash/pcgr"), () => new PCGRandom());
|
|
457
|
-
/**
|
|
458
|
-
* @since 2.0.0
|
|
459
|
-
* @category symbols
|
|
460
|
-
*/
|
|
461
|
-
const symbol$1 = /*#__PURE__*/Symbol.for("effect/Hash");
|
|
462
|
-
/**
|
|
463
|
-
* @since 2.0.0
|
|
464
|
-
* @category hashing
|
|
465
|
-
*/
|
|
466
|
-
const hash = self => {
|
|
467
|
-
switch (typeof self) {
|
|
468
|
-
case "number":
|
|
469
|
-
return number(self);
|
|
470
|
-
case "bigint":
|
|
471
|
-
return string(self.toString(10));
|
|
472
|
-
case "boolean":
|
|
473
|
-
return string(String(self));
|
|
474
|
-
case "symbol":
|
|
475
|
-
return string(String(self));
|
|
476
|
-
case "string":
|
|
477
|
-
return string(self);
|
|
478
|
-
case "undefined":
|
|
479
|
-
return string("undefined");
|
|
480
|
-
case "function":
|
|
481
|
-
case "object":
|
|
482
|
-
{
|
|
483
|
-
if (self === null) {
|
|
484
|
-
return string("null");
|
|
485
|
-
}
|
|
486
|
-
if (isHash(self)) {
|
|
487
|
-
return self[symbol$1]();
|
|
488
|
-
} else {
|
|
489
|
-
return random(self);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
default:
|
|
493
|
-
throw new Error(`BUG: unhandled typeof ${typeof self} - please report an issue at https://github.com/Effect-TS/effect/issues`);
|
|
494
|
-
}
|
|
495
|
-
};
|
|
496
|
-
/**
|
|
497
|
-
* @since 2.0.0
|
|
498
|
-
* @category hashing
|
|
499
|
-
*/
|
|
500
|
-
const random = self => {
|
|
501
|
-
if (!randomHashCache.has(self)) {
|
|
502
|
-
randomHashCache.set(self, number(pcgr.integer(Number.MAX_SAFE_INTEGER)));
|
|
503
|
-
}
|
|
504
|
-
return randomHashCache.get(self);
|
|
505
|
-
};
|
|
506
|
-
/**
|
|
507
|
-
* @since 2.0.0
|
|
508
|
-
* @category hashing
|
|
509
|
-
*/
|
|
510
|
-
const combine = b => self => self * 53 ^ b;
|
|
511
|
-
/**
|
|
512
|
-
* @since 2.0.0
|
|
513
|
-
* @category hashing
|
|
514
|
-
*/
|
|
515
|
-
const optimize = n => n & 0xbfffffff | n >>> 1 & 0x40000000;
|
|
516
|
-
/**
|
|
517
|
-
* @since 2.0.0
|
|
518
|
-
* @category guards
|
|
519
|
-
*/
|
|
520
|
-
const isHash = u => hasProperty(u, symbol$1);
|
|
521
|
-
/**
|
|
522
|
-
* @since 2.0.0
|
|
523
|
-
* @category hashing
|
|
524
|
-
*/
|
|
525
|
-
const number = n => {
|
|
526
|
-
if (n !== n || n === Infinity) {
|
|
527
|
-
return 0;
|
|
528
|
-
}
|
|
529
|
-
let h = n | 0;
|
|
530
|
-
if (h !== n) {
|
|
531
|
-
h ^= n * 0xffffffff;
|
|
532
|
-
}
|
|
533
|
-
while (n > 0xffffffff) {
|
|
534
|
-
h ^= n /= 0xffffffff;
|
|
535
|
-
}
|
|
536
|
-
return optimize(n);
|
|
537
|
-
};
|
|
538
|
-
/**
|
|
539
|
-
* @since 2.0.0
|
|
540
|
-
* @category hashing
|
|
541
|
-
*/
|
|
542
|
-
const string = str => {
|
|
543
|
-
let h = 5381,
|
|
544
|
-
i = str.length;
|
|
545
|
-
while (i) {
|
|
546
|
-
h = h * 33 ^ str.charCodeAt(--i);
|
|
547
|
-
}
|
|
548
|
-
return optimize(h);
|
|
549
|
-
};
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* @since 2.0.0
|
|
553
|
-
* @category symbols
|
|
554
|
-
*/
|
|
555
|
-
const symbol = /*#__PURE__*/Symbol.for("effect/Equal");
|
|
556
|
-
function equals() {
|
|
557
|
-
if (arguments.length === 1) {
|
|
558
|
-
return self => compareBoth(self, arguments[0]);
|
|
559
|
-
}
|
|
560
|
-
return compareBoth(arguments[0], arguments[1]);
|
|
561
|
-
}
|
|
562
|
-
function compareBoth(self, that) {
|
|
563
|
-
if (self === that) {
|
|
564
|
-
return true;
|
|
565
|
-
}
|
|
566
|
-
const selfType = typeof self;
|
|
567
|
-
if (selfType !== typeof that) {
|
|
568
|
-
return false;
|
|
569
|
-
}
|
|
570
|
-
if ((selfType === "object" || selfType === "function") && self !== null && that !== null) {
|
|
571
|
-
if (isEqual(self) && isEqual(that)) {
|
|
572
|
-
return hash(self) === hash(that) && self[symbol](that);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
return false;
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* @since 2.0.0
|
|
579
|
-
* @category guards
|
|
580
|
-
*/
|
|
581
|
-
const isEqual = u => hasProperty(u, symbol);
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
* @since 2.0.0
|
|
585
|
-
*/
|
|
586
|
-
/**
|
|
587
|
-
* @since 2.0.0
|
|
588
|
-
* @category symbols
|
|
589
|
-
*/
|
|
590
|
-
const NodeInspectSymbol = /*#__PURE__*/Symbol.for("nodejs.util.inspect.custom");
|
|
591
|
-
/**
|
|
592
|
-
* @since 2.0.0
|
|
593
|
-
*/
|
|
594
|
-
const toJSON = x => {
|
|
595
|
-
if (hasProperty(x, "toJSON") && isFunction(x["toJSON"]) && x["toJSON"].length === 0) {
|
|
596
|
-
return x.toJSON();
|
|
597
|
-
} else if (Array.isArray(x)) {
|
|
598
|
-
return x.map(toJSON);
|
|
599
|
-
}
|
|
600
|
-
return x;
|
|
601
|
-
};
|
|
602
|
-
/**
|
|
603
|
-
* @since 2.0.0
|
|
604
|
-
*/
|
|
605
|
-
const format = x => JSON.stringify(x, null, 2);
|
|
606
|
-
|
|
607
|
-
/**
|
|
608
|
-
* @since 2.0.0
|
|
609
|
-
*/
|
|
610
|
-
/**
|
|
611
|
-
* @since 2.0.0
|
|
612
|
-
*/
|
|
613
|
-
const pipeArguments = (self, args) => {
|
|
614
|
-
switch (args.length) {
|
|
615
|
-
case 1:
|
|
616
|
-
return args[0](self);
|
|
617
|
-
case 2:
|
|
618
|
-
return args[1](args[0](self));
|
|
619
|
-
case 3:
|
|
620
|
-
return args[2](args[1](args[0](self)));
|
|
621
|
-
case 4:
|
|
622
|
-
return args[3](args[2](args[1](args[0](self))));
|
|
623
|
-
case 5:
|
|
624
|
-
return args[4](args[3](args[2](args[1](args[0](self)))));
|
|
625
|
-
case 6:
|
|
626
|
-
return args[5](args[4](args[3](args[2](args[1](args[0](self))))));
|
|
627
|
-
case 7:
|
|
628
|
-
return args[6](args[5](args[4](args[3](args[2](args[1](args[0](self)))))));
|
|
629
|
-
case 8:
|
|
630
|
-
return args[7](args[6](args[5](args[4](args[3](args[2](args[1](args[0](self))))))));
|
|
631
|
-
case 9:
|
|
632
|
-
return args[8](args[7](args[6](args[5](args[4](args[3](args[2](args[1](args[0](self)))))))));
|
|
633
|
-
default:
|
|
634
|
-
{
|
|
635
|
-
let ret = self;
|
|
636
|
-
for (let i = 0, len = args.length; i < len; i++) {
|
|
637
|
-
ret = args[i](ret);
|
|
638
|
-
}
|
|
639
|
-
return ret;
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
};
|
|
643
|
-
|
|
644
|
-
/** @internal */
|
|
645
|
-
const EffectTypeId = /*#__PURE__*/Symbol.for("effect/Effect");
|
|
646
|
-
/** @internal */
|
|
647
|
-
const StreamTypeId = /*#__PURE__*/Symbol.for("effect/Stream");
|
|
648
|
-
/** @internal */
|
|
649
|
-
const SinkTypeId = /*#__PURE__*/Symbol.for("effect/Sink");
|
|
650
|
-
/** @internal */
|
|
651
|
-
const ChannelTypeId = /*#__PURE__*/Symbol.for("effect/Channel");
|
|
652
|
-
/** @internal */
|
|
653
|
-
const effectVariance = {
|
|
654
|
-
/* c8 ignore next */
|
|
655
|
-
_R: _ => _,
|
|
656
|
-
/* c8 ignore next */
|
|
657
|
-
_E: _ => _,
|
|
658
|
-
/* c8 ignore next */
|
|
659
|
-
_A: _ => _,
|
|
660
|
-
_V: moduleVersion
|
|
661
|
-
};
|
|
662
|
-
const sinkVariance = {
|
|
663
|
-
/* c8 ignore next */
|
|
664
|
-
_A: _ => _,
|
|
665
|
-
/* c8 ignore next */
|
|
666
|
-
_In: _ => _,
|
|
667
|
-
/* c8 ignore next */
|
|
668
|
-
_L: _ => _,
|
|
669
|
-
/* c8 ignore next */
|
|
670
|
-
_E: _ => _,
|
|
671
|
-
/* c8 ignore next */
|
|
672
|
-
_R: _ => _
|
|
673
|
-
};
|
|
674
|
-
const channelVariance = {
|
|
675
|
-
/* c8 ignore next */
|
|
676
|
-
_Env: _ => _,
|
|
677
|
-
/* c8 ignore next */
|
|
678
|
-
_InErr: _ => _,
|
|
679
|
-
/* c8 ignore next */
|
|
680
|
-
_InElem: _ => _,
|
|
681
|
-
/* c8 ignore next */
|
|
682
|
-
_InDone: _ => _,
|
|
683
|
-
/* c8 ignore next */
|
|
684
|
-
_OutErr: _ => _,
|
|
685
|
-
/* c8 ignore next */
|
|
686
|
-
_OutElem: _ => _,
|
|
687
|
-
/* c8 ignore next */
|
|
688
|
-
_OutDone: _ => _
|
|
689
|
-
};
|
|
690
|
-
/** @internal */
|
|
691
|
-
const EffectPrototype = {
|
|
692
|
-
[EffectTypeId]: effectVariance,
|
|
693
|
-
[StreamTypeId]: effectVariance,
|
|
694
|
-
[SinkTypeId]: sinkVariance,
|
|
695
|
-
[ChannelTypeId]: channelVariance,
|
|
696
|
-
[symbol](that) {
|
|
697
|
-
return this === that;
|
|
698
|
-
},
|
|
699
|
-
[symbol$1]() {
|
|
700
|
-
return random(this);
|
|
701
|
-
},
|
|
702
|
-
pipe() {
|
|
703
|
-
return pipeArguments(this, arguments);
|
|
704
|
-
}
|
|
705
|
-
};
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* @since 2.0.0
|
|
709
|
-
*/
|
|
710
|
-
const TypeId = /*#__PURE__*/Symbol.for("effect/Option");
|
|
711
|
-
const CommonProto = {
|
|
712
|
-
...EffectPrototype,
|
|
713
|
-
[TypeId]: {
|
|
714
|
-
_A: _ => _
|
|
715
|
-
},
|
|
716
|
-
[NodeInspectSymbol]() {
|
|
717
|
-
return this.toJSON();
|
|
718
|
-
},
|
|
719
|
-
toString() {
|
|
720
|
-
return format(this.toJSON());
|
|
721
|
-
}
|
|
722
|
-
};
|
|
723
|
-
const SomeProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(CommonProto), {
|
|
724
|
-
_tag: "Some",
|
|
725
|
-
_op: "Some",
|
|
726
|
-
[symbol](that) {
|
|
727
|
-
return isOption(that) && isSome$1(that) && equals(that.value, this.value);
|
|
728
|
-
},
|
|
729
|
-
[symbol$1]() {
|
|
730
|
-
return combine(hash(this._tag))(hash(this.value));
|
|
731
|
-
},
|
|
732
|
-
toJSON() {
|
|
733
|
-
return {
|
|
734
|
-
_id: "Option",
|
|
735
|
-
_tag: this._tag,
|
|
736
|
-
value: toJSON(this.value)
|
|
737
|
-
};
|
|
738
|
-
}
|
|
739
|
-
});
|
|
740
|
-
const NoneProto = /*#__PURE__*/Object.assign( /*#__PURE__*/Object.create(CommonProto), {
|
|
741
|
-
_tag: "None",
|
|
742
|
-
_op: "None",
|
|
743
|
-
[symbol](that) {
|
|
744
|
-
return isOption(that) && isNone$1(that);
|
|
745
|
-
},
|
|
746
|
-
[symbol$1]() {
|
|
747
|
-
return combine(hash(this._tag));
|
|
748
|
-
},
|
|
749
|
-
toJSON() {
|
|
750
|
-
return {
|
|
751
|
-
_id: "Option",
|
|
752
|
-
_tag: this._tag
|
|
753
|
-
};
|
|
754
|
-
}
|
|
755
|
-
});
|
|
756
|
-
/** @internal */
|
|
757
|
-
const isOption = input => hasProperty(input, TypeId);
|
|
758
|
-
/** @internal */
|
|
759
|
-
const isNone$1 = fa => fa._tag === "None";
|
|
760
|
-
/** @internal */
|
|
761
|
-
const isSome$1 = fa => fa._tag === "Some";
|
|
762
|
-
/** @internal */
|
|
763
|
-
const none$1 = /*#__PURE__*/Object.create(NoneProto);
|
|
764
|
-
/** @internal */
|
|
765
|
-
const some$1 = value => {
|
|
766
|
-
const a = Object.create(SomeProto);
|
|
767
|
-
a.value = value;
|
|
768
|
-
return a;
|
|
769
|
-
};
|
|
770
|
-
|
|
771
|
-
/**
|
|
772
|
-
* Creates a new `Option` that represents the absence of a value.
|
|
773
|
-
*
|
|
774
|
-
* @category constructors
|
|
775
|
-
* @since 2.0.0
|
|
776
|
-
*/
|
|
777
|
-
const none = () => none$1;
|
|
778
|
-
/**
|
|
779
|
-
* Creates a new `Option` that wraps the given value.
|
|
780
|
-
*
|
|
781
|
-
* @param value - The value to wrap.
|
|
782
|
-
*
|
|
783
|
-
* @category constructors
|
|
784
|
-
* @since 2.0.0
|
|
785
|
-
*/
|
|
786
|
-
const some = some$1;
|
|
787
|
-
/**
|
|
788
|
-
* Determine if a `Option` is a `None`.
|
|
789
|
-
*
|
|
790
|
-
* @param self - The `Option` to check.
|
|
791
|
-
*
|
|
792
|
-
* @example
|
|
793
|
-
* import { some, none, isNone } from 'effect/Option'
|
|
794
|
-
*
|
|
795
|
-
* assert.deepStrictEqual(isNone(some(1)), false)
|
|
796
|
-
* assert.deepStrictEqual(isNone(none()), true)
|
|
797
|
-
*
|
|
798
|
-
* @category guards
|
|
799
|
-
* @since 2.0.0
|
|
800
|
-
*/
|
|
801
|
-
const isNone = isNone$1;
|
|
802
|
-
/**
|
|
803
|
-
* Determine if a `Option` is a `Some`.
|
|
804
|
-
*
|
|
805
|
-
* @param self - The `Option` to check.
|
|
806
|
-
*
|
|
807
|
-
* @example
|
|
808
|
-
* import { some, none, isSome } from 'effect/Option'
|
|
809
|
-
*
|
|
810
|
-
* assert.deepStrictEqual(isSome(some(1)), true)
|
|
811
|
-
* assert.deepStrictEqual(isSome(none()), false)
|
|
812
|
-
*
|
|
813
|
-
* @category guards
|
|
814
|
-
* @since 2.0.0
|
|
815
|
-
*/
|
|
816
|
-
const isSome = isSome$1;
|
|
817
|
-
/**
|
|
818
|
-
* Returns the value of the `Option` if it is `Some`, otherwise returns `onNone`
|
|
819
|
-
*
|
|
820
|
-
* @param self - The `Option` to get the value of.
|
|
821
|
-
* @param onNone - Function that returns the default value to return if the `Option` is `None`.
|
|
822
|
-
*
|
|
823
|
-
* @example
|
|
824
|
-
* import { some, none, getOrElse } from 'effect/Option'
|
|
825
|
-
* import { pipe } from "effect/Function"
|
|
826
|
-
*
|
|
827
|
-
* assert.deepStrictEqual(pipe(some(1), getOrElse(() => 0)), 1)
|
|
828
|
-
* assert.deepStrictEqual(pipe(none(), getOrElse(() => 0)), 0)
|
|
829
|
-
*
|
|
830
|
-
* @category getters
|
|
831
|
-
* @since 2.0.0
|
|
832
|
-
*/
|
|
833
|
-
const getOrElse = /*#__PURE__*/dual(2, (self, onNone) => isNone(self) ? onNone() : self.value);
|
|
834
|
-
/**
|
|
835
|
-
* Returns the provided `Option` `that` if `self` is `None`, otherwise returns `self`.
|
|
836
|
-
*
|
|
837
|
-
* @param self - The first `Option` to be checked.
|
|
838
|
-
* @param that - The `Option` to return if `self` is `None`.
|
|
839
|
-
*
|
|
840
|
-
* @example
|
|
841
|
-
* import * as O from "effect/Option"
|
|
842
|
-
* import { pipe } from "effect/Function"
|
|
843
|
-
*
|
|
844
|
-
* assert.deepStrictEqual(
|
|
845
|
-
* pipe(
|
|
846
|
-
* O.none(),
|
|
847
|
-
* O.orElse(() => O.none())
|
|
848
|
-
* ),
|
|
849
|
-
* O.none()
|
|
850
|
-
* )
|
|
851
|
-
* assert.deepStrictEqual(
|
|
852
|
-
* pipe(
|
|
853
|
-
* O.some('a'),
|
|
854
|
-
* O.orElse(() => O.none())
|
|
855
|
-
* ),
|
|
856
|
-
* O.some('a')
|
|
857
|
-
* )
|
|
858
|
-
* assert.deepStrictEqual(
|
|
859
|
-
* pipe(
|
|
860
|
-
* O.none(),
|
|
861
|
-
* O.orElse(() => O.some('b'))
|
|
862
|
-
* ),
|
|
863
|
-
* O.some('b')
|
|
864
|
-
* )
|
|
865
|
-
* assert.deepStrictEqual(
|
|
866
|
-
* pipe(
|
|
867
|
-
* O.some('a'),
|
|
868
|
-
* O.orElse(() => O.some('b'))
|
|
869
|
-
* ),
|
|
870
|
-
* O.some('a')
|
|
871
|
-
* )
|
|
872
|
-
*
|
|
873
|
-
* @category error handling
|
|
874
|
-
* @since 2.0.0
|
|
875
|
-
*/
|
|
876
|
-
const orElse = /*#__PURE__*/dual(2, (self, that) => isNone(self) ? that() : self);
|
|
877
|
-
/**
|
|
878
|
-
* Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise
|
|
879
|
-
* returns the value wrapped in a `Some`.
|
|
880
|
-
*
|
|
881
|
-
* @param nullableValue - The nullable value to be converted to an `Option`.
|
|
882
|
-
*
|
|
883
|
-
* @example
|
|
884
|
-
* import * as O from "effect/Option"
|
|
885
|
-
*
|
|
886
|
-
* assert.deepStrictEqual(O.fromNullable(undefined), O.none())
|
|
887
|
-
* assert.deepStrictEqual(O.fromNullable(null), O.none())
|
|
888
|
-
* assert.deepStrictEqual(O.fromNullable(1), O.some(1))
|
|
889
|
-
*
|
|
890
|
-
* @category conversions
|
|
891
|
-
* @since 2.0.0
|
|
892
|
-
*/
|
|
893
|
-
const fromNullable = nullableValue => nullableValue == null ? none() : some(nullableValue);
|
|
894
|
-
/**
|
|
895
|
-
* Maps the `Some` side of an `Option` value to a new `Option` value.
|
|
896
|
-
*
|
|
897
|
-
* @param self - An `Option` to map
|
|
898
|
-
* @param f - The function to map over the value of the `Option`
|
|
899
|
-
*
|
|
900
|
-
* @category mapping
|
|
901
|
-
* @since 2.0.0
|
|
902
|
-
*/
|
|
903
|
-
const map = /*#__PURE__*/dual(2, (self, f) => isNone(self) ? none() : some(f(self.value)));
|
|
904
|
-
/**
|
|
905
|
-
* Applies a function to the value of an `Option` and flattens the result, if the input is `Some`.
|
|
906
|
-
*
|
|
907
|
-
* @category sequencing
|
|
908
|
-
* @since 2.0.0
|
|
909
|
-
*/
|
|
910
|
-
const flatMap = /*#__PURE__*/dual(2, (self, f) => isNone(self) ? none() : f(self.value));
|
|
911
|
-
/**
|
|
912
|
-
* This is `flatMap` + `fromNullable`, useful when working with optional values.
|
|
913
|
-
*
|
|
914
|
-
* @example
|
|
915
|
-
* import { some, none, flatMapNullable } from 'effect/Option'
|
|
916
|
-
* import { pipe } from "effect/Function"
|
|
917
|
-
*
|
|
918
|
-
* interface Employee {
|
|
919
|
-
* company?: {
|
|
920
|
-
* address?: {
|
|
921
|
-
* street?: {
|
|
922
|
-
* name?: string
|
|
923
|
-
* }
|
|
924
|
-
* }
|
|
925
|
-
* }
|
|
926
|
-
* }
|
|
927
|
-
*
|
|
928
|
-
* const employee1: Employee = { company: { address: { street: { name: 'high street' } } } }
|
|
929
|
-
*
|
|
930
|
-
* assert.deepStrictEqual(
|
|
931
|
-
* pipe(
|
|
932
|
-
* some(employee1),
|
|
933
|
-
* flatMapNullable(employee => employee.company?.address?.street?.name),
|
|
934
|
-
* ),
|
|
935
|
-
* some('high street')
|
|
936
|
-
* )
|
|
937
|
-
*
|
|
938
|
-
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
939
|
-
*
|
|
940
|
-
* assert.deepStrictEqual(
|
|
941
|
-
* pipe(
|
|
942
|
-
* some(employee2),
|
|
943
|
-
* flatMapNullable(employee => employee.company?.address?.street?.name),
|
|
944
|
-
* ),
|
|
945
|
-
* none()
|
|
946
|
-
* )
|
|
947
|
-
*
|
|
948
|
-
* @category sequencing
|
|
949
|
-
* @since 2.0.0
|
|
950
|
-
*/
|
|
951
|
-
const flatMapNullable = /*#__PURE__*/dual(2, (self, f) => isNone(self) ? none() : fromNullable(f(self.value)));
|
|
952
|
-
/**
|
|
953
|
-
* @category combining
|
|
954
|
-
* @since 2.0.0
|
|
955
|
-
*/
|
|
956
|
-
const product = (self, that) => isSome(self) && isSome(that) ? some([self.value, that.value]) : none();
|
|
957
|
-
/**
|
|
958
|
-
* Zips two `Option` values together using a provided function, returning a new `Option` of the result.
|
|
959
|
-
*
|
|
960
|
-
* @param self - The left-hand side of the zip operation
|
|
961
|
-
* @param that - The right-hand side of the zip operation
|
|
962
|
-
* @param f - The function used to combine the values of the two `Option`s
|
|
963
|
-
*
|
|
964
|
-
* @example
|
|
965
|
-
* import * as O from "effect/Option"
|
|
966
|
-
*
|
|
967
|
-
* type Complex = [real: number, imaginary: number]
|
|
968
|
-
*
|
|
969
|
-
* const complex = (real: number, imaginary: number): Complex => [real, imaginary]
|
|
970
|
-
*
|
|
971
|
-
* assert.deepStrictEqual(O.zipWith(O.none(), O.none(), complex), O.none())
|
|
972
|
-
* assert.deepStrictEqual(O.zipWith(O.some(1), O.none(), complex), O.none())
|
|
973
|
-
* assert.deepStrictEqual(O.zipWith(O.none(), O.some(1), complex), O.none())
|
|
974
|
-
* assert.deepStrictEqual(O.zipWith(O.some(1), O.some(2), complex), O.some([1, 2]))
|
|
975
|
-
*
|
|
976
|
-
* assert.deepStrictEqual(O.zipWith(O.some(1), complex)(O.some(2)), O.some([2, 1]))
|
|
977
|
-
*
|
|
978
|
-
* @category zipping
|
|
979
|
-
* @since 2.0.0
|
|
980
|
-
*/
|
|
981
|
-
const zipWith = /*#__PURE__*/dual(3, (self, that, f) => map(product(self, that), ([a, b]) => f(a, b)));
|
|
982
|
-
/**
|
|
983
|
-
* Maps over the value of an `Option` and filters out `None`s.
|
|
984
|
-
*
|
|
985
|
-
* Useful when in addition to filtering you also want to change the type of the `Option`.
|
|
986
|
-
*
|
|
987
|
-
* @param self - The `Option` to map over.
|
|
988
|
-
* @param f - A function to apply to the value of the `Option`.
|
|
989
|
-
*
|
|
990
|
-
* @example
|
|
991
|
-
* import * as O from "effect/Option"
|
|
992
|
-
*
|
|
993
|
-
* const evenNumber = (n: number) => n % 2 === 0 ? O.some(n) : O.none()
|
|
994
|
-
*
|
|
995
|
-
* assert.deepStrictEqual(O.filterMap(O.none(), evenNumber), O.none())
|
|
996
|
-
* assert.deepStrictEqual(O.filterMap(O.some(3), evenNumber), O.none())
|
|
997
|
-
* assert.deepStrictEqual(O.filterMap(O.some(2), evenNumber), O.some(2))
|
|
998
|
-
*
|
|
999
|
-
* @category filtering
|
|
1000
|
-
* @since 2.0.0
|
|
1001
|
-
*/
|
|
1002
|
-
const filterMap = /*#__PURE__*/dual(2, (self, f) => isNone(self) ? none() : f(self.value));
|
|
1003
|
-
/**
|
|
1004
|
-
* Filters an `Option` using a predicate. If the predicate is not satisfied or the `Option` is `None` returns `None`.
|
|
1005
|
-
*
|
|
1006
|
-
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
1007
|
-
*
|
|
1008
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
1009
|
-
* @param fb - The `Option` to filter.
|
|
1010
|
-
*
|
|
1011
|
-
* @example
|
|
1012
|
-
* import * as O from "effect/Option"
|
|
1013
|
-
*
|
|
1014
|
-
* // predicate
|
|
1015
|
-
* const isEven = (n: number) => n % 2 === 0
|
|
1016
|
-
*
|
|
1017
|
-
* assert.deepStrictEqual(O.filter(O.none(), isEven), O.none())
|
|
1018
|
-
* assert.deepStrictEqual(O.filter(O.some(3), isEven), O.none())
|
|
1019
|
-
* assert.deepStrictEqual(O.filter(O.some(2), isEven), O.some(2))
|
|
1020
|
-
*
|
|
1021
|
-
* // refinement
|
|
1022
|
-
* const isNumber = (v: unknown): v is number => typeof v === "number"
|
|
1023
|
-
*
|
|
1024
|
-
* assert.deepStrictEqual(O.filter(O.none(), isNumber), O.none())
|
|
1025
|
-
* assert.deepStrictEqual(O.filter(O.some('hello'), isNumber), O.none())
|
|
1026
|
-
* assert.deepStrictEqual(O.filter(O.some(2), isNumber), O.some(2))
|
|
1027
|
-
*
|
|
1028
|
-
* @category filtering
|
|
1029
|
-
* @since 2.0.0
|
|
1030
|
-
*/
|
|
1031
|
-
const filter = /*#__PURE__*/dual(2, (self, predicate) => filterMap(self, b => predicate(b) ? some$1(b) : none$1));
|
|
1032
|
-
/**
|
|
1033
|
-
* Transforms a `Predicate` function into a `Some` of the input value if the predicate returns `true` or `None`
|
|
1034
|
-
* if the predicate returns `false`.
|
|
1035
|
-
*
|
|
1036
|
-
* @param predicate - A `Predicate` function that takes in a value of type `A` and returns a boolean.
|
|
1037
|
-
*
|
|
1038
|
-
* @example
|
|
1039
|
-
* import * as O from "effect/Option"
|
|
1040
|
-
*
|
|
1041
|
-
* const getOption = O.liftPredicate((n: number) => n >= 0)
|
|
1042
|
-
*
|
|
1043
|
-
* assert.deepStrictEqual(getOption(-1), O.none())
|
|
1044
|
-
* assert.deepStrictEqual(getOption(1), O.some(1))
|
|
1045
|
-
*
|
|
1046
|
-
* @category lifting
|
|
1047
|
-
* @since 2.0.0
|
|
1048
|
-
*/
|
|
1049
|
-
const liftPredicate = predicate => b => predicate(b) ? some(b) : none();
|
|
1050
|
-
/**
|
|
1051
|
-
* Check if a value in an `Option` type meets a certain predicate.
|
|
1052
|
-
*
|
|
1053
|
-
* @param self - The `Option` to check.
|
|
1054
|
-
* @param predicate - The condition to check.
|
|
1055
|
-
*
|
|
1056
|
-
* @example
|
|
1057
|
-
* import { some, none, exists } from 'effect/Option'
|
|
1058
|
-
* import { pipe } from "effect/Function"
|
|
1059
|
-
*
|
|
1060
|
-
* const isEven = (n: number) => n % 2 === 0
|
|
1061
|
-
*
|
|
1062
|
-
* assert.deepStrictEqual(pipe(some(2), exists(isEven)), true)
|
|
1063
|
-
* assert.deepStrictEqual(pipe(some(1), exists(isEven)), false)
|
|
1064
|
-
* assert.deepStrictEqual(pipe(none(), exists(isEven)), false)
|
|
1065
|
-
*
|
|
1066
|
-
* @since 2.0.0
|
|
1067
|
-
*/
|
|
1068
|
-
const exists = /*#__PURE__*/dual(2, (self, refinement) => isNone(self) ? false : refinement(self.value));
|
|
1069
|
-
|
|
1070
|
-
/**
|
|
1071
|
-
* Bun currently has a bug where `setTimeout` doesn't behave correctly with a 0ms delay.
|
|
1072
|
-
*
|
|
1073
|
-
* @see https://github.com/oven-sh/bun/issues/3333
|
|
1074
|
-
*/
|
|
1075
|
-
/** @internal */
|
|
1076
|
-
typeof process === "undefined" ? false : !!process?.isBun;
|
|
1077
|
-
|
|
1078
|
-
const RE_JSX_ANNOTATION_REGEX = /@jsx\s+(\S+)/u;
|
|
1079
|
-
// Does not check for reserved keywords or unicode characters
|
|
1080
|
-
const RE_JS_IDENTIFIER_REGEX = /^[$A-Z_a-z][\w$]*$/u;
|
|
1081
|
-
function getFragmentFromContext(context) {
|
|
1082
|
-
const settings = shared.parseSchema(shared.ESLintSettingsSchema, context.settings);
|
|
1083
|
-
const fragment = settings.reactOptions?.jsxPragmaFrag;
|
|
1084
|
-
if (isString(fragment) && RE_JS_IDENTIFIER_REGEX.test(fragment)) return fragment;
|
|
1085
|
-
return "Fragment";
|
|
1086
|
-
}
|
|
1087
|
-
const getPragmaFromContext = memo((context)=>{
|
|
1088
|
-
const settings = shared.parseSchema(shared.ESLintSettingsSchema, context.settings);
|
|
1089
|
-
const pragma = settings.reactOptions?.jsxPragma;
|
|
1090
|
-
const { sourceCode } = context;
|
|
1091
|
-
const pragmaNode = sourceCode.getAllComments().find((node)=>RE_JSX_ANNOTATION_REGEX.test(node.value));
|
|
1092
|
-
return pipe(orElse(fromNullable(pragma), ()=>pipe(fromNullable(pragmaNode), map(({ value })=>RE_JSX_ANNOTATION_REGEX.exec(value)), flatMapNullable((matches)=>matches?.[1]?.split(".")[0]))), flatMap(liftPredicate((x)=>RE_JS_IDENTIFIER_REGEX.test(x))), getOrElse(constant("React")));
|
|
1093
|
-
});
|
|
1094
|
-
|
|
1095
|
-
const t=Symbol.for("@ts-pattern/matcher"),e=Symbol.for("@ts-pattern/isVariadic"),n="@ts-pattern/anonymous-select-key",r=t=>Boolean(t&&"object"==typeof t),i=e=>e&&!!e[t],s=(n,o,c)=>{if(i(n)){const e=n[t](),{matched:r,selections:i}=e.match(o);return r&&i&&Object.keys(i).forEach(t=>c(t,i[t])),r}if(r(n)){if(!r(o))return !1;if(Array.isArray(n)){if(!Array.isArray(o))return !1;let t=[],r=[],a=[];for(const s of n.keys()){const o=n[s];i(o)&&o[e]?a.push(o):a.length?r.push(o):t.push(o);}if(a.length){if(a.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(o.length<t.length+r.length)return !1;const e=o.slice(0,t.length),n=0===r.length?[]:o.slice(-r.length),i=o.slice(t.length,0===r.length?Infinity:-r.length);return t.every((t,n)=>s(t,e[n],c))&&r.every((t,e)=>s(t,n[e],c))&&(0===a.length||s(a[0],i,c))}return n.length===o.length&&n.every((t,e)=>s(t,o[e],c))}return Object.keys(n).every(e=>{const r=n[e];return (e in o||i(a=r)&&"optional"===a[t]().matcherType)&&s(r,o[e],c);var a;})}return Object.is(o,n)},o=e=>{var n,s,a;return r(e)?i(e)?null!=(n=null==(s=(a=e[t]()).getSelectionKeys)?void 0:s.call(a))?n:[]:Array.isArray(e)?c(e,o):c(Object.values(e),o):[]},c=(t,e)=>t.reduce((t,n)=>t.concat(e(n)),[]);function a(...t){if(1===t.length){const[e]=t;return t=>s(e,t,()=>{})}if(2===t.length){const[e,n]=t;return s(e,n,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}function u(t){return Object.assign(t,{optional:()=>l(t),and:e=>m(t,e),or:e=>y(t,e),select:e=>void 0===e?p(t):p(e,t)})}function h(t){return Object.assign((t=>Object.assign(t,{*[Symbol.iterator](){yield Object.assign(t,{[e]:!0});}}))(t),{optional:()=>h(l(t)),select:e=>h(void 0===e?p(t):p(e,t))})}function l(e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return void 0===t?(o(e).forEach(t=>r(t,void 0)),{matched:!0,selections:n}):{matched:s(e,t,r),selections:n}},getSelectionKeys:()=>o(e),matcherType:"optional"})})}const f=(t,e)=>{for(const n of t)if(!e(n))return !1;return !0},g=(t,e)=>{for(const[n,r]of t.entries())if(!e(r,n))return !1;return !0};function m(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return {matched:e.every(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"and"})})}function y(...e){return u({[t]:()=>({match:t=>{let n={};const r=(t,e)=>{n[t]=e;};return c(e,o).forEach(t=>r(t,void 0)),{matched:e.some(e=>s(e,t,r)),selections:n}},getSelectionKeys:()=>c(e,o),matcherType:"or"})})}function d(e){return {[t]:()=>({match:t=>({matched:Boolean(e(t))})})}}function p(...e){const r="string"==typeof e[0]?e[0]:void 0,i=2===e.length?e[1]:"string"==typeof e[0]?void 0:e[0];return u({[t]:()=>({match:t=>{let e={[null!=r?r:n]:t};return {matched:void 0===i||s(i,t,(t,n)=>{e[t]=n;}),selections:e}},getSelectionKeys:()=>[null!=r?r:n].concat(void 0===i?[]:o(i))})})}function v(t){return "number"==typeof t}function b(t){return "string"==typeof t}function w(t){return "bigint"==typeof t}const S=u(d(function(t){return !0})),O=S,j=t=>Object.assign(u(t),{startsWith:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.startsWith(n)))));var n;},endsWith:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.endsWith(n)))));var n;},minLength:e=>j(m(t,(t=>d(e=>b(e)&&e.length>=t))(e))),maxLength:e=>j(m(t,(t=>d(e=>b(e)&&e.length<=t))(e))),includes:e=>{return j(m(t,(n=e,d(t=>b(t)&&t.includes(n)))));var n;},regex:e=>{return j(m(t,(n=e,d(t=>b(t)&&Boolean(t.match(n))))));var n;}}),E=j(d(b)),K=t=>Object.assign(u(t),{between:(e,n)=>K(m(t,((t,e)=>d(n=>v(n)&&t<=n&&e>=n))(e,n))),lt:e=>K(m(t,(t=>d(e=>v(e)&&e<t))(e))),gt:e=>K(m(t,(t=>d(e=>v(e)&&e>t))(e))),lte:e=>K(m(t,(t=>d(e=>v(e)&&e<=t))(e))),gte:e=>K(m(t,(t=>d(e=>v(e)&&e>=t))(e))),int:()=>K(m(t,d(t=>v(t)&&Number.isInteger(t)))),finite:()=>K(m(t,d(t=>v(t)&&Number.isFinite(t)))),positive:()=>K(m(t,d(t=>v(t)&&t>0))),negative:()=>K(m(t,d(t=>v(t)&&t<0)))}),A=K(d(v)),x=t=>Object.assign(u(t),{between:(e,n)=>x(m(t,((t,e)=>d(n=>w(n)&&t<=n&&e>=n))(e,n))),lt:e=>x(m(t,(t=>d(e=>w(e)&&e<t))(e))),gt:e=>x(m(t,(t=>d(e=>w(e)&&e>t))(e))),lte:e=>x(m(t,(t=>d(e=>w(e)&&e<=t))(e))),gte:e=>x(m(t,(t=>d(e=>w(e)&&e>=t))(e))),positive:()=>x(m(t,d(t=>w(t)&&t>0))),negative:()=>x(m(t,d(t=>w(t)&&t<0)))}),P=x(d(w)),T=u(d(function(t){return "boolean"==typeof t})),k=u(d(function(t){return "symbol"==typeof t})),B=u(d(function(t){return null==t}));var _={__proto__:null,matcher:t,optional:l,array:function(...e){return h({[t]:()=>({match:t=>{if(!Array.isArray(t))return {matched:!1};if(0===e.length)return {matched:!0};const n=e[0];let r={};if(0===t.length)return o(n).forEach(t=>{r[t]=[];}),{matched:!0,selections:r};const i=(t,e)=>{r[t]=(r[t]||[]).concat([e]);};return {matched:t.every(t=>s(n,t,i)),selections:r}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},set:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Set))return {matched:!1};let n={};if(0===t.size)return {matched:!0,selections:n};if(0===e.length)return {matched:!0};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e]);},i=e[0];return {matched:f(t,t=>s(i,t,r)),selections:n}},getSelectionKeys:()=>0===e.length?[]:o(e[0])})})},map:function(...e){return u({[t]:()=>({match:t=>{if(!(t instanceof Map))return {matched:!1};let n={};if(0===t.size)return {matched:!0,selections:n};const r=(t,e)=>{n[t]=(n[t]||[]).concat([e]);};if(0===e.length)return {matched:!0};var i;if(1===e.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(i=e[0])?void 0:i.toString()}`);const[o,c]=e;return {matched:g(t,(t,e)=>{const n=s(o,e,r),i=s(c,t,r);return n&&i}),selections:n}},getSelectionKeys:()=>0===e.length?[]:[...o(e[0]),...o(e[1])]})})},intersection:m,union:y,not:function(e){return u({[t]:()=>({match:t=>({matched:!s(e,t,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:d,select:p,any:S,_:O,string:E,number:A,bigint:P,boolean:T,symbol:k,nullish:B,instanceOf:function(t){return u(d(function(t){return e=>e instanceof t}(t)))},shape:function(t){return u(d(a(t)))}};const W={matched:!1,value:void 0};function N(t){return new $(t,W)}class ${constructor(t,e){this.input=void 0,this.state=void 0,this.input=t,this.state=e;}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]];let i;3===t.length&&"function"==typeof t[1]?i=t[1]:t.length>2&&r.push(...t.slice(1,t.length-1));let o=!1,c={};const a=(t,e)=>{o=!0,c[t]=e;},u=!r.some(t=>s(t,this.input,a))||i&&!Boolean(i(this.input))?W:{matched:!0,value:e(o?n in c?c[n]:c:this.input,this.input)};return new $(this.input,u)}when(t,e){if(this.state.matched)return this;const n=Boolean(t(this.input));return new $(this.input,n?{matched:!0,value:e(this.input,this.input)}:W)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(){if(this.state.matched)return this.state.value;let t;try{t=JSON.stringify(this.input);}catch(e){t=this.input;}throw new Error(`Pattern matching error: no pattern matches value ${t}`)}run(){return this.exhaustive()}returnType(){return this}}
|
|
1096
|
-
|
|
1097
|
-
function isInitializedFromPragma(variableName, context, initialScope, pragma = getPragmaFromContext(context)) {
|
|
1098
|
-
const maybeVariable = _var.findVariable(variableName, initialScope);
|
|
1099
|
-
const maybeLatestDef = flatMapNullable(maybeVariable, (variable)=>variable.defs.at(-1));
|
|
1100
|
-
if (isNone(maybeLatestDef)) return false;
|
|
1101
|
-
const latestDef = maybeLatestDef.value;
|
|
1102
|
-
const { node, parent } = latestDef;
|
|
1103
|
-
if (node.type === ast.NodeType.VariableDeclarator && node.init) {
|
|
1104
|
-
const { init } = node;
|
|
1105
|
-
// check for: `variable = pragma.variable`
|
|
1106
|
-
if (a({
|
|
1107
|
-
type: "MemberExpression",
|
|
1108
|
-
object: {
|
|
1109
|
-
type: "Identifier",
|
|
1110
|
-
name: pragma
|
|
1111
|
-
}
|
|
1112
|
-
}, init)) return true;
|
|
1113
|
-
// check for: `{ variable } = pragma`
|
|
1114
|
-
if (a({
|
|
1115
|
-
type: "Identifier",
|
|
1116
|
-
name: pragma
|
|
1117
|
-
}, init)) return true;
|
|
1118
|
-
// check if from a require call: `require("react")`
|
|
1119
|
-
const maybeRequireExpression = N(init).with({
|
|
1120
|
-
type: ast.NodeType.CallExpression,
|
|
1121
|
-
callee: {
|
|
1122
|
-
type: ast.NodeType.Identifier,
|
|
1123
|
-
name: "require"
|
|
1124
|
-
}
|
|
1125
|
-
}, (exp)=>some(exp)).with({
|
|
1126
|
-
type: ast.NodeType.MemberExpression,
|
|
1127
|
-
object: {
|
|
1128
|
-
type: ast.NodeType.CallExpression,
|
|
1129
|
-
callee: {
|
|
1130
|
-
type: ast.NodeType.Identifier,
|
|
1131
|
-
name: "require"
|
|
1132
|
-
}
|
|
1133
|
-
}
|
|
1134
|
-
}, ({ object })=>some(object)).otherwise(none);
|
|
1135
|
-
if (isNone(maybeRequireExpression)) return false;
|
|
1136
|
-
const requireExpression = maybeRequireExpression.value;
|
|
1137
|
-
const [firstArg] = requireExpression.arguments;
|
|
1138
|
-
if (firstArg?.type !== ast.NodeType.Literal) return false;
|
|
1139
|
-
return firstArg.value === pragma.toLowerCase();
|
|
1140
|
-
}
|
|
1141
|
-
// latest definition is an import declaration: import { variable } from 'react'
|
|
1142
|
-
return a({
|
|
1143
|
-
type: "ImportDeclaration",
|
|
1144
|
-
source: {
|
|
1145
|
-
value: pragma.toLowerCase()
|
|
1146
|
-
}
|
|
1147
|
-
}, parent);
|
|
1148
|
-
}
|
|
1149
|
-
function isPropertyOfPragma(name, context, pragma = getPragmaFromContext(context)) {
|
|
1150
|
-
const isMatch = a({
|
|
1151
|
-
type: ast.NodeType.MemberExpression,
|
|
1152
|
-
object: {
|
|
1153
|
-
type: ast.NodeType.Identifier,
|
|
1154
|
-
name: pragma
|
|
1155
|
-
},
|
|
1156
|
-
property: {
|
|
1157
|
-
name
|
|
1158
|
-
}
|
|
1159
|
-
});
|
|
1160
|
-
return isMatch;
|
|
1161
|
-
}
|
|
1162
|
-
/**
|
|
1163
|
-
* Checks if the given node is a call expression to the given function or method of the pragma
|
|
1164
|
-
* @param name The name of the function or method to check
|
|
1165
|
-
* @returns A predicate that checks if the given node is a call expression to the given function or method
|
|
1166
|
-
*/ function isFromPragma(name) {
|
|
1167
|
-
return (node, context)=>{
|
|
1168
|
-
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
1169
|
-
if (node.type === ast.NodeType.MemberExpression) return isPropertyOfPragma(name, context)(node);
|
|
1170
|
-
if (node.name === name) return isInitializedFromPragma(name, context, initialScope);
|
|
1171
|
-
return false;
|
|
1172
|
-
};
|
|
1173
|
-
}
|
|
1174
|
-
/**
|
|
1175
|
-
* @internal
|
|
1176
|
-
* @param pragmaMemberName
|
|
1177
|
-
* @param name
|
|
1178
|
-
* @returns A function that checks if a given node is a member expression of a Pragma member.
|
|
1179
|
-
*/ function isFromPragmaMember(pragmaMemberName, name) {
|
|
1180
|
-
return (node, context, pragma = getPragmaFromContext(context))=>{
|
|
1181
|
-
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
1182
|
-
if (node.property.type !== ast.NodeType.Identifier || node.property.name !== name) return false;
|
|
1183
|
-
if (node.object.type === ast.NodeType.Identifier && node.object.name === pragmaMemberName) {
|
|
1184
|
-
return isInitializedFromPragma(node.object.name, context, initialScope, pragma);
|
|
1185
|
-
}
|
|
1186
|
-
if (node.object.type === ast.NodeType.MemberExpression && node.object.object.type === ast.NodeType.Identifier && node.object.object.name === pragma && node.object.property.type === ast.NodeType.Identifier) {
|
|
1187
|
-
return node.object.property.name === pragmaMemberName;
|
|
1188
|
-
}
|
|
1189
|
-
return false;
|
|
1190
|
-
};
|
|
1191
|
-
}
|
|
1192
|
-
function isCallFromPragma(name) {
|
|
1193
|
-
return (node, context)=>{
|
|
1194
|
-
if (!ast.isOneOf([
|
|
1195
|
-
ast.NodeType.Identifier,
|
|
1196
|
-
ast.NodeType.MemberExpression
|
|
1197
|
-
])(node.callee)) return false;
|
|
1198
|
-
return isFromPragma(name)(node.callee, context);
|
|
1199
|
-
};
|
|
1200
|
-
}
|
|
1201
|
-
function isCallFromPragmaMember(pragmaMemberName, name) {
|
|
1202
|
-
return (node, context)=>{
|
|
1203
|
-
if (!ast.is(ast.NodeType.MemberExpression)(node.callee)) return false;
|
|
1204
|
-
return isFromPragmaMember(pragmaMemberName, name)(node.callee, context);
|
|
1205
|
-
};
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
const isCreateElement = isFromPragma("createElement");
|
|
1209
|
-
const isCreateElementCall = (node, context)=>{
|
|
1210
|
-
if (!ast.isOneOf([
|
|
1211
|
-
ast.NodeType.Identifier,
|
|
1212
|
-
ast.NodeType.MemberExpression
|
|
1213
|
-
])(node.callee)) return false;
|
|
1214
|
-
return isCreateElement(node.callee, context);
|
|
1215
|
-
};
|
|
1216
|
-
const isCloneElement = isFromPragma("cloneElement");
|
|
1217
|
-
const isCloneElementCall = (node, context)=>{
|
|
1218
|
-
if (!ast.isOneOf([
|
|
1219
|
-
ast.NodeType.Identifier,
|
|
1220
|
-
ast.NodeType.MemberExpression
|
|
1221
|
-
])(node.callee)) return false;
|
|
1222
|
-
return isCloneElement(node.callee, context);
|
|
1223
|
-
};
|
|
1224
|
-
|
|
1225
|
-
function resolveMemberExpressions(object, property) {
|
|
1226
|
-
if (object.type === types.AST_NODE_TYPES.JSXMemberExpression) {
|
|
1227
|
-
return `${resolveMemberExpressions(object.object, object.property)}.${property.name}`;
|
|
1228
|
-
}
|
|
1229
|
-
if (object.type === types.AST_NODE_TYPES.JSXNamespacedName) {
|
|
1230
|
-
return `${object.namespace.name}:${object.name.name}.${property.name}`;
|
|
1231
|
-
}
|
|
1232
|
-
return `${object.name}.${property.name}`;
|
|
1233
|
-
}
|
|
1234
|
-
/**
|
|
1235
|
-
* Returns the tag name associated with a JSXOpeningElement.
|
|
1236
|
-
* @param node The visited JSXOpeningElement node object.
|
|
1237
|
-
* @returns The element's tag name.
|
|
1238
|
-
*/ function elementType(node) {
|
|
1239
|
-
if (node.type === types.AST_NODE_TYPES.JSXOpeningFragment) {
|
|
1240
|
-
return "<>";
|
|
1241
|
-
}
|
|
1242
|
-
const { name } = node;
|
|
1243
|
-
if (name.type === types.AST_NODE_TYPES.JSXMemberExpression) {
|
|
1244
|
-
const { object, property } = name;
|
|
1245
|
-
return resolveMemberExpressions(object, property);
|
|
1246
|
-
}
|
|
1247
|
-
if (name.type === types.AST_NODE_TYPES.JSXNamespacedName) {
|
|
1248
|
-
return `${name.namespace.name}:${name.name.name}`;
|
|
1249
|
-
}
|
|
1250
|
-
return name.name;
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
/**
|
|
1254
|
-
* Determines whether inside createElement's props.
|
|
1255
|
-
* @param node The AST node to check
|
|
1256
|
-
* @param context The rule context
|
|
1257
|
-
* @returns `true` if the node is inside createElement's props
|
|
1258
|
-
*/ function isInsideCreateElementProps(node, context) {
|
|
1259
|
-
return pipe(ast.traverseUp(node, (n)=>ast.is(ast.NodeType.CallExpression)(n) && isCreateElementCall(n, context)), filter(ast.is(ast.NodeType.CallExpression)), flatMapNullable((c)=>c.arguments.at(1)), filter(ast.is(ast.NodeType.ObjectExpression)), zipWith(ast.traverseUp(node, ast.is(ast.NodeType.ObjectExpression)), (a, b)=>a === b), getOrElse(constFalse));
|
|
1260
|
-
}
|
|
1261
|
-
function isChildrenOfCreateElement(node, context) {
|
|
1262
|
-
return pipe(fromNullable(node.parent), filter(ast.is(ast.NodeType.CallExpression)), filter((n)=>isCreateElementCall(n, context)), exists((n)=>n.arguments.slice(2).some((arg)=>arg === node)));
|
|
1263
|
-
}
|
|
1264
|
-
/**
|
|
1265
|
-
* Check if a `JSXElement` or `JSXFragment` has children
|
|
1266
|
-
* @param node The AST node to check
|
|
1267
|
-
* @param predicate A predicate to filter the children
|
|
1268
|
-
* @returns `true` if the node has children
|
|
1269
|
-
*/ function hasChildren(node, predicate) {
|
|
1270
|
-
if (isFunction(predicate)) return node.children.some(predicate);
|
|
1271
|
-
return node.children.length > 0;
|
|
1272
|
-
}
|
|
1273
|
-
/**
|
|
1274
|
-
* Check if a node is a child of a `JSXElement`
|
|
1275
|
-
* @param node The AST node to check
|
|
1276
|
-
* @returns `true` if the node is a child of a `JSXElement`
|
|
1277
|
-
*/ function isChildOfJSXElement(node) {
|
|
1278
|
-
return node.parent?.type === ast.NodeType.JSXElement && node.parent.children.some((child)=>child === node);
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
/**
|
|
1282
|
-
* Check if a node is a `JSXElement` of `User-Defined Component` type
|
|
1283
|
-
* @param node The AST node to check
|
|
1284
|
-
* @returns `true` if the node is a `JSXElement` of `User-Defined Component` type
|
|
1285
|
-
*/ function isJSXElementOfUserDefinedComponent(node) {
|
|
1286
|
-
return node.openingElement.name.type === ast.NodeType.JSXIdentifier && /^[A-Z]/u.test(node.openingElement.name.name);
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* Check if a node is a `JSXFragment` of `Built-in Component` type
|
|
1290
|
-
* @param node The AST node to check
|
|
1291
|
-
* @returns `true` if the node is a `JSXFragment` of `Built-in Component` type
|
|
1292
|
-
*/ function isJSXElementOfBuiltinComponent(node) {
|
|
1293
|
-
return node.openingElement.name.type === ast.NodeType.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
const isFragment = (node, pragma, fragment)=>{
|
|
1297
|
-
if (!ast.isOneOf([
|
|
1298
|
-
ast.NodeType.JSXElement,
|
|
1299
|
-
ast.NodeType.JSXFragment
|
|
1300
|
-
])(node)) return false;
|
|
1301
|
-
return isFragmentSyntax(node) || isFragmentElement(node, pragma, fragment);
|
|
1302
|
-
};
|
|
1303
|
-
/**
|
|
1304
|
-
* Check if a node is `<></>`
|
|
1305
|
-
*/ const isFragmentSyntax = ast.is(ast.NodeType.JSXFragment);
|
|
1306
|
-
/**
|
|
1307
|
-
* Check if a node is `<Fragment></Fragment>` or `<Pragma.Fragment></Pragma.Fragment>`
|
|
1308
|
-
* @param node
|
|
1309
|
-
* @param pragma
|
|
1310
|
-
* @param fragment
|
|
1311
|
-
*/ function isFragmentElement(node, pragma, fragment) {
|
|
1312
|
-
const { name } = node.openingElement;
|
|
1313
|
-
// <Fragment>
|
|
1314
|
-
if (name.type === ast.NodeType.JSXIdentifier && name.name === fragment) return true;
|
|
1315
|
-
// <Pragma.Fragment>
|
|
1316
|
-
return name.type === ast.NodeType.JSXMemberExpression && name.object.type === ast.NodeType.JSXIdentifier && name.object.name === pragma && name.property.name === fragment;
|
|
1317
|
-
}
|
|
1318
|
-
|
|
1319
|
-
// type ReactNode =
|
|
1320
|
-
// | ReactElement
|
|
1321
|
-
// | string
|
|
1322
|
-
// | number
|
|
1323
|
-
// | Iterable<ReactNode>
|
|
1324
|
-
// | ReactPortal
|
|
1325
|
-
// | boolean
|
|
1326
|
-
// | null
|
|
1327
|
-
// | undefined
|
|
1328
|
-
// | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
|
|
1329
|
-
// keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
|
|
1330
|
-
// ];
|
|
1331
|
-
/* eslint-disable perfectionist/sort-objects */ const JSXValueHint = {
|
|
1332
|
-
None: 0n,
|
|
1333
|
-
SkipNullLiteral: 1n << 0n,
|
|
1334
|
-
SkipUndefinedLiteral: 1n << 1n,
|
|
1335
|
-
SkipBooleanLiteral: 1n << 2n,
|
|
1336
|
-
SkipStringLiteral: 1n << 3n,
|
|
1337
|
-
SkipNumberLiteral: 1n << 4n,
|
|
1338
|
-
SkipCreateElement: 1n << 5n,
|
|
1339
|
-
StrictArray: 1n << 6n,
|
|
1340
|
-
StrictLogical: 1n << 7n,
|
|
1341
|
-
StrictConditional: 1n << 8n
|
|
1342
|
-
};
|
|
1343
|
-
/* eslint-enable perfectionist/sort-objects */ const DEFAULT_JSX_VALUE_HINT = JSXValueHint.SkipUndefinedLiteral | JSXValueHint.SkipBooleanLiteral;
|
|
1344
|
-
/**
|
|
1345
|
-
* Check if a node is a JSX value
|
|
1346
|
-
* @param node The AST node to check
|
|
1347
|
-
* @param context The rule context
|
|
1348
|
-
* @param hint The `JSXValueHint` to use
|
|
1349
|
-
* @returns boolean
|
|
1350
|
-
*/ function isJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
1351
|
-
if (!node) return false;
|
|
1352
|
-
return N(node).with({
|
|
1353
|
-
type: ast.NodeType.JSXElement
|
|
1354
|
-
}, constTrue).with({
|
|
1355
|
-
type: ast.NodeType.JSXFragment
|
|
1356
|
-
}, constTrue).with({
|
|
1357
|
-
type: ast.NodeType.JSXMemberExpression
|
|
1358
|
-
}, constTrue).with({
|
|
1359
|
-
type: ast.NodeType.JSXNamespacedName
|
|
1360
|
-
}, constTrue).with({
|
|
1361
|
-
type: ast.NodeType.Literal
|
|
1362
|
-
}, (node)=>{
|
|
1363
|
-
return N(node.value).with(null, ()=>!(hint & JSXValueHint.SkipNullLiteral)).with(_.boolean, ()=>!(hint & JSXValueHint.SkipBooleanLiteral)).with(_.string, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with(_.number, ()=>!(hint & JSXValueHint.SkipNumberLiteral)).otherwise(constFalse);
|
|
1364
|
-
}).with({
|
|
1365
|
-
type: ast.NodeType.TemplateLiteral
|
|
1366
|
-
}, ()=>!(hint & JSXValueHint.SkipStringLiteral)).with({
|
|
1367
|
-
type: ast.NodeType.ArrayExpression
|
|
1368
|
-
}, (node)=>{
|
|
1369
|
-
if (hint & JSXValueHint.StrictArray) return node.elements.every((n)=>isJSXValue(n, context, hint));
|
|
1370
|
-
return node.elements.some((n)=>isJSXValue(n, context, hint));
|
|
1371
|
-
}).with({
|
|
1372
|
-
type: ast.NodeType.ConditionalExpression
|
|
1373
|
-
}, (node)=>{
|
|
1374
|
-
function leftHasJSX(node) {
|
|
1375
|
-
if (Array.isArray(node.consequent)) {
|
|
1376
|
-
if (hint & JSXValueHint.StrictArray) {
|
|
1377
|
-
return node.consequent.every((n)=>isJSXValue(n, context, hint));
|
|
1378
|
-
}
|
|
1379
|
-
return node.consequent.some((n)=>isJSXValue(n, context, hint));
|
|
1380
|
-
}
|
|
1381
|
-
return isJSXValue(node.consequent, context, hint);
|
|
1382
|
-
}
|
|
1383
|
-
function rightHasJSX(node) {
|
|
1384
|
-
return isJSXValue(node.alternate, context, hint);
|
|
1385
|
-
}
|
|
1386
|
-
if (hint & JSXValueHint.StrictConditional) {
|
|
1387
|
-
return leftHasJSX(node) && rightHasJSX(node);
|
|
1388
|
-
}
|
|
1389
|
-
return leftHasJSX(node) || rightHasJSX(node);
|
|
1390
|
-
}).with({
|
|
1391
|
-
type: ast.NodeType.LogicalExpression
|
|
1392
|
-
}, (node)=>{
|
|
1393
|
-
return isJSXValue(node.left, context, hint) || isJSXValue(node.right, context, hint);
|
|
1394
|
-
}).with({
|
|
1395
|
-
type: ast.NodeType.SequenceExpression
|
|
1396
|
-
}, (node)=>{
|
|
1397
|
-
const exp = node.expressions.at(-1);
|
|
1398
|
-
return isJSXValue(exp, context, hint);
|
|
1399
|
-
}).with({
|
|
1400
|
-
type: ast.NodeType.CallExpression
|
|
1401
|
-
}, (node)=>{
|
|
1402
|
-
if (hint & JSXValueHint.SkipCreateElement) return false;
|
|
1403
|
-
return isCreateElementCall(node, context);
|
|
1404
|
-
}).with({
|
|
1405
|
-
type: ast.NodeType.Identifier
|
|
1406
|
-
}, (node)=>{
|
|
1407
|
-
const { name } = node;
|
|
1408
|
-
if (name === "undefined") return !(hint & JSXValueHint.SkipUndefinedLiteral);
|
|
1409
|
-
if (ast.isJSXTagNameExpression(node)) return true;
|
|
1410
|
-
const initialScope = context.sourceCode.getScope?.(node) ?? context.getScope();
|
|
1411
|
-
const maybeVariable = _var.findVariable(name, initialScope);
|
|
1412
|
-
return pipe(maybeVariable, flatMap(_var.getVariableInit(0)), exists((n)=>isJSXValue(n, context, hint)));
|
|
1413
|
-
}).otherwise(constFalse);
|
|
1414
|
-
}
|
|
1415
|
-
|
|
1416
|
-
/**
|
|
1417
|
-
* Check if function is returning JSX
|
|
1418
|
-
* @param node The return statement node to check
|
|
1419
|
-
* @param context The rule context
|
|
1420
|
-
* @param hint The `JSXValueHint` to use
|
|
1421
|
-
* @returns boolean
|
|
1422
|
-
*/ function isFunctionReturningJSXValue(node, context, hint = DEFAULT_JSX_VALUE_HINT) {
|
|
1423
|
-
if (node.body.type !== ast.NodeType.BlockStatement) {
|
|
1424
|
-
return isJSXValue(node.body, context, hint);
|
|
1425
|
-
}
|
|
1426
|
-
const statements = ast.getNestedReturnStatements(node.body);
|
|
1427
|
-
return statements.some((statement)=>isJSXValue(statement.argument, context, hint));
|
|
1428
|
-
}
|
|
1429
|
-
|
|
1430
|
-
const { getStaticValue } = ast.ESLintCommunityESLintUtils;
|
|
1431
|
-
/**
|
|
1432
|
-
* Get the name of a JSX attribute with namespace
|
|
1433
|
-
* @param node The JSX attribute node
|
|
1434
|
-
* @returns string
|
|
1435
|
-
*/ function getPropName(node) {
|
|
1436
|
-
return N(node.name).when(ast.is(ast.NodeType.JSXIdentifier), (n)=>n.name).when(ast.is(ast.NodeType.JSXNamespacedName), (n)=>`${n.namespace.name}:${n.name.name}`).exhaustive();
|
|
1437
|
-
}
|
|
1438
|
-
function getProp(props, propName, context, initialScope) {
|
|
1439
|
-
return findPropInAttributes(props, context, initialScope)(propName);
|
|
1440
|
-
}
|
|
1441
|
-
/**
|
|
1442
|
-
* Gets and resolves the static value of a JSX attribute
|
|
1443
|
-
* @param attribute The JSX attribute to get the value of
|
|
1444
|
-
* @param context The rule context
|
|
1445
|
-
* @returns The static value of the given JSX attribute
|
|
1446
|
-
*/ function getPropValue(attribute, context) {
|
|
1447
|
-
const initialScope = context.sourceCode.getScope?.(attribute) ?? context.getScope();
|
|
1448
|
-
if (attribute.type === ast.NodeType.JSXAttribute && "value" in attribute) {
|
|
1449
|
-
const { value } = attribute;
|
|
1450
|
-
if (value === null) return none();
|
|
1451
|
-
if (value.type === ast.NodeType.Literal) return some(getStaticValue(value, initialScope));
|
|
1452
|
-
if (value.type === ast.NodeType.JSXExpressionContainer) return some(getStaticValue(value.expression, initialScope));
|
|
1453
|
-
return none();
|
|
1454
|
-
}
|
|
1455
|
-
const { argument } = attribute;
|
|
1456
|
-
return some(getStaticValue(argument, initialScope));
|
|
1457
|
-
}
|
|
1458
|
-
/**
|
|
1459
|
-
* @param properties The properties to search in
|
|
1460
|
-
* @param context The rule context
|
|
1461
|
-
* @param initialScope
|
|
1462
|
-
* @param seenProps The properties that have already been seen
|
|
1463
|
-
* @returns A function that searches for a property in the given properties
|
|
1464
|
-
*/ function findPropInProperties(properties, context, initialScope, seenProps = []) {
|
|
1465
|
-
/**
|
|
1466
|
-
* Search for a property in the given properties
|
|
1467
|
-
* @param propName The name of the property to search for
|
|
1468
|
-
* @returns The property if found
|
|
1469
|
-
*/ return (propName)=>{
|
|
1470
|
-
return fromNullable(properties.find((prop)=>{
|
|
1471
|
-
return N(prop).when(ast.is(ast.NodeType.Property), (prop)=>{
|
|
1472
|
-
return "name" in prop.key && prop.key.name === propName;
|
|
1473
|
-
}).when(ast.is(ast.NodeType.SpreadElement), (prop)=>{
|
|
1474
|
-
return N(prop.argument).when(ast.is(ast.NodeType.Identifier), (argument)=>{
|
|
1475
|
-
const { name } = argument;
|
|
1476
|
-
const maybeInit = flatMap(_var.findVariable(name, initialScope), _var.getVariableInit(0));
|
|
1477
|
-
if (isNone(maybeInit)) return false;
|
|
1478
|
-
const init = maybeInit.value;
|
|
1479
|
-
if (init.type !== ast.NodeType.ObjectExpression) return false;
|
|
1480
|
-
if (seenProps.includes(name)) return false;
|
|
1481
|
-
return isSome(findPropInProperties(init.properties, context, initialScope, [
|
|
1482
|
-
...seenProps,
|
|
1483
|
-
name
|
|
1484
|
-
])(propName));
|
|
1485
|
-
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
|
|
1486
|
-
return isSome(findPropInProperties(argument.properties, context, initialScope, seenProps)(propName));
|
|
1487
|
-
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{
|
|
1488
|
-
// Not implemented
|
|
1489
|
-
}).when(ast.is(ast.NodeType.CallExpression), ()=>{
|
|
1490
|
-
// Not implemented
|
|
1491
|
-
}).otherwise(constFalse);
|
|
1492
|
-
}).when(ast.is(ast.NodeType.RestElement), ()=>{
|
|
1493
|
-
// Not implemented
|
|
1494
|
-
return false;
|
|
1495
|
-
}).otherwise(constFalse);
|
|
1496
|
-
}));
|
|
1497
|
-
};
|
|
1498
|
-
}
|
|
1499
|
-
/**
|
|
1500
|
-
* @param attributes The attributes to search in
|
|
1501
|
-
* @param context The rule context
|
|
1502
|
-
* @param initialScope
|
|
1503
|
-
* @returns A function that searches for a property in the given attributes
|
|
1504
|
-
*/ function findPropInAttributes(attributes, context, initialScope) {
|
|
1505
|
-
/**
|
|
1506
|
-
* Search for a property in the given attributes
|
|
1507
|
-
* @param propName The name of the property to search for
|
|
1508
|
-
* @returns The property if found
|
|
1509
|
-
*/ return (propName)=>{
|
|
1510
|
-
return fromNullable(attributes.find((attr)=>{
|
|
1511
|
-
return N(attr).when(ast.is(ast.NodeType.JSXAttribute), (attr)=>getPropName(attr) === propName).when(ast.is(ast.NodeType.JSXSpreadAttribute), (attr)=>{
|
|
1512
|
-
return N(attr.argument).with({
|
|
1513
|
-
type: ast.NodeType.Identifier
|
|
1514
|
-
}, (argument)=>{
|
|
1515
|
-
const { name } = argument;
|
|
1516
|
-
const maybeInit = flatMap(_var.findVariable(name, initialScope), _var.getVariableInit(0));
|
|
1517
|
-
if (isNone(maybeInit)) return false;
|
|
1518
|
-
const init = maybeInit.value;
|
|
1519
|
-
if (!("properties" in init)) return false;
|
|
1520
|
-
return isSome(findPropInProperties(init.properties, context, initialScope)(propName));
|
|
1521
|
-
}).when(ast.is(ast.NodeType.ObjectExpression), (argument)=>{
|
|
1522
|
-
return isSome(findPropInProperties(argument.properties, context, initialScope)(propName));
|
|
1523
|
-
}).when(ast.is(ast.NodeType.MemberExpression), ()=>{
|
|
1524
|
-
// Not implemented
|
|
1525
|
-
return false;
|
|
1526
|
-
}).when(ast.is(ast.NodeType.CallExpression), ()=>{
|
|
1527
|
-
// Not implemented
|
|
1528
|
-
return false;
|
|
1529
|
-
}).otherwise(constFalse);
|
|
1530
|
-
}).otherwise(constFalse);
|
|
1531
|
-
}));
|
|
1532
|
-
};
|
|
1533
|
-
}
|
|
1534
|
-
|
|
1535
|
-
/**
|
|
1536
|
-
* Check if the given prop name is present in the given attributes
|
|
1537
|
-
* @param attributes The attributes to search in
|
|
1538
|
-
* @param propName The prop name to search for
|
|
1539
|
-
* @param context The rule context
|
|
1540
|
-
* @param initialScope
|
|
1541
|
-
* @returns `true` if the given prop name is present in the given properties
|
|
1542
|
-
*/ function hasProp(attributes, propName, context, initialScope) {
|
|
1543
|
-
return isSome(findPropInAttributes(attributes, context, initialScope)(propName));
|
|
1544
|
-
}
|
|
1545
|
-
/**
|
|
1546
|
-
* Check if any of the given prop names are present in the given attributes
|
|
1547
|
-
* @param attributes The attributes to search in
|
|
1548
|
-
* @param propNames The prop names to search for
|
|
1549
|
-
* @param context The rule context
|
|
1550
|
-
* @param initialScope
|
|
1551
|
-
* @returns `true` if any of the given prop names are present in the given attributes
|
|
1552
|
-
*/ function hasAnyProp(attributes, propNames, context, initialScope) {
|
|
1553
|
-
return propNames.some((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
1554
|
-
}
|
|
1555
|
-
/**
|
|
1556
|
-
* Check if all of the given prop names are present in the given attributes
|
|
1557
|
-
* @param attributes The attributes to search in
|
|
1558
|
-
* @param propNames The prop names to search for
|
|
1559
|
-
* @param context The rule context
|
|
1560
|
-
* @param initialScope
|
|
1561
|
-
* @returns `true` if all of the given prop names are present in the given attributes
|
|
1562
|
-
*/ function hasEveryProp(attributes, propNames, context, initialScope) {
|
|
1563
|
-
return propNames.every((propName)=>hasProp(attributes, propName, context, initialScope));
|
|
1564
|
-
}
|
|
1565
|
-
|
|
1566
|
-
/**
|
|
1567
|
-
* Traverses up prop node
|
|
1568
|
-
* @param node The AST node to start traversing from
|
|
1569
|
-
* @param predicate The predicate to check each node
|
|
1570
|
-
* @returns prop node if found
|
|
1571
|
-
*/ function traverseUpProp(node, predicate = constTrue) {
|
|
1572
|
-
const guard = (node)=>{
|
|
1573
|
-
return node.type === ast.NodeType.JSXAttribute && predicate(node);
|
|
1574
|
-
};
|
|
1575
|
-
return ast.traverseUpGuard(node, guard);
|
|
1576
|
-
}
|
|
1577
|
-
|
|
1578
|
-
/**
|
|
1579
|
-
* Checks if the node is inside a prop's value
|
|
1580
|
-
* @param node The AST node to check
|
|
1581
|
-
* @returns `true` if the node is inside a prop's value
|
|
1582
|
-
*/ function isInsidePropValue(node) {
|
|
1583
|
-
if (ast.isStringLiteral(node)) return node.parent.type === ast.NodeType.JSXAttribute;
|
|
1584
|
-
return isSome(traverseUpProp(node, (n)=>n.value?.type === ast.NodeType.JSXExpressionContainer));
|
|
1585
|
-
}
|
|
1586
|
-
|
|
1587
|
-
/**
|
|
1588
|
-
* Check if a node is a Literal or JSXText
|
|
1589
|
-
* @param node The AST node to check
|
|
1590
|
-
* @returns boolean `true` if the node is a Literal or JSXText
|
|
1591
|
-
*/ const isLiteral = ast.isOneOf([
|
|
1592
|
-
ast.NodeType.Literal,
|
|
1593
|
-
ast.NodeType.JSXText
|
|
1594
|
-
]);
|
|
1595
|
-
/**
|
|
1596
|
-
* Check if a Literal or JSXText node is whitespace
|
|
1597
|
-
* @param node The AST node to check
|
|
1598
|
-
* @returns boolean `true` if the node is whitespace
|
|
1599
|
-
*/ function isWhiteSpace(node) {
|
|
1600
|
-
return isString(node.value) && node.value.trim() === "";
|
|
1601
|
-
}
|
|
1602
|
-
/**
|
|
1603
|
-
* Check if a Literal or JSXText node is a line break
|
|
1604
|
-
* @param node The AST node to check
|
|
1605
|
-
* @returns boolean
|
|
1606
|
-
*/ function isLineBreak(node) {
|
|
1607
|
-
return isLiteral(node) && isWhiteSpace(node) && ast.isMultiLine(node);
|
|
1608
|
-
}
|
|
1609
|
-
/**
|
|
1610
|
-
* Check if a Literal or JSXText node is padding spaces
|
|
1611
|
-
* @param node The AST node to check
|
|
1612
|
-
* @returns boolean
|
|
1613
|
-
*/ function isPaddingSpaces(node) {
|
|
1614
|
-
return isLiteral(node) && isWhiteSpace(node) && node.raw.includes("\n");
|
|
1615
|
-
}
|
|
1616
|
-
|
|
1617
|
-
exports.DEFAULT_JSX_VALUE_HINT = DEFAULT_JSX_VALUE_HINT;
|
|
1618
|
-
exports.JSXValueHint = JSXValueHint;
|
|
1619
|
-
exports.elementType = elementType;
|
|
1620
|
-
exports.findPropInAttributes = findPropInAttributes;
|
|
1621
|
-
exports.findPropInProperties = findPropInProperties;
|
|
1622
|
-
exports.getFragmentFromContext = getFragmentFromContext;
|
|
1623
|
-
exports.getPragmaFromContext = getPragmaFromContext;
|
|
1624
|
-
exports.getProp = getProp;
|
|
1625
|
-
exports.getPropName = getPropName;
|
|
1626
|
-
exports.getPropValue = getPropValue;
|
|
1627
|
-
exports.hasAnyProp = hasAnyProp;
|
|
1628
|
-
exports.hasChildren = hasChildren;
|
|
1629
|
-
exports.hasEveryProp = hasEveryProp;
|
|
1630
|
-
exports.hasProp = hasProp;
|
|
1631
|
-
exports.isCallFromPragma = isCallFromPragma;
|
|
1632
|
-
exports.isCallFromPragmaMember = isCallFromPragmaMember;
|
|
1633
|
-
exports.isChildOfJSXElement = isChildOfJSXElement;
|
|
1634
|
-
exports.isChildrenOfCreateElement = isChildrenOfCreateElement;
|
|
1635
|
-
exports.isCloneElement = isCloneElement;
|
|
1636
|
-
exports.isCloneElementCall = isCloneElementCall;
|
|
1637
|
-
exports.isCreateElement = isCreateElement;
|
|
1638
|
-
exports.isCreateElementCall = isCreateElementCall;
|
|
1639
|
-
exports.isFragment = isFragment;
|
|
1640
|
-
exports.isFragmentElement = isFragmentElement;
|
|
1641
|
-
exports.isFragmentSyntax = isFragmentSyntax;
|
|
1642
|
-
exports.isFromPragma = isFromPragma;
|
|
1643
|
-
exports.isFromPragmaMember = isFromPragmaMember;
|
|
1644
|
-
exports.isFunctionReturningJSXValue = isFunctionReturningJSXValue;
|
|
1645
|
-
exports.isInitializedFromPragma = isInitializedFromPragma;
|
|
1646
|
-
exports.isInsideCreateElementProps = isInsideCreateElementProps;
|
|
1647
|
-
exports.isInsidePropValue = isInsidePropValue;
|
|
1648
|
-
exports.isJSXElementOfBuiltinComponent = isJSXElementOfBuiltinComponent;
|
|
1649
|
-
exports.isJSXElementOfUserDefinedComponent = isJSXElementOfUserDefinedComponent;
|
|
1650
|
-
exports.isJSXValue = isJSXValue;
|
|
1651
|
-
exports.isLineBreak = isLineBreak;
|
|
1652
|
-
exports.isLiteral = isLiteral;
|
|
1653
|
-
exports.isPaddingSpaces = isPaddingSpaces;
|
|
1654
|
-
exports.isPropertyOfPragma = isPropertyOfPragma;
|
|
1655
|
-
exports.isWhiteSpace = isWhiteSpace;
|
|
1656
|
-
exports.traverseUpProp = traverseUpProp;
|