Skip to content

Commit 4ccd004

Browse files
committed
feat(no-use-context): improve auto-fix output format
1 parent da57163 commit 4ccd004

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-use-context.spec.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,61 @@ ruleTester.run(RULE_NAME, rule, {
7171
{ messageId: "noUseContext" },
7272
],
7373
output: /* tsx */ `
74-
import { use } from 'react'
74+
import { use } from 'react'
75+
76+
export const Component = () => {
77+
const value = use(MyContext)
78+
return <div>{value}</div>
79+
}
80+
`,
81+
settings: {
82+
"react-x": {
83+
version: "19.0.0",
84+
},
85+
},
86+
},
87+
{
88+
code: /* tsx */ `
89+
import { use, useContext, useState } from 'react'
90+
91+
export const Component = () => {
92+
const value = useContext(MyContext)
93+
return <div>{value}</div>
94+
}
95+
`,
96+
errors: [
97+
{ messageId: "noUseContext" },
98+
{ messageId: "noUseContext" },
99+
],
100+
output: /* tsx */ `
101+
import { use, useState } from 'react'
102+
103+
export const Component = () => {
104+
const value = use(MyContext)
105+
return <div>{value}</div>
106+
}
107+
`,
108+
settings: {
109+
"react-x": {
110+
version: "19.0.0",
111+
},
112+
},
113+
},
114+
{
115+
code: /* tsx */ `
116+
import {use,useContext,useState} from 'react'
117+
118+
export const Component = () => {
119+
const value = useContext(MyContext)
120+
return <div>{value}</div>
121+
}
122+
`,
123+
errors: [
124+
{ messageId: "noUseContext" },
125+
{ messageId: "noUseContext" },
126+
],
127+
output: /* tsx */ `
128+
import {use,useState} from 'react'
75129
76130
export const Component = () => {
77131
const value = use(MyContext)

packages/plugins/eslint-plugin-react-x/src/rules/no-use-context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ export default createRule<[], MessageID>({
8686
node: specifier,
8787
fix(fixer) {
8888
if (isUseImported) {
89+
const tokenBefore = context.sourceCode.getTokenBefore(specifier);
8990
return [
9091
fixer.remove(specifier),
92+
...tokenBefore?.value === ","
93+
? [fixer.replaceTextRange([tokenBefore.range[1], specifier.range[0]], "")]
94+
: [],
9195
...getAssociatedTokens(
9296
context,
9397
specifier,

0 commit comments

Comments
 (0)