Adds origin information to an AST, based on the source it was loaded from. This allows us to map the original source code line numbers to generated source code. Note: the AST may be a part of a larger context (e.g. a function is part of a module that may contain other things). However, thi
(node, source, context_filepath, context_lineno, context_col_offset)
| 222 | |
| 223 | |
| 224 | def resolve(node, source, context_filepath, context_lineno, context_col_offset): |
| 225 | """Adds origin information to an AST, based on the source it was loaded from. |
| 226 | |
| 227 | This allows us to map the original source code line numbers to generated |
| 228 | source code. |
| 229 | |
| 230 | Note: the AST may be a part of a larger context (e.g. a function is part of |
| 231 | a module that may contain other things). However, this function does not |
| 232 | assume the source argument contains the entire context, nor that it contains |
| 233 | only code corresponding to node itself. However, it assumes that node was |
| 234 | parsed from the given source code. |
| 235 | For this reason, two extra arguments are required, and they indicate the |
| 236 | location of the node in the original context. |
| 237 | |
| 238 | Args: |
| 239 | node: gast.AST, the AST to annotate. |
| 240 | source: Text, the source code representing node. |
| 241 | context_filepath: Text |
| 242 | context_lineno: int |
| 243 | context_col_offset: int |
| 244 | """ |
| 245 | # TODO(mdan): Pull this to a separate utility. |
| 246 | code_reader = six.StringIO(source) |
| 247 | comments_map = {} |
| 248 | for token in tokenize.generate_tokens(code_reader.readline): |
| 249 | tok_type, tok_string, loc, _, _ = token |
| 250 | srow, _ = loc |
| 251 | if tok_type == tokenize.COMMENT: |
| 252 | comments_map[srow] = tok_string.strip()[1:].strip() |
| 253 | |
| 254 | source_lines = source.split('\n') |
| 255 | visitor = OriginResolver(node, source_lines, comments_map, |
| 256 | context_lineno, context_col_offset, |
| 257 | context_filepath) |
| 258 | visitor.visit(node) |
| 259 | |
| 260 | |
| 261 | def resolve_entity(node, source, entity): |
no test coverage detected