@eslint-react/jsx 5.30.0-beta.8 → 5.30.0-rc.0

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
@@ -13,8 +13,7 @@ type AttributeTest = string | ((attribute: TSESTree.JSXAttribute) => boolean);
13
13
  *
14
14
  * - `boolean`: the attribute has no value (ex: `<input disabled />`).
15
15
  * - `literal`: the value is a literal.
16
- * - `unknown`: a non-empty expression, normally in an expression container;
17
- * the expression is not evaluated.
16
+ * - `unknown`: a non-empty expression, normally in an expression container; the expression is not evaluated.
18
17
  * - `element`: the value is a JSX element or fragment.
19
18
  * - `missing`: no expression, normally an empty expression container
20
19
  * (`{}` or `{/* comment *\/}`).
@@ -157,10 +156,6 @@ export declare function hasEveryAttribute(element: TSESTree.JSXElement, names: r
157
156
  * are outside this API.
158
157
  * @param attribute The `JSXAttribute` node to get the value from.
159
158
  * @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.
164
159
  */
165
160
  export declare function getAttributeValue(attribute: TSESTree.JSXAttribute): AttributeValue;
166
161
  /**
@@ -205,96 +200,118 @@ export declare function getAttributeStaticValue(context: RuleContext, value: Att
205
200
  */
206
201
  export declare function getAttributeStaticValueByName(context: RuleContext, element: TSESTree.JSXElement, name: string): unknown;
207
202
  /**
208
- * Check if the node is whitespace-only `JSXText`, based on its raw source text.
209
- *
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.
203
+ * Check if the node is whitespace-only `JSXText` by its raw source text.
213
204
  *
214
- * Reads `node.raw` rather than the entity-decoded `value`, so entity text such
215
- * as `&nbsp;` or `&#32;` 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.
205
+ * Uses `node.raw`, not the entity-decoded `value`, so `&nbsp;`/`&#32;` count as
206
+ * content even when their decoded value is whitespace; this is not a model of
207
+ * React's whitespace collapsing. Same-line whitespace matches here, while
208
+ * {@link isPaddingWhitespace} (the decoded-value counterpart) also requires a
209
+ * line break.
218
210
  * @param node The node to check.
219
211
  * @returns `true` if the node is whitespace-only `JSXText` in its raw form.
220
212
  * @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.
213
+ * A content predicate, not a type guard: `false` does not mean the node is not
214
+ * `JSXText`.
223
215
  */
224
216
  export declare function isWhitespaceText(node: TSESTree.Node | null | undefined): boolean;
225
217
  /**
226
218
  * 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`).
219
+ * padding: it cleans to nothing (see {@link collapseMultilineText}) and holds a
220
+ * line break (`\r\n`, `\n`, or `\r`), matching Babel's
221
+ * `cleanJSXElementLiteralChild` line splitting.
229
222
  *
230
- * Reads the entity-decoded `node.value`, so an entity-encoded line break such as
231
- * `&#10;` matches even though the raw text contains no line break. For a
232
- * whitespace check based on raw source text, see {@link isWhitespaceText}.
223
+ * Uses the entity-decoded `value`, so `&#10;`/`&#13;` match even though the raw
224
+ * text has no line break; {@link isWhitespaceText} is the raw-text counterpart.
233
225
  * @param node The node to check.
234
- * @returns `true` if the node is purely formatting whitespace by its decoded value.
226
+ * @returns `true` if the node is formatting whitespace by its decoded value.
235
227
  * @remarks
236
- * This is a content predicate, not a type guard: a `false` result does not
237
- * mean the node is not `JSXText`.
228
+ * Entity-decoded line breaks follow Babel's cleaning; `tsc --jsx` may emit them
229
+ * as children instead.
230
+ * A content predicate, not a type guard: `false` does not mean the node is not
231
+ * `JSXText`.
238
232
  */
239
233
  export declare function isPaddingWhitespace(node: TSESTree.Node | null | undefined): boolean;
240
234
  /**
241
- * Check if the node is an empty expression container (`{}`, `{/* comment *\/}`).
235
+ * Check if the node is an empty expression container (`{}`, `{ }`,
236
+ * `{/* comment *\/}`), including multiline and TypeScript directive comments.
242
237
  *
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.
238
+ * It holds a `JSXEmptyExpression`, so it renders nothing, like whitespace
239
+ * padding. A comment attached to a real expression (`{/* comment *\/} null`) is
240
+ * not empty.
245
241
  * @param node The node to check.
246
242
  * @returns `true` if the node is an empty `JSXExpressionContainer`.
247
243
  * @remarks
248
- * This is a content predicate, not a type guard: a `false` result does not
249
- * mean the node is not a `JSXExpressionContainer`.
244
+ * A content predicate, not a type guard: `false` does not mean the node is not
245
+ * a `JSXExpressionContainer`.
250
246
  */
251
247
  export declare function isEmptyExpressionContainer(node: TSESTree.Node | null | undefined): boolean;
252
248
  /**
253
- * Check if the node is an empty string literal expression (`{""}`).
249
+ * Check if the node is an empty string literal expression (`{""}`, `{''}`).
254
250
  *
255
- * Only a literal empty string matches; template literals and other expressions
256
- * that happen to evaluate to `""` are not evaluated by this predicate.
251
+ * Node-shape check, not an evaluation: TS wrappers (`{"" as string}`, `{""!}`,
252
+ * `{"" satisfies string}`), template literals (`` {``} ``, `` {`${''}`} ``), and
253
+ * calls (`{String("")}`) do not match even though their runtime value is `""`;
254
+ * only `value === ""` matches, so `{0}`, `{false}`, and `{NaN}` do not. A comment
255
+ * attached to the literal does not affect the result.
257
256
  * @param node The node to check.
258
- * @returns `true` if the node is an expression container holding an empty string literal.
257
+ * @returns `true` if the node holds a bare empty string literal.
259
258
  * @remarks
260
- * This is a content predicate, not a type guard: a `false` result does not
261
- * mean the node is not a `JSXExpressionContainer`.
259
+ * A content predicate, not a type guard: `false` does not mean the node is not
260
+ * a `JSXExpressionContainer`.
262
261
  */
263
262
  export declare function isEmptyStringExpression(node: TSESTree.Node | null | undefined): boolean;
264
263
  /**
265
- * Check if the child carries content for the "meaningful children" semantics.
264
+ * Check if the child is syntactically non-empty ("meaningful") content.
265
+ *
266
+ * Excluded: whitespace-only `JSXText` per {@link isWhitespaceText} (raw text, so
267
+ * entity text such as `&nbsp;`/`&#32;` is kept), empty expression containers, and
268
+ * bare empty string literals.
269
+ *
270
+ * Kept: dynamic values (`{null}`, `{false}`, `{undefined}`, `{0}`, `{NaN}`,
271
+ * `{cond ? <A /> : null}`), TS-wrapped empty strings (`{"" as string}`, `{""!}`),
272
+ * unevaluated template literals, and spread children.
266
273
  *
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.
274
+ * A syntactic check, not a rendering oracle: React renders nothing for `null`,
275
+ * `undefined`, booleans, and `""`, but those children are kept.
272
276
  * @param node The JSX child to check.
273
277
  * @returns `true` if the child is not syntactically empty.
278
+ * @remarks
279
+ * Definitional difference: JSXText `" "` is not meaningful, while `{" "}` and
280
+ * `&#32;` are, even though all three render a space text node.
274
281
  */
275
282
  export declare function isMeaningfulChild(node: TSESTree.JSXChild): boolean;
276
283
  /**
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.
284
+ * Get the children of a JSX element or fragment after removing padding
285
+ * whitespace (see {@link isPaddingWhitespace}), empty expression containers,
286
+ * and empty string literals.
287
+ *
288
+ * Same-line whitespace is kept (it can render a text node); this is a syntactic
289
+ * filter, not a rendering guarantee (`{null}`/`{false}` are kept). Compared with
290
+ * {@link getMeaningfulChildren}:
291
+ *
292
+ * | Input | `getChildren` | `getMeaningfulChildren` |
293
+ * | ------------------------ | ------------------------- | ----------------------- |
294
+ * | `" "`, `"\t"` | kept | excluded |
295
+ * | `\r \r` (CR-only) | excluded | excluded |
296
+ * | `&#10;`, `&#13;` | excluded (Babel cleaning) | kept (raw text) |
297
+ * | `&#32;`, `&nbsp;` | kept | kept |
298
+ * | `{null}`, `{false}` | kept | kept |
299
+ * | `{}`, `{/* comment *\/}` | excluded | excluded |
300
+ * | `{""}` | excluded | excluded |
301
+ * | `{"" as string}` | kept | kept |
285
302
  * @param element A `JSXElement` or `JSXFragment` node.
286
303
  * @returns The children that remain after removing padding artifacts.
287
304
  */
288
305
  export declare function getChildren(element: TSESTreeJSXElementLike): TSESTree.JSXChild[];
289
306
  /**
290
- * Get the children of a JSX element or fragment that are not syntactically empty.
307
+ * Get the children of a JSX element or fragment that are not syntactically
308
+ * empty.
291
309
  *
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.
310
+ * Stricter than {@link getChildren} for same-line whitespace
311
+ * (`<div> </div>` yields no children here). Dynamic expressions count, and
312
+ * entity line breaks (`&#10;`, `&#13;`) are kept because the raw text is not
313
+ * whitespace, whereas `getChildren` excludes them through Babel's decoded-value
314
+ * cleaning. See {@link getChildren} for a comparison table.
298
315
  * @param element A `JSXElement` or `JSXFragment` node.
299
316
  * @returns The children that are not syntactically empty.
300
317
  */
@@ -302,11 +319,11 @@ export declare function getMeaningfulChildren(element: TSESTreeJSXElementLike):
302
319
  /**
303
320
  * Check if the element has at least one syntactically non-empty child.
304
321
  *
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.
322
+ * A child counts unless it is whitespace-only `JSXText` per
323
+ * {@link isWhitespaceText}, an empty expression container, or a bare empty
324
+ * string literal. Same-line whitespace does not count, unlike in
325
+ * {@link getChildren}; `{null}`, `{false}`, and other dynamic expressions do. A
326
+ * syntactic check, not a rendering oracle.
310
327
  * @param element A `JSXElement` or `JSXFragment` node.
311
328
  * @returns `true` if the element has at least one syntactically non-empty child.
312
329
  */
@@ -378,8 +395,8 @@ export declare function findCreateElementProp(context: RuleContext, call: Create
378
395
  /**
379
396
  * Check if the node itself is passed as a children argument (the third argument
380
397
  * 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.
398
+ * and chain-expression ancestors are skipped, and the node is unwrapped before
399
+ * the comparison; expressions nested inside an argument are not considered.
383
400
  * @param context The ESLint rule context.
384
401
  * @param node The node to check; nullish values return `false`.
385
402
  * @returns `true` if the node is a direct children argument of a `createElement` call.
@@ -497,6 +514,11 @@ export declare function isHostElement(node: TSESTree.Node | null | undefined): b
497
514
  * non-last physical lines.
498
515
  * 5. Append a single space after each line that still has content, except the
499
516
  * last tracked line.
517
+ *
518
+ * This function is the module's authority for JSX whitespace semantics.
519
+ * `tsc --jsx` trims raw line breaks the same way, but it may emit
520
+ * entity-decoded line breaks (ex: `&#10;`, `&#13;`) as text children, which
521
+ * this cleaning removes.
500
522
  * @param text The JSX text string to collapse (typically a decoded `JSXText.value`).
501
523
  * @returns The collapsed string, or `null` when the collapsed result is empty
502
524
  * (for example, for empty input or multiline input containing only spaces, tabs,
package/dist/index.js CHANGED
@@ -65,8 +65,11 @@ const absurd = (_) => {
65
65
  throw new Error("Called `absurd` function which should be uncallable");
66
66
  };
67
67
  cast(absurd);
68
- dual(2, (a, b) => (data) => a(data) && b(data));
69
- dual(2, (a, b) => (data) => a(data) || b(data));
68
+ function not(predicate) {
69
+ return (data) => !predicate(data);
70
+ }
71
+ const and = dual(2, (a, b) => (data) => a(data) && b(data));
72
+ const or = dual(2, (a, b) => (data) => a(data) || b(data));
70
73
  dual(2, (a, b) => (data) => a(data) !== b(data));
71
74
  dual(2, (a, b) => (data) => a(data) === b(data));
72
75
  dual(2, (antecedent, consequent) => (data) => !antecedent(data) || consequent(data));
@@ -222,7 +225,7 @@ function isWhitespaceText(node) {
222
225
  return node?.type === AST_NODE_TYPES.JSXText && node.raw.trim() === "";
223
226
  }
224
227
  function isPaddingWhitespace(node) {
225
- return node?.type === AST_NODE_TYPES.JSXText && collapseMultilineText(node.value) == null && node.value.includes("\n");
228
+ return node?.type === AST_NODE_TYPES.JSXText && collapseMultilineText(node.value) == null && /[\r\n]/u.test(node.value);
226
229
  }
227
230
  function isEmptyExpressionContainer(node) {
228
231
  return node?.type === AST_NODE_TYPES.JSXExpressionContainer && node.expression.type === AST_NODE_TYPES.JSXEmptyExpression;
@@ -230,11 +233,14 @@ function isEmptyExpressionContainer(node) {
230
233
  function isEmptyStringExpression(node) {
231
234
  return node?.type === AST_NODE_TYPES.JSXExpressionContainer && node.expression.type === AST_NODE_TYPES.Literal && node.expression.value === "";
232
235
  }
236
+ const isEmptyExpression = or(isEmptyExpressionContainer, isEmptyStringExpression);
237
+ const isMeaningfulContent = and(not(isWhitespaceText), not(isEmptyExpression));
238
+ const isNonPaddingContent = and(not(isPaddingWhitespace), not(isEmptyExpression));
233
239
  function isMeaningfulChild(node) {
234
- return !isWhitespaceText(node) && !isEmptyExpressionContainer(node) && !isEmptyStringExpression(node);
240
+ return isMeaningfulContent(node);
235
241
  }
236
242
  function getChildren(element) {
237
- return element.children.filter((child) => !isPaddingWhitespace(child) && !isEmptyExpressionContainer(child) && !isEmptyStringExpression(child));
243
+ return element.children.filter(isNonPaddingContent);
238
244
  }
239
245
  function getMeaningfulChildren(element) {
240
246
  return element.children.filter(isMeaningfulChild);
@@ -261,8 +267,12 @@ function parseCreateElementCall(context, node) {
261
267
  };
262
268
  }
263
269
  function getStaticPropertyName(context, property) {
264
- if (!property.computed) return Extract.getPropertyName(property, "max");
265
270
  const key = Extract.unwrap(property.key);
271
+ if (!property.computed) {
272
+ if (Check.isIdentifier(key)) return key.name;
273
+ if (key.type === AST_NODE_TYPES.Literal && typeof key.value === "string") return key.value;
274
+ return null;
275
+ }
266
276
  const staticValue = getStaticValue(key, context.sourceCode.getScope(key));
267
277
  return typeof staticValue?.value === "string" ? staticValue.value : null;
268
278
  }
@@ -278,7 +288,7 @@ function findCreateElementProp(context, call, name) {
278
288
  function isCreateElementChildrenArgument(context, node) {
279
289
  if (node == null) return false;
280
290
  let parent = node.parent;
281
- while (Check.isTypeExpression(parent)) parent = parent.parent;
291
+ while (Check.isTypeOrChainExpression(parent)) parent = parent.parent;
282
292
  const target = Extract.unwrap(node);
283
293
  return parent?.type === AST_NODE_TYPES.CallExpression && isCreateElementCall(context, parent) && parent.arguments.slice(2).some((arg) => Extract.unwrap(arg) === target);
284
294
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint-react/jsx",
3
- "version": "5.30.0-beta.8",
3
+ "version": "5.30.0-rc.0",
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,8 +29,8 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@eslint-react/ast": "5.30.0-beta.8",
33
- "@eslint-react/eslint": "5.30.0-beta.8",
32
+ "@eslint-react/ast": "5.30.0-rc.0",
33
+ "@eslint-react/eslint": "5.30.0-rc.0",
34
34
  "@typescript-eslint/types": "^8.72.1",
35
35
  "@typescript-eslint/utils": "^8.72.1"
36
36
  },
@@ -40,8 +40,8 @@
40
40
  "@local/testkit": "0.0.0",
41
41
  "@tsconfig/node20": "^20.1.10",
42
42
  "@types/node": "^20.19.43",
43
- "eslint": "^10.13.0",
44
- "tsdown": "^0.23.2",
43
+ "eslint": "^10.14.0",
44
+ "tsdown": "^0.23.5",
45
45
  "typescript": "6.0.3"
46
46
  },
47
47
  "peerDependencies": {