| 112 | // declare the hooks the model didn't already bring into scope itself. |
| 113 | const ALL_HOOKS = ['useState', 'useEffect', 'useRef', 'useMemo', 'useCallback', 'useReducer', 'useContext', 'useLayoutEffect']; |
| 114 | const declares = (name: string): boolean => { |
| 115 | // `const { … useState … } = React` OR `const useState = …` OR `function useState` |
| 116 | const inDestructure = new RegExp(`(?:const|let|var)\\s*\\{[^}]*\\b${name}\\b[^}]*\\}\\s*=`).test(code); |
| 117 | const asBinding = new RegExp(`(?:const|let|var|function)\\s+${name}\\b`).test(code); |
| 118 | return inDestructure || asBinding; |
| 119 | }; |
| 120 | const hooksToInject = ALL_HOOKS.filter(h => !declares(h)); |
| 121 | const hookLine = hooksToInject.length |
| 122 | ? `const { ${hooksToInject.join(', ')} } = React;\n` |