eslint-plugin-react-jsx 5.30.0-beta.2 → 5.30.0-beta.20

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.
Files changed (2) hide show
  1. package/dist/index.js +92 -102
  2. package/package.json +10 -10
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { DEFAULT_ESLINT_REACT_SETTINGS } from "@eslint-react/shared";
2
2
  import * as ESLintUtils from "@typescript-eslint/utils/eslint-utils";
3
3
  import { getText } from "@eslint-react/eslint";
4
- import { collapseMultilineText, findAttribute, getChildren, getCreateElementChildrenArguments, getCreateElementProp, getElementFullType, hasAnyAttribute, hasChildren, isAttribute, isEmptyStringExpression, isFragmentElement, isHostElement, isWhitespaceText } from "@eslint-react/jsx";
5
- import { AST_NODE_TYPES } from "@typescript-eslint/types";
4
+ import { collapseMultilineText, findAttribute, findCreateElementProp, getChildren, getElementFullType, hasAnyAttribute, hasMeaningfulChildren, isFragmentElement, isHostElement, isMeaningfulChild, isPlainAttribute, isWhitespaceText, parseCreateElementCall } from "@eslint-react/jsx";
6
5
  import { Check } from "@eslint-react/ast";
6
+ import { AST_NODE_TYPES } from "@typescript-eslint/types";
7
7
  import * as core from "@eslint-react/core";
8
8
  import ts from "typescript";
9
9
  // \0rolldown/runtime.js
@@ -19,7 +19,7 @@ var __exportAll = (all, no_symbols) => {
19
19
  };
20
20
  // package.json
21
21
  var name$2 = "eslint-plugin-react-jsx";
22
- var version = "5.30.0-beta.2";
22
+ var version = "5.30.0-beta.20";
23
23
  // src/utils/create-rule.ts
24
24
  function getDocsUrl(ruleName) {
25
25
  return `https://eslint-react.xyz/docs/rules/jsx-${ruleName}`;
@@ -50,24 +50,18 @@ var no_children_prop_with_children_default = createRule({
50
50
  function create$7(context) {
51
51
  return {
52
52
  CallExpression(node) {
53
- const prop = getCreateElementProp(context, node, "children");
54
- if (prop == null) return;
55
- if (getCreateElementChildrenArguments(context, node).length === 0) return;
53
+ const call = parseCreateElementCall(context, node);
54
+ if (call == null) return;
55
+ const prop = findCreateElementProp(context, call, "children");
56
+ if (prop == null || call.childrenArguments.length === 0) return;
56
57
  context.report({
57
58
  messageId: "default",
58
59
  node: prop
59
60
  });
60
61
  },
61
62
  JSXElement(node) {
62
- const prop = findAttribute(context, node, "children");
63
- if (prop == null || !hasChildren(node)) return;
64
- if (prop.type !== AST_NODE_TYPES.JSXAttribute) {
65
- context.report({
66
- messageId: "default",
67
- node: prop
68
- });
69
- return;
70
- }
63
+ const prop = findAttribute(node, "children");
64
+ if (prop == null || !hasMeaningfulChildren(node)) return;
71
65
  context.report({
72
66
  messageId: "default",
73
67
  node: prop,
@@ -107,27 +101,21 @@ var no_children_prop_default = createRule({
107
101
  function create$6(context) {
108
102
  return {
109
103
  CallExpression(node) {
110
- const prop = getCreateElementProp(context, node, "children");
111
- if (prop == null) return;
112
- if (getCreateElementChildrenArguments(context, node).length > 0) return;
104
+ const call = parseCreateElementCall(context, node);
105
+ if (call == null) return;
106
+ const prop = findCreateElementProp(context, call, "children");
107
+ if (prop == null || call.childrenArguments.length > 0) return;
113
108
  context.report({
114
109
  messageId: "default",
115
110
  node: prop
116
111
  });
117
112
  },
118
113
  JSXElement(node) {
119
- const prop = findAttribute(context, node, "children");
120
- if (prop == null) return;
121
- if (hasChildren(node)) return;
122
- if (prop.type !== AST_NODE_TYPES.JSXAttribute) {
123
- context.report({
124
- messageId: "default",
125
- node: prop
126
- });
127
- return;
128
- }
114
+ const prop = findAttribute(node, "children");
115
+ if (prop == null || hasMeaningfulChildren(node)) return;
116
+ const hasFollowingSpread = node.openingElement.attributes.some((a) => a.type === AST_NODE_TYPES.JSXSpreadAttribute && a.range[0] > prop.range[0]);
129
117
  const childrenText = getChildrenText(context, prop);
130
- if (childrenText == null) {
118
+ if (childrenText == null || hasFollowingSpread) {
131
119
  context.report({
132
120
  messageId: "default",
133
121
  node: prop
@@ -259,7 +247,7 @@ function not(predicate) {
259
247
  return (data) => !predicate(data);
260
248
  }
261
249
  dual(2, (a, b) => (data) => a(data) && b(data));
262
- const or = dual(2, (a, b) => (data) => a(data) || b(data));
250
+ dual(2, (a, b) => (data) => a(data) || b(data));
263
251
  dual(2, (a, b) => (data) => a(data) !== b(data));
264
252
  dual(2, (a, b) => (data) => a(data) === b(data));
265
253
  dual(2, (antecedent, consequent) => (data) => !antecedent(data) || consequent(data));
@@ -301,7 +289,7 @@ function create$4(context) {
301
289
  const { jsx } = core.getJSXConfig(context);
302
290
  if (jsx !== ts.JsxEmit.ReactJSX && jsx !== ts.JsxEmit.ReactJSXDev) return {};
303
291
  return { JSXOpeningElement(node) {
304
- for (const n of dropWhile(node.attributes, not(Check.isJSXSpreadAttribute)).filter(isAttribute("key"))) context.report({
292
+ for (const n of dropWhile(node.attributes, not(Check.isJSXSpreadAttribute)).filter(isPlainAttribute("key"))) context.report({
305
293
  messageId: "default",
306
294
  node: n
307
295
  });
@@ -324,39 +312,44 @@ var no_leaked_dollar_default = createRule({
324
312
  });
325
313
  function create$3(context) {
326
314
  if (!context.sourceCode.text.includes("$")) return {};
327
- function visit(node) {
328
- for (const [index, child] of node.children.entries()) {
329
- if (child.type !== AST_NODE_TYPES.JSXText || !child.value.endsWith("$")) continue;
330
- if (node.children[index + 1]?.type !== AST_NODE_TYPES.JSXExpressionContainer) continue;
331
- if (child.value.trim() === "$" && node.children.every((sibling, siblingIndex) => siblingIndex === index || siblingIndex === index + 1 || isNonSubstantiveChild(sibling))) continue;
332
- if (!getText(context, child).endsWith("$")) continue;
333
- const dollarStart = child.range[1] - 1;
334
- const dollarEnd = child.range[1];
335
- context.report({
336
- loc: {
337
- end: context.sourceCode.getLocFromIndex(dollarEnd),
338
- start: context.sourceCode.getLocFromIndex(dollarStart)
339
- },
340
- messageId: "default",
341
- node: child,
342
- suggest: [{
343
- fix(fixer) {
344
- return fixer.removeRange([dollarStart, dollarEnd]);
345
- },
346
- messageId: "suggest"
347
- }]
348
- });
349
- }
350
- }
351
315
  return {
352
- JSXElement: visit,
353
- JSXFragment: visit
316
+ JSXElement(node) {
317
+ visitForLeakedDollar(context, node);
318
+ },
319
+ JSXFragment(node) {
320
+ visitForLeakedDollar(context, node);
321
+ }
354
322
  };
355
323
  }
356
- const isWhitespaceOrEmptyStringExpression = or(isWhitespaceText, isEmptyStringExpression);
357
- function isNonSubstantiveChild(child) {
358
- if (isWhitespaceOrEmptyStringExpression(child)) return true;
359
- return child.type === AST_NODE_TYPES.JSXExpressionContainer && child.expression.type === AST_NODE_TYPES.JSXEmptyExpression;
324
+ function visitForLeakedDollar(context, node) {
325
+ let meaningfulChildren = null;
326
+ for (const [index, child] of node.children.entries()) {
327
+ if (child.type !== AST_NODE_TYPES.JSXText || !child.value.endsWith("$")) continue;
328
+ const next = node.children[index + 1];
329
+ if (next?.type !== AST_NODE_TYPES.JSXExpressionContainer) continue;
330
+ meaningfulChildren ??= node.children.filter(isMeaningfulChild).length;
331
+ if (isIsolatedDollar(child, next, meaningfulChildren)) continue;
332
+ if (!getText(context, child).endsWith("$")) continue;
333
+ const dollarStart = child.range[1] - 1;
334
+ const dollarEnd = child.range[1];
335
+ context.report({
336
+ loc: {
337
+ end: context.sourceCode.getLocFromIndex(dollarEnd),
338
+ start: context.sourceCode.getLocFromIndex(dollarStart)
339
+ },
340
+ messageId: "default",
341
+ node: child,
342
+ suggest: [{
343
+ fix(fixer) {
344
+ return fixer.removeRange([dollarStart, dollarEnd]);
345
+ },
346
+ messageId: "suggest"
347
+ }]
348
+ });
349
+ }
350
+ }
351
+ function isIsolatedDollar(child, next, meaningfulChildren) {
352
+ return child.value.trim() === "$" && meaningfulChildren === 1 + (isMeaningfulChild(next) ? 1 : 0);
360
353
  }
361
354
  var no_leaked_semicolon_default = createRule({
362
355
  meta: {
@@ -376,7 +369,11 @@ var no_leaked_semicolon_default = createRule({
376
369
  function create$2(context) {
377
370
  return { JSXText(node) {
378
371
  if (!Check.isJSXElementOrFragment(node.parent)) return;
379
- if (!/^;+[ \t]*(?:\r\n|\r|\n)/u.test(getText(context, node))) return;
372
+ const text = getText(context, node);
373
+ const children = node.parent.children;
374
+ const previous = children.at(-2);
375
+ const isSameLineLeak = children.at(-1) === node && previous != null && Check.isJSXElementOrFragment(previous) && /^;+[ \t]*$/u.test(text);
376
+ if (!/^;+[ \t]*(?:\r\n|\r|\n)/u.test(text) && !isSameLineLeak) return;
380
377
  context.report({
381
378
  messageId: "default",
382
379
  node,
@@ -401,68 +398,63 @@ var no_namespace_default = createRule({
401
398
  });
402
399
  function create$1(context) {
403
400
  return { JSXElement(node) {
404
- const name = getElementFullType(node);
405
- if (!name.includes(":")) return;
401
+ const { name } = node.openingElement;
402
+ if (name.type !== AST_NODE_TYPES.JSXNamespacedName) return;
406
403
  context.report({
407
- data: { name },
404
+ data: { name: getElementFullType(node) },
408
405
  messageId: "default",
409
- node: node.openingElement.name
406
+ node: name
410
407
  });
411
408
  } };
412
409
  }
413
- // src/rules/no-useless-fragment/no-useless-fragment.ts
414
- const RULE_NAME = "no-useless-fragment";
415
- const defaultOptions = [{
416
- allowEmptyFragment: false,
417
- allowExpressions: true
418
- }];
419
- const schema = [{
420
- type: "object",
421
- additionalProperties: false,
422
- properties: {
423
- allowEmptyFragment: {
424
- type: "boolean",
425
- description: "Allow empty fragments"
426
- },
427
- allowExpressions: {
428
- type: "boolean",
429
- description: "Allow fragments with a single expression child"
430
- }
431
- }
432
- }];
433
410
  var no_useless_fragment_default = createRule({
434
411
  meta: {
435
412
  type: "suggestion",
436
- defaultOptions: [...defaultOptions],
413
+ defaultOptions: [{}],
437
414
  docs: { description: "Disallows useless fragment elements." },
438
415
  fixable: "code",
439
416
  messages: { default: "A fragment {{reason}} is useless." },
440
- schema
417
+ schema: [{
418
+ type: "object",
419
+ additionalProperties: false,
420
+ properties: {
421
+ allowEmptyFragment: {
422
+ type: "boolean",
423
+ description: "Allow empty fragments"
424
+ },
425
+ allowExpressions: {
426
+ type: "boolean",
427
+ description: "Allow fragments with a single expression child"
428
+ }
429
+ }
430
+ }]
441
431
  },
442
- name: RULE_NAME,
432
+ name: "no-useless-fragment",
443
433
  create
444
434
  });
445
- function create(context, [option]) {
446
- const options = {
447
- allowEmptyFragment: option.allowEmptyFragment ?? false,
448
- allowExpressions: option.allowExpressions ?? true
449
- };
435
+ function create(context) {
436
+ const options = context.options[0] ?? {};
437
+ const allowEmptyFragment = options.allowEmptyFragment ?? false;
438
+ const allowExpressions = options.allowExpressions ?? true;
450
439
  const { jsxFragmentFactory } = core.getJSXConfig(context);
451
440
  return {
452
441
  JSXElement(node) {
453
442
  if (!isFragmentElement(node, jsxFragmentFactory)) return;
454
- if (hasAnyAttribute(context, node, ["key", "ref"])) return;
455
- visitFragment(context, node, options);
443
+ if (hasIdentityAttribute(node)) return;
444
+ visitFragment(context, node, allowEmptyFragment, allowExpressions);
456
445
  },
457
446
  JSXFragment(node) {
458
- visitFragment(context, node, options);
447
+ visitFragment(context, node, allowEmptyFragment, allowExpressions);
459
448
  }
460
449
  };
461
450
  }
462
- function visitFragment(context, node, options) {
451
+ function hasIdentityAttribute(node) {
452
+ return node.openingElement.attributes.some((a) => a.type === AST_NODE_TYPES.JSXSpreadAttribute) || hasAnyAttribute(node, ["key", "ref"]);
453
+ }
454
+ function visitFragment(context, node, allowEmptyFragment, allowExpressions) {
463
455
  const reasons = [];
464
456
  if (isHostElement(node.parent)) reasons.push("placed inside a host component");
465
- if (isContentUseless(node, options)) reasons.push("contains less than two children");
457
+ if (isContentUseless(node, allowEmptyFragment, allowExpressions)) reasons.push("contains less than two children");
466
458
  if (reasons.length === 0) return;
467
459
  const fix = buildFix(context, node);
468
460
  for (const reason of reasons) context.report({
@@ -472,8 +464,7 @@ function visitFragment(context, node, options) {
472
464
  node
473
465
  });
474
466
  }
475
- function isContentUseless(node, options) {
476
- const { allowEmptyFragment, allowExpressions } = options;
467
+ function isContentUseless(node, allowEmptyFragment, allowExpressions) {
477
468
  if (node.children.length === 0) return !allowEmptyFragment;
478
469
  const insideJSX = Check.isJSXElementOrFragment(node.parent);
479
470
  if (allowExpressions && !insideJSX && node.children.length === 1 && node.children[0]?.type === AST_NODE_TYPES.JSXText) return false;
@@ -484,7 +475,6 @@ function isContentUseless(node, options) {
484
475
  return true;
485
476
  }
486
477
  function isSafeToFix(node) {
487
- if (node.type === AST_NODE_TYPES.JSXElement && node.openingElement.attributes.some((attr) => attr.type === AST_NODE_TYPES.JSXSpreadAttribute)) return false;
488
478
  if (Check.isJSXElementOrFragment(node.parent)) return isHostElement(node.parent);
489
479
  if (!node.children.some((child) => Check.isJSXElementOrFragment(child))) return false;
490
480
  return node.children.every((child) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-react-jsx",
3
- "version": "5.30.0-beta.2",
3
+ "version": "5.30.0-beta.20",
4
4
  "description": "ESLint React's ESLint plugin for React Flavored JSX rules.",
5
5
  "keywords": [
6
6
  "react",
@@ -37,13 +37,13 @@
37
37
  "dist"
38
38
  ],
39
39
  "dependencies": {
40
- "@eslint-react/ast": "5.30.0-beta.2",
41
- "@eslint-react/core": "5.30.0-beta.2",
42
- "@eslint-react/eslint": "5.30.0-beta.2",
43
- "@eslint-react/jsx": "5.30.0-beta.2",
44
- "@eslint-react/shared": "5.30.0-beta.2",
45
- "@typescript-eslint/types": "^8.72.0",
46
- "@typescript-eslint/utils": "^8.72.0"
40
+ "@eslint-react/ast": "5.30.0-beta.20",
41
+ "@eslint-react/core": "5.30.0-beta.20",
42
+ "@eslint-react/eslint": "5.30.0-beta.20",
43
+ "@eslint-react/jsx": "5.30.0-beta.20",
44
+ "@eslint-react/shared": "5.30.0-beta.20",
45
+ "@typescript-eslint/types": "^8.72.1",
46
+ "@typescript-eslint/utils": "^8.72.1"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@local/configs": "0.0.0",
@@ -54,8 +54,8 @@
54
54
  "@types/react": "^19.3.0",
55
55
  "@types/react-dom": "^19.3.0",
56
56
  "dedent": "^1.7.2",
57
- "eslint": "^10.13.0",
58
- "tsdown": "^0.23.2",
57
+ "eslint": "^10.14.0",
58
+ "tsdown": "^0.23.4",
59
59
  "typescript": "6.0.3"
60
60
  },
61
61
  "peerDependencies": {