@eslint-react/jsx 5.30.0-beta.0 → 5.30.0-beta.10
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 +414 -228
- package/dist/index.js +164 -163
- package/package.json +5 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,78 +1,56 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TSESTreeJSXElementLike } from "@eslint-react/ast";
|
|
2
2
|
import { TSESTree } from "@typescript-eslint/types";
|
|
3
3
|
import { RuleContext } from "@eslint-react/eslint";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* A test used to select a `JSXAttribute`.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* that resolves to an object expression, the object's properties are searched for a
|
|
10
|
-
* matching key (see {@link findSpreadProperty}).
|
|
11
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
12
|
-
* @param element The `JSXElement` node to search.
|
|
13
|
-
* @param name The attribute name to look for (ex: "className").
|
|
14
|
-
* @returns The matching `JSXAttribute` or `JSXSpreadAttribute`, or `undefined` when not found.
|
|
15
|
-
*/
|
|
16
|
-
export declare function findAttribute(context: RuleContext, element: TSESTree.JSXElement, name: string): TSESTreeJSXAttributeLike | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* Walk up the AST from `node` to find the nearest `JSXAttribute` ancestor, optionally matching a predicate.
|
|
19
|
-
* @param node The starting node for the upward search.
|
|
20
|
-
* @param test Optional predicate to filter candidate `JSXAttribute` nodes.
|
|
21
|
-
* @returns The first matching `JSXAttribute` ancestor, or `undefined` when none is found.
|
|
22
|
-
*/
|
|
23
|
-
export declare function findParentAttribute(node: TSESTree.Node, test?: (node: TSESTree.JSXAttribute) => boolean): TSESTree.JSXAttribute | undefined;
|
|
24
|
-
/**
|
|
25
|
-
* Find the `Property` node that provides a given key inside a spread argument.
|
|
26
|
-
*
|
|
27
|
-
* This is the single resolution routine shared by {@link findAttribute} (existence
|
|
28
|
-
* checks) and the `spreadProps` variant of `resolveAttributeValue` (value extraction):
|
|
29
|
-
*
|
|
30
|
-
* - An `Identifier` argument is resolved to its initializer via variable
|
|
31
|
-
* resolution, following alias chains (`const b = a`) like `getStaticValue`
|
|
32
|
-
* does; an `ObjectExpression` argument is searched directly.
|
|
33
|
-
* - Properties are walked **in reverse** so that later entries win, matching
|
|
34
|
-
* JavaScript object semantics (`{ ...a, k: 1 }` -> the literal `k`).
|
|
35
|
-
* - Nested `SpreadElement`s (identifiers or inline object expressions) are
|
|
36
|
-
* searched recursively; a `seen` set guards against circular references.
|
|
37
|
-
* - Plain identifier keys and string literal keys are matched directly;
|
|
38
|
-
* computed keys are matched when they are statically evaluable
|
|
39
|
-
* (ex: `{ ["class" + "Name"]: 1 }`).
|
|
40
|
-
* @param context The ESLint rule context (needed for variable resolution).
|
|
41
|
-
* @param argument The spread argument expression to search.
|
|
42
|
-
* @param name The property name to look for.
|
|
43
|
-
* @param seen Internal set of already-visited nodes (cycle guard).
|
|
44
|
-
* @returns The matching `Property` node, or `undefined` when the key is not found.
|
|
45
|
-
*/
|
|
46
|
-
export declare function findSpreadProperty(context: RuleContext, argument: TSESTree.Expression, name: string, seen?: Set<TSESTree.Node>): TSESTree.Property | undefined;
|
|
47
|
-
/**
|
|
48
|
-
* Check if the element has an attribute with the given name.
|
|
49
|
-
*
|
|
50
|
-
* Spread attributes are taken into account: `<Comp {...{ disabled: true }} />`
|
|
51
|
-
* reports `true` for `"disabled"` (see {@link findAttribute}).
|
|
52
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
53
|
-
* @param element The `JSXElement` node to check.
|
|
54
|
-
* @param name The attribute name to look for (ex: "className").
|
|
55
|
-
* @returns `true` if the attribute is present on the element.
|
|
56
|
-
*/
|
|
57
|
-
export declare function hasAttribute(context: RuleContext, element: TSESTree.JSXElement, name: string): boolean;
|
|
58
|
-
/**
|
|
59
|
-
* Check if the element has at least one of the given attributes.
|
|
60
|
-
* @param context The ESLint rule context (needed for variable resolution in spread attributes).
|
|
61
|
-
* @param element The `JSXElement` node to check.
|
|
62
|
-
* @param names The attribute names to look for.
|
|
63
|
-
* @returns `true` if at least one of the attributes is present.
|
|
7
|
+
* A string is matched against the full attribute name (including a namespace);
|
|
8
|
+
* a function receives the attribute node.
|
|
64
9
|
*/
|
|
65
|
-
|
|
10
|
+
type AttributeTest = string | ((attribute: TSESTree.JSXAttribute) => boolean);
|
|
66
11
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
12
|
+
* The structural value of a direct JSX attribute.
|
|
13
|
+
*
|
|
14
|
+
* - `boolean`: the attribute has no value (ex: `<input disabled />`).
|
|
15
|
+
* - `literal`: the value is a literal.
|
|
16
|
+
* - `unknown`: a non-empty expression, normally in an expression container;
|
|
17
|
+
* the expression is not evaluated.
|
|
18
|
+
* - `element`: the value is a JSX element or fragment.
|
|
19
|
+
* - `missing`: no expression, normally an empty expression container
|
|
20
|
+
* (`{}` or `{/* comment *\/}`).
|
|
21
|
+
*
|
|
22
|
+
* Every variant carries the source `attribute`; `node` is `null` only for the
|
|
23
|
+
* `boolean` kind. This is plain data; use {@link getAttributeStaticValue} to
|
|
24
|
+
* collapse values to a plain JavaScript value.
|
|
25
|
+
* @remarks
|
|
26
|
+
* The `spreadChild` variant is omitted because it is not valid in attribute
|
|
27
|
+
* value position and is not produced by current parsers. The `spreadProps`
|
|
28
|
+
* variant is omitted because spread props are not resolved and direct attribute
|
|
29
|
+
* lookup never produces a spread descriptor.
|
|
72
30
|
*/
|
|
73
|
-
|
|
31
|
+
type AttributeValue = {
|
|
32
|
+
readonly kind: "boolean";
|
|
33
|
+
readonly attribute: TSESTree.JSXAttribute;
|
|
34
|
+
readonly node: null;
|
|
35
|
+
} | {
|
|
36
|
+
readonly kind: "literal";
|
|
37
|
+
readonly attribute: TSESTree.JSXAttribute;
|
|
38
|
+
readonly node: TSESTree.Literal;
|
|
39
|
+
} | {
|
|
40
|
+
readonly kind: "unknown";
|
|
41
|
+
readonly attribute: TSESTree.JSXAttribute;
|
|
42
|
+
readonly node: TSESTree.Expression;
|
|
43
|
+
} | {
|
|
44
|
+
readonly kind: "element";
|
|
45
|
+
readonly attribute: TSESTree.JSXAttribute;
|
|
46
|
+
readonly node: TSESTreeJSXElementLike;
|
|
47
|
+
} | {
|
|
48
|
+
readonly kind: "missing";
|
|
49
|
+
readonly attribute: TSESTree.JSXAttribute;
|
|
50
|
+
readonly node: TSESTree.JSXEmptyExpression;
|
|
51
|
+
};
|
|
74
52
|
/**
|
|
75
|
-
* Get the
|
|
53
|
+
* Get the full name of a `JSXAttribute` node.
|
|
76
54
|
*
|
|
77
55
|
* - `className` -> `"className"`
|
|
78
56
|
* - `aria-label` -> `"aria-label"`
|
|
@@ -82,171 +60,356 @@ export declare function hasEveryAttribute(context: RuleContext, element: TSESTre
|
|
|
82
60
|
*/
|
|
83
61
|
export declare function getAttributeName(node: TSESTree.JSXAttribute): string;
|
|
84
62
|
/**
|
|
85
|
-
* Check if the node is a `JSXAttribute` with the given name.
|
|
63
|
+
* Check if the node is a `JSXAttribute` with the given plain identifier name.
|
|
86
64
|
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
65
|
+
* Namespaced attributes (ex: `xml:space`) never match; use
|
|
66
|
+
* {@link isAttributeNamed} for full-name matching.
|
|
89
67
|
*
|
|
90
68
|
* Supports both data-first and data-last (curried) call styles:
|
|
91
|
-
* - `
|
|
92
|
-
* - `
|
|
69
|
+
* - `isPlainAttribute(node, "className")`
|
|
70
|
+
* - `isPlainAttribute("className")(node)`.
|
|
93
71
|
* @param node The node to check.
|
|
94
|
-
* @param name The attribute name to match (ex: "className").
|
|
95
|
-
* @returns `true` if the node is a `JSXAttribute`
|
|
72
|
+
* @param name The plain attribute name to match (ex: "className").
|
|
73
|
+
* @returns `true` if the node is a `JSXAttribute` whose identifier name is `name`.
|
|
74
|
+
* @remarks
|
|
75
|
+
* This is a name predicate, not a type guard: a `false` result does not mean
|
|
76
|
+
* the node is not a `JSXAttribute`, only that it does not match `name`.
|
|
96
77
|
*/
|
|
97
|
-
export declare const
|
|
98
|
-
(name: string): (node: TSESTree.Node) =>
|
|
99
|
-
(node: TSESTree.Node, name: string):
|
|
78
|
+
export declare const isPlainAttribute: {
|
|
79
|
+
(name: string): (node: TSESTree.Node | null | undefined) => boolean;
|
|
80
|
+
(node: TSESTree.Node | null | undefined, name: string): boolean;
|
|
100
81
|
};
|
|
101
82
|
/**
|
|
102
|
-
*
|
|
83
|
+
* Check if the node is a `JSXAttribute` with the given full name.
|
|
84
|
+
*
|
|
85
|
+
* The name includes the namespace when present, so `xml:space` can be matched.
|
|
86
|
+
* This is the name semantics used by {@link findAttribute} and the string form
|
|
87
|
+
* of {@link AttributeTest}.
|
|
103
88
|
*
|
|
104
|
-
*
|
|
105
|
-
* node
|
|
106
|
-
*
|
|
107
|
-
* `
|
|
108
|
-
*
|
|
89
|
+
* Supports both data-first and data-last (curried) call styles.
|
|
90
|
+
* @param node The node to check.
|
|
91
|
+
* @param name The full attribute name to match (ex: "className", "xml:space").
|
|
92
|
+
* @returns `true` if the node is a `JSXAttribute` whose full name is `name`.
|
|
93
|
+
* @remarks
|
|
94
|
+
* This is a name predicate, not a type guard: a `false` result does not mean
|
|
95
|
+
* the node is not a `JSXAttribute`, only that it does not match `name`.
|
|
109
96
|
*/
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
toStatic(): true;
|
|
114
|
-
} | {
|
|
115
|
-
readonly kind: "literal";
|
|
116
|
-
readonly node: TSESTree.Literal;
|
|
117
|
-
toStatic(): TSESTree.Literal["value"];
|
|
118
|
-
} | {
|
|
119
|
-
readonly kind: "unknown";
|
|
120
|
-
readonly node: TSESTree.Expression;
|
|
121
|
-
toStatic(): unknown;
|
|
122
|
-
} | {
|
|
123
|
-
readonly kind: "element";
|
|
124
|
-
readonly node: TSESTreeJSXElementLike;
|
|
125
|
-
toStatic(): undefined;
|
|
126
|
-
} | {
|
|
127
|
-
readonly kind: "missing";
|
|
128
|
-
readonly node: TSESTree.JSXEmptyExpression;
|
|
129
|
-
toStatic(): undefined;
|
|
130
|
-
} | {
|
|
131
|
-
readonly kind: "spreadChild";
|
|
132
|
-
readonly node: TSESTree.JSXSpreadChild;
|
|
133
|
-
toStatic(): undefined;
|
|
134
|
-
} | {
|
|
135
|
-
readonly kind: "spreadProps";
|
|
136
|
-
getProperty(name: string): unknown;
|
|
137
|
-
readonly node: TSESTree.JSXSpreadAttribute["argument"];
|
|
138
|
-
toStatic(): unknown;
|
|
97
|
+
export declare const isAttributeNamed: {
|
|
98
|
+
(name: string): (node: TSESTree.Node | null | undefined) => boolean;
|
|
99
|
+
(node: TSESTree.Node | null | undefined, name: string): boolean;
|
|
139
100
|
};
|
|
140
101
|
/**
|
|
141
|
-
*
|
|
102
|
+
* Find the last direct JSX attribute with the given full name.
|
|
142
103
|
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
* @param
|
|
147
|
-
* @param attribute
|
|
148
|
-
* @
|
|
104
|
+
* Later direct attributes take precedence, matching the order in which JSX
|
|
105
|
+
* props are written. Spread attributes are opaque: they are ignored by this lookup, and a
|
|
106
|
+
* spread after the returned attribute may still override it at runtime.
|
|
107
|
+
* @param element The `JSXElement` node to search.
|
|
108
|
+
* @param name The full attribute name to look for (ex: "className", "xml:space").
|
|
109
|
+
* @returns The last matching `JSXAttribute`, or `null` when not found.
|
|
110
|
+
*/
|
|
111
|
+
export declare function findAttribute(element: TSESTree.JSXElement, name: string): TSESTree.JSXAttribute | null;
|
|
112
|
+
/**
|
|
113
|
+
* Walk up the AST from `node` to find the nearest `JSXAttribute` ancestor,
|
|
114
|
+
* optionally matching a string name or a predicate.
|
|
115
|
+
*
|
|
116
|
+
* The search starts at `node.parent`; `node` itself is not considered. When
|
|
117
|
+
* `test` is omitted, the nearest `JSXAttribute` ancestor matches.
|
|
118
|
+
* @param node The starting node for the upward search; nullish values return `null`.
|
|
119
|
+
* @param test Optional test to filter candidate `JSXAttribute` nodes. A string
|
|
120
|
+
* matches the full attribute name (see {@link isAttributeNamed}); a function
|
|
121
|
+
* receives the candidate attribute.
|
|
122
|
+
* @returns The first matching `JSXAttribute` ancestor, or `null` when none is found.
|
|
123
|
+
*/
|
|
124
|
+
export declare function findParentAttribute(node: TSESTree.Node | null | undefined, test?: AttributeTest): TSESTree.JSXAttribute | null;
|
|
125
|
+
/**
|
|
126
|
+
* Check if the element has a direct attribute with the given full name.
|
|
127
|
+
*
|
|
128
|
+
* Spread attributes are ignored; only direct `JSXAttribute` nodes are checked.
|
|
129
|
+
* @param element The `JSXElement` node to check.
|
|
130
|
+
* @param name The full attribute name to look for (ex: "className", "xml:space").
|
|
131
|
+
* @returns `true` if the attribute is directly present on the element.
|
|
132
|
+
*/
|
|
133
|
+
export declare function hasAttribute(element: TSESTree.JSXElement, name: string): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Check if the element has at least one of the given direct attributes.
|
|
136
|
+
*
|
|
137
|
+
* Spread attributes are ignored; only direct `JSXAttribute` nodes are checked.
|
|
138
|
+
* @param element The `JSXElement` node to check.
|
|
139
|
+
* @param names The full attribute names to look for (namespaces included).
|
|
140
|
+
* @returns `true` if at least one direct attribute is present; `false` for an empty list.
|
|
141
|
+
*/
|
|
142
|
+
export declare function hasAnyAttribute(element: TSESTree.JSXElement, names: readonly string[]): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Check if the element has all of the given direct attributes.
|
|
145
|
+
*
|
|
146
|
+
* Spread attributes are ignored; only direct `JSXAttribute` nodes are checked.
|
|
147
|
+
* @param element The `JSXElement` node to check.
|
|
148
|
+
* @param names The full attribute names to look for (namespaces included).
|
|
149
|
+
* @returns `true` if every direct attribute is present; an empty list is vacuously `true`.
|
|
150
|
+
*/
|
|
151
|
+
export declare function hasEveryAttribute(element: TSESTree.JSXElement, names: readonly string[]): boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Get the structural value of a direct `JSXAttribute`.
|
|
154
|
+
*
|
|
155
|
+
* The attribute's value is classified syntactically; expressions are not
|
|
156
|
+
* evaluated. Only direct `JSXAttribute` nodes are accepted; spread attributes
|
|
157
|
+
* are outside this API.
|
|
158
|
+
* @param attribute The `JSXAttribute` node to get the value from.
|
|
149
159
|
* @returns A discriminated-union descriptor of the attribute's value.
|
|
160
|
+
* @remarks
|
|
161
|
+
* A `JSXSpreadChild` in attribute value position is not valid JSX and is not
|
|
162
|
+
* produced by current parsers, but it is part of the TSESTree union. It is
|
|
163
|
+
* treated as its inner expression, or as `missing` when that expression is empty.
|
|
150
164
|
*/
|
|
151
|
-
export declare function
|
|
165
|
+
export declare function getAttributeValue(attribute: TSESTree.JSXAttribute): AttributeValue;
|
|
152
166
|
/**
|
|
153
|
-
* Find
|
|
167
|
+
* Find the last direct attribute with the given full name and get its value.
|
|
154
168
|
*
|
|
155
|
-
* Convenience composition of {@link findAttribute} and {@link
|
|
156
|
-
*
|
|
169
|
+
* Convenience composition of {@link findAttribute} and {@link getAttributeValue}.
|
|
170
|
+
* Spread attributes are ignored; a spread after the matched attribute may still
|
|
171
|
+
* override it at runtime.
|
|
157
172
|
* @param element The `JSXElement` node to search.
|
|
158
|
-
* @param name The attribute name to look up (ex: "className").
|
|
159
|
-
* @returns An {@link AttributeValue} descriptor, or `
|
|
173
|
+
* @param name The full attribute name to look up (ex: "className", "xml:space").
|
|
174
|
+
* @returns An {@link AttributeValue} descriptor, or `null` when no matching direct attribute is present.
|
|
175
|
+
*/
|
|
176
|
+
export declare function getAttributeValueByName(element: TSESTree.JSXElement, name: string): AttributeValue | null;
|
|
177
|
+
/**
|
|
178
|
+
* Get the statically known value of an attribute value descriptor when possible.
|
|
179
|
+
*
|
|
180
|
+
* Boolean attributes resolve to `true`; literal attributes resolve to their
|
|
181
|
+
* literal value; other expressions are evaluated statically when possible.
|
|
182
|
+
* Returns `undefined` when the descriptor is null, is a JSX element or fragment,
|
|
183
|
+
* is a `missing` descriptor, cannot be statically evaluated, or evaluates to
|
|
184
|
+
* `undefined`. A statically known `undefined` is therefore indistinguishable
|
|
185
|
+
* from an indeterminate value.
|
|
186
|
+
* @param context The ESLint rule context (needed for scope look-ups).
|
|
187
|
+
* @param value The attribute value descriptor, or `null`.
|
|
188
|
+
* @returns The static value, or `undefined` when no statically known plain value is available.
|
|
160
189
|
*/
|
|
161
|
-
export declare function
|
|
190
|
+
export declare function getAttributeStaticValue(context: RuleContext, value: AttributeValue | null): unknown;
|
|
162
191
|
/**
|
|
163
|
-
* Find
|
|
192
|
+
* Find the last direct attribute with the given full name and get its static value.
|
|
164
193
|
*
|
|
165
|
-
* Returns `undefined`
|
|
166
|
-
*
|
|
194
|
+
* Returns `undefined` when no matching direct attribute is present, or when its
|
|
195
|
+
* value is a JSX element or fragment, is a `missing` descriptor, cannot be
|
|
196
|
+
* statically evaluated, or evaluates to `undefined`. A statically known
|
|
197
|
+
* `undefined` is likewise indistinguishable from an absent or indeterminate
|
|
198
|
+
* value; use {@link hasAttribute} when presence itself matters. Spread
|
|
199
|
+
* attributes are ignored; a spread after the matched attribute may still
|
|
200
|
+
* override it at runtime.
|
|
167
201
|
* @param context The ESLint rule context.
|
|
168
202
|
* @param element The `JSXElement` node to check.
|
|
169
|
-
* @param name The attribute name to look up (ex: "className").
|
|
203
|
+
* @param name The full attribute name to look up (ex: "className", "xml:space").
|
|
170
204
|
* @returns The static value of the attribute, or `undefined` when absent or indeterminate.
|
|
171
205
|
*/
|
|
172
|
-
export declare function
|
|
206
|
+
export declare function getAttributeStaticValueByName(context: RuleContext, element: TSESTree.JSXElement, name: string): unknown;
|
|
173
207
|
/**
|
|
174
|
-
*
|
|
208
|
+
* Check if the node is whitespace-only `JSXText`, based on its raw source text.
|
|
175
209
|
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
*
|
|
210
|
+
* Matches any `JSXText` node whose raw text is empty after trimming, regardless
|
|
211
|
+
* of newlines. Same-line whitespace matches here; {@link isPaddingWhitespace}
|
|
212
|
+
* additionally requires a line feed and uses the decoded value.
|
|
213
|
+
*
|
|
214
|
+
* Reads `node.raw` rather than the entity-decoded `value`, so entity text such
|
|
215
|
+
* as ` ` or ` ` is treated as content even when its decoded value is
|
|
216
|
+
* whitespace. This is a syntactic check on the source text, not a model of
|
|
217
|
+
* React's whitespace collapsing.
|
|
218
|
+
* @param node The node to check.
|
|
219
|
+
* @returns `true` if the node is whitespace-only `JSXText` in its raw form.
|
|
220
|
+
* @remarks
|
|
221
|
+
* This is a content predicate, not a type guard: a `false` result does not
|
|
222
|
+
* mean the node is not `JSXText`, only that it is not whitespace-only text.
|
|
223
|
+
*/
|
|
224
|
+
export declare function isWhitespaceText(node: TSESTree.Node | null | undefined): boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Check if the node is whitespace-only `JSXText` that this module treats as
|
|
227
|
+
* padding: it cleans to nothing (see {@link collapseMultilineText}) and contains
|
|
228
|
+
* a line feed (`\n`).
|
|
229
|
+
*
|
|
230
|
+
* Reads the entity-decoded `node.value`, so an entity-encoded line break such as
|
|
231
|
+
* ` ` matches even though the raw text contains no line break. For a
|
|
232
|
+
* whitespace check based on raw source text, see {@link isWhitespaceText}.
|
|
233
|
+
* @param node The node to check.
|
|
234
|
+
* @returns `true` if the node is purely formatting whitespace by its decoded value.
|
|
235
|
+
* @remarks
|
|
236
|
+
* This is a content predicate, not a type guard: a `false` result does not
|
|
237
|
+
* mean the node is not `JSXText`.
|
|
238
|
+
*/
|
|
239
|
+
export declare function isPaddingWhitespace(node: TSESTree.Node | null | undefined): boolean;
|
|
240
|
+
/**
|
|
241
|
+
* Check if the node is an empty expression container (`{}`, `{/* comment *\/}`).
|
|
242
|
+
*
|
|
243
|
+
* An empty `JSXExpressionContainer` holds no expression, so it produces no DOM
|
|
244
|
+
* node and is treated as non-rendered content, same as whitespace padding.
|
|
245
|
+
* @param node The node to check.
|
|
246
|
+
* @returns `true` if the node is an empty `JSXExpressionContainer`.
|
|
247
|
+
* @remarks
|
|
248
|
+
* This is a content predicate, not a type guard: a `false` result does not
|
|
249
|
+
* mean the node is not a `JSXExpressionContainer`.
|
|
250
|
+
*/
|
|
251
|
+
export declare function isEmptyExpressionContainer(node: TSESTree.Node | null | undefined): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Check if the node is an empty string literal expression (`{""}`).
|
|
254
|
+
*
|
|
255
|
+
* Only a literal empty string matches; template literals and other expressions
|
|
256
|
+
* that happen to evaluate to `""` are not evaluated by this predicate.
|
|
257
|
+
* @param node The node to check.
|
|
258
|
+
* @returns `true` if the node is an expression container holding an empty string literal.
|
|
259
|
+
* @remarks
|
|
260
|
+
* This is a content predicate, not a type guard: a `false` result does not
|
|
261
|
+
* mean the node is not a `JSXExpressionContainer`.
|
|
262
|
+
*/
|
|
263
|
+
export declare function isEmptyStringExpression(node: TSESTree.Node | null | undefined): boolean;
|
|
264
|
+
/**
|
|
265
|
+
* Check if the child carries content for the "meaningful children" semantics.
|
|
266
|
+
*
|
|
267
|
+
* Whitespace-only `JSXText` as determined by {@link isWhitespaceText} (which
|
|
268
|
+
* reads raw source text and therefore keeps entity text), empty expression
|
|
269
|
+
* containers, and empty string literal expressions are excluded. This is a
|
|
270
|
+
* syntactic check: `{null}`, `{false}`, and other dynamic expressions still
|
|
271
|
+
* count as children content.
|
|
272
|
+
* @param node The JSX child to check.
|
|
273
|
+
* @returns `true` if the child is not syntactically empty.
|
|
274
|
+
*/
|
|
275
|
+
export declare function isMeaningfulChild(node: TSESTree.JSXChild): boolean;
|
|
276
|
+
/**
|
|
277
|
+
* Get the children of a JSX element or fragment after removing syntactic JSX
|
|
278
|
+
* artifacts: padding whitespace (see {@link isPaddingWhitespace}), empty
|
|
279
|
+
* expression containers, and empty string literal expressions.
|
|
280
|
+
*
|
|
281
|
+
* Same-line whitespace is kept because it is not padding by this definition and
|
|
282
|
+
* can render as a text node. This is a syntactic filter, not a runtime-rendering
|
|
283
|
+
* guarantee: `{null}`, `{false}`, and other expressions that may render nothing
|
|
284
|
+
* are kept.
|
|
186
285
|
* @param element A `JSXElement` or `JSXFragment` node.
|
|
187
|
-
* @returns The children
|
|
286
|
+
* @returns The children that remain after removing padding artifacts.
|
|
188
287
|
*/
|
|
189
288
|
export declare function getChildren(element: TSESTreeJSXElementLike): TSESTree.JSXChild[];
|
|
190
289
|
/**
|
|
191
|
-
*
|
|
192
|
-
* is not purely whitespace text, an empty expression container (`{}`), or an
|
|
193
|
-
* empty string expression (`{""}`).
|
|
290
|
+
* Get the children of a JSX element or fragment that are not syntactically empty.
|
|
194
291
|
*
|
|
195
|
-
* Unlike {@link getChildren}
|
|
196
|
-
*
|
|
197
|
-
*
|
|
292
|
+
* Unlike {@link getChildren}, all whitespace-only `JSXText` as determined by
|
|
293
|
+
* {@link isWhitespaceText} is excluded, so `getMeaningfulChildren` is stricter
|
|
294
|
+
* for same-line whitespace (ex: `<div> </div>` yields no children here but one
|
|
295
|
+
* child from `getChildren`). Like `getChildren`, empty expression containers and
|
|
296
|
+
* empty string literal expressions are also excluded, and this is a syntactic
|
|
297
|
+
* check: `{null}`, `{false}`, and other dynamic expressions count as children.
|
|
198
298
|
* @param element A `JSXElement` or `JSXFragment` node.
|
|
199
|
-
* @returns
|
|
299
|
+
* @returns The children that are not syntactically empty.
|
|
200
300
|
*/
|
|
201
|
-
export declare function
|
|
301
|
+
export declare function getMeaningfulChildren(element: TSESTreeJSXElementLike): TSESTree.JSXChild[];
|
|
202
302
|
/**
|
|
203
|
-
*
|
|
303
|
+
* Check if the element has at least one syntactically non-empty child.
|
|
204
304
|
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
305
|
+
* A child counts when it is not whitespace-only `JSXText` as determined by
|
|
306
|
+
* {@link isWhitespaceText}, an empty expression container (`{}` or
|
|
307
|
+
* `{/* comment *\/}`), or an empty string literal expression (`{""}`). Same-line
|
|
308
|
+
* whitespace does not count, unlike in {@link getChildren}. `{null}`, `{false}`,
|
|
309
|
+
* and other dynamic expressions do count.
|
|
310
|
+
* @param element A `JSXElement` or `JSXFragment` node.
|
|
311
|
+
* @returns `true` if the element has at least one syntactically non-empty child.
|
|
208
312
|
*/
|
|
209
|
-
|
|
313
|
+
export declare function hasMeaningfulChildren(element: TSESTreeJSXElementLike): boolean;
|
|
210
314
|
/**
|
|
211
|
-
*
|
|
315
|
+
* A parsed `createElement` call.
|
|
212
316
|
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
* @returns `true` if the node is a matching JSX element.
|
|
317
|
+
* The raw positional argument fields (`typeArgument`, `propsArgument`, and
|
|
318
|
+
* `childrenArguments`) may contain `SpreadElement` nodes. `propsObject` is the
|
|
319
|
+
* only derived field: it is the second argument after unwrapping type/chain
|
|
320
|
+
* expressions, and is non-null only when that argument is syntactically an
|
|
321
|
+
* object literal.
|
|
219
322
|
*/
|
|
220
|
-
|
|
323
|
+
interface CreateElementCall {
|
|
324
|
+
/** The raw arguments after the props argument. */
|
|
325
|
+
readonly childrenArguments: readonly TSESTree.CallExpressionArgument[];
|
|
326
|
+
/** The call expression node. */
|
|
327
|
+
readonly node: TSESTree.CallExpression;
|
|
328
|
+
/** The raw second positional argument (the props argument), if present. */
|
|
329
|
+
readonly propsArgument: TSESTree.CallExpressionArgument | null;
|
|
330
|
+
/** The second argument unwrapped, when it is an object literal. */
|
|
331
|
+
readonly propsObject: TSESTree.ObjectExpression | null;
|
|
332
|
+
/** The raw first positional argument (the element type), if present. */
|
|
333
|
+
readonly typeArgument: TSESTree.CallExpressionArgument | null;
|
|
334
|
+
}
|
|
221
335
|
/**
|
|
222
|
-
* Check if the node is a
|
|
336
|
+
* Check if the node is a call to a `createElement` API.
|
|
223
337
|
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
338
|
+
* Matching is name-based and namespace-agnostic: the fully qualified callee name
|
|
339
|
+
* must be `createElement` or end with `.createElement`. No import provenance
|
|
340
|
+
* check is performed, matching `isAPICall("createElement")` in `@eslint-react/core`.
|
|
341
|
+
* @param context The ESLint rule context.
|
|
342
|
+
* @param node The node to inspect; nullish or non-call nodes return `false`.
|
|
343
|
+
* @returns `true` if the node is a `createElement` call.
|
|
344
|
+
* @remarks
|
|
345
|
+
* This is a call-name predicate, not a type guard: a `false` result does not
|
|
346
|
+
* mean the node is not a `CallExpression`, only that it is not a
|
|
347
|
+
* `createElement` call.
|
|
348
|
+
*/
|
|
349
|
+
export declare function isCreateElementCall(context: RuleContext, node: TSESTree.Node | null | undefined): boolean;
|
|
350
|
+
/**
|
|
351
|
+
* Parse a `createElement` call into its positional arguments.
|
|
229
352
|
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* @
|
|
236
|
-
* @param jsxFragmentFactory The configured fragment factory string (ex: "React.Fragment").
|
|
237
|
-
* @returns `true` if the node represents a React Fragment.
|
|
353
|
+
* A nullish or non-`createElement` node yields `null`; a matched call always
|
|
354
|
+
* yields a descriptor, even when it has no arguments. This removes the ambiguity
|
|
355
|
+
* between "not a `createElement` call" and "a call with no props/children".
|
|
356
|
+
* @param context The ESLint rule context.
|
|
357
|
+
* @param node The node to inspect.
|
|
358
|
+
* @returns The parsed call, or `null` when the node is not a `createElement` call.
|
|
238
359
|
*/
|
|
239
|
-
export declare function
|
|
360
|
+
export declare function parseCreateElementCall(context: RuleContext, node: TSESTree.Node | null | undefined): CreateElementCall | null;
|
|
240
361
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
362
|
+
* Find a statically named direct property in the props object of a `createElement` call.
|
|
363
|
+
*
|
|
364
|
+
* Properties are scanned in reverse so that the last direct property takes precedence.
|
|
365
|
+
* Spread properties are ignored rather than statically resolved; a spread after
|
|
366
|
+
* the returned property may still override it at runtime. Statically evaluable
|
|
367
|
+
* computed keys are matched.
|
|
368
|
+
*
|
|
369
|
+
* Returns `null` when the call is `null` or has no statically identifiable props
|
|
370
|
+
* object, that is, when the second argument is absent or is not an object
|
|
371
|
+
* literal after unwrapping type/chain expressions.
|
|
372
|
+
* @param context The ESLint rule context.
|
|
373
|
+
* @param call The parsed call, or `null`.
|
|
374
|
+
* @param name The property name to look for (ex: `"children"`, `"key"`).
|
|
375
|
+
* @returns The last matching `Property` node, or `null` when not found.
|
|
376
|
+
*/
|
|
377
|
+
export declare function findCreateElementProp(context: RuleContext, call: CreateElementCall | null, name: string): TSESTree.Property | null;
|
|
378
|
+
/**
|
|
379
|
+
* Check if the node itself is passed as a children argument (the third argument
|
|
380
|
+
* or later) of a `createElement` call. Contiguous TypeScript type-expression
|
|
381
|
+
* ancestors are skipped, and the node is unwrapped before the comparison;
|
|
382
|
+
* expressions nested inside an argument are not considered.
|
|
383
|
+
* @param context The ESLint rule context.
|
|
384
|
+
* @param node The node to check; nullish values return `false`.
|
|
385
|
+
* @returns `true` if the node is a direct children argument of a `createElement` call.
|
|
386
|
+
*/
|
|
387
|
+
export declare function isCreateElementChildrenArgument(context: RuleContext, node: TSESTree.Node | null | undefined): boolean;
|
|
388
|
+
/**
|
|
389
|
+
* Check if the node's closest `ObjectExpression` ancestor is the props object
|
|
390
|
+
* (the second argument) of its nearest enclosing `createElement` call. The
|
|
391
|
+
* second argument is unwrapped when it is a type or chain expression. Nodes that
|
|
392
|
+
* cross another object literal (ex: a value inside an object nested in the props
|
|
393
|
+
* object) are not considered inside.
|
|
394
|
+
* @param context The ESLint rule context.
|
|
395
|
+
* @param node The node to check; nullish values return `false`.
|
|
396
|
+
* @returns `true` if the node's closest object literal is `createElement`'s props object.
|
|
246
397
|
*/
|
|
247
|
-
export declare function
|
|
398
|
+
export declare function isInsideCreateElementProps(context: RuleContext, node: TSESTree.Node | null | undefined): boolean;
|
|
248
399
|
/**
|
|
249
|
-
*
|
|
400
|
+
* A test that determines whether a JSX element or fragment matches.
|
|
401
|
+
*
|
|
402
|
+
* - `string` matches the full element type (ex: "div", "React.Fragment"; a
|
|
403
|
+
* fragment has type `""`)
|
|
404
|
+
* - `readonly string[]` matches when the full element type equals any entry
|
|
405
|
+
* - `function` receives the full element type string and the element or fragment node.
|
|
406
|
+
*
|
|
407
|
+
* The function form receives `(elementType, node)`; this order is intentional:
|
|
408
|
+
* the element type is the primary subject of the test.
|
|
409
|
+
*/
|
|
410
|
+
type ElementTest = string | readonly string[] | ((elementType: string, node: TSESTreeJSXElementLike) => boolean);
|
|
411
|
+
/**
|
|
412
|
+
* Get the string representation of a JSX element or fragment's type.
|
|
250
413
|
*
|
|
251
414
|
* - `<div>` -> `"div"`
|
|
252
415
|
* - `<Foo.Bar>` -> `"Foo.Bar"`
|
|
@@ -258,64 +421,87 @@ export declare function isHostElement(node: TSESTree.Node): node is TSESTree.JSX
|
|
|
258
421
|
*/
|
|
259
422
|
export declare function getElementFullType(node: TSESTreeJSXElementLike): string;
|
|
260
423
|
/**
|
|
261
|
-
* Get the self name (last dot-separated segment) of a JSX element type.
|
|
424
|
+
* Get the self name (last dot-separated segment) of a JSX element or fragment type.
|
|
262
425
|
*
|
|
263
426
|
* - `<Foo.Bar.Baz>` -> `"Baz"`
|
|
264
427
|
* - `<div>` -> `"div"`
|
|
428
|
+
* - `<xml:space>` -> `"xml:space"` (only `.` separates segments, not `:`)
|
|
265
429
|
* - `<></>` -> `""`
|
|
266
430
|
* @param node The `JSXElement` or `JSXFragment` node.
|
|
267
|
-
* @returns The last segment of the element type, or `""` for fragments.
|
|
431
|
+
* @returns The last dot-separated segment of the element type, or `""` for fragments.
|
|
268
432
|
*/
|
|
269
433
|
export declare function getElementSelfType(node: TSESTreeJSXElementLike): string;
|
|
270
434
|
/**
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
* 1. Split the raw text into lines.
|
|
275
|
-
* 2. Find the last non-empty line.
|
|
276
|
-
* 3. Collapse tabs into spaces.
|
|
277
|
-
* 4. Trim leading spaces on non-first lines and trailing spaces on non-last lines.
|
|
278
|
-
* 5. Append a single space after each non-last non-empty line.
|
|
279
|
-
* @param text The raw JSX text string to collapse.
|
|
280
|
-
* @returns The collapsed string, or `null` when the text contains only whitespace.
|
|
281
|
-
* @see https://github.com/babel/babel/blob/main/packages/babel-types/src/utils/react/cleanJSXElementLiteralChild.ts
|
|
435
|
+
* Check if the node is a `JSXElement` or `JSXFragment`.
|
|
436
|
+
* @param node The node to check.
|
|
437
|
+
* @returns `true` if the node is a JSX element or fragment.
|
|
282
438
|
*/
|
|
283
|
-
export declare function
|
|
439
|
+
export declare function isElement(node: TSESTree.Node | null | undefined): node is TSESTreeJSXElementLike;
|
|
284
440
|
/**
|
|
285
|
-
* Check if the node is
|
|
286
|
-
* rendering, that is, a `JSXText` node that cleans to nothing (see
|
|
287
|
-
* {@link collapseMultilineText}) and contains a newline.
|
|
288
|
-
*
|
|
289
|
-
* For the looser "any whitespace-only text" check, see {@link isWhitespaceText}.
|
|
441
|
+
* Check if the node is a `JSXElement` or `JSXFragment` matching the given test.
|
|
290
442
|
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
* @param
|
|
295
|
-
* @returns `true` if the node is
|
|
443
|
+
* Modelled after [`hast-util-is-element`](https://github.com/syntax-tree/hast-util-is-element):
|
|
444
|
+
* the `test` parameter controls what counts as a match.
|
|
445
|
+
* @param node The node to check.
|
|
446
|
+
* @param test Test to match the full element type against.
|
|
447
|
+
* @returns `true` if the node is a matching JSX element or fragment.
|
|
448
|
+
* @remarks
|
|
449
|
+
* This is a match predicate, not a type guard: a `false` result does not mean
|
|
450
|
+
* the node is not a JSX element or fragment, only that it does not match the test.
|
|
296
451
|
*/
|
|
297
|
-
export declare function
|
|
452
|
+
export declare function isElement(node: TSESTree.Node | null | undefined, test: ElementTest): boolean;
|
|
298
453
|
/**
|
|
299
|
-
* Check if the node is
|
|
454
|
+
* Check if the node is a React Fragment element.
|
|
300
455
|
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
456
|
+
* Recognizes the shorthand `<>...</>` syntax (`JSXFragment`) and the explicit
|
|
457
|
+
* `<Fragment>` / `<React.Fragment>` form (`JSXElement`). Matching compares only
|
|
458
|
+
* the last dot-separated segment, so with the usual `"React.Fragment"` factory
|
|
459
|
+
* both `<React.Fragment>` and `<Fragment>` match.
|
|
303
460
|
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
461
|
+
* The factory is a required parameter (no implicit `"React.Fragment"` default) so
|
|
462
|
+
* that callers always pass the project-configured pragma — see `getJSXConfig` in
|
|
463
|
+
* `@eslint-react/core` — and cannot silently use the wrong factory under a custom
|
|
464
|
+
* JSX runtime (ex: Preact). Note the self-name comparison is a heuristic: any
|
|
465
|
+
* `<*.Fragment>` member expression matches, regardless of the qualifier.
|
|
466
|
+
* @param node The node to check.
|
|
467
|
+
* @param jsxFragmentFactory The configured fragment factory string (ex: "React.Fragment");
|
|
468
|
+
* only its last dot-separated segment is compared.
|
|
469
|
+
* @returns `true` if the node is a `JSXFragment` or its element type's self name matches the configured fragment self name.
|
|
470
|
+
* @remarks
|
|
471
|
+
* This is a match predicate, not a type guard: a `false` result does not mean
|
|
472
|
+
* the node is not a JSX element or fragment.
|
|
473
|
+
*/
|
|
474
|
+
export declare function isFragmentElement(node: TSESTree.Node | null | undefined, jsxFragmentFactory: string): boolean;
|
|
475
|
+
/**
|
|
476
|
+
* Check if the node is a host (intrinsic / DOM) element, that is, a `JSXElement`
|
|
477
|
+
* whose tag name starts with an ASCII lowercase letter (ex: `<div>` vs
|
|
478
|
+
* `<MyComponent>`). For namespaced names, the local part is checked (ex: `rect`
|
|
479
|
+
* in `<svg:rect>`). Member-expression names (ex: `<foo.Bar>`) are not host elements.
|
|
480
|
+
* @param node The node to check.
|
|
481
|
+
* @returns `true` if the node is a `JSXElement` with a plain or namespaced tag
|
|
482
|
+
* name starting with an ASCII lowercase letter.
|
|
483
|
+
* @remarks
|
|
484
|
+
* This is a match predicate, not a type guard: a `false` result does not mean
|
|
485
|
+
* the node is not a `JSXElement`; custom components are also JSX elements.
|
|
309
486
|
*/
|
|
310
|
-
export declare function
|
|
487
|
+
export declare function isHostElement(node: TSESTree.Node | null | undefined): boolean;
|
|
311
488
|
/**
|
|
312
|
-
*
|
|
489
|
+
* Collapse a JSX text string using Babel's `cleanJSXElementLiteralChild`
|
|
490
|
+
* whitespace rules.
|
|
313
491
|
*
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
492
|
+
* 1. Split the input on CRLF, LF, or CR.
|
|
493
|
+
* 2. Track the last line containing a character other than a space or tab,
|
|
494
|
+
* defaulting to the first line when there is none.
|
|
495
|
+
* 3. Replace each tab with a space.
|
|
496
|
+
* 4. Remove leading spaces from non-first lines and trailing spaces from
|
|
497
|
+
* non-last physical lines.
|
|
498
|
+
* 5. Append a single space after each line that still has content, except the
|
|
499
|
+
* last tracked line.
|
|
500
|
+
* @param text The JSX text string to collapse (typically a decoded `JSXText.value`).
|
|
501
|
+
* @returns The collapsed string, or `null` when the collapsed result is empty
|
|
502
|
+
* (for example, for empty input or multiline input containing only spaces, tabs,
|
|
503
|
+
* and line breaks).
|
|
504
|
+
* @see https://github.com/babel/babel/blob/main/packages/babel-types/src/utils/react/cleanJSXElementLiteralChild.ts
|
|
319
505
|
*/
|
|
320
|
-
export declare function
|
|
321
|
-
export type { AttributeValue, ElementTest };
|
|
506
|
+
export declare function collapseMultilineText(text: string): string | null;
|
|
507
|
+
export type { AttributeTest, AttributeValue, CreateElementCall, ElementTest };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Check, Traverse } from "@eslint-react/ast";
|
|
2
|
-
import { resolve } from "@eslint-react/var";
|
|
1
|
+
import { Check, Extract, Traverse } from "@eslint-react/ast";
|
|
3
2
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
4
3
|
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
5
|
-
import {
|
|
4
|
+
import { getText } from "@eslint-react/eslint";
|
|
6
5
|
// ../../.pkgs/eff/dist/index.js
|
|
7
6
|
const pipeArguments = (self, args) => {
|
|
8
7
|
switch (args.length) {
|
|
@@ -67,12 +66,15 @@ const absurd = (_) => {
|
|
|
67
66
|
};
|
|
68
67
|
cast(absurd);
|
|
69
68
|
dual(2, (a, b) => (data) => a(data) && b(data));
|
|
70
|
-
|
|
69
|
+
dual(2, (a, b) => (data) => a(data) || b(data));
|
|
71
70
|
dual(2, (a, b) => (data) => a(data) !== b(data));
|
|
72
71
|
dual(2, (a, b) => (data) => a(data) === b(data));
|
|
73
72
|
dual(2, (antecedent, consequent) => (data) => !antecedent(data) || consequent(data));
|
|
74
73
|
dual(2, (a, b) => (data) => !a(data) && !b(data));
|
|
75
74
|
dual(2, (a, b) => (data) => !a(data) || !b(data));
|
|
75
|
+
function isString(input) {
|
|
76
|
+
return typeof input === "string";
|
|
77
|
+
}
|
|
76
78
|
function isFunction(input) {
|
|
77
79
|
return typeof input === "function";
|
|
78
80
|
}
|
|
@@ -94,148 +96,105 @@ dual(2, (self, predicate) => {
|
|
|
94
96
|
while (idx < len && predicate(input[idx], idx)) idx++;
|
|
95
97
|
return input.slice(0, idx);
|
|
96
98
|
});
|
|
97
|
-
// src/attribute
|
|
99
|
+
// src/attribute.ts
|
|
98
100
|
function getAttributeName(node) {
|
|
99
|
-
|
|
100
|
-
return node.name.namespace.name + ":" + node.name.name.name;
|
|
101
|
+
return Extract.getJSXName(node.name);
|
|
101
102
|
}
|
|
102
|
-
const
|
|
103
|
-
return node
|
|
103
|
+
const isPlainAttribute = dual(2, (node, name) => {
|
|
104
|
+
return node?.type === AST_NODE_TYPES.JSXAttribute && node.name.type === AST_NODE_TYPES.JSXIdentifier && node.name.name === name;
|
|
104
105
|
});
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const guard = (n) => {
|
|
114
|
-
return n.type === AST_NODE_TYPES.JSXAttribute && test(n);
|
|
115
|
-
};
|
|
116
|
-
return Traverse.findParent(node, guard) ?? void 0;
|
|
117
|
-
}
|
|
118
|
-
function findSpreadProperty(context, argument, name, seen = /* @__PURE__ */ new Set()) {
|
|
119
|
-
let objectExpression;
|
|
120
|
-
if (Check.isIdentifier(argument)) {
|
|
121
|
-
let initNode = resolve(context, argument);
|
|
122
|
-
while (initNode != null && Check.isIdentifier(initNode) && !seen.has(initNode)) {
|
|
123
|
-
seen.add(initNode);
|
|
124
|
-
initNode = resolve(context, initNode);
|
|
125
|
-
}
|
|
126
|
-
if (initNode?.type === AST_NODE_TYPES.ObjectExpression) objectExpression = initNode;
|
|
127
|
-
} else if (argument.type === AST_NODE_TYPES.ObjectExpression) objectExpression = argument;
|
|
128
|
-
if (objectExpression == null || seen.has(objectExpression)) return void 0;
|
|
129
|
-
seen.add(objectExpression);
|
|
130
|
-
const { properties } = objectExpression;
|
|
131
|
-
for (let i = properties.length - 1; i >= 0; i--) {
|
|
132
|
-
const property = properties[i];
|
|
133
|
-
if (property == null) continue;
|
|
134
|
-
if (property.type === AST_NODE_TYPES.Property) {
|
|
135
|
-
const { key } = property;
|
|
136
|
-
if (property.computed) {
|
|
137
|
-
const keyScope = context.sourceCode.getScope(key);
|
|
138
|
-
if (getStaticValue(key, keyScope)?.value === name) return property;
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
if (Check.isIdentifier(key, name)) return property;
|
|
142
|
-
if (key.type === AST_NODE_TYPES.Literal && key.value === name) return property;
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
const found = findSpreadProperty(context, property.argument, name, seen);
|
|
146
|
-
if (found != null) return found;
|
|
106
|
+
const isAttributeNamed = dual(2, (node, name) => {
|
|
107
|
+
return node?.type === AST_NODE_TYPES.JSXAttribute && getAttributeName(node) === name;
|
|
108
|
+
});
|
|
109
|
+
function findAttribute(element, name) {
|
|
110
|
+
const { attributes } = element.openingElement;
|
|
111
|
+
for (let i = attributes.length - 1; i >= 0; i--) {
|
|
112
|
+
const attribute = attributes[i];
|
|
113
|
+
if (attribute?.type === AST_NODE_TYPES.JSXAttribute && isAttributeNamed(attribute, name)) return attribute;
|
|
147
114
|
}
|
|
115
|
+
return null;
|
|
148
116
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
function hasEveryAttribute(context, element, names) {
|
|
157
|
-
return names.every((name) => findAttribute(context, element, name) != null);
|
|
117
|
+
function findParentAttribute(node, test) {
|
|
118
|
+
if (node == null) return null;
|
|
119
|
+
const predicate = test == null ? () => true : typeof test === "function" ? test : isAttributeNamed(test);
|
|
120
|
+
const guard = (n) => {
|
|
121
|
+
return n.type === AST_NODE_TYPES.JSXAttribute && predicate(n);
|
|
122
|
+
};
|
|
123
|
+
return Traverse.findParent(node, guard);
|
|
158
124
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
if (attribute.type === AST_NODE_TYPES.JSXAttribute) return resolveJSXAttribute(context, attribute);
|
|
162
|
-
return resolveJSXSpreadAttribute(context, attribute, name);
|
|
125
|
+
function hasAttribute(element, name) {
|
|
126
|
+
return findAttribute(element, name) != null;
|
|
163
127
|
}
|
|
164
|
-
function
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return resolveAttributeValue(context, attr, name);
|
|
128
|
+
function hasAnyAttribute(element, names) {
|
|
129
|
+
if (names.length === 0) return false;
|
|
130
|
+
return element.openingElement.attributes.some((a) => a.type === AST_NODE_TYPES.JSXAttribute && names.includes(getAttributeName(a)));
|
|
168
131
|
}
|
|
169
|
-
function
|
|
170
|
-
|
|
132
|
+
function hasEveryAttribute(element, names) {
|
|
133
|
+
if (names.length === 0) return true;
|
|
134
|
+
const remaining = new Set(names);
|
|
135
|
+
for (const attribute of element.openingElement.attributes) {
|
|
136
|
+
if (attribute.type === AST_NODE_TYPES.JSXAttribute) remaining.delete(getAttributeName(attribute));
|
|
137
|
+
if (remaining.size === 0) return true;
|
|
138
|
+
}
|
|
139
|
+
return false;
|
|
171
140
|
}
|
|
172
|
-
function
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
141
|
+
function getAttributeValue(attribute) {
|
|
142
|
+
const { value } = attribute;
|
|
143
|
+
if (value == null) return {
|
|
175
144
|
kind: "boolean",
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
145
|
+
attribute,
|
|
146
|
+
node: null
|
|
180
147
|
};
|
|
181
|
-
switch (
|
|
182
|
-
case AST_NODE_TYPES.Literal: {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
case AST_NODE_TYPES.JSXExpressionContainer: {
|
|
193
|
-
const expr = node.value.expression;
|
|
194
|
-
if (expr.type === AST_NODE_TYPES.JSXEmptyExpression) return {
|
|
148
|
+
switch (value.type) {
|
|
149
|
+
case AST_NODE_TYPES.Literal: return {
|
|
150
|
+
kind: "literal",
|
|
151
|
+
attribute,
|
|
152
|
+
node: value
|
|
153
|
+
};
|
|
154
|
+
case AST_NODE_TYPES.JSXExpressionContainer:
|
|
155
|
+
if (value.expression.type === AST_NODE_TYPES.JSXEmptyExpression) return {
|
|
195
156
|
kind: "missing",
|
|
196
|
-
|
|
197
|
-
|
|
157
|
+
attribute,
|
|
158
|
+
node: value.expression
|
|
198
159
|
};
|
|
199
160
|
return {
|
|
200
161
|
kind: "unknown",
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return getStaticValue(expr, scope)?.value;
|
|
204
|
-
}
|
|
162
|
+
attribute,
|
|
163
|
+
node: value.expression
|
|
205
164
|
};
|
|
206
|
-
|
|
207
|
-
case AST_NODE_TYPES.JSXElement: return {
|
|
208
|
-
kind: "element",
|
|
209
|
-
node: node.value,
|
|
210
|
-
toStatic() {}
|
|
211
|
-
};
|
|
165
|
+
case AST_NODE_TYPES.JSXElement:
|
|
212
166
|
case AST_NODE_TYPES.JSXFragment: return {
|
|
213
167
|
kind: "element",
|
|
214
|
-
|
|
215
|
-
|
|
168
|
+
attribute,
|
|
169
|
+
node: value
|
|
216
170
|
};
|
|
217
|
-
case AST_NODE_TYPES.JSXSpreadChild: return {
|
|
218
|
-
kind: "
|
|
219
|
-
|
|
220
|
-
|
|
171
|
+
case AST_NODE_TYPES.JSXSpreadChild: return value.expression.type === AST_NODE_TYPES.JSXEmptyExpression ? {
|
|
172
|
+
kind: "missing",
|
|
173
|
+
attribute,
|
|
174
|
+
node: value.expression
|
|
175
|
+
} : {
|
|
176
|
+
kind: "unknown",
|
|
177
|
+
attribute,
|
|
178
|
+
node: value.expression
|
|
221
179
|
};
|
|
222
180
|
}
|
|
223
181
|
}
|
|
224
|
-
function
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
182
|
+
function getAttributeValueByName(element, name) {
|
|
183
|
+
const attribute = findAttribute(element, name);
|
|
184
|
+
return attribute == null ? null : getAttributeValue(attribute);
|
|
185
|
+
}
|
|
186
|
+
function getAttributeStaticValue(context, value) {
|
|
187
|
+
if (value == null) return void 0;
|
|
188
|
+
switch (value.kind) {
|
|
189
|
+
case "boolean": return true;
|
|
190
|
+
case "literal": return value.node.value;
|
|
191
|
+
case "unknown": return getStaticValue(value.node, context.sourceCode.getScope(value.node))?.value;
|
|
192
|
+
case "element":
|
|
193
|
+
case "missing": return;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function getAttributeStaticValueByName(context, element, name) {
|
|
197
|
+
return getAttributeStaticValue(context, getAttributeValueByName(element, name));
|
|
239
198
|
}
|
|
240
199
|
// src/text.ts
|
|
241
200
|
function collapseMultilineText(text) {
|
|
@@ -258,73 +217,115 @@ function collapseMultilineText(text) {
|
|
|
258
217
|
}
|
|
259
218
|
return str === "" ? null : str;
|
|
260
219
|
}
|
|
261
|
-
|
|
262
|
-
if (node.type !== AST_NODE_TYPES.JSXText) return false;
|
|
263
|
-
return collapseMultilineText(node.value) == null && node.value.includes("\n");
|
|
264
|
-
}
|
|
220
|
+
// src/children.ts
|
|
265
221
|
function isWhitespaceText(node) {
|
|
266
|
-
|
|
267
|
-
|
|
222
|
+
return node?.type === AST_NODE_TYPES.JSXText && node.raw.trim() === "";
|
|
223
|
+
}
|
|
224
|
+
function isPaddingWhitespace(node) {
|
|
225
|
+
return node?.type === AST_NODE_TYPES.JSXText && collapseMultilineText(node.value) == null && node.value.includes("\n");
|
|
268
226
|
}
|
|
269
227
|
function isEmptyExpressionContainer(node) {
|
|
270
|
-
|
|
271
|
-
return node.expression.type === AST_NODE_TYPES.JSXEmptyExpression;
|
|
228
|
+
return node?.type === AST_NODE_TYPES.JSXExpressionContainer && node.expression.type === AST_NODE_TYPES.JSXEmptyExpression;
|
|
272
229
|
}
|
|
273
230
|
function isEmptyStringExpression(node) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
return
|
|
231
|
+
return node?.type === AST_NODE_TYPES.JSXExpressionContainer && node.expression.type === AST_NODE_TYPES.Literal && node.expression.value === "";
|
|
232
|
+
}
|
|
233
|
+
function isMeaningfulChild(node) {
|
|
234
|
+
return !isWhitespaceText(node) && !isEmptyExpressionContainer(node) && !isEmptyStringExpression(node);
|
|
278
235
|
}
|
|
279
|
-
// src/children.ts
|
|
280
236
|
function getChildren(element) {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
237
|
+
return element.children.filter((child) => !isPaddingWhitespace(child) && !isEmptyExpressionContainer(child) && !isEmptyStringExpression(child));
|
|
238
|
+
}
|
|
239
|
+
function getMeaningfulChildren(element) {
|
|
240
|
+
return element.children.filter(isMeaningfulChild);
|
|
241
|
+
}
|
|
242
|
+
function hasMeaningfulChildren(element) {
|
|
243
|
+
return element.children.some(isMeaningfulChild);
|
|
244
|
+
}
|
|
245
|
+
// src/create-element.ts
|
|
246
|
+
function isCreateElementCall(context, node) {
|
|
247
|
+
if (node?.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
248
|
+
const name = Extract.getFullyQualifiedName(Extract.unwrap(node.callee), getText(context));
|
|
249
|
+
return name === "createElement" || name.endsWith(".createElement");
|
|
250
|
+
}
|
|
251
|
+
function parseCreateElementCall(context, node) {
|
|
252
|
+
if (node?.type !== AST_NODE_TYPES.CallExpression || !isCreateElementCall(context, node)) return null;
|
|
253
|
+
const propsArgument = node.arguments[1] ?? null;
|
|
254
|
+
const propsExpression = propsArgument == null ? null : Extract.unwrap(propsArgument);
|
|
255
|
+
return {
|
|
256
|
+
childrenArguments: node.arguments.slice(2),
|
|
257
|
+
node,
|
|
258
|
+
propsArgument,
|
|
259
|
+
propsObject: propsExpression?.type === AST_NODE_TYPES.ObjectExpression ? propsExpression : null,
|
|
260
|
+
typeArgument: node.arguments[0] ?? null
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
function getStaticPropertyName(context, property) {
|
|
264
|
+
if (!property.computed) return Extract.getPropertyName(property, "max");
|
|
265
|
+
const key = Extract.unwrap(property.key);
|
|
266
|
+
const staticValue = getStaticValue(key, context.sourceCode.getScope(key));
|
|
267
|
+
return typeof staticValue?.value === "string" ? staticValue.value : null;
|
|
268
|
+
}
|
|
269
|
+
function findCreateElementProp(context, call, name) {
|
|
270
|
+
if (call?.propsObject == null) return null;
|
|
271
|
+
const { properties } = call.propsObject;
|
|
272
|
+
for (let i = properties.length - 1; i >= 0; i--) {
|
|
273
|
+
const property = properties[i];
|
|
274
|
+
if (property?.type === AST_NODE_TYPES.Property && getStaticPropertyName(context, property) === name) return property;
|
|
287
275
|
}
|
|
288
|
-
return
|
|
276
|
+
return null;
|
|
289
277
|
}
|
|
290
|
-
function
|
|
291
|
-
if (
|
|
292
|
-
|
|
278
|
+
function isCreateElementChildrenArgument(context, node) {
|
|
279
|
+
if (node == null) return false;
|
|
280
|
+
let parent = node.parent;
|
|
281
|
+
while (Check.isTypeExpression(parent)) parent = parent.parent;
|
|
282
|
+
const target = Extract.unwrap(node);
|
|
283
|
+
return parent?.type === AST_NODE_TYPES.CallExpression && isCreateElementCall(context, parent) && parent.arguments.slice(2).some((arg) => Extract.unwrap(arg) === target);
|
|
284
|
+
}
|
|
285
|
+
function isInsideCreateElementProps(context, node) {
|
|
286
|
+
if (node == null) return false;
|
|
287
|
+
const call = Traverse.findParent(node, (parent) => isCreateElementCall(context, parent));
|
|
288
|
+
if (call == null) return false;
|
|
289
|
+
const prop = Traverse.findParent(node, Check.isObjectExpression);
|
|
290
|
+
if (prop == null) return false;
|
|
291
|
+
const propsArg = call.arguments[1];
|
|
292
|
+
if (propsArg == null) return false;
|
|
293
|
+
return prop === Extract.unwrap(propsArg);
|
|
293
294
|
}
|
|
294
|
-
// src/element
|
|
295
|
+
// src/element.ts
|
|
295
296
|
function getElementFullType(node) {
|
|
296
297
|
if (node.type === AST_NODE_TYPES.JSXFragment) return "";
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
default: return getQualifiedName(node.object) + "." + getQualifiedName(node.property);
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
return getQualifiedName(node.openingElement.name);
|
|
298
|
+
return Extract.getJSXName(node.openingElement.name);
|
|
299
|
+
}
|
|
300
|
+
function getSelfName(name) {
|
|
301
|
+
return name.slice(name.lastIndexOf(".") + 1);
|
|
305
302
|
}
|
|
306
303
|
function getElementSelfType(node) {
|
|
307
|
-
return getElementFullType(node)
|
|
304
|
+
return getSelfName(getElementFullType(node));
|
|
305
|
+
}
|
|
306
|
+
function isElementTestFunction(test) {
|
|
307
|
+
return isFunction(test);
|
|
308
308
|
}
|
|
309
|
-
// src/element-is.ts
|
|
310
309
|
function isElement(node, test) {
|
|
311
310
|
if (node == null) return false;
|
|
312
311
|
if (node.type !== AST_NODE_TYPES.JSXElement && node.type !== AST_NODE_TYPES.JSXFragment) return false;
|
|
313
312
|
if (test == null) return true;
|
|
314
313
|
const type = getElementFullType(node);
|
|
315
|
-
|
|
314
|
+
if (isString(test)) return type === test;
|
|
315
|
+
if (isElementTestFunction(test)) return test(type, node);
|
|
316
|
+
return test.includes(type);
|
|
316
317
|
}
|
|
317
318
|
function isFragmentElement(node, jsxFragmentFactory) {
|
|
319
|
+
if (node == null) return false;
|
|
318
320
|
if (node.type === AST_NODE_TYPES.JSXFragment) return true;
|
|
319
321
|
if (node.type !== AST_NODE_TYPES.JSXElement) return false;
|
|
320
|
-
|
|
321
|
-
return getElementFullType(node).split(".").at(-1) === fragment;
|
|
322
|
+
return getElementSelfType(node) === getSelfName(jsxFragmentFactory);
|
|
322
323
|
}
|
|
323
324
|
function isHostElement(node) {
|
|
324
|
-
if (node
|
|
325
|
+
if (node?.type !== AST_NODE_TYPES.JSXElement) return false;
|
|
325
326
|
const name = node.openingElement.name;
|
|
326
327
|
if (name.type === AST_NODE_TYPES.JSXIdentifier) return /^[a-z]/u.test(name.name);
|
|
327
328
|
if (name.type === AST_NODE_TYPES.JSXNamespacedName) return /^[a-z]/u.test(name.name.name);
|
|
328
329
|
return false;
|
|
329
330
|
}
|
|
330
|
-
export { collapseMultilineText, findAttribute,
|
|
331
|
+
export { collapseMultilineText, findAttribute, findCreateElementProp, findParentAttribute, getAttributeName, getAttributeStaticValue, getAttributeStaticValueByName, getAttributeValue, getAttributeValueByName, getChildren, getElementFullType, getElementSelfType, getMeaningfulChildren, hasAnyAttribute, hasAttribute, hasEveryAttribute, hasMeaningfulChildren, isAttributeNamed, isCreateElementCall, isCreateElementChildrenArgument, isElement, isEmptyExpressionContainer, isEmptyStringExpression, isFragmentElement, isHostElement, isInsideCreateElementProps, isMeaningfulChild, isPaddingWhitespace, isPlainAttribute, isWhitespaceText, parseCreateElementCall };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/jsx",
|
|
3
|
-
"version": "5.30.0-beta.
|
|
3
|
+
"version": "5.30.0-beta.10",
|
|
4
4
|
"description": "ESLint React's TSESTree JSX utility module for static analysis of JSX patterns.",
|
|
5
5
|
"homepage": "https://github.com/Rel1cx/eslint-react",
|
|
6
6
|
"bugs": {
|
|
@@ -29,12 +29,10 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eslint-react/ast": "5.30.0-beta.
|
|
33
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
34
|
-
"@eslint
|
|
35
|
-
"@typescript-eslint/
|
|
36
|
-
"@typescript-eslint/utils": "^8.72.0",
|
|
37
|
-
"ts-pattern": "^5.9.2"
|
|
32
|
+
"@eslint-react/ast": "5.30.0-beta.10",
|
|
33
|
+
"@eslint-react/eslint": "5.30.0-beta.10",
|
|
34
|
+
"@typescript-eslint/types": "^8.72.1",
|
|
35
|
+
"@typescript-eslint/utils": "^8.72.1"
|
|
38
36
|
},
|
|
39
37
|
"devDependencies": {
|
|
40
38
|
"@local/configs": "0.0.0",
|