|
1 | 1 | import tsx from "dedent"; |
2 | 2 | import { ruleTester } from "tsl/ruleTester"; |
| 3 | +import ts from "typescript"; |
3 | 4 | import { expect, test } from "vitest"; |
4 | 5 |
|
5 | 6 | import { messages, rulesOfJsx } from "./rules-of-jsx"; |
6 | 7 |
|
| 8 | +const compilerOptions = { |
| 9 | + strict: true, |
| 10 | + jsx: ts.JsxEmit.ReactJSX, |
| 11 | +} as const satisfies ts.CompilerOptions; |
| 12 | + |
7 | 13 | test("rules-of-jsx", () => { |
8 | 14 | const ret = ruleTester({ |
9 | | - invalid: [], |
10 | | - ruleFn: rulesOfJsx, |
11 | 15 | tsx: true, |
12 | | - valid: [], |
| 16 | + ruleFn: rulesOfJsx, |
| 17 | + invalid: [ |
| 18 | + // Rule000: Don't pass children as a prop |
| 19 | + { |
| 20 | + code: tsx` |
| 21 | + function Component() { |
| 22 | + return <div children="child" />; |
| 23 | + } |
| 24 | + `, |
| 25 | + compilerOptions, |
| 26 | + errors: [ |
| 27 | + { message: "Don't pass children as a prop. Instead, put the children between the opening and closing tags." }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + // Rule001: key after spread |
| 31 | + { |
| 32 | + code: tsx` |
| 33 | + function Component({ props }) { |
| 34 | + return <div {...props} key="id" />; |
| 35 | + } |
| 36 | + `, |
| 37 | + compilerOptions, |
| 38 | + errors: [ |
| 39 | + { message: "The 'key' prop must be placed before any spread props when using the new JSX transform." }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + { |
| 43 | + code: tsx` |
| 44 | + function Component({ props1, props2 }) { |
| 45 | + return <div {...props1} {...props2} key="id" />; |
| 46 | + } |
| 47 | + `, |
| 48 | + compilerOptions, |
| 49 | + errors: [ |
| 50 | + { message: "The 'key' prop must be placed before any spread props when using the new JSX transform." }, |
| 51 | + ], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: tsx` |
| 55 | + function Component({ props }) { |
| 56 | + return <div className="test" {...props} key="id" />; |
| 57 | + } |
| 58 | + `, |
| 59 | + compilerOptions, |
| 60 | + errors: [ |
| 61 | + { message: "The 'key' prop must be placed before any spread props when using the new JSX transform." }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + { |
| 65 | + code: tsx` |
| 66 | + function Component({ props }) { |
| 67 | + return <div {...props} key="id" className="test" />; |
| 68 | + } |
| 69 | + `, |
| 70 | + compilerOptions, |
| 71 | + errors: [ |
| 72 | + { message: "The 'key' prop must be placed before any spread props when using the new JSX transform." }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + // key after spread (with ref too, but only key triggers Rule000) |
| 76 | + { |
| 77 | + code: tsx` |
| 78 | + function Component({ props }) { |
| 79 | + return <div {...props} key="id" ref={null} />; |
| 80 | + } |
| 81 | + `, |
| 82 | + compilerOptions, |
| 83 | + errors: [ |
| 84 | + { message: "The 'key' prop must be placed before any spread props when using the new JSX transform." }, |
| 85 | + ], |
| 86 | + }, |
| 87 | + ], |
| 88 | + valid: [ |
| 89 | + // key before spread |
| 90 | + { |
| 91 | + code: tsx` |
| 92 | + function Component({ props }) { |
| 93 | + return <div key="id" {...props} />; |
| 94 | + } |
| 95 | + `, |
| 96 | + compilerOptions, |
| 97 | + }, |
| 98 | + // ref before spread |
| 99 | + { |
| 100 | + code: tsx` |
| 101 | + function Component({ props }) { |
| 102 | + return <div ref={null} {...props} />; |
| 103 | + } |
| 104 | + `, |
| 105 | + compilerOptions, |
| 106 | + }, |
| 107 | + // key and ref before spread |
| 108 | + { |
| 109 | + code: tsx` |
| 110 | + function Component({ props }) { |
| 111 | + return <div key="id" ref={null} {...props} />; |
| 112 | + } |
| 113 | + `, |
| 114 | + compilerOptions, |
| 115 | + }, |
| 116 | + // no spread |
| 117 | + { |
| 118 | + code: tsx` |
| 119 | + function Component() { |
| 120 | + return <div key="id" />; |
| 121 | + } |
| 122 | + `, |
| 123 | + compilerOptions, |
| 124 | + }, |
| 125 | + { |
| 126 | + code: tsx` |
| 127 | + function Component() { |
| 128 | + return <div ref={null} />; |
| 129 | + } |
| 130 | + `, |
| 131 | + compilerOptions, |
| 132 | + }, |
| 133 | + // spread without key or ref |
| 134 | + { |
| 135 | + code: tsx` |
| 136 | + function Component({ props }) { |
| 137 | + return <div {...props} />; |
| 138 | + } |
| 139 | + `, |
| 140 | + compilerOptions, |
| 141 | + }, |
| 142 | + { |
| 143 | + code: tsx` |
| 144 | + function Component({ props }) { |
| 145 | + return <div key="id" key="id2" {...props} />; |
| 146 | + } |
| 147 | + `, |
| 148 | + compilerOptions, |
| 149 | + }, |
| 150 | + { |
| 151 | + code: tsx` |
| 152 | + function Component({ props }) { |
| 153 | + return <div className="test" {...props} />; |
| 154 | + } |
| 155 | + `, |
| 156 | + compilerOptions, |
| 157 | + }, |
| 158 | + // ref after spread (no rule for this — only key-after-spread is checked) |
| 159 | + { |
| 160 | + code: tsx` |
| 161 | + function Component({ props }) { |
| 162 | + return <div {...props} ref={null} />; |
| 163 | + } |
| 164 | + `, |
| 165 | + compilerOptions, |
| 166 | + }, |
| 167 | + { |
| 168 | + code: tsx` |
| 169 | + function Component({ props1, props2 }) { |
| 170 | + return <div {...props1} {...props2} ref={null} />; |
| 171 | + } |
| 172 | + `, |
| 173 | + compilerOptions, |
| 174 | + }, |
| 175 | + { |
| 176 | + code: tsx` |
| 177 | + function Component({ props }) { |
| 178 | + return <div className="test" {...props} ref={null} />; |
| 179 | + } |
| 180 | + `, |
| 181 | + compilerOptions, |
| 182 | + }, |
| 183 | + { |
| 184 | + code: tsx` |
| 185 | + function Component({ props }) { |
| 186 | + return <div {...props} ref={null} className="test" />; |
| 187 | + } |
| 188 | + `, |
| 189 | + compilerOptions, |
| 190 | + }, |
| 191 | + // non-new JSX transforms: key after spread is OK |
| 192 | + { |
| 193 | + code: tsx` |
| 194 | + function Component({ props }) { |
| 195 | + return <div {...props} key="id" />; |
| 196 | + } |
| 197 | + `, |
| 198 | + compilerOptions: { |
| 199 | + strict: true, |
| 200 | + jsx: ts.JsxEmit.None, |
| 201 | + }, |
| 202 | + }, |
| 203 | + { |
| 204 | + code: tsx` |
| 205 | + function Component({ props }) { |
| 206 | + return <div {...props} key="id" />; |
| 207 | + } |
| 208 | + `, |
| 209 | + compilerOptions: { |
| 210 | + strict: true, |
| 211 | + jsx: ts.JsxEmit.Preserve, |
| 212 | + }, |
| 213 | + }, |
| 214 | + { |
| 215 | + code: tsx` |
| 216 | + function Component({ props }) { |
| 217 | + return <div {...props} key="id" />; |
| 218 | + } |
| 219 | + `, |
| 220 | + compilerOptions: { |
| 221 | + strict: true, |
| 222 | + jsx: ts.JsxEmit.ReactNative, |
| 223 | + }, |
| 224 | + }, |
| 225 | + // non-new JSX transforms: ref after spread is OK |
| 226 | + { |
| 227 | + code: tsx` |
| 228 | + function Component({ props }) { |
| 229 | + return <div {...props} ref={null} />; |
| 230 | + } |
| 231 | + `, |
| 232 | + compilerOptions: { |
| 233 | + strict: true, |
| 234 | + jsx: ts.JsxEmit.None, |
| 235 | + }, |
| 236 | + }, |
| 237 | + { |
| 238 | + code: tsx` |
| 239 | + function Component({ props }) { |
| 240 | + return <div {...props} ref={null} />; |
| 241 | + } |
| 242 | + `, |
| 243 | + compilerOptions: { |
| 244 | + strict: true, |
| 245 | + jsx: ts.JsxEmit.Preserve, |
| 246 | + }, |
| 247 | + }, |
| 248 | + { |
| 249 | + code: tsx` |
| 250 | + function Component({ props }) { |
| 251 | + return <div {...props} ref={null} />; |
| 252 | + } |
| 253 | + `, |
| 254 | + compilerOptions: { |
| 255 | + strict: true, |
| 256 | + jsx: ts.JsxEmit.ReactNative, |
| 257 | + }, |
| 258 | + }, |
| 259 | + ], |
13 | 260 | }); |
14 | 261 | expect(ret).toBe(false); |
15 | 262 | }); |
0 commit comments