what-core 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ // What Framework - Dev-mode Warning System
2
+ // Helpful, not noisy: each unique warning fires only once.
3
+ // All warnings are dev-only and tree-shaken in production builds.
4
+
5
+ import { __DEV__ } from './reactive.js';
6
+
7
+ // Track which warnings have already been emitted (fire-once per unique key)
8
+ const _emitted = new Set();
9
+
10
+ /**
11
+ * Emit a dev-mode warning. Fires only once per unique key.
12
+ * @param {string} key - Unique identifier for de-duplication
13
+ * @param {string} message - Warning message
14
+ */
15
+ export function warn(key, message) {
16
+ if (!__DEV__) return;
17
+ if (_emitted.has(key)) return;
18
+ _emitted.add(key);
19
+ console.warn(message);
20
+ }
21
+
22
+ /**
23
+ * Reset all emitted warnings (for testing).
24
+ */
25
+ export function _resetWarnings() {
26
+ _emitted.clear();
27
+ }
28
+
29
+ /**
30
+ * Check if a warning has been emitted (for testing).
31
+ * @param {string} key
32
+ * @returns {boolean}
33
+ */
34
+ export function _wasWarned(key) {
35
+ return _emitted.has(key);
36
+ }
37
+
38
+ // --- Warning functions ---
39
+
40
+ /**
41
+ * Warn when a signal is used in JSX without being called.
42
+ * Detects: {count} instead of {count()} where count is a signal function.
43
+ * @param {string} signalName - The signal's debug name
44
+ * @param {string} [componentName] - The component where it occurred
45
+ */
46
+ export function warnMissingSignalRead(signalName, componentName) {
47
+ if (!__DEV__) return;
48
+ const ctx = componentName ? ` in <${componentName}>` : '';
49
+ warn(
50
+ `missing-read:${signalName}:${componentName || ''}`,
51
+ `[what] Warning: Signal '${signalName}' used without being called${ctx}. Did you mean {${signalName}()}?`
52
+ );
53
+ }
54
+
55
+ /**
56
+ * Warn when a signal is written during render (component function execution).
57
+ * Writes should happen in effects or event handlers, not during render.
58
+ * @param {string} signalName - The signal's debug name
59
+ * @param {string} [componentName] - The component where it occurred
60
+ */
61
+ export function warnSignalWriteDuringRender(signalName, componentName) {
62
+ if (!__DEV__) return;
63
+ const ctx = componentName ? ` of <${componentName}>` : '';
64
+ warn(
65
+ `write-during-render:${signalName}:${componentName || ''}`,
66
+ `[what] Warning: Signal '${signalName}' written during render${ctx}. Move to effect or event handler.`
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Warn when an effect adds an event listener but has no cleanup return.
72
+ * @param {string} [componentName] - The component where it occurred
73
+ */
74
+ export function warnEffectWithoutCleanup(componentName) {
75
+ if (!__DEV__) return;
76
+ const ctx = componentName ? ` in <${componentName}>` : '';
77
+ warn(
78
+ `effect-no-cleanup:${componentName || 'unknown'}`,
79
+ `[what] Warning: Effect${ctx} adds event listener but has no cleanup. Return a cleanup function from the effect to avoid memory leaks.`
80
+ );
81
+ }
82
+
83
+ /**
84
+ * Warn when rendering a large list without keys.
85
+ * @param {number} count - Number of items in the list
86
+ * @param {string} [componentName] - The component where it occurred
87
+ */
88
+ export function warnLargeListWithoutKeys(count, componentName) {
89
+ if (!__DEV__) return;
90
+ const ctx = componentName ? ` in <${componentName}>` : '';
91
+ warn(
92
+ `no-keys:${componentName || 'unknown'}`,
93
+ `[what] Warning: Rendering ${count} items without keys${ctx}. Add key props for efficient DOM updates.`
94
+ );
95
+ }
96
+
97
+ /**
98
+ * Warn when a signal is created but never read.
99
+ * Called lazily (e.g., on component unmount or root dispose).
100
+ * @param {string} signalName - The signal's debug name
101
+ * @param {string} [componentName] - The component where it occurred
102
+ */
103
+ export function warnUnusedSignal(signalName, componentName) {
104
+ if (!__DEV__) return;
105
+ const ctx = componentName ? ` in <${componentName}>` : '';
106
+ warn(
107
+ `unused-signal:${signalName}:${componentName || ''}`,
108
+ `[what] Warning: Signal '${signalName}' created${ctx} but never read.`
109
+ );
110
+ }