( root: SyntaxNode, source: string, language: Language, line: number, column: number | null )
| 1063 | * would list them. |
| 1064 | */ |
| 1065 | export function guardsInTree( |
| 1066 | root: SyntaxNode, |
| 1067 | source: string, |
| 1068 | language: Language, |
| 1069 | line: number, |
| 1070 | column: number | null |
| 1071 | ): BranchGuard[] { |
| 1072 | const row = line - 1; |
| 1073 | if (row < 0) return []; |
| 1074 | let col = column ?? 0; |
| 1075 | if (column === null) { |
| 1076 | const text = source.split('\n')[row] ?? ''; |
| 1077 | const first = text.search(/\S/); |
| 1078 | col = first < 0 ? 0 : first; |
| 1079 | } |
| 1080 | let node: SyntaxNode | null = innermostAt(root, row, col); |
| 1081 | if (!node) return []; |
| 1082 | const rules = RULES_BY_LANGUAGE.get(language); |
| 1083 | if (!rules) return []; |
| 1084 | const found: BranchGuard[] = []; |
| 1085 | |
| 1086 | // Innermost → outermost. `found` is reversed at the end, so within one level |
| 1087 | // anything meant to read as OUTER must be pushed LATER. |
| 1088 | while (node) { |
| 1089 | const parent: SyntaxNode | null = node.parent; |
| 1090 | if (!parent || rules.boundaries.has(parent.type)) break; |
| 1091 | if (rules.inlineFunctions.has(parent.type)) { |
| 1092 | const holder = parent.parent?.type ?? ''; |
| 1093 | if (rules.bindingParents.has(holder)) break; |
| 1094 | node = parent; |
| 1095 | continue; |
| 1096 | } |
| 1097 | // Anything `enclosing` pushed came from THIS construct, and the arm the |
| 1098 | // site is in is the child the walk came up through: stamp both, unless the |
| 1099 | // rule already named a different branch (a `switch` case's is the switch). |
| 1100 | const before = found.length; |
| 1101 | rules.enclosing(parent, node, found); |
| 1102 | for (let i = before; i < found.length; i++) { |
| 1103 | const g = found[i]!; |
| 1104 | if (!g.branch) g.branch = branchKey(parent); |
| 1105 | const arm = exitKind(node); |
| 1106 | if (arm && !g.armExit) g.armExit = arm; |
| 1107 | } |
| 1108 | if (rules.blocks.has(parent.type)) rules.earlyExits(parent, node, found); |
| 1109 | node = parent; |
| 1110 | } |
| 1111 | found.reverse(); |
| 1112 | return found; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * The innermost named node containing (row, col). `descendantForPosition` is |
no test coverage detected