Emit a diagnostic error by raising with source location context. Parameters ---------- node : doc.AST The node with diagnostic error. message : str The diagnostic message.
(self, node: doc.AST, message: str)
| 276 | self.source = source |
| 277 | |
| 278 | def error(self, node: doc.AST, message: str) -> None: |
| 279 | """Emit a diagnostic error by raising with source location context. |
| 280 | |
| 281 | Parameters |
| 282 | ---------- |
| 283 | node : doc.AST |
| 284 | The node with diagnostic error. |
| 285 | |
| 286 | message : str |
| 287 | The diagnostic message. |
| 288 | """ |
| 289 | lineno = getattr(node, "lineno", 1) |
| 290 | col_offset = getattr(node, "col_offset", self.source.start_column) |
| 291 | end_lineno = getattr(node, "end_lineno", lineno) |
| 292 | end_col_offset = getattr(node, "end_col_offset", col_offset) |
| 293 | lineno += self.source.start_line - 1 |
| 294 | end_lineno += self.source.start_line - 1 |
| 295 | col_offset += self.source.start_column + 1 |
| 296 | end_col_offset += self.source.start_column + 1 |
| 297 | |
| 298 | source_lines = self.source.full_source.splitlines(keepends=True) |
| 299 | snippet = _format_source_snippet( |
| 300 | source_lines, lineno, col_offset, end_lineno, end_col_offset |
| 301 | ) |
| 302 | |
| 303 | location = f"{self.source.source_name}:{lineno}:{col_offset}" |
| 304 | formatted = f"error: {message}\n --> {location}\n{snippet}" |
| 305 | raise DiagnosticError(formatted) |
no test coverage detected