@eslint-react/jsx 5.30.0-beta.0 → 5.30.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -199,6 +199,67 @@ export declare function getChildren(element: TSESTreeJSXElementLike): TSESTree.J
199
199
  * @returns `true` if the element has at least one meaningful child.
200
200
  */
201
201
  export declare function hasChildren(element: TSESTreeJSXElementLike): boolean;
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;
202
263
  /**
203
264
  * A test that determines whether a JSX element matches.
204
265
  *
package/dist/index.js CHANGED
@@ -1,7 +1,8 @@
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) => {
@@ -291,6 +292,47 @@ function hasChildren(element) {
291
292
  if (element.children.length === 0) return false;
292
293
  return !element.children.every(or(or(isWhitespaceText, isEmptyExpressionContainer), isEmptyStringExpression));
293
294
  }
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
+ }
294
336
  // src/element-type.ts
295
337
  function getElementFullType(node) {
296
338
  if (node.type === AST_NODE_TYPES.JSXFragment) return "";
@@ -327,4 +369,4 @@ function isHostElement(node) {
327
369
  if (name.type === AST_NODE_TYPES.JSXNamespacedName) return /^[a-z]/u.test(name.name.name);
328
370
  return false;
329
371
  }
330
- 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.30.0-beta.0",
3
+ "version": "5.30.0-beta.2",
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.30.0-beta.0",
33
- "@eslint-react/eslint": "5.30.0-beta.0",
34
- "@eslint-react/var": "5.30.0-beta.0",
32
+ "@eslint-react/ast": "5.30.0-beta.2",
33
+ "@eslint-react/eslint": "5.30.0-beta.2",
34
+ "@eslint-react/var": "5.30.0-beta.2",
35
35
  "@typescript-eslint/types": "^8.72.0",
36
36
  "@typescript-eslint/utils": "^8.72.0",
37
37
  "ts-pattern": "^5.9.2"