@eslint-react/jsx 5.30.0-beta.10 → 5.30.0-beta.11
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 +84 -62
- package/dist/index.js +11 -5
- package/package.json +3 -3
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
|
|
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
|
-
*
|
|
215
|
-
*
|
|
216
|
-
* whitespace.
|
|
217
|
-
*
|
|
205
|
+
* Uses `node.raw`, not the entity-decoded `value`, so ` `/` ` 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
|
-
*
|
|
222
|
-
*
|
|
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
|
|
228
|
-
*
|
|
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
|
-
*
|
|
231
|
-
*
|
|
232
|
-
* whitespace check based on raw source text, see {@link isWhitespaceText}.
|
|
223
|
+
* Uses the entity-decoded `value`, so ` `/` ` 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
|
|
226
|
+
* @returns `true` if the node is formatting whitespace by its decoded value.
|
|
235
227
|
* @remarks
|
|
236
|
-
*
|
|
237
|
-
*
|
|
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 (`{}`, `{
|
|
235
|
+
* Check if the node is an empty expression container (`{}`, `{ }`,
|
|
236
|
+
* `{/* comment *\/}`), including multiline and TypeScript directive comments.
|
|
242
237
|
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
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
|
-
*
|
|
249
|
-
*
|
|
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
|
-
*
|
|
256
|
-
*
|
|
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
|
|
257
|
+
* @returns `true` if the node holds a bare empty string literal.
|
|
259
258
|
* @remarks
|
|
260
|
-
*
|
|
261
|
-
*
|
|
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
|
|
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 ` `/` ` 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
|
-
*
|
|
268
|
-
*
|
|
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
|
+
* ` ` 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
|
|
278
|
-
*
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
* Same-line whitespace is kept
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
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
|
+
* | ` `, ` ` | excluded (Babel cleaning) | kept (raw text) |
|
|
297
|
+
* | ` `, ` ` | 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
|
|
307
|
+
* Get the children of a JSX element or fragment that are not syntactically
|
|
308
|
+
* empty.
|
|
291
309
|
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
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 (` `, ` `) 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
|
|
306
|
-
* {@link isWhitespaceText}, an empty expression container
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
*
|
|
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
|
*/
|
|
@@ -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: ` `, ` `) 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
|
-
|
|
69
|
-
|
|
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
|
|
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
|
|
240
|
+
return isMeaningfulContent(node);
|
|
235
241
|
}
|
|
236
242
|
function getChildren(element) {
|
|
237
|
-
return element.children.filter(
|
|
243
|
+
return element.children.filter(isNonPaddingContent);
|
|
238
244
|
}
|
|
239
245
|
function getMeaningfulChildren(element) {
|
|
240
246
|
return element.children.filter(isMeaningfulChild);
|
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.11",
|
|
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.
|
|
33
|
-
"@eslint-react/eslint": "5.30.0-beta.
|
|
32
|
+
"@eslint-react/ast": "5.30.0-beta.11",
|
|
33
|
+
"@eslint-react/eslint": "5.30.0-beta.11",
|
|
34
34
|
"@typescript-eslint/types": "^8.72.1",
|
|
35
35
|
"@typescript-eslint/utils": "^8.72.1"
|
|
36
36
|
},
|