(root: SyntaxNode, source: string, line: number, column: number | null)
| 833 | } |
| 834 | |
| 835 | export function triggerInTree(root: SyntaxNode, source: string, line: number, column: number | null): SiteTrigger | null { |
| 836 | const row = line - 1; |
| 837 | const col = column ?? firstNonBlankColumn(source, row); |
| 838 | let node: SyntaxNode | null = innermostAt(root, row, col); |
| 839 | let prev: SyntaxNode | null = null; |
| 840 | // Whether the climb crossed an inline function: `onPress={() => go()}` |
| 841 | // fires later, `behavior={isAndroid() ? 'a' : 'b'}` runs at render. |
| 842 | let deferred = false; |
| 843 | for (let up = 0; node && up < MAX_TRIGGER_CLIMB; up++, prev = node, node = node.parent) { |
| 844 | const type = node.type; |
| 845 | if (TRIGGER_BOUNDARIES.has(type)) return null; |
| 846 | // A named handler is its own story: `const handleX = useCallback(() => …)` |
| 847 | // binds a name, and whoever uses the name is the trigger of what is inside. |
| 848 | if (type === 'arrow_function' || type === 'function_expression') { |
| 849 | const p = node.parent; |
| 850 | if (p?.type === 'variable_declarator') return null; |
| 851 | if (p?.type === 'arguments' && p.parent) { |
| 852 | const callee = lastSegment(calleeText(p.parent)); |
| 853 | if (callee === 'useCallback' || callee === 'useMemo' || callee === 'useEffectEvent' || callee === 'useEvent') return null; |
| 854 | } |
| 855 | deferred = true; |
| 856 | } |
| 857 | if (type === 'jsx_attribute') { |
| 858 | const name = node.namedChild(0); |
| 859 | const propName = name ? name.text : 'prop'; |
| 860 | // An event prop, or any prop given a function: fired later. A value |
| 861 | // computed in the attribute (`behavior={isAndroid() ? …}`) is not. |
| 862 | if (!deferred && !/^on[A-Z]/.test(propName)) return null; |
| 863 | const element = node.parent; |
| 864 | const tag = element ? element.childForFieldName('name') : null; |
| 865 | return { kind: 'prop', name: propName, of: tag ? collapseText(tag.text) : null }; |
| 866 | } |
| 867 | if (type === 'pair') { |
| 868 | const key = node.childForFieldName('key'); |
| 869 | const keyText = key ? key.text.replace(/^['"`]|['"`]$/g, '') : ''; |
| 870 | if (/^on[A-Z]\w*$/.test(keyText)) { |
| 871 | // `useFormik({ onSubmit: … })`, `Alert.alert(t, m, [{ onPress: … }])`: |
| 872 | // the object — possibly inside an array — is an argument of a call. |
| 873 | let holder: SyntaxNode | null = node.parent; |
| 874 | for (let hop = 0; holder && hop < 4 && (holder.type === 'object' || holder.type === 'array' || holder.type === 'pair'); hop++) { |
| 875 | holder = holder.parent; |
| 876 | } |
| 877 | const call = holder?.type === 'arguments' ? holder.parent : null; |
| 878 | return { kind: 'option', name: keyText, of: call && CALL_TYPES.has(call.type) ? calleeText(call) : null }; |
| 879 | } |
| 880 | } |
| 881 | if (type === 'arguments' && node.parent && CALL_TYPES.has(node.parent.type) && prev !== null) { |
| 882 | const callee = lastSegment(calleeText(node.parent)); |
| 883 | if (callee !== null && LATER_CALLEES.has(callee)) { |
| 884 | const first = node.namedChild(0); |
| 885 | const of = first && STRING_TYPES.has(first.type) ? cut(collapseText(first.text), MAX_ARG_TEXT) : null; |
| 886 | return { kind: 'callback', name: callee, of }; |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | return null; |
| 891 | } |
| 892 |
no test coverage detected