@eslint-react/jsx 1.24.0-next.1 → 1.24.0-next.12

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.mts CHANGED
@@ -1,66 +1,68 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ import { TSESTree as TSESTree$1 } from '@typescript-eslint/types';
1
3
  import { _ } from '@eslint-react/eff';
2
- import { TSESTree } from '@typescript-eslint/types';
3
- import * as VAR from '@eslint-react/var';
4
4
  import { Scope } from '@typescript-eslint/scope-manager';
5
- import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
6
-
7
- declare function findParentProp(node: TSESTree.Node, test?: (node: TSESTree.JSXAttribute) => boolean): TSESTree.JSXAttribute | _;
5
+ import * as VAR from '@eslint-react/var';
8
6
 
9
7
  /**
10
- * Returns the tag name associated with a JSXOpeningElement.
11
- * @param node The visited JSXOpeningElement node object.
12
- * @returns The element's tag name.
8
+ * Get the stringified name of a JSX attribute
9
+ * @param node The JSX attribute node
10
+ * @returns The name of the attribute
13
11
  */
14
- declare function getElementName(node: TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment): string;
12
+ declare function getAttributeName(node: TSESTree.JSXAttribute): string;
15
13
 
16
14
  /**
17
- * Get the name of a JSX attribute with namespace
18
- * @param node The JSX attribute node
19
- * @returns string
15
+ * Get the stringified name of a JSX element
16
+ * @param node The JSX element node
17
+ * @returns The name of the element
20
18
  */
21
- declare function getPropName(node: TSESTree$1.JSXAttribute): string;
22
- declare function getProp(name: string, initialScope: Scope, props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[]): TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | _;
19
+ declare function getElementName(node: TSESTree$1.JSXElement | TSESTree$1.JSXFragment): string;
20
+
23
21
  /**
24
- * Gets and resolves the static value of a JSX attribute
25
- * @param attribute The JSX attribute to get the value of
26
- * @param initialScope The initial scope to start from
27
- * @returns The static value of the given JSX attribute
22
+ * Find the parent JSX attribute node of a node
23
+ * @param node The node to find the parent attribute of
24
+ * @param test The test to apply to the parent attribute
25
+ * @returns The parent attribute node or undefined
28
26
  */
29
- declare function getPropValue(attribute: TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute, initialScope: Scope): VAR.StaticValue;
30
- declare function findPropInProperties(name: string, properties: (TSESTree$1.Property | TSESTree$1.RestElement | TSESTree$1.SpreadElement)[], initialScope: Scope, seenProps?: string[]): (typeof properties)[number] | _;
31
- declare function findPropInAttributes(name: string, initialScope: Scope, attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[]): TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | _;
27
+ declare function findParentAttribute(node: TSESTree$1.Node, test?: (node: TSESTree$1.JSXAttribute) => boolean): TSESTree$1.JSXAttribute | _;
32
28
 
33
- declare function hasProp(propName: string, initialScope: Scope, attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[]): boolean;
34
29
  /**
35
- * Check if any of the given prop names are present in the given attributes
36
- * @param attributes The attributes to search in
37
- * @param propNames The prop names to search for
38
- * @param initialScope The initial scope to start from
39
- * @returns `true` if any of the given prop names are present in the given attributes
30
+ * Get the JSX attribute node with the given name
31
+ * @param name The name of the attribute
32
+ * @param initialScope The initial scope to use for variable resolution
33
+ * @param attributes The attributes to search
34
+ * @returns The JSX attribute node or undefined
40
35
  */
41
- declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], initialScope: Scope): boolean;
36
+ declare function getAttribute(name: string, initialScope: Scope, attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[]): TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute | _;
37
+
42
38
  /**
43
- * Check if all of the given prop names are present in the given attributes
44
- * @param attributes The attributes to search in
45
- * @param propNames The prop names to search for
46
- * @param initialScope The initial scope to start from
47
- * @returns `true` if all of the given prop names are present in the given attributes
39
+ * Get a StaticValue of the attribute value
40
+ * @param name The name of the attribute
41
+ * @param node The JSX attribute node
42
+ * @param initialScope The initial scope to use
43
+ * @returns The StaticValue of the attribute value
48
44
  */
49
- declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], initialScope: Scope): boolean;
45
+ declare function getAttributeValue(name: string, node: TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute, initialScope: Scope): Exclude<VAR.LazyValue, {
46
+ kind: "lazy";
47
+ }>;
48
+
49
+ declare function hasAttribute(name: string, initialScope: Scope, attributes: TSESTree$1.JSXOpeningElement["attributes"]): boolean;
50
+ declare function hasAnyAttribute(names: string[], initialScope: Scope, attributes: TSESTree$1.JSXOpeningElement["attributes"]): boolean;
51
+ declare function hasEveryAttribute(names: string[], initialScope: Scope, attributes: TSESTree$1.JSXOpeningElement["attributes"]): boolean;
50
52
 
51
- declare function isKeyedElement(node: TSESTree.Node, initialScope: Scope): boolean;
53
+ declare function isKeyedElement(node: TSESTree$1.Node, initialScope: Scope): boolean;
52
54
  /**
53
55
  * Check if a node is a `JSXFragment` of `Built-in Component` type
54
56
  * @param node The AST node to check
55
57
  * @returns `true` if the node is a `JSXFragment` of `Built-in Component` type
56
58
  */
57
- declare function isBuiltInElement(node: TSESTree.Node): boolean;
59
+ declare function isBuiltInElement(node: TSESTree$1.Node): boolean;
58
60
  /**
59
61
  * Check if a node is a `JSXElement` of `User-Defined Component` type
60
62
  * @param node The AST node to check
61
63
  * @returns `true` if the node is a `JSXElement` of `User-Defined Component` type
62
64
  */
63
- declare function isUserDefinedElement(node: TSESTree.Node): boolean;
65
+ declare function isUserDefinedElement(node: TSESTree$1.Node): boolean;
64
66
 
65
67
  declare const JSXValueHint: {
66
68
  readonly None: 0n;
@@ -78,15 +80,15 @@ declare const JSXValueHint: {
78
80
  };
79
81
  declare const DEFAULT_JSX_VALUE_HINT: bigint;
80
82
  /**
81
- * Check if a node is a JSX value
83
+ * Heruistic decision to determine if a node is a JSX value
82
84
  * @param node The AST node to check
83
85
  * @param jsxCtx The requirements for the check
84
86
  * @param jsxCtx.getScope The function to get the scope of a node
85
87
  * @param hint The `JSXValueHint` to use
86
88
  * @returns boolean
87
89
  */
88
- declare function isJSXValue(node: TSESTree$1.Node | _ | null, jsxCtx: {
89
- getScope: (node: TSESTree$1.Node) => Scope;
90
+ declare function isJSXValue(node: TSESTree.Node | _ | null, jsxCtx: {
91
+ getScope: (node: TSESTree.Node) => Scope;
90
92
  }, hint?: bigint): boolean;
91
93
 
92
94
  /**
@@ -94,25 +96,32 @@ declare function isJSXValue(node: TSESTree$1.Node | _ | null, jsxCtx: {
94
96
  * @param node The AST node to check
95
97
  * @returns boolean `true` if the node is a Literal or JSXText
96
98
  */
97
- declare const isLiteral: (node: TSESTree.Node | null | undefined) => node is TSESTree.JSXText | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral;
99
+ declare const isLiteral: (node: TSESTree$1.Node | null | undefined) => node is TSESTree$1.JSXText | TSESTree$1.BigIntLiteral | TSESTree$1.BooleanLiteral | TSESTree$1.NullLiteral | TSESTree$1.NumberLiteral | TSESTree$1.RegExpLiteral | TSESTree$1.StringLiteral;
98
100
  /**
99
101
  * Check if a Literal or JSXText node is whitespace
100
102
  * @param node The AST node to check
101
103
  * @returns boolean `true` if the node is whitespace
102
104
  */
103
- declare function isWhiteSpace(node: TSESTree.JSXText | TSESTree.Literal): boolean;
105
+ declare function isWhiteSpace(node: TSESTree$1.JSXText | TSESTree$1.Literal): boolean;
104
106
  /**
105
107
  * Check if a Literal or JSXText node is a line break
106
108
  * @param node The AST node to check
107
109
  * @returns boolean
108
110
  */
109
- declare function isLineBreak(node: TSESTree.Node): boolean;
111
+ declare function isLineBreak(node: TSESTree$1.Node): boolean;
110
112
  /**
111
113
  * Check if a Literal or JSXText node is padding spaces
112
114
  * @param node The AST node to check
113
115
  * @returns boolean
114
116
  */
115
- declare function isPaddingSpaces(node: TSESTree.Node): boolean;
117
+ declare function isPaddingSpaces(node: TSESTree$1.Node): boolean;
118
+
119
+ /**
120
+ * Get the stringified representation of a JSX node
121
+ * @param node The JSX node
122
+ * @returns The stringified representation
123
+ */
124
+ declare function toString(node: TSESTree.JSXIdentifier | TSESTree.JSXMemberExpression | TSESTree.JSXNamespacedName | TSESTree.JSXOpeningElement | TSESTree.JSXClosingElement | TSESTree.JSXOpeningFragment | TSESTree.JSXClosingFragment | TSESTree.JSXText): string;
116
125
 
117
126
  /**
118
127
  * Unescape the text content of string literals, e.g. &amp; -> &
@@ -123,4 +132,4 @@ declare function unescapeStringLiteralText(text: string): string;
123
132
 
124
133
  declare const xhtmlEntities: Record<string, string>;
125
134
 
126
- export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findParentProp, findPropInAttributes, findPropInProperties, getElementName, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, unescapeStringLiteralText, xhtmlEntities };
135
+ export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findParentAttribute, getAttribute, getAttributeName, getAttributeValue, getElementName, hasAnyAttribute, hasAttribute, hasEveryAttribute, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, toString, unescapeStringLiteralText, xhtmlEntities };
package/dist/index.d.ts CHANGED
@@ -1,66 +1,68 @@
1
+ import { TSESTree } from '@typescript-eslint/utils';
2
+ import { TSESTree as TSESTree$1 } from '@typescript-eslint/types';
1
3
  import { _ } from '@eslint-react/eff';
2
- import { TSESTree } from '@typescript-eslint/types';
3
- import * as VAR from '@eslint-react/var';
4
4
  import { Scope } from '@typescript-eslint/scope-manager';
5
- import { TSESTree as TSESTree$1 } from '@typescript-eslint/utils';
6
-
7
- declare function findParentProp(node: TSESTree.Node, test?: (node: TSESTree.JSXAttribute) => boolean): TSESTree.JSXAttribute | _;
5
+ import * as VAR from '@eslint-react/var';
8
6
 
9
7
  /**
10
- * Returns the tag name associated with a JSXOpeningElement.
11
- * @param node The visited JSXOpeningElement node object.
12
- * @returns The element's tag name.
8
+ * Get the stringified name of a JSX attribute
9
+ * @param node The JSX attribute node
10
+ * @returns The name of the attribute
13
11
  */
14
- declare function getElementName(node: TSESTree.JSXOpeningElement | TSESTree.JSXOpeningFragment): string;
12
+ declare function getAttributeName(node: TSESTree.JSXAttribute): string;
15
13
 
16
14
  /**
17
- * Get the name of a JSX attribute with namespace
18
- * @param node The JSX attribute node
19
- * @returns string
15
+ * Get the stringified name of a JSX element
16
+ * @param node The JSX element node
17
+ * @returns The name of the element
20
18
  */
21
- declare function getPropName(node: TSESTree$1.JSXAttribute): string;
22
- declare function getProp(name: string, initialScope: Scope, props: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[]): TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | _;
19
+ declare function getElementName(node: TSESTree$1.JSXElement | TSESTree$1.JSXFragment): string;
20
+
23
21
  /**
24
- * Gets and resolves the static value of a JSX attribute
25
- * @param attribute The JSX attribute to get the value of
26
- * @param initialScope The initial scope to start from
27
- * @returns The static value of the given JSX attribute
22
+ * Find the parent JSX attribute node of a node
23
+ * @param node The node to find the parent attribute of
24
+ * @param test The test to apply to the parent attribute
25
+ * @returns The parent attribute node or undefined
28
26
  */
29
- declare function getPropValue(attribute: TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute, initialScope: Scope): VAR.StaticValue;
30
- declare function findPropInProperties(name: string, properties: (TSESTree$1.Property | TSESTree$1.RestElement | TSESTree$1.SpreadElement)[], initialScope: Scope, seenProps?: string[]): (typeof properties)[number] | _;
31
- declare function findPropInAttributes(name: string, initialScope: Scope, attributes: (TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute)[]): TSESTree$1.JSXAttribute | TSESTree$1.JSXSpreadAttribute | _;
27
+ declare function findParentAttribute(node: TSESTree$1.Node, test?: (node: TSESTree$1.JSXAttribute) => boolean): TSESTree$1.JSXAttribute | _;
32
28
 
33
- declare function hasProp(propName: string, initialScope: Scope, attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[]): boolean;
34
29
  /**
35
- * Check if any of the given prop names are present in the given attributes
36
- * @param attributes The attributes to search in
37
- * @param propNames The prop names to search for
38
- * @param initialScope The initial scope to start from
39
- * @returns `true` if any of the given prop names are present in the given attributes
30
+ * Get the JSX attribute node with the given name
31
+ * @param name The name of the attribute
32
+ * @param initialScope The initial scope to use for variable resolution
33
+ * @param attributes The attributes to search
34
+ * @returns The JSX attribute node or undefined
40
35
  */
41
- declare function hasAnyProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], initialScope: Scope): boolean;
36
+ declare function getAttribute(name: string, initialScope: Scope, attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[]): TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute | _;
37
+
42
38
  /**
43
- * Check if all of the given prop names are present in the given attributes
44
- * @param attributes The attributes to search in
45
- * @param propNames The prop names to search for
46
- * @param initialScope The initial scope to start from
47
- * @returns `true` if all of the given prop names are present in the given attributes
39
+ * Get a StaticValue of the attribute value
40
+ * @param name The name of the attribute
41
+ * @param node The JSX attribute node
42
+ * @param initialScope The initial scope to use
43
+ * @returns The StaticValue of the attribute value
48
44
  */
49
- declare function hasEveryProp(attributes: (TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute)[], propNames: string[], initialScope: Scope): boolean;
45
+ declare function getAttributeValue(name: string, node: TSESTree.JSXAttribute | TSESTree.JSXSpreadAttribute, initialScope: Scope): Exclude<VAR.LazyValue, {
46
+ kind: "lazy";
47
+ }>;
48
+
49
+ declare function hasAttribute(name: string, initialScope: Scope, attributes: TSESTree$1.JSXOpeningElement["attributes"]): boolean;
50
+ declare function hasAnyAttribute(names: string[], initialScope: Scope, attributes: TSESTree$1.JSXOpeningElement["attributes"]): boolean;
51
+ declare function hasEveryAttribute(names: string[], initialScope: Scope, attributes: TSESTree$1.JSXOpeningElement["attributes"]): boolean;
50
52
 
51
- declare function isKeyedElement(node: TSESTree.Node, initialScope: Scope): boolean;
53
+ declare function isKeyedElement(node: TSESTree$1.Node, initialScope: Scope): boolean;
52
54
  /**
53
55
  * Check if a node is a `JSXFragment` of `Built-in Component` type
54
56
  * @param node The AST node to check
55
57
  * @returns `true` if the node is a `JSXFragment` of `Built-in Component` type
56
58
  */
57
- declare function isBuiltInElement(node: TSESTree.Node): boolean;
59
+ declare function isBuiltInElement(node: TSESTree$1.Node): boolean;
58
60
  /**
59
61
  * Check if a node is a `JSXElement` of `User-Defined Component` type
60
62
  * @param node The AST node to check
61
63
  * @returns `true` if the node is a `JSXElement` of `User-Defined Component` type
62
64
  */
63
- declare function isUserDefinedElement(node: TSESTree.Node): boolean;
65
+ declare function isUserDefinedElement(node: TSESTree$1.Node): boolean;
64
66
 
65
67
  declare const JSXValueHint: {
66
68
  readonly None: 0n;
@@ -78,15 +80,15 @@ declare const JSXValueHint: {
78
80
  };
79
81
  declare const DEFAULT_JSX_VALUE_HINT: bigint;
80
82
  /**
81
- * Check if a node is a JSX value
83
+ * Heruistic decision to determine if a node is a JSX value
82
84
  * @param node The AST node to check
83
85
  * @param jsxCtx The requirements for the check
84
86
  * @param jsxCtx.getScope The function to get the scope of a node
85
87
  * @param hint The `JSXValueHint` to use
86
88
  * @returns boolean
87
89
  */
88
- declare function isJSXValue(node: TSESTree$1.Node | _ | null, jsxCtx: {
89
- getScope: (node: TSESTree$1.Node) => Scope;
90
+ declare function isJSXValue(node: TSESTree.Node | _ | null, jsxCtx: {
91
+ getScope: (node: TSESTree.Node) => Scope;
90
92
  }, hint?: bigint): boolean;
91
93
 
92
94
  /**
@@ -94,25 +96,32 @@ declare function isJSXValue(node: TSESTree$1.Node | _ | null, jsxCtx: {
94
96
  * @param node The AST node to check
95
97
  * @returns boolean `true` if the node is a Literal or JSXText
96
98
  */
97
- declare const isLiteral: (node: TSESTree.Node | null | undefined) => node is TSESTree.JSXText | TSESTree.BigIntLiteral | TSESTree.BooleanLiteral | TSESTree.NullLiteral | TSESTree.NumberLiteral | TSESTree.RegExpLiteral | TSESTree.StringLiteral;
99
+ declare const isLiteral: (node: TSESTree$1.Node | null | undefined) => node is TSESTree$1.JSXText | TSESTree$1.BigIntLiteral | TSESTree$1.BooleanLiteral | TSESTree$1.NullLiteral | TSESTree$1.NumberLiteral | TSESTree$1.RegExpLiteral | TSESTree$1.StringLiteral;
98
100
  /**
99
101
  * Check if a Literal or JSXText node is whitespace
100
102
  * @param node The AST node to check
101
103
  * @returns boolean `true` if the node is whitespace
102
104
  */
103
- declare function isWhiteSpace(node: TSESTree.JSXText | TSESTree.Literal): boolean;
105
+ declare function isWhiteSpace(node: TSESTree$1.JSXText | TSESTree$1.Literal): boolean;
104
106
  /**
105
107
  * Check if a Literal or JSXText node is a line break
106
108
  * @param node The AST node to check
107
109
  * @returns boolean
108
110
  */
109
- declare function isLineBreak(node: TSESTree.Node): boolean;
111
+ declare function isLineBreak(node: TSESTree$1.Node): boolean;
110
112
  /**
111
113
  * Check if a Literal or JSXText node is padding spaces
112
114
  * @param node The AST node to check
113
115
  * @returns boolean
114
116
  */
115
- declare function isPaddingSpaces(node: TSESTree.Node): boolean;
117
+ declare function isPaddingSpaces(node: TSESTree$1.Node): boolean;
118
+
119
+ /**
120
+ * Get the stringified representation of a JSX node
121
+ * @param node The JSX node
122
+ * @returns The stringified representation
123
+ */
124
+ declare function toString(node: TSESTree.JSXIdentifier | TSESTree.JSXMemberExpression | TSESTree.JSXNamespacedName | TSESTree.JSXOpeningElement | TSESTree.JSXClosingElement | TSESTree.JSXOpeningFragment | TSESTree.JSXClosingFragment | TSESTree.JSXText): string;
116
125
 
117
126
  /**
118
127
  * Unescape the text content of string literals, e.g. &amp; -> &
@@ -123,4 +132,4 @@ declare function unescapeStringLiteralText(text: string): string;
123
132
 
124
133
  declare const xhtmlEntities: Record<string, string>;
125
134
 
126
- export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findParentProp, findPropInAttributes, findPropInProperties, getElementName, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, unescapeStringLiteralText, xhtmlEntities };
135
+ export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findParentAttribute, getAttribute, getAttributeName, getAttributeValue, getElementName, hasAnyAttribute, hasAttribute, hasEveryAttribute, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, toString, unescapeStringLiteralText, xhtmlEntities };
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  'use strict';
2
2
 
3
+ var types = require('@typescript-eslint/types');
3
4
  var AST3 = require('@eslint-react/ast');
4
5
  var eff = require('@eslint-react/eff');
5
- var types = require('@typescript-eslint/types');
6
6
  var VAR = require('@eslint-react/var');
7
+ var tsPattern = require('ts-pattern');
7
8
 
8
9
  function _interopNamespace(e) {
9
10
  if (e && e.__esModule) return e;
@@ -26,146 +27,116 @@ function _interopNamespace(e) {
26
27
  var AST3__namespace = /*#__PURE__*/_interopNamespace(AST3);
27
28
  var VAR__namespace = /*#__PURE__*/_interopNamespace(VAR);
28
29
 
29
- // src/find-parent-prop.ts
30
- function findParentProp(node, test = eff.returnTrue) {
31
- const guard = (node2) => {
32
- return node2.type === types.AST_NODE_TYPES.JSXAttribute && test(node2);
33
- };
34
- return AST3__namespace.findParentNodeGuard(node, guard);
35
- }
36
- function resolveJSXMemberExpressions(object, property) {
37
- if (object.type === types.AST_NODE_TYPES.JSXMemberExpression) {
38
- return `${resolveJSXMemberExpressions(object.object, object.property)}.${property.name}`;
39
- }
40
- if (object.type === types.AST_NODE_TYPES.JSXNamespacedName) {
41
- return `${object.namespace.name}:${object.name.name}.${property.name}`;
42
- }
43
- return `${object.name}.${property.name}`;
44
- }
45
- function getElementName(node) {
46
- if (node.type === types.AST_NODE_TYPES.JSXOpeningFragment) {
47
- return "<>";
48
- }
49
- const { name } = node;
50
- if (name.type === types.AST_NODE_TYPES.JSXMemberExpression) {
51
- const { object, property } = name;
52
- return resolveJSXMemberExpressions(object, property);
53
- }
54
- if (name.type === types.AST_NODE_TYPES.JSXNamespacedName) {
55
- return `${name.namespace.name}:${name.name.name}`;
56
- }
57
- return name.name;
58
- }
59
- function getPropName(node) {
60
- switch (node.name.type) {
30
+ // src/to-string.ts
31
+ function toString(node) {
32
+ switch (node.type) {
61
33
  case types.AST_NODE_TYPES.JSXIdentifier:
62
- return node.name.name;
34
+ return node.name;
63
35
  case types.AST_NODE_TYPES.JSXNamespacedName:
64
- return `${node.name.namespace.name}:${node.name.name.name}`;
36
+ return `${node.namespace.name}:${node.name.name}`;
37
+ case types.AST_NODE_TYPES.JSXMemberExpression:
38
+ return `${toString(node.object)}.${toString(node.property)}`;
39
+ case types.AST_NODE_TYPES.JSXText:
40
+ return node.value;
41
+ case types.AST_NODE_TYPES.JSXOpeningElement:
42
+ return `<${toString(node.name)}>`;
43
+ case types.AST_NODE_TYPES.JSXClosingElement:
44
+ return `</${toString(node.name)}>`;
45
+ case types.AST_NODE_TYPES.JSXOpeningFragment:
46
+ return "<>";
47
+ case types.AST_NODE_TYPES.JSXClosingFragment:
48
+ return "</>";
65
49
  }
66
50
  }
67
- function getProp(name, initialScope, props) {
68
- return findPropInAttributes(name, initialScope, props);
51
+
52
+ // src/attribute-name.ts
53
+ function getAttributeName(node) {
54
+ return toString(node.name);
69
55
  }
70
- function getPropValue(attribute, initialScope) {
71
- switch (attribute.type) {
72
- case types.AST_NODE_TYPES.JSXAttribute:
73
- if (attribute.value?.type === types.AST_NODE_TYPES.Literal) {
74
- return {
75
- kind: "some",
76
- node: attribute.value,
77
- initialScope,
78
- value: attribute.value.value
79
- };
80
- }
81
- if (attribute.value?.type === types.AST_NODE_TYPES.JSXExpressionContainer) {
82
- return {
83
- kind: "lazy",
84
- node: attribute.value.expression,
85
- initialScope
86
- };
87
- }
88
- return { kind: "none", node: attribute, initialScope };
89
- case types.AST_NODE_TYPES.JSXSpreadAttribute:
90
- return {
91
- kind: "lazy",
92
- node: attribute.argument,
93
- initialScope
94
- };
95
- default:
96
- return { kind: "none", node: attribute, initialScope };
56
+ function getElementName(node) {
57
+ if (node.type === types.AST_NODE_TYPES.JSXFragment) {
58
+ return "";
97
59
  }
60
+ return toString(node.openingElement.name);
98
61
  }
99
- function findPropInProperties(name, properties, initialScope, seenProps = []) {
100
- return properties.findLast((prop) => {
101
- if (prop.type === types.AST_NODE_TYPES.Property) {
102
- return "name" in prop.key && prop.key.name === name;
103
- }
104
- if (prop.type === types.AST_NODE_TYPES.SpreadElement) {
105
- switch (prop.argument.type) {
106
- case types.AST_NODE_TYPES.Identifier: {
107
- if (seenProps.includes(prop.argument.name)) {
108
- return false;
109
- }
110
- const variable = VAR__namespace.findVariable(prop.argument.name, initialScope);
111
- const variableNode = VAR__namespace.getVariableNode(variable, 0);
112
- if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) {
113
- return findPropInProperties(
114
- name,
115
- variableNode.properties,
116
- initialScope,
117
- [...seenProps, prop.argument.name]
118
- ) != null;
119
- }
120
- return false;
121
- }
122
- case types.AST_NODE_TYPES.ObjectExpression: {
123
- return findPropInProperties(
124
- name,
125
- prop.argument.properties,
126
- initialScope,
127
- seenProps
128
- ) != null;
129
- }
130
- }
131
- return false;
132
- }
133
- return false;
134
- });
62
+ function findParentAttribute(node, test = eff.returnTrue) {
63
+ const guard = (node2) => {
64
+ return node2.type === types.AST_NODE_TYPES.JSXAttribute && test(node2);
65
+ };
66
+ return AST3__namespace.findParentNode(node, guard);
135
67
  }
136
- function findPropInAttributes(name, initialScope, attributes) {
68
+ function getAttribute(name, initialScope, attributes) {
137
69
  return attributes.findLast((attr) => {
138
70
  if (attr.type === types.AST_NODE_TYPES.JSXAttribute) {
139
- return getPropName(attr) === name;
71
+ return getAttributeName(attr) === name;
140
72
  }
141
73
  switch (attr.argument.type) {
142
74
  case types.AST_NODE_TYPES.Identifier: {
143
75
  const variable = VAR__namespace.findVariable(attr.argument.name, initialScope);
144
76
  const variableNode = VAR__namespace.getVariableNode(variable, 0);
145
77
  if (variableNode?.type === types.AST_NODE_TYPES.ObjectExpression) {
146
- return findPropInProperties(name, variableNode.properties, initialScope) != null;
78
+ return VAR__namespace.findPropertyInProperties(name, variableNode.properties, initialScope) != null;
147
79
  }
148
80
  return false;
149
81
  }
150
82
  case types.AST_NODE_TYPES.ObjectExpression:
151
- return findPropInProperties(name, attr.argument.properties, initialScope) != null;
83
+ return VAR__namespace.findPropertyInProperties(name, attr.argument.properties, initialScope) != null;
152
84
  }
153
85
  return false;
154
86
  });
155
87
  }
88
+ function getAttributeValue(name, node, initialScope) {
89
+ switch (node.type) {
90
+ case types.AST_NODE_TYPES.JSXAttribute:
91
+ if (node.value?.type === types.AST_NODE_TYPES.Literal) {
92
+ return {
93
+ kind: "some",
94
+ node: node.value,
95
+ initialScope,
96
+ value: node.value.value
97
+ };
98
+ }
99
+ if (node.value?.type === types.AST_NODE_TYPES.JSXExpressionContainer) {
100
+ return VAR__namespace.toStaticValue({
101
+ kind: "lazy",
102
+ node: node.value.expression,
103
+ initialScope
104
+ });
105
+ }
106
+ return { kind: "none", node, initialScope };
107
+ case types.AST_NODE_TYPES.JSXSpreadAttribute: {
108
+ const staticValue = VAR__namespace.toStaticValue({
109
+ kind: "lazy",
110
+ node: node.argument,
111
+ initialScope
112
+ });
113
+ if (staticValue.kind === "none") {
114
+ return staticValue;
115
+ }
116
+ return tsPattern.match(staticValue.value).with({ [name]: tsPattern.P.select(tsPattern.P.any) }, (value) => ({
117
+ kind: "some",
118
+ node: node.argument,
119
+ initialScope,
120
+ value
121
+ })).otherwise(() => ({ kind: "none", node, initialScope }));
122
+ }
123
+ default:
124
+ return { kind: "none", node, initialScope };
125
+ }
126
+ }
156
127
 
157
- // src/has-prop.ts
158
- function hasProp(propName, initialScope, attributes) {
159
- return findPropInAttributes(propName, initialScope, attributes) != null;
128
+ // src/has-attribute.ts
129
+ function hasAttribute(name, initialScope, attributes) {
130
+ return getAttribute(name, initialScope, attributes) != null;
160
131
  }
161
- function hasAnyProp(attributes, propNames, initialScope) {
162
- return propNames.some((propName) => hasProp(propName, initialScope, attributes));
132
+ function hasAnyAttribute(names, initialScope, attributes) {
133
+ return names.some((n) => hasAttribute(n, initialScope, attributes));
163
134
  }
164
- function hasEveryProp(attributes, propNames, initialScope) {
165
- return propNames.every((propName) => hasProp(propName, initialScope, attributes));
135
+ function hasEveryAttribute(names, initialScope, attributes) {
136
+ return names.every((n) => hasAttribute(n, initialScope, attributes));
166
137
  }
167
138
  function isKeyedElement(node, initialScope) {
168
- return node.type === types.AST_NODE_TYPES.JSXElement && hasProp("key", initialScope, node.openingElement.attributes);
139
+ return node.type === types.AST_NODE_TYPES.JSXElement && hasAttribute("key", initialScope, node.openingElement.attributes);
169
140
  }
170
141
  function isBuiltInElement(node) {
171
142
  return node.type === types.AST_NODE_TYPES.JSXElement && node.openingElement.name.type === types.AST_NODE_TYPES.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
@@ -560,16 +531,14 @@ function unescapeStringLiteralText(text) {
560
531
 
561
532
  exports.DEFAULT_JSX_VALUE_HINT = DEFAULT_JSX_VALUE_HINT;
562
533
  exports.JSXValueHint = JSXValueHint;
563
- exports.findParentProp = findParentProp;
564
- exports.findPropInAttributes = findPropInAttributes;
565
- exports.findPropInProperties = findPropInProperties;
534
+ exports.findParentAttribute = findParentAttribute;
535
+ exports.getAttribute = getAttribute;
536
+ exports.getAttributeName = getAttributeName;
537
+ exports.getAttributeValue = getAttributeValue;
566
538
  exports.getElementName = getElementName;
567
- exports.getProp = getProp;
568
- exports.getPropName = getPropName;
569
- exports.getPropValue = getPropValue;
570
- exports.hasAnyProp = hasAnyProp;
571
- exports.hasEveryProp = hasEveryProp;
572
- exports.hasProp = hasProp;
539
+ exports.hasAnyAttribute = hasAnyAttribute;
540
+ exports.hasAttribute = hasAttribute;
541
+ exports.hasEveryAttribute = hasEveryAttribute;
573
542
  exports.isBuiltInElement = isBuiltInElement;
574
543
  exports.isJSXValue = isJSXValue;
575
544
  exports.isKeyedElement = isKeyedElement;
@@ -578,5 +547,6 @@ exports.isLiteral = isLiteral;
578
547
  exports.isPaddingSpaces = isPaddingSpaces;
579
548
  exports.isUserDefinedElement = isUserDefinedElement;
580
549
  exports.isWhiteSpace = isWhiteSpace;
550
+ exports.toString = toString;
581
551
  exports.unescapeStringLiteralText = unescapeStringLiteralText;
582
552
  exports.xhtmlEntities = xhtmlEntities;
package/dist/index.mjs CHANGED
@@ -1,148 +1,119 @@
1
+ import { AST_NODE_TYPES } from '@typescript-eslint/types';
1
2
  import * as AST3 from '@eslint-react/ast';
2
3
  import { returnTrue } from '@eslint-react/eff';
3
- import { AST_NODE_TYPES } from '@typescript-eslint/types';
4
4
  import * as VAR from '@eslint-react/var';
5
+ import { match, P } from 'ts-pattern';
5
6
 
6
- // src/find-parent-prop.ts
7
- function findParentProp(node, test = returnTrue) {
8
- const guard = (node2) => {
9
- return node2.type === AST_NODE_TYPES.JSXAttribute && test(node2);
10
- };
11
- return AST3.findParentNodeGuard(node, guard);
12
- }
13
- function resolveJSXMemberExpressions(object, property) {
14
- if (object.type === AST_NODE_TYPES.JSXMemberExpression) {
15
- return `${resolveJSXMemberExpressions(object.object, object.property)}.${property.name}`;
16
- }
17
- if (object.type === AST_NODE_TYPES.JSXNamespacedName) {
18
- return `${object.namespace.name}:${object.name.name}.${property.name}`;
19
- }
20
- return `${object.name}.${property.name}`;
21
- }
22
- function getElementName(node) {
23
- if (node.type === AST_NODE_TYPES.JSXOpeningFragment) {
24
- return "<>";
25
- }
26
- const { name } = node;
27
- if (name.type === AST_NODE_TYPES.JSXMemberExpression) {
28
- const { object, property } = name;
29
- return resolveJSXMemberExpressions(object, property);
30
- }
31
- if (name.type === AST_NODE_TYPES.JSXNamespacedName) {
32
- return `${name.namespace.name}:${name.name.name}`;
33
- }
34
- return name.name;
35
- }
36
- function getPropName(node) {
37
- switch (node.name.type) {
7
+ // src/to-string.ts
8
+ function toString(node) {
9
+ switch (node.type) {
38
10
  case AST_NODE_TYPES.JSXIdentifier:
39
- return node.name.name;
11
+ return node.name;
40
12
  case AST_NODE_TYPES.JSXNamespacedName:
41
- return `${node.name.namespace.name}:${node.name.name.name}`;
13
+ return `${node.namespace.name}:${node.name.name}`;
14
+ case AST_NODE_TYPES.JSXMemberExpression:
15
+ return `${toString(node.object)}.${toString(node.property)}`;
16
+ case AST_NODE_TYPES.JSXText:
17
+ return node.value;
18
+ case AST_NODE_TYPES.JSXOpeningElement:
19
+ return `<${toString(node.name)}>`;
20
+ case AST_NODE_TYPES.JSXClosingElement:
21
+ return `</${toString(node.name)}>`;
22
+ case AST_NODE_TYPES.JSXOpeningFragment:
23
+ return "<>";
24
+ case AST_NODE_TYPES.JSXClosingFragment:
25
+ return "</>";
42
26
  }
43
27
  }
44
- function getProp(name, initialScope, props) {
45
- return findPropInAttributes(name, initialScope, props);
28
+
29
+ // src/attribute-name.ts
30
+ function getAttributeName(node) {
31
+ return toString(node.name);
46
32
  }
47
- function getPropValue(attribute, initialScope) {
48
- switch (attribute.type) {
49
- case AST_NODE_TYPES.JSXAttribute:
50
- if (attribute.value?.type === AST_NODE_TYPES.Literal) {
51
- return {
52
- kind: "some",
53
- node: attribute.value,
54
- initialScope,
55
- value: attribute.value.value
56
- };
57
- }
58
- if (attribute.value?.type === AST_NODE_TYPES.JSXExpressionContainer) {
59
- return {
60
- kind: "lazy",
61
- node: attribute.value.expression,
62
- initialScope
63
- };
64
- }
65
- return { kind: "none", node: attribute, initialScope };
66
- case AST_NODE_TYPES.JSXSpreadAttribute:
67
- return {
68
- kind: "lazy",
69
- node: attribute.argument,
70
- initialScope
71
- };
72
- default:
73
- return { kind: "none", node: attribute, initialScope };
33
+ function getElementName(node) {
34
+ if (node.type === AST_NODE_TYPES.JSXFragment) {
35
+ return "";
74
36
  }
37
+ return toString(node.openingElement.name);
75
38
  }
76
- function findPropInProperties(name, properties, initialScope, seenProps = []) {
77
- return properties.findLast((prop) => {
78
- if (prop.type === AST_NODE_TYPES.Property) {
79
- return "name" in prop.key && prop.key.name === name;
80
- }
81
- if (prop.type === AST_NODE_TYPES.SpreadElement) {
82
- switch (prop.argument.type) {
83
- case AST_NODE_TYPES.Identifier: {
84
- if (seenProps.includes(prop.argument.name)) {
85
- return false;
86
- }
87
- const variable = VAR.findVariable(prop.argument.name, initialScope);
88
- const variableNode = VAR.getVariableNode(variable, 0);
89
- if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
90
- return findPropInProperties(
91
- name,
92
- variableNode.properties,
93
- initialScope,
94
- [...seenProps, prop.argument.name]
95
- ) != null;
96
- }
97
- return false;
98
- }
99
- case AST_NODE_TYPES.ObjectExpression: {
100
- return findPropInProperties(
101
- name,
102
- prop.argument.properties,
103
- initialScope,
104
- seenProps
105
- ) != null;
106
- }
107
- }
108
- return false;
109
- }
110
- return false;
111
- });
39
+ function findParentAttribute(node, test = returnTrue) {
40
+ const guard = (node2) => {
41
+ return node2.type === AST_NODE_TYPES.JSXAttribute && test(node2);
42
+ };
43
+ return AST3.findParentNode(node, guard);
112
44
  }
113
- function findPropInAttributes(name, initialScope, attributes) {
45
+ function getAttribute(name, initialScope, attributes) {
114
46
  return attributes.findLast((attr) => {
115
47
  if (attr.type === AST_NODE_TYPES.JSXAttribute) {
116
- return getPropName(attr) === name;
48
+ return getAttributeName(attr) === name;
117
49
  }
118
50
  switch (attr.argument.type) {
119
51
  case AST_NODE_TYPES.Identifier: {
120
52
  const variable = VAR.findVariable(attr.argument.name, initialScope);
121
53
  const variableNode = VAR.getVariableNode(variable, 0);
122
54
  if (variableNode?.type === AST_NODE_TYPES.ObjectExpression) {
123
- return findPropInProperties(name, variableNode.properties, initialScope) != null;
55
+ return VAR.findPropertyInProperties(name, variableNode.properties, initialScope) != null;
124
56
  }
125
57
  return false;
126
58
  }
127
59
  case AST_NODE_TYPES.ObjectExpression:
128
- return findPropInProperties(name, attr.argument.properties, initialScope) != null;
60
+ return VAR.findPropertyInProperties(name, attr.argument.properties, initialScope) != null;
129
61
  }
130
62
  return false;
131
63
  });
132
64
  }
65
+ function getAttributeValue(name, node, initialScope) {
66
+ switch (node.type) {
67
+ case AST_NODE_TYPES.JSXAttribute:
68
+ if (node.value?.type === AST_NODE_TYPES.Literal) {
69
+ return {
70
+ kind: "some",
71
+ node: node.value,
72
+ initialScope,
73
+ value: node.value.value
74
+ };
75
+ }
76
+ if (node.value?.type === AST_NODE_TYPES.JSXExpressionContainer) {
77
+ return VAR.toStaticValue({
78
+ kind: "lazy",
79
+ node: node.value.expression,
80
+ initialScope
81
+ });
82
+ }
83
+ return { kind: "none", node, initialScope };
84
+ case AST_NODE_TYPES.JSXSpreadAttribute: {
85
+ const staticValue = VAR.toStaticValue({
86
+ kind: "lazy",
87
+ node: node.argument,
88
+ initialScope
89
+ });
90
+ if (staticValue.kind === "none") {
91
+ return staticValue;
92
+ }
93
+ return match(staticValue.value).with({ [name]: P.select(P.any) }, (value) => ({
94
+ kind: "some",
95
+ node: node.argument,
96
+ initialScope,
97
+ value
98
+ })).otherwise(() => ({ kind: "none", node, initialScope }));
99
+ }
100
+ default:
101
+ return { kind: "none", node, initialScope };
102
+ }
103
+ }
133
104
 
134
- // src/has-prop.ts
135
- function hasProp(propName, initialScope, attributes) {
136
- return findPropInAttributes(propName, initialScope, attributes) != null;
105
+ // src/has-attribute.ts
106
+ function hasAttribute(name, initialScope, attributes) {
107
+ return getAttribute(name, initialScope, attributes) != null;
137
108
  }
138
- function hasAnyProp(attributes, propNames, initialScope) {
139
- return propNames.some((propName) => hasProp(propName, initialScope, attributes));
109
+ function hasAnyAttribute(names, initialScope, attributes) {
110
+ return names.some((n) => hasAttribute(n, initialScope, attributes));
140
111
  }
141
- function hasEveryProp(attributes, propNames, initialScope) {
142
- return propNames.every((propName) => hasProp(propName, initialScope, attributes));
112
+ function hasEveryAttribute(names, initialScope, attributes) {
113
+ return names.every((n) => hasAttribute(n, initialScope, attributes));
143
114
  }
144
115
  function isKeyedElement(node, initialScope) {
145
- return node.type === AST_NODE_TYPES.JSXElement && hasProp("key", initialScope, node.openingElement.attributes);
116
+ return node.type === AST_NODE_TYPES.JSXElement && hasAttribute("key", initialScope, node.openingElement.attributes);
146
117
  }
147
118
  function isBuiltInElement(node) {
148
119
  return node.type === AST_NODE_TYPES.JSXElement && node.openingElement.name.type === AST_NODE_TYPES.JSXIdentifier && node.openingElement.name.name.toLowerCase() === node.openingElement.name.name && /^[a-z]/u.test(node.openingElement.name.name);
@@ -535,4 +506,4 @@ function unescapeStringLiteralText(text) {
535
506
  });
536
507
  }
537
508
 
538
- export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findParentProp, findPropInAttributes, findPropInProperties, getElementName, getProp, getPropName, getPropValue, hasAnyProp, hasEveryProp, hasProp, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, unescapeStringLiteralText, xhtmlEntities };
509
+ export { DEFAULT_JSX_VALUE_HINT, JSXValueHint, findParentAttribute, getAttribute, getAttributeName, getAttributeValue, getElementName, hasAnyAttribute, hasAttribute, hasEveryAttribute, isBuiltInElement, isJSXValue, isKeyedElement, isLineBreak, isLiteral, isPaddingSpaces, isUserDefinedElement, isWhiteSpace, toString, unescapeStringLiteralText, xhtmlEntities };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/jsx",
3
- "version": "1.24.0-next.1",
3
+ "version": "1.24.0-next.12",
4
4
  "description": "ESLint React's TSESTree AST utility module for static analysis of JSX.",
5
5
  "homepage": "https://github.com/Rel1cx/eslint-react",
6
6
  "bugs": {
@@ -33,14 +33,13 @@
33
33
  "./package.json"
34
34
  ],
35
35
  "dependencies": {
36
- "@typescript-eslint/scope-manager": "^8.19.1",
37
- "@typescript-eslint/types": "^8.19.1",
38
- "@typescript-eslint/utils": "^8.19.1",
36
+ "@typescript-eslint/scope-manager": "^8.20.0",
37
+ "@typescript-eslint/types": "^8.20.0",
38
+ "@typescript-eslint/utils": "^8.20.0",
39
39
  "ts-pattern": "^5.6.0",
40
- "@eslint-react/ast": "1.24.0-next.1",
41
- "@eslint-react/types": "1.24.0-next.1",
42
- "@eslint-react/var": "1.24.0-next.1",
43
- "@eslint-react/eff": "1.24.0-next.1"
40
+ "@eslint-react/ast": "1.24.0-next.12",
41
+ "@eslint-react/eff": "1.24.0-next.12",
42
+ "@eslint-react/var": "1.24.0-next.12"
44
43
  },
45
44
  "devDependencies": {
46
45
  "tsup": "^8.3.5",