|
| 1 | +import { allValid, ruleTester } from "../../../../../test"; |
| 2 | +import rule, { RULE_NAME } from "./no-use-in-try-catch"; |
| 3 | + |
| 4 | +ruleTester.run(RULE_NAME, rule, { |
| 5 | + invalid: [ |
| 6 | + { |
| 7 | + code: /* tsx */ ` |
| 8 | + function Message({ messagePromise }) { |
| 9 | + try { |
| 10 | + const content = use(messagePromise); |
| 11 | + // ^^^ |
| 12 | + // - 'use' cannot be called in a try-catch block. Instead of a try-catch block wrap your component in an Error Boundary, or provide an alternative value to use with the Promise’s .catch method. |
| 13 | + return <p>Here is the message: {content}</p>; |
| 14 | + } catch (error) { |
| 15 | + return <p>⚠️Something went wrong</p>; |
| 16 | + } |
| 17 | + } |
| 18 | + `, |
| 19 | + errors: [{ messageId: "noUseInTryCatch" }], |
| 20 | + }, |
| 21 | + { |
| 22 | + code: /* tsx */ ` |
| 23 | + function Message({ messagePromise }) { |
| 24 | + try { |
| 25 | + const content = React.use(messagePromise); |
| 26 | + // ^^^ |
| 27 | + // - 'use' cannot be called in a try-catch block. Instead of a try-catch block wrap your component in an Error Boundary, or provide an alternative value to use with the Promise’s .catch method. |
| 28 | + return <p>Here is the message: {content}</p>; |
| 29 | + } catch (error) { |
| 30 | + return <p>⚠️Something went wrong</p>; |
| 31 | + } |
| 32 | + } |
| 33 | + `, |
| 34 | + errors: [{ messageId: "noUseInTryCatch" }], |
| 35 | + }, |
| 36 | + { |
| 37 | + code: /* tsx */ ` |
| 38 | + import { use } from "react"; |
| 39 | +
|
| 40 | + function Message({ messagePromise }) { |
| 41 | + try { |
| 42 | + const content = use(messagePromise); |
| 43 | + // ^^^ |
| 44 | + // - 'use' cannot be called in a try-catch block. Instead of a try-catch block wrap your component in an Error Boundary, or provide an alternative value to use with the Promise’s .catch method. |
| 45 | + return <p>Here is the message: {content}</p>; |
| 46 | + } catch (error) { |
| 47 | + return <p>⚠️Something went wrong</p>; |
| 48 | + } |
| 49 | + } |
| 50 | + `, |
| 51 | + errors: [{ messageId: "noUseInTryCatch" }], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: /* tsx */ ` |
| 55 | + import * as React from "react"; |
| 56 | +
|
| 57 | + function Message({ messagePromise }) { |
| 58 | + try { |
| 59 | + const content = React.use(messagePromise); |
| 60 | + // ^^^ |
| 61 | + // - 'use' cannot be called in a try-catch block. Instead of a try-catch block wrap your component in an Error Boundary, or provide an alternative value to use with the Promise’s .catch method. |
| 62 | + return <p>Here is the message: {content}</p>; |
| 63 | + } catch (error) { |
| 64 | + return <p>⚠️Something went wrong</p>; |
| 65 | + } |
| 66 | + } |
| 67 | + `, |
| 68 | + errors: [{ messageId: "noUseInTryCatch" }], |
| 69 | + }, |
| 70 | + ], |
| 71 | + valid: [ |
| 72 | + ...allValid, |
| 73 | + ], |
| 74 | +}); |
0 commit comments