(ast: ParseResult)
| 137 | } |
| 138 | |
| 139 | private buildClosureScopeTree(ast: ParseResult): ClosureScope { |
| 140 | const root = { |
| 141 | functionName: null, |
| 142 | functionType: ast.program.type, |
| 143 | variablesDefined: [] as string[], |
| 144 | usedVariablesFromParentScope: [] as string[], |
| 145 | nestedClosures: [] as ClosureScope[], |
| 146 | loc: ast.program.loc, |
| 147 | }; |
| 148 | |
| 149 | const locToClosureScopeMap = new Map<string, ClosureScope>(); |
| 150 | locToClosureScopeMap.set(this.locToStr(ast.program.loc), root); |
| 151 | |
| 152 | // eslint-disable-next-line @typescript-eslint/no-this-alias |
| 153 | const self = this; |
| 154 | |
| 155 | // build closure scope hierarchy |
| 156 | const buildClosureScopeFromFunction = function ( |
| 157 | path: NodePath<FunctionClass>, |
| 158 | ) { |
| 159 | const closureScope = { |
| 160 | functionName: 'id' in path.node ? path.node?.id?.name : null, |
| 161 | functionType: path.node.type, |
| 162 | variablesDefined: Object.keys(path.scope.bindings), |
| 163 | usedVariablesFromParentScope: [] as string[], |
| 164 | nestedClosures: [] as ClosureScope[], |
| 165 | loc: path.node.loc, |
| 166 | }; |
| 167 | locToClosureScopeMap.set(self.locToStr(path.node.loc), closureScope); |
| 168 | // find and connect to parent closure scope |
| 169 | const parentClosureScope = self.findParentFunctionClosureScope( |
| 170 | locToClosureScopeMap, |
| 171 | path, |
| 172 | ); |
| 173 | parentClosureScope.nestedClosures.push(closureScope); |
| 174 | }; |
| 175 | |
| 176 | // Traverse the parent scope of the containing scope |
| 177 | // to find out if both of the conditions are true: |
| 178 | // 1. This is a var use in the containing scope |
| 179 | // 2. This is a var defined in the direct parent scope |
| 180 | // of the containing scope |
| 181 | // If true, add the identifer name to the usedVariablesFromParentScope |
| 182 | // of the containing scope |
| 183 | const fillInVarInfo = function (path: NodePath<Identifier>) { |
| 184 | // if the identifier is a function name of a function definition |
| 185 | if (isFunctionType(path.parentPath.node.type)) { |
| 186 | return; |
| 187 | } |
| 188 | const name = path.node.name; |
| 189 | let parentPath = self.findParentFunctionPath(path); |
| 190 | let parentClosureScope = parentPath |
| 191 | ? locToClosureScopeMap.get(self.locToStr(parentPath.node.loc)) |
| 192 | : null; |
| 193 | // eslint-disable-next-line no-constant-condition |
| 194 | while (true) { |
| 195 | if (!parentPath || !parentClosureScope) { |
| 196 | break; |
no test coverage detected