|
| 1 | +import { |
| 2 | + useModel, |
| 3 | + useSignal, |
| 4 | + useComputed, |
| 5 | + useSignalEffect, |
| 6 | +} from "@preact/signals"; |
| 7 | +import { signal, computed, effect, untracked } from "@preact/signals-core"; |
| 8 | +import { Model } from "./model"; |
| 9 | + |
| 10 | +export const Linting = () => { |
| 11 | + const sig = useSignal(0); |
| 12 | + const model = useModel(Model); |
| 13 | + |
| 14 | + signal(0); |
| 15 | + effect(() => {}); |
| 16 | + computed(() => {}); |
| 17 | + |
| 18 | + // @ts-ignore - we are not using bar, just want to test the linting |
| 19 | + const bar = useComputed(() => { |
| 20 | + // Both should complain about these |
| 21 | + model.count.value = 5; |
| 22 | + sig.value = 6; |
| 23 | + return "foo"; |
| 24 | + }); |
| 25 | + |
| 26 | + useSignalEffect(() => { |
| 27 | + const perform = async () => { |
| 28 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 29 | + // Both should complain about these |
| 30 | + // @ts-ignore - we are not using count, just want to test the linting |
| 31 | + const count = model.count.value; |
| 32 | + // @ts-ignore - we are not using sigValue, just want to test the linting |
| 33 | + const sigValue = sig.value; |
| 34 | + |
| 35 | + // Should not complain about this |
| 36 | + sig.value++; |
| 37 | + // Should not complain about this |
| 38 | + sig.value = 10; |
| 39 | + }; |
| 40 | + |
| 41 | + perform(); |
| 42 | + }); |
| 43 | + |
| 44 | + // Issue #621: .value after a non-reactive guard (.peek / plain variable) |
| 45 | + // should warn because the signal won't be tracked as a dependency. |
| 46 | + useSignalEffect(() => { |
| 47 | + const id = model.count.peek(); |
| 48 | + if (!id) return; |
| 49 | + // Both should complain about this — .value after non-reactive guard |
| 50 | + console.log(sig.value); |
| 51 | + }); |
| 52 | + |
| 53 | + // .value after a .value guard is OK — the guard signal IS tracked |
| 54 | + useSignalEffect(() => { |
| 55 | + if (!model.count.value) return; |
| 56 | + // Should NOT complain — model.count is tracked by the guard above |
| 57 | + console.log(sig.value); |
| 58 | + }); |
| 59 | + |
| 60 | + // untracked() guard — .value inside untracked is non-reactive, like .peek() |
| 61 | + useSignalEffect(() => { |
| 62 | + if (!untracked(() => model.count.value)) return; |
| 63 | + // Should complain — untracked guard is non-reactive |
| 64 | + console.log(sig.value); |
| 65 | + }); |
| 66 | + |
| 67 | + // ESLint should complain about this, oxlint not |
| 68 | + if (model.count) return <p>Lint error</p>; |
| 69 | + // Both should complain about this |
| 70 | + if (sig) return <p>Lint error</p>; |
| 71 | +}; |
0 commit comments