|
| 1 | +import { ruleTester } from "../../../../../test"; |
| 2 | +import rule, { RULE_NAME } from "./no-use-context"; |
| 3 | + |
| 4 | +ruleTester.run(RULE_NAME, rule, { |
| 5 | + invalid: [ |
| 6 | + { |
| 7 | + code: /* tsx */ ` |
| 8 | + import { useContext } from 'react' |
| 9 | +
|
| 10 | + export const Component = () => { |
| 11 | + const value = useContext(MyContext) |
| 12 | + return <div>{value}</div> |
| 13 | + } |
| 14 | + `, |
| 15 | + errors: [ |
| 16 | + { messageId: "noUseContext" }, |
| 17 | + { messageId: "noUseContext" }, |
| 18 | + ], |
| 19 | + output: /* tsx */ ` |
| 20 | + import { use } from 'react' |
| 21 | +
|
| 22 | + export const Component = () => { |
| 23 | + const value = use(MyContext) |
| 24 | + return <div>{value}</div> |
| 25 | + } |
| 26 | + `, |
| 27 | + settings: { |
| 28 | + "react-x": { |
| 29 | + version: "19.0.0", |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + code: /* tsx */ ` |
| 35 | + import { useContext } from 'react' |
| 36 | +
|
| 37 | + export const Component = () => { |
| 38 | + const value = useContext<MyContext>(MyContext) |
| 39 | + return <div>{value}</div> |
| 40 | + } |
| 41 | + `, |
| 42 | + errors: [ |
| 43 | + { messageId: "noUseContext" }, |
| 44 | + { messageId: "noUseContext" }, |
| 45 | + ], |
| 46 | + output: /* tsx */ ` |
| 47 | + import { use } from 'react' |
| 48 | +
|
| 49 | + export const Component = () => { |
| 50 | + const value = use<MyContext>(MyContext) |
| 51 | + return <div>{value}</div> |
| 52 | + } |
| 53 | + `, |
| 54 | + settings: { |
| 55 | + "react-x": { |
| 56 | + version: "19.0.0", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + code: /* tsx */ ` |
| 62 | + import { use, useContext } from 'react' |
| 63 | +
|
| 64 | + export const Component = () => { |
| 65 | + const value = useContext(MyContext) |
| 66 | + return <div>{value}</div> |
| 67 | + } |
| 68 | + `, |
| 69 | + errors: [ |
| 70 | + { messageId: "noUseContext" }, |
| 71 | + { messageId: "noUseContext" }, |
| 72 | + ], |
| 73 | + output: /* tsx */ ` |
| 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 React from 'react' |
| 90 | +
|
| 91 | + export const Component = () => { |
| 92 | + const value = React.useContext(MyContext) |
| 93 | + return <div>{value}</div> |
| 94 | + } |
| 95 | + `, |
| 96 | + errors: [ |
| 97 | + { messageId: "noUseContext" }, |
| 98 | + ], |
| 99 | + output: /* tsx */ ` |
| 100 | + import React from 'react' |
| 101 | +
|
| 102 | + export const Component = () => { |
| 103 | + const value = React.use(MyContext) |
| 104 | + return <div>{value}</div> |
| 105 | + } |
| 106 | + `, |
| 107 | + settings: { |
| 108 | + "react-x": { |
| 109 | + version: "19.0.0", |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + code: /* tsx */ ` |
| 115 | + import { use, useContext as useCtx } from 'react' |
| 116 | +
|
| 117 | + export const Component = () => { |
| 118 | + const value = useCtx(MyContext) |
| 119 | + return <div>{value}</div> |
| 120 | + } |
| 121 | + `, |
| 122 | + errors: [ |
| 123 | + { messageId: "noUseContext" }, |
| 124 | + { messageId: "noUseContext" }, |
| 125 | + ], |
| 126 | + output: /* tsx */ ` |
| 127 | + import { use, useContext as useCtx } from 'react' |
| 128 | +
|
| 129 | + export const Component = () => { |
| 130 | + const value = use(MyContext) |
| 131 | + return <div>{value}</div> |
| 132 | + } |
| 133 | + `, |
| 134 | + settings: { |
| 135 | + "react-x": { |
| 136 | + version: "19.0.0", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + ], |
| 141 | + valid: [ |
| 142 | + { |
| 143 | + code: /* tsx */ ` |
| 144 | + import { useContext } from 'react' |
| 145 | +
|
| 146 | + export const Component = () => { |
| 147 | + const value = useContext(MyContext) |
| 148 | + return <div>{value}</div> |
| 149 | + } |
| 150 | + `, |
| 151 | + settings: { |
| 152 | + "react-x": { |
| 153 | + version: "18.3.1", |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + { |
| 158 | + code: /* tsx */ ` |
| 159 | + import { use } from 'react' |
| 160 | +
|
| 161 | + export const Component = () => { |
| 162 | + const value = use(MyContext) |
| 163 | + return <div>{value}</div> |
| 164 | + } |
| 165 | + `, |
| 166 | + settings: { |
| 167 | + "react-x": { |
| 168 | + version: "19.0.0", |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + { |
| 173 | + code: /* tsx */ ` |
| 174 | + import React from 'react' |
| 175 | +
|
| 176 | + export const Component = () => { |
| 177 | + const value = React.use(MyContext) |
| 178 | + return <div>{value}</div> |
| 179 | + } |
| 180 | + `, |
| 181 | + settings: { |
| 182 | + "react-x": { |
| 183 | + version: "19.0.0", |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + ], |
| 188 | +}); |
0 commit comments