@eslint-react/jsx 5.28.8 → 5.30.0-beta.1
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 +61 -16
- package/dist/index.js +53 -20
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { TSESTreeJSXAttributeLike, TSESTreeJSXElementLike } from "@eslint-react/ast";
|
|
2
2
|
import { TSESTree } from "@typescript-eslint/types";
|
|
3
3
|
import { RuleContext } from "@eslint-react/eslint";
|
|
4
|
-
//#region src/attribute-find.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Find a JSX attribute (or a spread attribute containing the property) by name.
|
|
7
6
|
*
|
|
@@ -45,8 +44,6 @@ export declare function findParentAttribute(node: TSESTree.Node, test?: (node: T
|
|
|
45
44
|
* @returns The matching `Property` node, or `undefined` when the key is not found.
|
|
46
45
|
*/
|
|
47
46
|
export declare function findSpreadProperty(context: RuleContext, argument: TSESTree.Expression, name: string, seen?: Set<TSESTree.Node>): TSESTree.Property | undefined;
|
|
48
|
-
//#endregion
|
|
49
|
-
//#region src/attribute-has.d.ts
|
|
50
47
|
/**
|
|
51
48
|
* Check if the element has an attribute with the given name.
|
|
52
49
|
*
|
|
@@ -74,8 +71,6 @@ export declare function hasAnyAttribute(context: RuleContext, element: TSESTree.
|
|
|
74
71
|
* @returns `true` if every attribute is present on the element.
|
|
75
72
|
*/
|
|
76
73
|
export declare function hasEveryAttribute(context: RuleContext, element: TSESTree.JSXElement, names: string[]): boolean;
|
|
77
|
-
//#endregion
|
|
78
|
-
//#region src/attribute-name.d.ts
|
|
79
74
|
/**
|
|
80
75
|
* Get the stringified name of a `JSXAttribute` node.
|
|
81
76
|
*
|
|
@@ -103,8 +98,6 @@ export declare const isAttribute: {
|
|
|
103
98
|
(name: string): (node: TSESTree.Node) => node is TSESTree.JSXAttribute;
|
|
104
99
|
(node: TSESTree.Node, name: string): node is TSESTree.JSXAttribute;
|
|
105
100
|
};
|
|
106
|
-
//#endregion
|
|
107
|
-
//#region src/attribute-value.d.ts
|
|
108
101
|
/**
|
|
109
102
|
* Discriminated union representing the resolved value of a JSX attribute.
|
|
110
103
|
*
|
|
@@ -177,8 +170,6 @@ export declare function getAttributeValue(context: RuleContext, element: TSESTre
|
|
|
177
170
|
* @returns The static value of the attribute, or `undefined` when absent or indeterminate.
|
|
178
171
|
*/
|
|
179
172
|
export declare function getAttributeStaticValue(context: RuleContext, element: TSESTree.JSXElement, name: string): unknown;
|
|
180
|
-
//#endregion
|
|
181
|
-
//#region src/children.d.ts
|
|
182
173
|
/**
|
|
183
174
|
* Get the meaningful children of a JSX element or fragment.
|
|
184
175
|
*
|
|
@@ -208,8 +199,67 @@ export declare function getChildren(element: TSESTreeJSXElementLike): TSESTree.J
|
|
|
208
199
|
* @returns `true` if the element has at least one meaningful child.
|
|
209
200
|
*/
|
|
210
201
|
export declare function hasChildren(element: TSESTreeJSXElementLike): boolean;
|
|
211
|
-
|
|
212
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Get the type argument (the first argument) of a `createElement` call.
|
|
204
|
+
* @param context The ESLint rule context.
|
|
205
|
+
* @param node The node to inspect.
|
|
206
|
+
* @returns The type argument, or `null` when the node is not a `createElement` call or has no arguments.
|
|
207
|
+
*/
|
|
208
|
+
export declare function getCreateElementTypeArgument(context: RuleContext, node: null | TSESTree.Node): TSESTree.CallExpressionArgument | null;
|
|
209
|
+
/**
|
|
210
|
+
* Get the props object (the second argument) of a `createElement` call.
|
|
211
|
+
*
|
|
212
|
+
* Type expressions and chain expressions wrapping the argument are unwrapped
|
|
213
|
+
* before the object check; `null`, spread, or otherwise non-object props
|
|
214
|
+
* arguments yield `null`.
|
|
215
|
+
*
|
|
216
|
+
* @param context The ESLint rule context.
|
|
217
|
+
* @param node The node to inspect.
|
|
218
|
+
* @returns The props `ObjectExpression`, or `null` when absent or not statically an object literal.
|
|
219
|
+
*/
|
|
220
|
+
export declare function getCreateElementPropsObject(context: RuleContext, node: null | TSESTree.Node): TSESTree.ObjectExpression | null;
|
|
221
|
+
/**
|
|
222
|
+
* Get the children arguments (the arguments after the props object) of a `createElement` call.
|
|
223
|
+
* @param context The ESLint rule context.
|
|
224
|
+
* @param node The node to inspect.
|
|
225
|
+
* @returns The children arguments, or an empty array when the node is not a `createElement` call.
|
|
226
|
+
*/
|
|
227
|
+
export declare function getCreateElementChildrenArguments(context: RuleContext, node: null | TSESTree.Node): TSESTree.CallExpressionArgument[];
|
|
228
|
+
/**
|
|
229
|
+
* Find a statically named property in the props object of a `createElement` call.
|
|
230
|
+
*
|
|
231
|
+
* Statically resolvable names include plain identifier keys as well as
|
|
232
|
+
* string-literal and simple template-literal keys (computed or not).
|
|
233
|
+
* @param context The ESLint rule context.
|
|
234
|
+
* @param node The node to inspect.
|
|
235
|
+
* @param name The property name to look for (ex: `"children"`, `"key"`).
|
|
236
|
+
* @returns The matching `Property` node, or `null` when the call has no static property with that name.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* import { getCreateElementProp } from "@eslint-react/jsx";
|
|
241
|
+
*
|
|
242
|
+
* const childrenProp = getCreateElementProp(context, node, "children");
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export declare function getCreateElementProp(context: RuleContext, node: null | TSESTree.Node, name: string): TSESTree.Property | null;
|
|
246
|
+
/**
|
|
247
|
+
* Check if the node is passed as a children argument (the third argument or
|
|
248
|
+
* later) of a `createElement` call.
|
|
249
|
+
* @param context The ESLint rule context.
|
|
250
|
+
* @param node The node to check.
|
|
251
|
+
* @returns `true` if the node is a direct children argument of a `createElement` call.
|
|
252
|
+
*/
|
|
253
|
+
export declare function isCreateElementChildrenArgument(context: RuleContext, node: TSESTree.Node): boolean;
|
|
254
|
+
/**
|
|
255
|
+
* Check if the node is directly inside the props object (the second argument) of a `createElement` call,
|
|
256
|
+
* that is, its closest `ObjectExpression` ancestor is the props object itself. Nodes nested deeper
|
|
257
|
+
* (ex: inside an object nested in the props object) are not considered inside.
|
|
258
|
+
* @param context The ESLint rule context.
|
|
259
|
+
* @param node The node to check.
|
|
260
|
+
* @returns `true` if the node is directly inside `createElement`'s props object.
|
|
261
|
+
*/
|
|
262
|
+
export declare function isInsideCreateElementProps(context: RuleContext, node: TSESTree.Node): boolean;
|
|
213
263
|
/**
|
|
214
264
|
* A test that determines whether a JSX element matches.
|
|
215
265
|
*
|
|
@@ -256,8 +306,6 @@ export declare function isFragmentElement(node: TSESTree.Node, jsxFragmentFactor
|
|
|
256
306
|
* @returns `true` if the node is a `JSXElement` with a lowercase tag name.
|
|
257
307
|
*/
|
|
258
308
|
export declare function isHostElement(node: TSESTree.Node): node is TSESTree.JSXElement;
|
|
259
|
-
//#endregion
|
|
260
|
-
//#region src/element-type.d.ts
|
|
261
309
|
/**
|
|
262
310
|
* Get the string representation of a JSX element's type.
|
|
263
311
|
*
|
|
@@ -280,8 +328,6 @@ export declare function getElementFullType(node: TSESTreeJSXElementLike): string
|
|
|
280
328
|
* @returns The last segment of the element type, or `""` for fragments.
|
|
281
329
|
*/
|
|
282
330
|
export declare function getElementSelfType(node: TSESTreeJSXElementLike): string;
|
|
283
|
-
//#endregion
|
|
284
|
-
//#region src/text.d.ts
|
|
285
331
|
/**
|
|
286
332
|
* Collapse a multiline JSX text string following React's whitespace rules.
|
|
287
333
|
*
|
|
@@ -333,5 +379,4 @@ export declare function isWhitespaceText(node: TSESTree.JSXChild): boolean;
|
|
|
333
379
|
* @returns `true` if the node is a `{""}` expression container.
|
|
334
380
|
*/
|
|
335
381
|
export declare function isEmptyStringExpression(node: TSESTree.JSXChild): boolean;
|
|
336
|
-
//#endregion
|
|
337
382
|
export type { AttributeValue, ElementTest };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Check, Traverse } from "@eslint-react/ast";
|
|
1
|
+
import { Check, Extract, Traverse } from "@eslint-react/ast";
|
|
2
2
|
import { resolve } from "@eslint-react/var";
|
|
3
3
|
import { AST_NODE_TYPES } from "@typescript-eslint/types";
|
|
4
4
|
import { getStaticValue } from "@typescript-eslint/utils/ast-utils";
|
|
5
|
+
import { getText } from "@eslint-react/eslint";
|
|
5
6
|
import { P, match } from "ts-pattern";
|
|
6
|
-
|
|
7
|
+
// ../../.pkgs/eff/dist/index.js
|
|
7
8
|
const pipeArguments = (self, args) => {
|
|
8
9
|
switch (args.length) {
|
|
9
10
|
case 0: return self;
|
|
@@ -94,8 +95,7 @@ dual(2, (self, predicate) => {
|
|
|
94
95
|
while (idx < len && predicate(input[idx], idx)) idx++;
|
|
95
96
|
return input.slice(0, idx);
|
|
96
97
|
});
|
|
97
|
-
|
|
98
|
-
//#region src/attribute-name.ts
|
|
98
|
+
// src/attribute-name.ts
|
|
99
99
|
function getAttributeName(node) {
|
|
100
100
|
if (node.name.type === AST_NODE_TYPES.JSXIdentifier) return node.name.name;
|
|
101
101
|
return node.name.namespace.name + ":" + node.name.name.name;
|
|
@@ -103,8 +103,7 @@ function getAttributeName(node) {
|
|
|
103
103
|
const isAttribute = dual(2, (node, name) => {
|
|
104
104
|
return node.type === AST_NODE_TYPES.JSXAttribute && node.name.type === AST_NODE_TYPES.JSXIdentifier && node.name.name === name;
|
|
105
105
|
});
|
|
106
|
-
|
|
107
|
-
//#region src/attribute-find.ts
|
|
106
|
+
// src/attribute-find.ts
|
|
108
107
|
function findAttribute(context, element, name) {
|
|
109
108
|
return element.openingElement.attributes.findLast((attr) => {
|
|
110
109
|
if (attr.type === AST_NODE_TYPES.JSXAttribute) return getAttributeName(attr) === name;
|
|
@@ -148,8 +147,7 @@ function findSpreadProperty(context, argument, name, seen = /* @__PURE__ */ new
|
|
|
148
147
|
if (found != null) return found;
|
|
149
148
|
}
|
|
150
149
|
}
|
|
151
|
-
|
|
152
|
-
//#region src/attribute-has.ts
|
|
150
|
+
// src/attribute-has.ts
|
|
153
151
|
function hasAttribute(context, element, name) {
|
|
154
152
|
return findAttribute(context, element, name) != null;
|
|
155
153
|
}
|
|
@@ -159,8 +157,7 @@ function hasAnyAttribute(context, element, names) {
|
|
|
159
157
|
function hasEveryAttribute(context, element, names) {
|
|
160
158
|
return names.every((name) => findAttribute(context, element, name) != null);
|
|
161
159
|
}
|
|
162
|
-
|
|
163
|
-
//#region src/attribute-value.ts
|
|
160
|
+
// src/attribute-value.ts
|
|
164
161
|
function resolveAttributeValue(context, attribute, name) {
|
|
165
162
|
if (attribute.type === AST_NODE_TYPES.JSXAttribute) return resolveJSXAttribute(context, attribute);
|
|
166
163
|
return resolveJSXSpreadAttribute(context, attribute, name);
|
|
@@ -241,8 +238,7 @@ function resolveJSXSpreadAttribute(context, node, name) {
|
|
|
241
238
|
}
|
|
242
239
|
};
|
|
243
240
|
}
|
|
244
|
-
|
|
245
|
-
//#region src/text.ts
|
|
241
|
+
// src/text.ts
|
|
246
242
|
function collapseMultilineText(text) {
|
|
247
243
|
const lines = text.split(/\r\n|\n|\r/);
|
|
248
244
|
let lastNonEmptyLine = 0;
|
|
@@ -281,8 +277,7 @@ function isEmptyStringExpression(node) {
|
|
|
281
277
|
if (expr.type !== AST_NODE_TYPES.Literal) return false;
|
|
282
278
|
return expr.value === "";
|
|
283
279
|
}
|
|
284
|
-
|
|
285
|
-
//#region src/children.ts
|
|
280
|
+
// src/children.ts
|
|
286
281
|
function getChildren(element) {
|
|
287
282
|
const children = [];
|
|
288
283
|
for (const child of element.children) {
|
|
@@ -297,8 +292,48 @@ function hasChildren(element) {
|
|
|
297
292
|
if (element.children.length === 0) return false;
|
|
298
293
|
return !element.children.every(or(or(isWhitespaceText, isEmptyExpressionContainer), isEmptyStringExpression));
|
|
299
294
|
}
|
|
300
|
-
|
|
301
|
-
|
|
295
|
+
// src/create-element.ts
|
|
296
|
+
function isCreateElementCall(context, node) {
|
|
297
|
+
if (node?.type !== AST_NODE_TYPES.CallExpression) return false;
|
|
298
|
+
const name = Extract.getFullyQualifiedName(Extract.unwrap(node.callee), getText(context));
|
|
299
|
+
return name === "createElement" || name.endsWith(".createElement");
|
|
300
|
+
}
|
|
301
|
+
function getCreateElementTypeArgument(context, node) {
|
|
302
|
+
if (!isCreateElementCall(context, node)) return null;
|
|
303
|
+
return node.arguments[0] ?? null;
|
|
304
|
+
}
|
|
305
|
+
function getCreateElementPropsObject(context, node) {
|
|
306
|
+
if (!isCreateElementCall(context, node)) return null;
|
|
307
|
+
const propsArg = node.arguments[1];
|
|
308
|
+
if (propsArg == null) return null;
|
|
309
|
+
const expr = Extract.unwrap(propsArg);
|
|
310
|
+
return expr.type === AST_NODE_TYPES.ObjectExpression ? expr : null;
|
|
311
|
+
}
|
|
312
|
+
function getCreateElementChildrenArguments(context, node) {
|
|
313
|
+
if (!isCreateElementCall(context, node)) return [];
|
|
314
|
+
return node.arguments.slice(2);
|
|
315
|
+
}
|
|
316
|
+
function getCreateElementProp(context, node, name) {
|
|
317
|
+
const propsObject = getCreateElementPropsObject(context, node);
|
|
318
|
+
if (propsObject == null) return null;
|
|
319
|
+
for (const prop of propsObject.properties) if (prop.type === AST_NODE_TYPES.Property && Extract.getPropertyName(prop, "max") === name) return prop;
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
function isCreateElementChildrenArgument(context, node) {
|
|
323
|
+
let parent = node.parent;
|
|
324
|
+
while (Check.isTypeExpression(parent)) parent = parent.parent;
|
|
325
|
+
return parent?.type === AST_NODE_TYPES.CallExpression && isCreateElementCall(context, parent) && parent.arguments.slice(2).some((arg) => Extract.unwrap(arg) === Extract.unwrap(node));
|
|
326
|
+
}
|
|
327
|
+
function isInsideCreateElementProps(context, node) {
|
|
328
|
+
const call = Traverse.findParent(node, (parent) => isCreateElementCall(context, parent));
|
|
329
|
+
if (call == null) return false;
|
|
330
|
+
const prop = Traverse.findParent(node, Check.isObjectExpression);
|
|
331
|
+
if (prop == null) return false;
|
|
332
|
+
const propsArg = call.arguments[1];
|
|
333
|
+
if (propsArg == null) return false;
|
|
334
|
+
return prop === Extract.unwrap(propsArg);
|
|
335
|
+
}
|
|
336
|
+
// src/element-type.ts
|
|
302
337
|
function getElementFullType(node) {
|
|
303
338
|
if (node.type === AST_NODE_TYPES.JSXFragment) return "";
|
|
304
339
|
function getQualifiedName(node) {
|
|
@@ -313,8 +348,7 @@ function getElementFullType(node) {
|
|
|
313
348
|
function getElementSelfType(node) {
|
|
314
349
|
return getElementFullType(node).split(".").at(-1) ?? "";
|
|
315
350
|
}
|
|
316
|
-
|
|
317
|
-
//#region src/element-is.ts
|
|
351
|
+
// src/element-is.ts
|
|
318
352
|
function isElement(node, test) {
|
|
319
353
|
if (node == null) return false;
|
|
320
354
|
if (node.type !== AST_NODE_TYPES.JSXElement && node.type !== AST_NODE_TYPES.JSXFragment) return false;
|
|
@@ -335,5 +369,4 @@ function isHostElement(node) {
|
|
|
335
369
|
if (name.type === AST_NODE_TYPES.JSXNamespacedName) return /^[a-z]/u.test(name.name.name);
|
|
336
370
|
return false;
|
|
337
371
|
}
|
|
338
|
-
|
|
339
|
-
export { collapseMultilineText, findAttribute, findParentAttribute, findSpreadProperty, getAttributeName, getAttributeStaticValue, getAttributeValue, getChildren, getElementFullType, getElementSelfType, hasAnyAttribute, hasAttribute, hasChildren, hasEveryAttribute, isAttribute, isElement, isEmptyStringExpression, isFragmentElement, isHostElement, isPaddingWhitespace, isWhitespaceText, resolveAttributeValue };
|
|
372
|
+
export { collapseMultilineText, findAttribute, findParentAttribute, findSpreadProperty, getAttributeName, getAttributeStaticValue, getAttributeValue, getChildren, getCreateElementChildrenArguments, getCreateElementProp, getCreateElementPropsObject, getCreateElementTypeArgument, getElementFullType, getElementSelfType, hasAnyAttribute, hasAttribute, hasChildren, hasEveryAttribute, isAttribute, isCreateElementChildrenArgument, isElement, isEmptyStringExpression, isFragmentElement, isHostElement, isInsideCreateElementProps, isPaddingWhitespace, isWhitespaceText, resolveAttributeValue };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/jsx",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.30.0-beta.1",
|
|
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,9 +29,9 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eslint-react/ast": "5.
|
|
33
|
-
"@eslint-react/eslint": "5.
|
|
34
|
-
"@eslint-react/var": "5.
|
|
32
|
+
"@eslint-react/ast": "5.30.0-beta.1",
|
|
33
|
+
"@eslint-react/eslint": "5.30.0-beta.1",
|
|
34
|
+
"@eslint-react/var": "5.30.0-beta.1",
|
|
35
35
|
"@typescript-eslint/types": "^8.72.0",
|
|
36
36
|
"@typescript-eslint/utils": "^8.72.0",
|
|
37
37
|
"ts-pattern": "^5.9.2"
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@tsconfig/node20": "^20.1.10",
|
|
44
44
|
"@types/node": "^20.19.43",
|
|
45
45
|
"eslint": "^10.13.0",
|
|
46
|
-
"tsdown": "^0.23.
|
|
46
|
+
"tsdown": "^0.23.2",
|
|
47
47
|
"typescript": "6.0.3"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|