@eslint-react/ast 1.48.1 → 1.48.2-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,61 +1,28 @@
1
1
  'use strict';
2
2
 
3
- var eff = require('@eslint-react/eff');
4
3
  var types = require('@typescript-eslint/types');
4
+ var eff = require('@eslint-react/eff');
5
5
  var utils = require('@typescript-eslint/utils');
6
6
  var typescriptEstree = require('@typescript-eslint/typescript-estree');
7
7
  var stringTs = require('string-ts');
8
8
 
9
- // src/find-parent-node.ts
10
- function findParentNode(node, test) {
11
- if (node == null) return eff._;
12
- let parent = node.parent;
13
- while (parent != null && parent.type !== types.AST_NODE_TYPES.Program) {
14
- if (test(parent)) {
15
- return parent;
16
- }
17
- parent = parent.parent;
18
- }
19
- return eff._;
20
- }
21
- function getFunctionInitPath(node) {
22
- if (node.type === types.AST_NODE_TYPES.FunctionDeclaration) {
23
- return [node];
24
- }
25
- const { parent } = node;
26
- switch (true) {
27
- case parent.type === types.AST_NODE_TYPES.VariableDeclarator:
28
- return [parent.parent, parent, node];
29
- case (parent.type === types.AST_NODE_TYPES.CallExpression && parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator):
30
- return [parent.parent.parent, parent.parent, parent, node];
31
- case (parent.type === types.AST_NODE_TYPES.CallExpression && parent.parent.type === types.AST_NODE_TYPES.CallExpression && parent.parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator):
32
- return [parent.parent.parent.parent, parent.parent.parent, parent.parent, parent, node];
33
- case (parent.type === types.AST_NODE_TYPES.Property && parent.parent.type === types.AST_NODE_TYPES.ObjectExpression && parent.parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator):
34
- return [parent.parent.parent.parent, parent.parent.parent, parent.parent, parent, node];
35
- case (parent.type === types.AST_NODE_TYPES.MethodDefinition && parent.parent.parent.type === types.AST_NODE_TYPES.ClassDeclaration):
36
- return [parent.parent.parent, parent.parent, parent, node];
37
- case (parent.type === types.AST_NODE_TYPES.PropertyDefinition && parent.parent.parent.type === types.AST_NODE_TYPES.ClassDeclaration):
38
- return [parent.parent.parent, parent.parent, parent, node];
39
- }
40
- return eff._;
9
+ // src/ast-array-method.ts
10
+ function isArrayFromCall(node, loose = true) {
11
+ if (node.type !== types.AST_NODE_TYPES.CallExpression) return false;
12
+ if (node.callee.type !== types.AST_NODE_TYPES.MemberExpression) return false;
13
+ if (node.callee.property.type !== types.AST_NODE_TYPES.Identifier) return false;
14
+ const name = node.callee.property.name;
15
+ return name === "from" || loose && name.startsWith("from");
41
16
  }
42
- function hasCallInFunctionInitPath(callName, initPath) {
43
- return initPath.some((n) => {
44
- if (n.type !== types.AST_NODE_TYPES.CallExpression) {
45
- return false;
46
- }
47
- switch (n.callee.type) {
48
- case types.AST_NODE_TYPES.Identifier:
49
- return n.callee.name === callName;
50
- case types.AST_NODE_TYPES.MemberExpression:
51
- return "name" in n.callee.property && n.callee.property.name === callName;
52
- default:
53
- return false;
54
- }
55
- });
17
+ function isArrayMapCall(node, loose = true) {
18
+ if (node.type !== types.AST_NODE_TYPES.CallExpression) return false;
19
+ if (node.callee.type !== types.AST_NODE_TYPES.MemberExpression) return false;
20
+ if (node.callee.property.type !== types.AST_NODE_TYPES.Identifier) return false;
21
+ const name = node.callee.property.name;
22
+ return name === "map" || loose && name.endsWith("Map");
56
23
  }
57
24
 
58
- // src/get-array-method-callback-index-param-position.ts
25
+ // src/ast-array-method-callback.ts
59
26
  var indexParamPosition = /* @__PURE__ */ new Map([
60
27
  ["every", 1],
61
28
  ["filter", 1],
@@ -224,54 +191,18 @@ var isTypeAssertionExpression = isOneOf([
224
191
  types.AST_NODE_TYPES.TSSatisfiesExpression
225
192
  ]);
226
193
 
227
- // src/get-ecma-expression.ts
228
- function getEcmaExpression(node) {
194
+ // src/ast-expression.ts
195
+ function getJSExpression(node) {
229
196
  if (isTypeExpression(node)) {
230
- return getEcmaExpression(node.expression);
197
+ return getJSExpression(node.expression);
231
198
  }
232
199
  return node;
233
200
  }
234
- function getFunctionIdentifier(node) {
235
- switch (true) {
236
- // function MaybeComponent() {}
237
- case ("id" in node && node.id != null):
238
- return node.id;
239
- // const whatever = function MaybeComponent() {};
240
- case (node.parent.type === types.AST_NODE_TYPES.VariableDeclarator && node.parent.init === node && node.parent.id.type === types.AST_NODE_TYPES.Identifier):
241
- return node.parent.id;
242
- // MaybeComponent = () => {};
243
- case (node.parent.type === types.AST_NODE_TYPES.AssignmentExpression && node.parent.right === node && node.parent.operator === "=" && node.parent.left.type === types.AST_NODE_TYPES.Identifier):
244
- return node.parent.left;
245
- // {MaybeComponent: () => {}}
246
- // {MaybeComponent() {}}
247
- case (node.parent.type === types.AST_NODE_TYPES.Property && node.parent.value === node && !node.parent.computed && node.parent.key.type === types.AST_NODE_TYPES.Identifier):
248
- return node.parent.key;
249
- // class {MaybeComponent = () => {}}
250
- // class {MaybeComponent() {}}
251
- case (isMethodOrProperty(node.parent) && node.parent.value === node && node.parent.key.type === types.AST_NODE_TYPES.Identifier):
252
- return node.parent.key;
253
- // Follow spec convention for `IsAnonymousFunctionDefinition()` usage.
254
- //
255
- // const {MaybeComponent = () => {}} = {};
256
- // ({MaybeComponent = () => {}} = {});
257
- case (node.parent.type === types.AST_NODE_TYPES.AssignmentPattern && node.parent.right === node && node.parent.left.type === types.AST_NODE_TYPES.Identifier):
258
- return node.parent.left;
259
- // const MaybeComponent = (() => {})!;
260
- // const MaybeComponent = (() => {}) as FunctionComponent;
261
- // const MaybeComponent = (() => {}) satisfies FunctionComponent;
262
- case isTypeAssertionExpression(node.parent):
263
- return getFunctionIdentifier(node.parent);
264
- }
265
- return eff._;
266
- }
267
-
268
- // src/get-literal-value-type.ts
269
- function getLiteralValueType(input) {
270
- if (input === null) return "null";
271
- return typeof input;
201
+ function isThisExpression(node) {
202
+ return getJSExpression(node).type === types.AST_NODE_TYPES.ThisExpression;
272
203
  }
273
204
  function getNestedExpressionsOfType(type) {
274
- const isNodeOfType = utils.ASTUtils.isNodeOfType(type);
205
+ const isNodeOfType = is(type);
275
206
  return function(node) {
276
207
  const boundGetNestedExpressionsOfType = getNestedExpressionsOfType(type);
277
208
  const expressions = [];
@@ -318,43 +249,126 @@ function getNestedExpressionsOfType(type) {
318
249
  const chunk = node.expressions.map(boundGetNestedExpressionsOfType).flat(1);
319
250
  expressions.push(...chunk);
320
251
  }
321
- if (node.type === typescriptEstree.AST_NODE_TYPES.Property) {
252
+ if (node.type === types.AST_NODE_TYPES.Property) {
322
253
  const chunk = boundGetNestedExpressionsOfType(node.value);
323
254
  expressions.push(...chunk);
324
255
  }
325
- if (node.type === typescriptEstree.AST_NODE_TYPES.SpreadElement) {
256
+ if (node.type === types.AST_NODE_TYPES.SpreadElement) {
326
257
  const chunk = boundGetNestedExpressionsOfType(node.argument);
327
258
  expressions.push(...chunk);
328
259
  }
329
- if (node.type === typescriptEstree.AST_NODE_TYPES.MemberExpression) {
260
+ if (node.type === types.AST_NODE_TYPES.MemberExpression) {
330
261
  const chunk = boundGetNestedExpressionsOfType(node.object);
331
262
  expressions.push(...chunk);
332
263
  }
333
- if (node.type === typescriptEstree.AST_NODE_TYPES.UnaryExpression) {
264
+ if (node.type === types.AST_NODE_TYPES.UnaryExpression) {
334
265
  const chunk = boundGetNestedExpressionsOfType(node.argument);
335
266
  expressions.push(...chunk);
336
267
  }
337
- if (node.type === typescriptEstree.AST_NODE_TYPES.ChainExpression) {
268
+ if (node.type === types.AST_NODE_TYPES.ChainExpression) {
338
269
  const chunk = boundGetNestedExpressionsOfType(node.expression);
339
270
  expressions.push(...chunk);
340
271
  }
341
- if (node.type === typescriptEstree.AST_NODE_TYPES.TSNonNullExpression) {
272
+ if (node.type === types.AST_NODE_TYPES.TSNonNullExpression) {
342
273
  const chunk = boundGetNestedExpressionsOfType(node.expression);
343
274
  expressions.push(...chunk);
344
275
  }
345
- if (node.type === typescriptEstree.AST_NODE_TYPES.TSAsExpression) {
276
+ if (node.type === types.AST_NODE_TYPES.TSAsExpression) {
346
277
  const chunk = boundGetNestedExpressionsOfType(node.expression);
347
278
  expressions.push(...chunk);
348
279
  }
349
- if (node.type === typescriptEstree.AST_NODE_TYPES.TSSatisfiesExpression) {
280
+ if (node.type === types.AST_NODE_TYPES.TSSatisfiesExpression) {
350
281
  const chunk = boundGetNestedExpressionsOfType(node.expression);
351
282
  expressions.push(...chunk);
352
283
  }
353
284
  return expressions;
354
285
  };
355
286
  }
356
- var getNestedNewExpressions = getNestedExpressionsOfType(typescriptEstree.AST_NODE_TYPES.NewExpression);
357
- var getNestedCallExpressions = getNestedExpressionsOfType(typescriptEstree.AST_NODE_TYPES.CallExpression);
287
+ var getNestedNewExpressions = getNestedExpressionsOfType(types.AST_NODE_TYPES.NewExpression);
288
+ var getNestedCallExpressions = getNestedExpressionsOfType(types.AST_NODE_TYPES.CallExpression);
289
+ function getFunctionIdentifier(node) {
290
+ switch (true) {
291
+ // function MaybeComponent() {}
292
+ case ("id" in node && node.id != null):
293
+ return node.id;
294
+ // const whatever = function MaybeComponent() {};
295
+ case (node.parent.type === types.AST_NODE_TYPES.VariableDeclarator && node.parent.init === node && node.parent.id.type === types.AST_NODE_TYPES.Identifier):
296
+ return node.parent.id;
297
+ // MaybeComponent = () => {};
298
+ case (node.parent.type === types.AST_NODE_TYPES.AssignmentExpression && node.parent.right === node && node.parent.operator === "=" && node.parent.left.type === types.AST_NODE_TYPES.Identifier):
299
+ return node.parent.left;
300
+ // {MaybeComponent: () => {}}
301
+ // {MaybeComponent() {}}
302
+ case (node.parent.type === types.AST_NODE_TYPES.Property && node.parent.value === node && !node.parent.computed && node.parent.key.type === types.AST_NODE_TYPES.Identifier):
303
+ return node.parent.key;
304
+ // class {MaybeComponent = () => {}}
305
+ // class {MaybeComponent() {}}
306
+ case (isMethodOrProperty(node.parent) && node.parent.value === node && node.parent.key.type === types.AST_NODE_TYPES.Identifier):
307
+ return node.parent.key;
308
+ // Follow spec convention for `IsAnonymousFunctionDefinition()` usage.
309
+ //
310
+ // const {MaybeComponent = () => {}} = {};
311
+ // ({MaybeComponent = () => {}} = {});
312
+ case (node.parent.type === types.AST_NODE_TYPES.AssignmentPattern && node.parent.right === node && node.parent.left.type === types.AST_NODE_TYPES.Identifier):
313
+ return node.parent.left;
314
+ // const MaybeComponent = (() => {})!;
315
+ // const MaybeComponent = (() => {}) as FunctionComponent;
316
+ // const MaybeComponent = (() => {}) satisfies FunctionComponent;
317
+ case isTypeAssertionExpression(node.parent):
318
+ return getFunctionIdentifier(node.parent);
319
+ }
320
+ return eff._;
321
+ }
322
+ function getFunctionInitPath(node) {
323
+ if (node.type === types.AST_NODE_TYPES.FunctionDeclaration) {
324
+ return [node];
325
+ }
326
+ const { parent } = node;
327
+ switch (true) {
328
+ case parent.type === types.AST_NODE_TYPES.VariableDeclarator:
329
+ return [parent.parent, parent, node];
330
+ case (parent.type === types.AST_NODE_TYPES.CallExpression && parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator):
331
+ return [parent.parent.parent, parent.parent, parent, node];
332
+ case (parent.type === types.AST_NODE_TYPES.CallExpression && parent.parent.type === types.AST_NODE_TYPES.CallExpression && parent.parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator):
333
+ return [parent.parent.parent.parent, parent.parent.parent, parent.parent, parent, node];
334
+ case (parent.type === types.AST_NODE_TYPES.Property && parent.parent.type === types.AST_NODE_TYPES.ObjectExpression && parent.parent.parent.type === types.AST_NODE_TYPES.VariableDeclarator):
335
+ return [parent.parent.parent.parent, parent.parent.parent, parent.parent, parent, node];
336
+ case (parent.type === types.AST_NODE_TYPES.MethodDefinition && parent.parent.parent.type === types.AST_NODE_TYPES.ClassDeclaration):
337
+ return [parent.parent.parent, parent.parent, parent, node];
338
+ case (parent.type === types.AST_NODE_TYPES.PropertyDefinition && parent.parent.parent.type === types.AST_NODE_TYPES.ClassDeclaration):
339
+ return [parent.parent.parent, parent.parent, parent, node];
340
+ }
341
+ return eff._;
342
+ }
343
+ function hasCallInFunctionInitPath(callName, initPath) {
344
+ return initPath.some((n) => {
345
+ if (n.type !== types.AST_NODE_TYPES.CallExpression) {
346
+ return false;
347
+ }
348
+ switch (n.callee.type) {
349
+ case types.AST_NODE_TYPES.Identifier:
350
+ return n.callee.name === callName;
351
+ case types.AST_NODE_TYPES.MemberExpression:
352
+ return "name" in n.callee.property && n.callee.property.name === callName;
353
+ default:
354
+ return false;
355
+ }
356
+ });
357
+ }
358
+ function isEmptyFunction(node) {
359
+ return node.body.type === types.AST_NODE_TYPES.BlockStatement && node.body.body.length === 0;
360
+ }
361
+ function findParentNode(node, test) {
362
+ if (node == null) return eff._;
363
+ let parent = node.parent;
364
+ while (parent != null && parent.type !== types.AST_NODE_TYPES.Program) {
365
+ if (test(parent)) {
366
+ return parent;
367
+ }
368
+ parent = parent.parent;
369
+ }
370
+ return eff._;
371
+ }
358
372
  function getNestedIdentifiers(node) {
359
373
  const identifiers = [];
360
374
  if (node.type === types.AST_NODE_TYPES.Identifier) {
@@ -418,62 +432,9 @@ function getNestedIdentifiers(node) {
418
432
  }
419
433
  return identifiers;
420
434
  }
421
- function getNestedReturnStatements(node) {
422
- const statements = [];
423
- const boundaryNode = isFunction(node) ? node : findParentNode(node, isFunction);
424
- typescriptEstree.simpleTraverse(node, {
425
- enter(node2) {
426
- if (node2.type !== types.AST_NODE_TYPES.ReturnStatement) {
427
- return;
428
- }
429
- const parentFunction = findParentNode(node2, isFunction);
430
- if (parentFunction !== boundaryNode) {
431
- return;
432
- }
433
- statements.push(node2);
434
- }
435
- });
436
- return statements;
437
- }
438
- function getPropertyName(node) {
439
- if (isTypeExpression(node)) {
440
- return getPropertyName(getEcmaExpression(node));
441
- }
442
- if (node.type === types.AST_NODE_TYPES.Identifier || node.type === types.AST_NODE_TYPES.PrivateIdentifier) {
443
- return node.name;
444
- }
445
- if (node.type === types.AST_NODE_TYPES.Literal) {
446
- return String(node.value);
447
- }
448
- if (node.type === types.AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
449
- return node.quasis[0]?.value.raw;
450
- }
451
- return eff._;
452
- }
453
- function isArrayFromCall(node, loose = true) {
454
- if (node.type !== types.AST_NODE_TYPES.CallExpression) return false;
455
- if (node.callee.type !== types.AST_NODE_TYPES.MemberExpression) return false;
456
- if (node.callee.property.type !== types.AST_NODE_TYPES.Identifier) return false;
457
- const name = node.callee.property.name;
458
- return name === "from" || loose && name.startsWith("from");
459
- }
460
- function isArrayMapCall(node, loose = true) {
461
- if (node.type !== types.AST_NODE_TYPES.CallExpression) return false;
462
- if (node.callee.type !== types.AST_NODE_TYPES.MemberExpression) return false;
463
- if (node.callee.property.type !== types.AST_NODE_TYPES.Identifier) return false;
464
- const name = node.callee.property.name;
465
- return name === "map" || loose && name.endsWith("Map");
466
- }
467
- function isEmptyFunction(node) {
468
- return node.body.type === types.AST_NODE_TYPES.BlockStatement && node.body.body.length === 0;
469
- }
470
-
471
- // src/is-multi-line.ts
472
435
  function isMultiLine(node) {
473
436
  return node.loc.start.line !== node.loc.end.line;
474
437
  }
475
-
476
- // src/is-line-break.ts
477
438
  function isLineBreak(node) {
478
439
  return isOneOf([types.AST_NODE_TYPES.Literal, types.AST_NODE_TYPES.JSXText])(node) && typeof node.value === "string" && node.value.trim() === "" && isMultiLine(node);
479
440
  }
@@ -559,20 +520,65 @@ function isProcessEnvNodeEnvCompare(node, operator, value) {
559
520
  }
560
521
  return false;
561
522
  }
562
- function isThisExpression(node) {
563
- return getEcmaExpression(node).type === types.AST_NODE_TYPES.ThisExpression;
523
+ function getPropertyName(node) {
524
+ if (isTypeExpression(node)) {
525
+ return getPropertyName(getJSExpression(node));
526
+ }
527
+ if (node.type === types.AST_NODE_TYPES.Identifier || node.type === types.AST_NODE_TYPES.PrivateIdentifier) {
528
+ return node.name;
529
+ }
530
+ if (node.type === types.AST_NODE_TYPES.Literal) {
531
+ return String(node.value);
532
+ }
533
+ if (node.type === types.AST_NODE_TYPES.TemplateLiteral && node.expressions.length === 0) {
534
+ return node.quasis[0]?.value.raw;
535
+ }
536
+ return eff._;
537
+ }
538
+ function getNestedReturnStatements(node) {
539
+ const statements = [];
540
+ const boundaryNode = isFunction(node) ? node : findParentNode(node, isFunction);
541
+ typescriptEstree.simpleTraverse(node, {
542
+ enter(node2) {
543
+ if (node2.type !== types.AST_NODE_TYPES.ReturnStatement) {
544
+ return;
545
+ }
546
+ const parentFunction = findParentNode(node2, isFunction);
547
+ if (parentFunction !== boundaryNode) {
548
+ return;
549
+ }
550
+ statements.push(node2);
551
+ }
552
+ });
553
+ return statements;
554
+ }
555
+ function getLiteralValueType(input) {
556
+ if (input === null) return "null";
557
+ return typeof input;
558
+ }
559
+ function toReadableNodeType(node) {
560
+ if (node.type === types.AST_NODE_TYPES.Literal) {
561
+ if ("regex" in node) {
562
+ return "RegExp literal";
563
+ }
564
+ return `${getLiteralValueType(node.value)} literal`;
565
+ }
566
+ if (isJSX(node)) {
567
+ return `JSX ${stringTs.toLowerCase(stringTs.delimiterCase(stringTs.replace(node.type, "JSX", ""), " "))}`;
568
+ }
569
+ return stringTs.toLowerCase(stringTs.delimiterCase(node.type, " "));
564
570
  }
565
- function stringify(node, getText) {
571
+ function toString(node, getText) {
566
572
  switch (node.type) {
567
573
  case types.AST_NODE_TYPES.CallExpression:
568
- return stringify(node.callee, getText);
574
+ return toString(node.callee, getText);
569
575
  case types.AST_NODE_TYPES.Identifier:
570
576
  case types.AST_NODE_TYPES.PrivateIdentifier:
571
577
  return node.name;
572
578
  case types.AST_NODE_TYPES.JSXIdentifier:
573
579
  return `<${node.name}>`;
574
580
  case types.AST_NODE_TYPES.JSXMemberExpression:
575
- return `${stringify(node.object, getText)}.${stringify(node.property, getText)}`;
581
+ return `${toString(node.object, getText)}.${toString(node.property, getText)}`;
576
582
  case types.AST_NODE_TYPES.JSXNamespacedName:
577
583
  return `${node.namespace.name}:${node.name.name}`;
578
584
  case types.AST_NODE_TYPES.JSXText:
@@ -580,31 +586,18 @@ function stringify(node, getText) {
580
586
  case types.AST_NODE_TYPES.Literal:
581
587
  return node.raw;
582
588
  case types.AST_NODE_TYPES.MemberExpression:
583
- return `${stringify(node.object, getText)}.${stringify(node.property, getText)}`;
589
+ return `${toString(node.object, getText)}.${toString(node.property, getText)}`;
584
590
  default:
585
591
  return getText(node);
586
592
  }
587
593
  }
588
- function toReadableNodeType(node) {
589
- if (node.type === types.AST_NODE_TYPES.Literal) {
590
- if ("regex" in node) {
591
- return "RegExp literal";
592
- }
593
- return `${getLiteralValueType(node.value)} literal`;
594
- }
595
- if (isJSX(node)) {
596
- return `JSX ${stringTs.toLowerCase(stringTs.delimiterCase(stringTs.replace(node.type, "JSX", ""), " "))}`;
597
- }
598
- return stringTs.toLowerCase(stringTs.delimiterCase(node.type, " "));
599
- }
600
594
 
601
595
  exports.findParentNode = findParentNode;
602
596
  exports.getArrayMethodCallbackIndexParamPosition = getArrayMethodCallbackIndexParamPosition;
603
597
  exports.getClassIdentifier = getClassIdentifier;
604
- exports.getEcmaExpression = getEcmaExpression;
605
598
  exports.getFunctionIdentifier = getFunctionIdentifier;
606
599
  exports.getFunctionInitPath = getFunctionInitPath;
607
- exports.getLiteralValueType = getLiteralValueType;
600
+ exports.getJSExpression = getJSExpression;
608
601
  exports.getNestedCallExpressions = getNestedCallExpressions;
609
602
  exports.getNestedExpressionsOfType = getNestedExpressionsOfType;
610
603
  exports.getNestedIdentifiers = getNestedIdentifiers;
@@ -643,5 +636,5 @@ exports.isThisExpression = isThisExpression;
643
636
  exports.isTypeAssertionExpression = isTypeAssertionExpression;
644
637
  exports.isTypeDeclaration = isTypeDeclaration;
645
638
  exports.isTypeExpression = isTypeExpression;
646
- exports.stringify = stringify;
647
639
  exports.toReadableNodeType = toReadableNodeType;
640
+ exports.toString = toString;