|
| 1 | +import { describe, expect, it } from "vite-plus/test"; |
| 2 | +import { runRule } from "../../../test-utils/run-rule.js"; |
| 3 | +import { noImpureStateUpdater } from "./no-impure-state-updater.js"; |
| 4 | + |
| 5 | +describe("updater parameter reassignment", () => { |
| 6 | + it.each(["++previous", "--previous", "previous++", "previous += 1", "previous = 2"])( |
| 7 | + "accepts local parameter operation %s", |
| 8 | + (operation) => { |
| 9 | + const result = runRule( |
| 10 | + noImpureStateUpdater, |
| 11 | + ` |
| 12 | + import { useState } from 'react'; |
| 13 | + const Counter = () => { |
| 14 | + const [count, setCount] = useState(0); |
| 15 | + return <button onClick={() => setCount(previous => ${operation})}>{count}</button>; |
| 16 | + }; |
| 17 | + `, |
| 18 | + ); |
| 19 | + expect(result.parseErrors).toEqual([]); |
| 20 | + expect(result.diagnostics).toHaveLength(0); |
| 21 | + }, |
| 22 | + ); |
| 23 | + |
| 24 | + it.each(["++previous.count", "previous.count = 2", "++captured", "captured = 2"])( |
| 25 | + "still reports external mutation %s", |
| 26 | + (operation) => { |
| 27 | + const result = runRule( |
| 28 | + noImpureStateUpdater, |
| 29 | + ` |
| 30 | + import { useState } from 'react'; |
| 31 | + let captured = 0; |
| 32 | + const Counter = () => { |
| 33 | + const [value, setValue] = useState({ count: 0 }); |
| 34 | + return <button onClick={() => setValue(previous => { ${operation}; return previous; })}>Change</button>; |
| 35 | + }; |
| 36 | + `, |
| 37 | + ); |
| 38 | + expect(result.diagnostics).toHaveLength(1); |
| 39 | + }, |
| 40 | + ); |
| 41 | +}); |
0 commit comments