@eslint-react/jsx 5.6.2 → 5.6.3-next.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 +31 -128
- package/dist/index.js +21 -211
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -472,82 +472,6 @@ declare function isFragmentElement(node: TSESTree.Node, jsxFragmentFactory?: str
|
|
|
472
472
|
*/
|
|
473
473
|
declare function isHostElement(node: TSESTree.Node): node is TSESTree.JSXElement;
|
|
474
474
|
//#endregion
|
|
475
|
-
//#region src/jsx-detection-hint.d.ts
|
|
476
|
-
/**
|
|
477
|
-
* BitFlags for configuring JSX detection behavior.
|
|
478
|
-
*
|
|
479
|
-
* Used by {@link isJsxLike} to control which AST node kinds are
|
|
480
|
-
* considered "JSX-like". Combine flags with the `|` operator.
|
|
481
|
-
*
|
|
482
|
-
* @example
|
|
483
|
-
* ```ts
|
|
484
|
-
* const hint = JsxDetectionHint.DoNotIncludeJsxWithBooleanValue
|
|
485
|
-
* | JsxDetectionHint.DoNotIncludeJsxWithStringValue;
|
|
486
|
-
*
|
|
487
|
-
* isJsxLike(context, node, hint);
|
|
488
|
-
* ```
|
|
489
|
-
*/
|
|
490
|
-
type JsxDetectionHint = bigint;
|
|
491
|
-
declare const JsxDetectionHint: {
|
|
492
|
-
readonly None: 0n;
|
|
493
|
-
readonly DoNotIncludeJsxWithNullValue: bigint;
|
|
494
|
-
readonly DoNotIncludeJsxWithNumberValue: bigint;
|
|
495
|
-
readonly DoNotIncludeJsxWithBigIntValue: bigint;
|
|
496
|
-
readonly DoNotIncludeJsxWithStringValue: bigint;
|
|
497
|
-
readonly DoNotIncludeJsxWithBooleanValue: bigint;
|
|
498
|
-
readonly DoNotIncludeJsxWithUndefinedValue: bigint;
|
|
499
|
-
readonly DoNotIncludeJsxWithEmptyArrayValue: bigint;
|
|
500
|
-
readonly DoNotIncludeJsxWithCreateElementValue: bigint;
|
|
501
|
-
readonly RequireAllArrayElementsToBeJsx: bigint;
|
|
502
|
-
readonly RequireBothSidesOfLogicalExpressionToBeJsx: bigint;
|
|
503
|
-
readonly RequireBothBranchesOfConditionalExpressionToBeJsx: bigint;
|
|
504
|
-
};
|
|
505
|
-
/**
|
|
506
|
-
* Default JSX detection configuration.
|
|
507
|
-
*
|
|
508
|
-
* Skips number, bigint, boolean, string, and undefined literals –
|
|
509
|
-
* the value types that are commonly returned alongside JSX in React
|
|
510
|
-
* components but are not themselves renderable elements.
|
|
511
|
-
*/
|
|
512
|
-
declare const DEFAULT_JSX_DETECTION_HINT: JsxDetectionHint;
|
|
513
|
-
//#endregion
|
|
514
|
-
//#region src/is-jsx-like.d.ts
|
|
515
|
-
/**
|
|
516
|
-
* Determine whether a node represents JSX-like content based on heuristics.
|
|
517
|
-
*
|
|
518
|
-
* The detection behaviour is configurable through {@link JsxDetectionHint}
|
|
519
|
-
* bit-flags so that callers can opt individual value kinds in or out.
|
|
520
|
-
*
|
|
521
|
-
* @param context - The ESLint rule context (needed for variable resolution).
|
|
522
|
-
* @param node - The AST node to analyse.
|
|
523
|
-
* @param hint - Optional bit-flags to adjust detection behaviour.
|
|
524
|
-
* Defaults to {@link DEFAULT_JSX_DETECTION_HINT}.
|
|
525
|
-
* @returns Whether the node is considered JSX-like.
|
|
526
|
-
*
|
|
527
|
-
* @example
|
|
528
|
-
* ```ts
|
|
529
|
-
* import { isJsxLike } from "@eslint-react/jsx";
|
|
530
|
-
*
|
|
531
|
-
* if (isJsxLike(context, node)) {
|
|
532
|
-
* // node looks like it evaluates to a React element
|
|
533
|
-
* }
|
|
534
|
-
* ```
|
|
535
|
-
*/
|
|
536
|
-
declare function isJsxLike(context: RuleContext, node: TSESTree.Node | null, hint?: JsxDetectionHint): boolean;
|
|
537
|
-
//#endregion
|
|
538
|
-
//#region src/is-jsx-text.d.ts
|
|
539
|
-
/**
|
|
540
|
-
* Check whether a node is a JSX text node.
|
|
541
|
-
*
|
|
542
|
-
* Returns `true` for both `JSXText` nodes and `Literal` nodes that appear
|
|
543
|
-
* as direct children of a JSX element (the parser may represent inline text
|
|
544
|
-
* with either node type depending on context).
|
|
545
|
-
*
|
|
546
|
-
* @param node - The AST node to test.
|
|
547
|
-
* @returns `true` when `node` is a `JSXText` or `Literal`.
|
|
548
|
-
*/
|
|
549
|
-
declare function isJsxText(node: TSESTree.Node | null): node is TSESTree.JSXText | TSESTree.Literal;
|
|
550
|
-
//#endregion
|
|
551
475
|
//#region src/is-whitespace.d.ts
|
|
552
476
|
/**
|
|
553
477
|
* Check whether a JSX child node is **whitespace padding** that React would
|
|
@@ -592,65 +516,44 @@ declare function isWhitespace(node: TSESTree.JSXChild): boolean;
|
|
|
592
516
|
*/
|
|
593
517
|
declare function isWhitespaceText(node: TSESTree.JSXChild): boolean;
|
|
594
518
|
//#endregion
|
|
595
|
-
//#region src/jsx-
|
|
596
|
-
/**
|
|
597
|
-
* TypeScript `jsx` compiler option values.
|
|
598
|
-
*
|
|
599
|
-
* Mirrors `ts.JsxEmit` so that consumers do not need a direct dependency on
|
|
600
|
-
* the TypeScript compiler.
|
|
601
|
-
*/
|
|
602
|
-
declare const JsxEmit: {
|
|
603
|
-
readonly None: 0;
|
|
604
|
-
readonly Preserve: 1;
|
|
605
|
-
readonly React: 2;
|
|
606
|
-
readonly ReactNative: 3;
|
|
607
|
-
readonly ReactJSX: 4;
|
|
608
|
-
readonly ReactJSXDev: 5;
|
|
609
|
-
};
|
|
610
|
-
/**
|
|
611
|
-
* Resolved JSX configuration derived from compiler options and / or pragma
|
|
612
|
-
* annotations found in the source file.
|
|
613
|
-
*/
|
|
614
|
-
interface JsxConfig {
|
|
615
|
-
jsx?: number;
|
|
616
|
-
jsxFactory?: string;
|
|
617
|
-
jsxFragmentFactory?: string;
|
|
618
|
-
jsxImportSource?: string;
|
|
619
|
-
}
|
|
519
|
+
//#region src/jsx-detection-hint.d.ts
|
|
620
520
|
/**
|
|
621
|
-
*
|
|
622
|
-
* parser services.
|
|
623
|
-
*
|
|
624
|
-
* Falls back to sensible React defaults when no compiler options are
|
|
625
|
-
* available (e.g. when the file is parsed without type information).
|
|
521
|
+
* BitFlags for configuring JSX detection behavior.
|
|
626
522
|
*
|
|
627
|
-
* @
|
|
628
|
-
*
|
|
629
|
-
*/
|
|
630
|
-
declare function getJsxConfigFromCompilerOptions(context: RuleContext): Required<JsxConfig>;
|
|
631
|
-
/**
|
|
632
|
-
* Extract JSX configuration from `@jsx`, `@jsxFrag`, `@jsxRuntime` and
|
|
633
|
-
* `@jsxImportSource` pragma comments in the source file.
|
|
523
|
+
* Used by {@link isJsxLike} to control which AST node kinds are
|
|
524
|
+
* considered "JSX-like". Combine flags with the `|` operator.
|
|
634
525
|
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```ts
|
|
528
|
+
* const hint = JsxDetectionHint.DoNotIncludeJsxWithBooleanValue
|
|
529
|
+
* | JsxDetectionHint.DoNotIncludeJsxWithStringValue;
|
|
637
530
|
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
531
|
+
* isJsxLike(context, node, hint);
|
|
532
|
+
* ```
|
|
640
533
|
*/
|
|
641
|
-
|
|
534
|
+
type JsxDetectionHint = bigint;
|
|
535
|
+
declare const JsxDetectionHint: {
|
|
536
|
+
readonly None: 0n;
|
|
537
|
+
readonly DoNotIncludeJsxWithNullValue: bigint;
|
|
538
|
+
readonly DoNotIncludeJsxWithNumberValue: bigint;
|
|
539
|
+
readonly DoNotIncludeJsxWithBigIntValue: bigint;
|
|
540
|
+
readonly DoNotIncludeJsxWithStringValue: bigint;
|
|
541
|
+
readonly DoNotIncludeJsxWithBooleanValue: bigint;
|
|
542
|
+
readonly DoNotIncludeJsxWithUndefinedValue: bigint;
|
|
543
|
+
readonly DoNotIncludeJsxWithEmptyArrayValue: bigint;
|
|
544
|
+
readonly DoNotIncludeJsxWithCreateElementValue: bigint;
|
|
545
|
+
readonly RequireAllArrayElementsToBeJsx: bigint;
|
|
546
|
+
readonly RequireBothSidesOfLogicalExpressionToBeJsx: bigint;
|
|
547
|
+
readonly RequireBothBranchesOfConditionalExpressionToBeJsx: bigint;
|
|
548
|
+
};
|
|
642
549
|
/**
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
* Compiler options provide the base values; pragma annotations found in the
|
|
646
|
-
* source override them where present. The result is cached per `sourceCode`.
|
|
647
|
-
*
|
|
648
|
-
* This is the main entry‑point most consumers should use.
|
|
550
|
+
* Default JSX detection configuration.
|
|
649
551
|
*
|
|
650
|
-
*
|
|
651
|
-
*
|
|
552
|
+
* Skips number, bigint, boolean, string, and undefined literals –
|
|
553
|
+
* the value types that are commonly returned alongside JSX in React
|
|
554
|
+
* components but are not themselves renderable elements.
|
|
652
555
|
*/
|
|
653
|
-
declare
|
|
556
|
+
declare const DEFAULT_JSX_DETECTION_HINT: JsxDetectionHint;
|
|
654
557
|
//#endregion
|
|
655
558
|
//#region src/resolve-attribute-value.d.ts
|
|
656
559
|
/**
|
|
@@ -680,4 +583,4 @@ declare function getJsxConfig(context: RuleContext): Required<JsxConfig>;
|
|
|
680
583
|
*/
|
|
681
584
|
declare function resolveAttributeValue(context: RuleContext, attribute: TSESTreeJSXAttributeLike): JsxAttributeValue;
|
|
682
585
|
//#endregion
|
|
683
|
-
export { DEFAULT_JSX_DETECTION_HINT, ElementTest, JsxAttributeValue,
|
|
586
|
+
export { DEFAULT_JSX_DETECTION_HINT, ElementTest, JsxAttributeValue, JsxDetectionHint, findAttribute, findParentAttribute, getAttributeName, getAttributeStaticValue, getAttributeValue, getChildren, getElementFullType, getElementSelfType, hasAnyAttribute, hasAttribute, hasChildren, hasEveryAttribute, isElement, isFragmentElement, isHostElement, isWhitespace, isWhitespaceText, resolveAttributeValue };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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
5
|
import { P, match } from "ts-pattern";
|
|
6
|
-
import { RE_ANNOTATION_JSX, RE_ANNOTATION_JSX_FRAG, RE_ANNOTATION_JSX_IMPORT_SOURCE, RE_ANNOTATION_JSX_RUNTIME } from "@eslint-react/shared";
|
|
7
6
|
|
|
8
7
|
//#region src/get-attribute-name.ts
|
|
9
8
|
/**
|
|
@@ -615,121 +614,6 @@ function isHostElement(node) {
|
|
|
615
614
|
return node.type === AST_NODE_TYPES.JSXElement && node.openingElement.name.type === AST_NODE_TYPES.JSXIdentifier && /^[a-z]/u.test(node.openingElement.name.name);
|
|
616
615
|
}
|
|
617
616
|
|
|
618
|
-
//#endregion
|
|
619
|
-
//#region src/jsx-detection-hint.ts
|
|
620
|
-
const JsxDetectionHint = {
|
|
621
|
-
None: 0n,
|
|
622
|
-
DoNotIncludeJsxWithNullValue: 1n << 0n,
|
|
623
|
-
DoNotIncludeJsxWithNumberValue: 1n << 1n,
|
|
624
|
-
DoNotIncludeJsxWithBigIntValue: 1n << 2n,
|
|
625
|
-
DoNotIncludeJsxWithStringValue: 1n << 3n,
|
|
626
|
-
DoNotIncludeJsxWithBooleanValue: 1n << 4n,
|
|
627
|
-
DoNotIncludeJsxWithUndefinedValue: 1n << 5n,
|
|
628
|
-
DoNotIncludeJsxWithEmptyArrayValue: 1n << 6n,
|
|
629
|
-
DoNotIncludeJsxWithCreateElementValue: 1n << 7n,
|
|
630
|
-
RequireAllArrayElementsToBeJsx: 1n << 8n,
|
|
631
|
-
RequireBothSidesOfLogicalExpressionToBeJsx: 1n << 9n,
|
|
632
|
-
RequireBothBranchesOfConditionalExpressionToBeJsx: 1n << 10n
|
|
633
|
-
};
|
|
634
|
-
/**
|
|
635
|
-
* Default JSX detection configuration.
|
|
636
|
-
*
|
|
637
|
-
* Skips number, bigint, boolean, string, and undefined literals –
|
|
638
|
-
* the value types that are commonly returned alongside JSX in React
|
|
639
|
-
* components but are not themselves renderable elements.
|
|
640
|
-
*/
|
|
641
|
-
const DEFAULT_JSX_DETECTION_HINT = 0n | JsxDetectionHint.DoNotIncludeJsxWithNumberValue | JsxDetectionHint.DoNotIncludeJsxWithBigIntValue | JsxDetectionHint.DoNotIncludeJsxWithBooleanValue | JsxDetectionHint.DoNotIncludeJsxWithStringValue | JsxDetectionHint.DoNotIncludeJsxWithUndefinedValue;
|
|
642
|
-
|
|
643
|
-
//#endregion
|
|
644
|
-
//#region src/is-jsx-like.ts
|
|
645
|
-
/**
|
|
646
|
-
* Determine whether a node represents JSX-like content based on heuristics.
|
|
647
|
-
*
|
|
648
|
-
* The detection behaviour is configurable through {@link JsxDetectionHint}
|
|
649
|
-
* bit-flags so that callers can opt individual value kinds in or out.
|
|
650
|
-
*
|
|
651
|
-
* @param context - The ESLint rule context (needed for variable resolution).
|
|
652
|
-
* @param node - The AST node to analyse.
|
|
653
|
-
* @param hint - Optional bit-flags to adjust detection behaviour.
|
|
654
|
-
* Defaults to {@link DEFAULT_JSX_DETECTION_HINT}.
|
|
655
|
-
* @returns Whether the node is considered JSX-like.
|
|
656
|
-
*
|
|
657
|
-
* @example
|
|
658
|
-
* ```ts
|
|
659
|
-
* import { isJsxLike } from "@eslint-react/jsx";
|
|
660
|
-
*
|
|
661
|
-
* if (isJsxLike(context, node)) {
|
|
662
|
-
* // node looks like it evaluates to a React element
|
|
663
|
-
* }
|
|
664
|
-
* ```
|
|
665
|
-
*/
|
|
666
|
-
function isJsxLike(context, node, hint = DEFAULT_JSX_DETECTION_HINT) {
|
|
667
|
-
if (node == null) return false;
|
|
668
|
-
if (Check.isJSX(node)) return true;
|
|
669
|
-
switch (node.type) {
|
|
670
|
-
case AST_NODE_TYPES.Literal:
|
|
671
|
-
switch (typeof node.value) {
|
|
672
|
-
case "boolean": return !(hint & JsxDetectionHint.DoNotIncludeJsxWithBooleanValue);
|
|
673
|
-
case "string": return !(hint & JsxDetectionHint.DoNotIncludeJsxWithStringValue);
|
|
674
|
-
case "number": return !(hint & JsxDetectionHint.DoNotIncludeJsxWithNumberValue);
|
|
675
|
-
case "bigint": return !(hint & JsxDetectionHint.DoNotIncludeJsxWithBigIntValue);
|
|
676
|
-
}
|
|
677
|
-
if (node.value == null) return !(hint & JsxDetectionHint.DoNotIncludeJsxWithNullValue);
|
|
678
|
-
return false;
|
|
679
|
-
case AST_NODE_TYPES.TemplateLiteral: return !(hint & JsxDetectionHint.DoNotIncludeJsxWithStringValue);
|
|
680
|
-
case AST_NODE_TYPES.ArrayExpression:
|
|
681
|
-
if (node.elements.length === 0) return !(hint & JsxDetectionHint.DoNotIncludeJsxWithEmptyArrayValue);
|
|
682
|
-
if (hint & JsxDetectionHint.RequireAllArrayElementsToBeJsx) return node.elements.every((n) => isJsxLike(context, n, hint));
|
|
683
|
-
return node.elements.some((n) => isJsxLike(context, n, hint));
|
|
684
|
-
case AST_NODE_TYPES.LogicalExpression:
|
|
685
|
-
if (hint & JsxDetectionHint.RequireBothSidesOfLogicalExpressionToBeJsx) return isJsxLike(context, node.left, hint) && isJsxLike(context, node.right, hint);
|
|
686
|
-
return isJsxLike(context, node.left, hint) || isJsxLike(context, node.right, hint);
|
|
687
|
-
case AST_NODE_TYPES.ConditionalExpression: {
|
|
688
|
-
const consequentIsJsx = Array.isArray(node.consequent) ? checkArray(context, node.consequent, hint) : isJsxLike(context, node.consequent, hint);
|
|
689
|
-
const alternateIsJsx = isJsxLike(context, node.alternate, hint);
|
|
690
|
-
if (hint & JsxDetectionHint.RequireBothBranchesOfConditionalExpressionToBeJsx) return consequentIsJsx && alternateIsJsx;
|
|
691
|
-
return consequentIsJsx || alternateIsJsx;
|
|
692
|
-
}
|
|
693
|
-
case AST_NODE_TYPES.SequenceExpression: return isJsxLike(context, node.expressions.at(-1) ?? null, hint);
|
|
694
|
-
case AST_NODE_TYPES.CallExpression: {
|
|
695
|
-
if (hint & JsxDetectionHint.DoNotIncludeJsxWithCreateElementValue) return false;
|
|
696
|
-
const callee = Extract.unwrap(node.callee);
|
|
697
|
-
switch (callee.type) {
|
|
698
|
-
case AST_NODE_TYPES.Identifier: return callee.name === "createElement";
|
|
699
|
-
case AST_NODE_TYPES.MemberExpression: return callee.property.type === AST_NODE_TYPES.Identifier && callee.property.name === "createElement";
|
|
700
|
-
}
|
|
701
|
-
return false;
|
|
702
|
-
}
|
|
703
|
-
case AST_NODE_TYPES.Identifier:
|
|
704
|
-
if (node.name === "undefined") return !(hint & JsxDetectionHint.DoNotIncludeJsxWithUndefinedValue);
|
|
705
|
-
if (Check.isJSXTagNameExpression(node)) return true;
|
|
706
|
-
return isJsxLike(context, resolve(context, node), hint);
|
|
707
|
-
}
|
|
708
|
-
return false;
|
|
709
|
-
}
|
|
710
|
-
function checkArray(context, elements, hint) {
|
|
711
|
-
if (elements.length === 0) return !(hint & JsxDetectionHint.DoNotIncludeJsxWithEmptyArrayValue);
|
|
712
|
-
if (hint & JsxDetectionHint.RequireAllArrayElementsToBeJsx) return elements.every((n) => isJsxLike(context, n, hint));
|
|
713
|
-
return elements.some((n) => isJsxLike(context, n, hint));
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
//#endregion
|
|
717
|
-
//#region src/is-jsx-text.ts
|
|
718
|
-
/**
|
|
719
|
-
* Check whether a node is a JSX text node.
|
|
720
|
-
*
|
|
721
|
-
* Returns `true` for both `JSXText` nodes and `Literal` nodes that appear
|
|
722
|
-
* as direct children of a JSX element (the parser may represent inline text
|
|
723
|
-
* with either node type depending on context).
|
|
724
|
-
*
|
|
725
|
-
* @param node - The AST node to test.
|
|
726
|
-
* @returns `true` when `node` is a `JSXText` or `Literal`.
|
|
727
|
-
*/
|
|
728
|
-
function isJsxText(node) {
|
|
729
|
-
if (node == null) return false;
|
|
730
|
-
return node.type === AST_NODE_TYPES.JSXText || node.type === AST_NODE_TYPES.Literal;
|
|
731
|
-
}
|
|
732
|
-
|
|
733
617
|
//#endregion
|
|
734
618
|
//#region src/is-whitespace.ts
|
|
735
619
|
/**
|
|
@@ -782,103 +666,29 @@ function isWhitespaceText(node) {
|
|
|
782
666
|
}
|
|
783
667
|
|
|
784
668
|
//#endregion
|
|
785
|
-
//#region src/jsx-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
669
|
+
//#region src/jsx-detection-hint.ts
|
|
670
|
+
const JsxDetectionHint = {
|
|
671
|
+
None: 0n,
|
|
672
|
+
DoNotIncludeJsxWithNullValue: 1n << 0n,
|
|
673
|
+
DoNotIncludeJsxWithNumberValue: 1n << 1n,
|
|
674
|
+
DoNotIncludeJsxWithBigIntValue: 1n << 2n,
|
|
675
|
+
DoNotIncludeJsxWithStringValue: 1n << 3n,
|
|
676
|
+
DoNotIncludeJsxWithBooleanValue: 1n << 4n,
|
|
677
|
+
DoNotIncludeJsxWithUndefinedValue: 1n << 5n,
|
|
678
|
+
DoNotIncludeJsxWithEmptyArrayValue: 1n << 6n,
|
|
679
|
+
DoNotIncludeJsxWithCreateElementValue: 1n << 7n,
|
|
680
|
+
RequireAllArrayElementsToBeJsx: 1n << 8n,
|
|
681
|
+
RequireBothSidesOfLogicalExpressionToBeJsx: 1n << 9n,
|
|
682
|
+
RequireBothBranchesOfConditionalExpressionToBeJsx: 1n << 10n
|
|
799
683
|
};
|
|
800
684
|
/**
|
|
801
|
-
*
|
|
802
|
-
* pragma‑scanning pass runs at most once per file.
|
|
803
|
-
*/
|
|
804
|
-
const annotationCache = /* @__PURE__ */ new WeakMap();
|
|
805
|
-
/**
|
|
806
|
-
* Weak‑map cache for the fully‑merged config (compiler options + annotation).
|
|
807
|
-
*/
|
|
808
|
-
const mergedCache = /* @__PURE__ */ new WeakMap();
|
|
809
|
-
/**
|
|
810
|
-
* Read JSX configuration from the TypeScript compiler options exposed by the
|
|
811
|
-
* parser services.
|
|
812
|
-
*
|
|
813
|
-
* Falls back to sensible React defaults when no compiler options are
|
|
814
|
-
* available (e.g. when the file is parsed without type information).
|
|
815
|
-
*
|
|
816
|
-
* @param context - The ESLint rule context.
|
|
817
|
-
* @returns Fully‑populated `JsxConfig` derived from compiler options.
|
|
818
|
-
*/
|
|
819
|
-
function getJsxConfigFromCompilerOptions(context) {
|
|
820
|
-
const options = context.sourceCode.parserServices?.program?.getCompilerOptions() ?? {};
|
|
821
|
-
return {
|
|
822
|
-
jsx: options.jsx ?? JsxEmit.ReactJSX,
|
|
823
|
-
jsxFactory: options.jsxFactory ?? "React.createElement",
|
|
824
|
-
jsxFragmentFactory: options.jsxFragmentFactory ?? "React.Fragment",
|
|
825
|
-
jsxImportSource: options.jsxImportSource ?? "react"
|
|
826
|
-
};
|
|
827
|
-
}
|
|
828
|
-
/**
|
|
829
|
-
* Extract JSX configuration from `@jsx`, `@jsxFrag`, `@jsxRuntime` and
|
|
830
|
-
* `@jsxImportSource` pragma comments in the source file.
|
|
831
|
-
*
|
|
832
|
-
* The result is cached per `sourceCode` instance via a `WeakMap` so that
|
|
833
|
-
* repeated calls from different rules analysing the same file are free.
|
|
834
|
-
*
|
|
835
|
-
* @param context - The ESLint rule context.
|
|
836
|
-
* @returns Partial `JsxConfig` containing only the values found in pragmas.
|
|
837
|
-
*/
|
|
838
|
-
function getJsxConfigFromAnnotation(context) {
|
|
839
|
-
const cached = annotationCache.get(context.sourceCode);
|
|
840
|
-
if (cached != null) return cached;
|
|
841
|
-
const options = {};
|
|
842
|
-
if (!context.sourceCode.text.includes("@jsx")) {
|
|
843
|
-
annotationCache.set(context.sourceCode, options);
|
|
844
|
-
return options;
|
|
845
|
-
}
|
|
846
|
-
let jsx, jsxFrag, jsxRuntime, jsxImportSource;
|
|
847
|
-
for (const comment of context.sourceCode.getAllComments().reverse()) {
|
|
848
|
-
const value = comment.value;
|
|
849
|
-
jsx ??= value.match(RE_ANNOTATION_JSX)?.[1];
|
|
850
|
-
jsxFrag ??= value.match(RE_ANNOTATION_JSX_FRAG)?.[1];
|
|
851
|
-
jsxRuntime ??= value.match(RE_ANNOTATION_JSX_RUNTIME)?.[1];
|
|
852
|
-
jsxImportSource ??= value.match(RE_ANNOTATION_JSX_IMPORT_SOURCE)?.[1];
|
|
853
|
-
}
|
|
854
|
-
if (jsx != null) options.jsxFactory = jsx;
|
|
855
|
-
if (jsxFrag != null) options.jsxFragmentFactory = jsxFrag;
|
|
856
|
-
if (jsxRuntime != null) options.jsx = jsxRuntime === "classic" ? JsxEmit.React : JsxEmit.ReactJSX;
|
|
857
|
-
if (jsxImportSource != null) options.jsxImportSource = jsxImportSource;
|
|
858
|
-
annotationCache.set(context.sourceCode, options);
|
|
859
|
-
return options;
|
|
860
|
-
}
|
|
861
|
-
/**
|
|
862
|
-
* Get the fully‑merged JSX configuration for the current file.
|
|
863
|
-
*
|
|
864
|
-
* Compiler options provide the base values; pragma annotations found in the
|
|
865
|
-
* source override them where present. The result is cached per `sourceCode`.
|
|
866
|
-
*
|
|
867
|
-
* This is the main entry‑point most consumers should use.
|
|
685
|
+
* Default JSX detection configuration.
|
|
868
686
|
*
|
|
869
|
-
*
|
|
870
|
-
*
|
|
687
|
+
* Skips number, bigint, boolean, string, and undefined literals –
|
|
688
|
+
* the value types that are commonly returned alongside JSX in React
|
|
689
|
+
* components but are not themselves renderable elements.
|
|
871
690
|
*/
|
|
872
|
-
|
|
873
|
-
const cached = mergedCache.get(context.sourceCode);
|
|
874
|
-
if (cached != null) return cached;
|
|
875
|
-
const merged = {
|
|
876
|
-
...getJsxConfigFromCompilerOptions(context),
|
|
877
|
-
...getJsxConfigFromAnnotation(context)
|
|
878
|
-
};
|
|
879
|
-
mergedCache.set(context.sourceCode, merged);
|
|
880
|
-
return merged;
|
|
881
|
-
}
|
|
691
|
+
const DEFAULT_JSX_DETECTION_HINT = 0n | JsxDetectionHint.DoNotIncludeJsxWithNumberValue | JsxDetectionHint.DoNotIncludeJsxWithBigIntValue | JsxDetectionHint.DoNotIncludeJsxWithBooleanValue | JsxDetectionHint.DoNotIncludeJsxWithStringValue | JsxDetectionHint.DoNotIncludeJsxWithUndefinedValue;
|
|
882
692
|
|
|
883
693
|
//#endregion
|
|
884
|
-
export { DEFAULT_JSX_DETECTION_HINT, JsxDetectionHint,
|
|
694
|
+
export { DEFAULT_JSX_DETECTION_HINT, JsxDetectionHint, findAttribute, findParentAttribute, getAttributeName, getAttributeStaticValue, getAttributeValue, getChildren, getElementFullType, getElementSelfType, hasAnyAttribute, hasAttribute, hasChildren, hasEveryAttribute, isElement, isFragmentElement, isHostElement, isWhitespace, isWhitespaceText, resolveAttributeValue };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eslint-react/jsx",
|
|
3
|
-
"version": "5.6.
|
|
3
|
+
"version": "5.6.3-next.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": {
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"@typescript-eslint/types": "^8.59.1",
|
|
33
33
|
"@typescript-eslint/utils": "^8.59.1",
|
|
34
34
|
"ts-pattern": "^5.9.0",
|
|
35
|
-
"@eslint-react/ast": "5.6.
|
|
36
|
-
"@eslint-react/
|
|
37
|
-
"@eslint-react/
|
|
38
|
-
"@eslint-react/
|
|
35
|
+
"@eslint-react/ast": "5.6.3-next.0",
|
|
36
|
+
"@eslint-react/eslint": "5.6.3-next.0",
|
|
37
|
+
"@eslint-react/shared": "5.6.3-next.0",
|
|
38
|
+
"@eslint-react/var": "5.6.3-next.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"eslint": "^10.2.1",
|