Skip to content

Commit 32d4ae1

Browse files
committed
refactor: minor improvements
1 parent 20f6088 commit 32d4ae1

File tree

6 files changed

+55
-56
lines changed

6 files changed

+55
-56
lines changed

packages/core/docs/functions/stringifyJsx.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
> **stringifyJsx**(`node`): `string`
1010
11-
Converts a JSX AST node to its string representation
12-
Handles different JSX node types and returns their textual form
11+
Incomplete but sufficient stringification of JSX nodes for common use cases
1312

1413
## Parameters
1514

packages/core/src/jsx/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export * from "./jsx-attribute";
22
export * from "./jsx-attribute-name";
33
export * from "./jsx-attribute-value";
44
export * from "./jsx-detection";
5-
export * from "./jsx-detection-hint";
65
export * from "./jsx-element-is";
76
export * from "./jsx-element-type";
87
export * from "./jsx-has";

packages/core/src/jsx/jsx-detection-hint.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

packages/core/src/jsx/jsx-detection.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,56 @@ import * as VAR from "@eslint-react/var";
55
import type { Scope } from "@typescript-eslint/scope-manager";
66
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
77
import type { TSESTree } from "@typescript-eslint/utils";
8-
import { DEFAULT_JSX_DETECTION_HINT, JSXDetectionHint } from "./jsx-detection-hint";
8+
9+
// This is a representation of React Node types for reference
10+
// type ReactNode =
11+
// | ReactElement
12+
// | string
13+
// | number
14+
// | Iterable<ReactNode>
15+
// | ReactPortal
16+
// | boolean
17+
// | null
18+
// | undefined
19+
// | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
20+
// keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
21+
// ];
22+
23+
/**
24+
* BitFlags for configuring JSX detection behavior
25+
* Uses BigInt for bit operations to support many flags
26+
*/
27+
export type JSXDetectionHint = bigint;
28+
29+
/* eslint-disable perfectionist/sort-objects */
30+
/**
31+
* Flags to control JSX detection behavior:
32+
* - Skip* flags: Ignore specific node types when detecting JSX
33+
* - Strict* flags: Enforce stricter rules for container types
34+
*/
35+
export const JSXDetectionHint = {
36+
None: 0n,
37+
SkipUndefined: 1n << 0n, // Ignore undefined values
38+
SkipNullLiteral: 1n << 1n, // Ignore null literals
39+
SkipBooleanLiteral: 1n << 2n, // Ignore boolean literals
40+
SkipStringLiteral: 1n << 3n, // Ignore string literals
41+
SkipNumberLiteral: 1n << 4n, // Ignore number literals
42+
SkipBigIntLiteral: 1n << 5n, // Ignore bigint literals
43+
SkipEmptyArray: 1n << 6n, // Ignore empty arrays
44+
SkipCreateElement: 1n << 7n, // Ignore React.createElement calls
45+
StrictArray: 1n << 8n, // Require all array elements to be JSX
46+
StrictLogical: 1n << 9n, // Require both sides of logical expr to be JSX
47+
StrictConditional: 1n << 10n, // Require both branches of conditional to be JSX
48+
} as const;
49+
/* eslint-enable perfectionist/sort-objects */
50+
51+
/**
52+
* Default JSX detection configuration
53+
* Skips undefined and boolean literals (common in React)
54+
*/
55+
export const DEFAULT_JSX_DETECTION_HINT = 0n
56+
| JSXDetectionHint.SkipUndefined
57+
| JSXDetectionHint.SkipBooleanLiteral;
958

1059
/**
1160
* Checks if a node is a `JSXText` or a `Literal` node

packages/core/src/jsx/jsx-stringify.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
22
import type { TSESTree } from "@typescript-eslint/utils";
33

44
/**
5-
* Converts a JSX AST node to its string representation
6-
* Handles different JSX node types and returns their textual form
5+
* Incomplete but sufficient stringification of JSX nodes for common use cases
76
*
87
* @param node - JSX node from TypeScript ESTree
98
* @returns String representation of the JSX node
109
*/
1110
export function stringifyJsx(
1211
node:
1312
| TSESTree.JSXIdentifier
14-
| TSESTree.JSXMemberExpression
1513
| TSESTree.JSXNamespacedName
14+
| TSESTree.JSXMemberExpression
1615
| TSESTree.JSXOpeningElement
1716
| TSESTree.JSXClosingElement
1817
| TSESTree.JSXOpeningFragment

packages/utilities/ast/src/node-format.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export function toDelimiterFormat(node: TSESTree.Node, delimiter = " ") {
2828
}
2929

3030
/**
31+
* Incomplete but sufficient stringification of AST nodes for common use cases
32+
*
3133
* @internal
3234
*/
3335
export function toStringFormat(node: TSESTree.Node, getText: (node: TSESTree.Node) => string): string {

0 commit comments

Comments
 (0)