@@ -2,13 +2,7 @@ import type { TSESTree } from "@typescript-eslint/types";
2
2
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint" ;
3
3
4
4
import type { CamelCase } from "string-ts" ;
5
- import {
6
- JsxRuntime ,
7
- RE_JSX_ANNOTATION ,
8
- RE_JSX_FRAG_ANNOTATION ,
9
- type RuleContext ,
10
- type RuleFeature ,
11
- } from "@eslint-react/kit" ;
5
+ import { JsxRuntime , type RuleContext , type RuleFeature } from "@eslint-react/kit" ;
12
6
import { JsxEmit } from "typescript" ;
13
7
import { createRule } from "../utils" ;
14
8
@@ -18,13 +12,13 @@ export const RULE_FEATURES = [] as const satisfies RuleFeature[];
18
12
19
13
export type MessageID = CamelCase < typeof RULE_NAME > ;
20
14
21
- export const debug = true ;
15
+ export const debug = false ;
22
16
23
17
export default createRule < [ ] , MessageID > ( {
24
18
meta : {
25
19
type : "problem" ,
26
20
docs : {
27
- description : "Marks React variables as used when JSX is used in the file ." ,
21
+ description : "Marks React variables as used when JSX is used." ,
28
22
[ Symbol . for ( "rule_features" ) ] : RULE_FEATURES ,
29
23
} ,
30
24
messages : {
@@ -38,32 +32,24 @@ export default createRule<[], MessageID>({
38
32
} ) ;
39
33
40
34
export function create ( context : RuleContext < MessageID , [ ] > ) : RuleListener {
41
- const { jsx, jsxFactory, jsxFragmentFactory, reactNamespace } = JsxRuntime . getJsxRuntimeOptionsFromContext ( context ) ;
42
- const jsxAnnotation = getJsxAnnotation ( context ) ;
35
+ const jsxOptions = JsxRuntime . getJsxRuntimeOptions ( context ) ;
36
+ const jsxAnnotation = JsxRuntime . getJsxRuntimeAnnotation ( context ) ;
37
+ const jsx = jsxAnnotation . jsxRuntime === "classic"
38
+ ? JsxEmit . React
39
+ : jsxOptions . jsx ;
43
40
if ( jsx === JsxEmit . ReactJSX || jsx === JsxEmit . ReactJSXDev ) return { } ;
44
41
42
+ const jsxFactory = jsxAnnotation . jsx ?? jsxOptions . jsxFactory ;
43
+ const jsxFragmentFactory = jsxAnnotation . jsxFrag ?? jsxOptions . jsxFragmentFactory ;
44
+
45
45
function handleJsxElement ( node : TSESTree . Node ) {
46
- if ( jsxAnnotation == null ) {
47
- context . sourceCode . markVariableAsUsed ( reactNamespace , node ) ;
48
- context . sourceCode . markVariableAsUsed ( jsxFactory , node ) ;
49
- debugReport ( context , node , reactNamespace ) ;
50
- debugReport ( context , node , jsxFactory ) ;
51
- }
52
- if ( jsxAnnotation ?. jsx != null ) {
53
- context . sourceCode . markVariableAsUsed ( jsxAnnotation . jsx , node ) ;
54
- debugReport ( context , node , jsxAnnotation . jsx ) ;
55
- }
46
+ context . sourceCode . markVariableAsUsed ( jsxFactory , node ) ;
47
+ debugReport ( context , node , jsxFactory ) ;
56
48
}
57
49
58
50
function handleJsxFragment ( node : TSESTree . Node ) {
59
- if ( jsxAnnotation == null ) {
60
- context . sourceCode . markVariableAsUsed ( jsxFragmentFactory , node ) ;
61
- debugReport ( context , node , jsxFragmentFactory ) ;
62
- }
63
- if ( jsxAnnotation ?. jsxFrag != null ) {
64
- context . sourceCode . markVariableAsUsed ( jsxAnnotation . jsxFrag , node ) ;
65
- debugReport ( context , node , jsxAnnotation . jsxFrag ) ;
66
- }
51
+ context . sourceCode . markVariableAsUsed ( jsxFragmentFactory , node ) ;
52
+ debugReport ( context , node , jsxFragmentFactory ) ;
67
53
}
68
54
69
55
return {
@@ -73,19 +59,6 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
73
59
} ;
74
60
}
75
61
76
- function getJsxAnnotation ( context : RuleContext ) {
77
- if ( ! context . sourceCode . text . includes ( "@jsx" ) ) return ;
78
- const allComments = context . sourceCode . getAllComments ( ) ;
79
- const jsxComment = allComments . find ( ( n ) => RE_JSX_ANNOTATION . test ( n . value ) ) ;
80
- const jsxFragComment = allComments . find ( ( n ) => RE_JSX_FRAG_ANNOTATION . test ( n . value ) ) ;
81
- const jsx = jsxComment ?. value . match ( RE_JSX_ANNOTATION ) ?. [ 1 ] ;
82
- const jsxFrag = jsxFragComment ?. value . match ( RE_JSX_FRAG_ANNOTATION ) ?. [ 1 ] ;
83
- return {
84
- jsx,
85
- jsxFrag,
86
- } ;
87
- }
88
-
89
62
function debugReport ( context : RuleContext , node : TSESTree . Node , name : string ) {
90
63
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
91
64
if ( ! debug ) return ;
0 commit comments