@eslint-react/ast 5.17.2 → 5.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ type TSESTreeDirective = TSESTree.ExpressionStatement & {
12
12
  };
13
13
  type TSESTreeTypeExpression = TSESTree.TSAsExpression | TSESTree.TSTypeAssertion | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSInstantiationExpression;
14
14
  declare namespace check_d_exports {
15
- export { is, isClass, isDirective, isExpression, isFunction, isIdentifier, isJSX, isJSXElement, isJSXElementOrFragment, isJSXFragment, isJSXTagNameExpression, isOneOf, isProperty, isPropertyOrMethod, isTypeAssertionExpression, isTypeExpression };
15
+ export { is, isClass, isConditional, isDirective, isExpression, isFunction, isIdentifier, isJSX, isJSXElement, isJSXElementOrFragment, isJSXFragment, isJSXTagNameExpression, isOneOf, isProperty, isPropertyOrMethod, isTypeAssertionExpression, isTypeExpression };
16
16
  }
17
17
  declare const is: <NodeType extends AST_NODE_TYPES>(nodeType: NodeType) => (node: TSESTree.Node | null | undefined) => node is Extract<TSESTree.Node, {
18
18
  type: NodeType;
@@ -34,6 +34,12 @@ declare const isJSX: (node: TSESTree.Node | null | undefined) => node is TSESTre
34
34
  declare const isTypeExpression: (node: TSESTree.Node | null | undefined) => node is TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion;
35
35
  declare const isTypeAssertionExpression: (node: TSESTree.Node | null | undefined) => node is TSESTree.TSAsExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion;
36
36
  declare const isExpression: (node: TSESTree.Node | null | undefined) => node is TSESTree.ArrayExpression | TSESTree.ArrayPattern | TSESTree.ArrowFunctionExpressionWithBlockBody | TSESTree.ArrowFunctionExpressionWithExpressionBody | TSESTree.AssignmentExpression | TSESTree.AwaitExpression | TSESTree.PrivateInExpression | TSESTree.SymmetricBinaryExpression | TSESTree.CallExpression | TSESTree.ChainExpression | TSESTree.ClassExpression | TSESTree.ConditionalExpression | TSESTree.FunctionExpression | TSESTree.Identifier | TSESTree.ImportExpression | TSESTree.JSXElement | TSESTree.JSXFragment | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral | TSESTree.LogicalExpression | TSESTree.MemberExpressionComputedName | TSESTree.MemberExpressionNonComputedName | TSESTree.MetaProperty | TSESTree.NewExpression | TSESTree.ObjectExpression | TSESTree.ObjectPattern | TSESTree.SequenceExpression | TSESTree.Super | TSESTree.TaggedTemplateExpression | TSESTree.TemplateLiteral | TSESTree.ThisExpression | TSESTree.TSAsExpression | TSESTree.TSInstantiationExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion | TSESTree.UnaryExpressionBitwiseNot | TSESTree.UnaryExpressionDelete | TSESTree.UnaryExpressionMinus | TSESTree.UnaryExpressionNot | TSESTree.UnaryExpressionPlus | TSESTree.UnaryExpressionTypeof | TSESTree.UnaryExpressionVoid | TSESTree.UpdateExpression | TSESTree.YieldNoStarExpression | TSESTree.YieldStarExpression;
37
+ /**
38
+ * Check if a node is a conditional expression or control flow statement
39
+ * @param node The node to check
40
+ * @returns True if the node is conditional
41
+ */
42
+ declare const isConditional: (node: TSESTree.Node | null | undefined) => node is TSESTree.ConditionalExpression | TSESTree.DoWhileStatement | TSESTree.ForInStatement | TSESTree.ForOfStatement | TSESTree.ForStatement | TSESTree.IfStatement | TSESTree.LogicalExpression | TSESTree.SwitchStatement | TSESTree.WhileStatement;
37
43
  declare namespace compare_d_exports {
38
44
  export { isEqual };
39
45
  }
@@ -49,13 +55,14 @@ declare const isEqual: {
49
55
  (a: TSESTree.Node, b: TSESTree.Node): boolean;
50
56
  };
51
57
  declare namespace extract_d_exports {
52
- export { getCalleeName, getFullyQualifiedName, getRootIdentifier, getStaticPropertyName, unwrap };
58
+ export { findProperty, getCalleeName, getFullyQualifiedName, getIdentifierAt, getPropertyName, unwrap };
53
59
  }
54
60
  declare function unwrap(node: TSESTree.Node): Exclude<TSESTree.Node, TSESTreeTypeExpression>;
55
- declare function getRootIdentifier(node: TSESTree.Expression | TSESTree.PrivateIdentifier): TSESTree.Identifier | null;
61
+ declare function findProperty(properties: TSESTree.ObjectLiteralElement[], name: string): TSESTree.Property | null;
56
62
  declare function getCalleeName(node: TSESTree.CallExpression): string | null;
57
- declare function getStaticPropertyName(prop: TSESTree.Property): string | null;
63
+ declare function getPropertyName(property: TSESTree.Property, effort?: "min" | "max"): string | null;
58
64
  declare function getFullyQualifiedName(node: TSESTree.Node, getText: (node: TSESTree.Node) => string): string;
65
+ declare function getIdentifierAt(node: TSESTree.Expression | TSESTree.PrivateIdentifier, position: number): TSESTree.Identifier | null;
59
66
  declare namespace traverse_d_exports {
60
67
  export { findParent };
61
68
  }
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ import { ASTUtils } from "@typescript-eslint/utils";
6
6
  var check_exports = /* @__PURE__ */ __exportAll({
7
7
  is: () => is,
8
8
  isClass: () => isClass,
9
+ isConditional: () => isConditional,
9
10
  isDirective: () => isDirective,
10
11
  isExpression: () => isExpression,
11
12
  isFunction: () => isFunction,
@@ -117,6 +118,22 @@ const isExpression = isOneOf([
117
118
  AST_NODE_TYPES.UpdateExpression,
118
119
  AST_NODE_TYPES.YieldExpression
119
120
  ]);
121
+ /**
122
+ * Check if a node is a conditional expression or control flow statement
123
+ * @param node The node to check
124
+ * @returns True if the node is conditional
125
+ */
126
+ const isConditional = isOneOf([
127
+ AST_NODE_TYPES.DoWhileStatement,
128
+ AST_NODE_TYPES.ForInStatement,
129
+ AST_NODE_TYPES.ForOfStatement,
130
+ AST_NODE_TYPES.ForStatement,
131
+ AST_NODE_TYPES.WhileStatement,
132
+ AST_NODE_TYPES.IfStatement,
133
+ AST_NODE_TYPES.SwitchStatement,
134
+ AST_NODE_TYPES.LogicalExpression,
135
+ AST_NODE_TYPES.ConditionalExpression
136
+ ]);
120
137
 
121
138
  //#endregion
122
139
  //#region ../../.pkgs/eff/dist/index.js
@@ -595,20 +612,25 @@ const takeWhile = dual(2, (xs, pred) => {
595
612
  //#endregion
596
613
  //#region src/extract.ts
597
614
  var extract_exports = /* @__PURE__ */ __exportAll({
615
+ findProperty: () => findProperty,
598
616
  getCalleeName: () => getCalleeName,
599
617
  getFullyQualifiedName: () => getFullyQualifiedName,
600
- getRootIdentifier: () => getRootIdentifier,
601
- getStaticPropertyName: () => getStaticPropertyName,
618
+ getIdentifierAt: () => getIdentifierAt,
619
+ getPropertyName: () => getPropertyName,
602
620
  unwrap: () => unwrap
603
621
  });
604
622
  function unwrap(node) {
605
623
  if (isTypeExpression(node) || node.type === AST_NODE_TYPES.ChainExpression) return unwrap(node.expression);
606
624
  return node;
607
625
  }
608
- function getRootIdentifier(node) {
609
- const expr = unwrap(node);
610
- if (expr.type === AST_NODE_TYPES.Identifier) return expr;
611
- if (expr.type === AST_NODE_TYPES.MemberExpression) return getRootIdentifier(expr.object);
626
+ function findProperty(properties, name) {
627
+ for (const property of properties) {
628
+ if (property.type === AST_NODE_TYPES.Property && getPropertyName(property) === name) return property;
629
+ if (property.type === AST_NODE_TYPES.SpreadElement && property.argument.type === AST_NODE_TYPES.ObjectExpression) {
630
+ const found = findProperty(property.argument.properties, name);
631
+ if (found != null) return found;
632
+ }
633
+ }
612
634
  return null;
613
635
  }
614
636
  function getCalleeName(node) {
@@ -617,9 +639,10 @@ function getCalleeName(node) {
617
639
  if (callee.type === AST_NODE_TYPES.MemberExpression && !callee.computed && callee.property.type === AST_NODE_TYPES.Identifier) return callee.property.name;
618
640
  return null;
619
641
  }
620
- function getStaticPropertyName(prop) {
621
- const key = unwrap(prop.key);
622
- if (key.type === AST_NODE_TYPES.Identifier && !prop.computed) return key.name;
642
+ function getPropertyName(property, effort = "min") {
643
+ const key = unwrap(property.key);
644
+ if (key.type === AST_NODE_TYPES.Identifier && !property.computed) return key.name;
645
+ if (effort === "min") return null;
623
646
  if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") return key.value;
624
647
  if (key.type === AST_NODE_TYPES.TemplateLiteral && key.expressions.length === 0) return key.quasis[0]?.value.cooked ?? key.quasis[0]?.value.raw ?? null;
625
648
  return null;
@@ -640,6 +663,17 @@ function getFullyQualifiedName(node, getText) {
640
663
  default: return getText(expr);
641
664
  }
642
665
  }
666
+ function getIdentifierAt(node, position) {
667
+ const identifiers = [];
668
+ let current = unwrap(node);
669
+ while (current.type === AST_NODE_TYPES.MemberExpression) {
670
+ const property = unwrap(current.property);
671
+ identifiers.unshift(property.type === AST_NODE_TYPES.Identifier ? property : null);
672
+ current = unwrap(current.object);
673
+ }
674
+ identifiers.unshift(current.type === AST_NODE_TYPES.Identifier ? current : null);
675
+ return identifiers.at(position) ?? null;
676
+ }
643
677
 
644
678
  //#endregion
645
679
  //#region src/compare.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/ast",
3
- "version": "5.17.2",
3
+ "version": "5.17.3",
4
4
  "description": "ESLint React's TSESTree AST utility module.",
5
5
  "homepage": "https://github.com/Rel1cx/eslint-react",
6
6
  "bugs": {
@@ -41,8 +41,8 @@
41
41
  "typescript": "6.0.3",
42
42
  "vitest": "^4.1.10",
43
43
  "@local/configs": "0.0.0",
44
- "@local/testkit": "0.0.0",
45
- "@local/eff": "0.0.0"
44
+ "@local/eff": "0.0.0",
45
+ "@local/testkit": "0.0.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "eslint": "*",