mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 22:55:08 +00:00
Remove source path from parser errors (#9322)
## Summary I always found it odd that we had to pass this in, since it's really higher-level context for the error. The awkwardness is further evidenced by the fact that we pass in fake values everywhere (even outside of tests). The source path isn't actually used to display the error; it's only accessed elsewhere to _re-display_ the error in certain cases. This PR modifies to instead pass the path directly in those cases.
This commit is contained in:
parent
eb9a1bc5f1
commit
e80260a3c5
45 changed files with 623 additions and 714 deletions
|
@ -12,7 +12,7 @@ else:
|
|||
pass
|
||||
"
|
||||
.trim();
|
||||
let stmts = parse_suite(contents, "<filename>")?;
|
||||
let stmts = parse_suite(contents)?;
|
||||
let stmt = stmts.first().unwrap();
|
||||
let range = identifier::else_(stmt, contents).unwrap();
|
||||
assert_eq!(&contents[range], "else");
|
||||
|
|
|
@ -6,7 +6,7 @@ use ruff_text_size::TextRange;
|
|||
#[test]
|
||||
fn test_parenthesized_name() {
|
||||
let source_code = r"(x) + 1";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let bin_op = expr.as_bin_op_expr().unwrap();
|
||||
let name = bin_op.left.as_ref();
|
||||
|
@ -23,7 +23,7 @@ fn test_parenthesized_name() {
|
|||
#[test]
|
||||
fn test_non_parenthesized_name() {
|
||||
let source_code = r"x + 1";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let bin_op = expr.as_bin_op_expr().unwrap();
|
||||
let name = bin_op.left.as_ref();
|
||||
|
@ -40,7 +40,7 @@ fn test_non_parenthesized_name() {
|
|||
#[test]
|
||||
fn test_parenthesized_argument() {
|
||||
let source_code = r"f((a))";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let call = expr.as_call_expr().unwrap();
|
||||
let arguments = &call.arguments;
|
||||
|
@ -58,7 +58,7 @@ fn test_parenthesized_argument() {
|
|||
#[test]
|
||||
fn test_non_parenthesized_argument() {
|
||||
let source_code = r"f(a)";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let call = expr.as_call_expr().unwrap();
|
||||
let arguments = &call.arguments;
|
||||
|
@ -76,7 +76,7 @@ fn test_non_parenthesized_argument() {
|
|||
#[test]
|
||||
fn test_parenthesized_tuple_member() {
|
||||
let source_code = r"(a, (b))";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let tuple = expr.as_tuple_expr().unwrap();
|
||||
let member = tuple.elts.last().unwrap();
|
||||
|
@ -93,7 +93,7 @@ fn test_parenthesized_tuple_member() {
|
|||
#[test]
|
||||
fn test_non_parenthesized_tuple_member() {
|
||||
let source_code = r"(a, b)";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let tuple = expr.as_tuple_expr().unwrap();
|
||||
let member = tuple.elts.last().unwrap();
|
||||
|
@ -110,7 +110,7 @@ fn test_non_parenthesized_tuple_member() {
|
|||
#[test]
|
||||
fn test_twice_parenthesized_name() {
|
||||
let source_code = r"((x)) + 1";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let bin_op = expr.as_bin_op_expr().unwrap();
|
||||
let name = bin_op.left.as_ref();
|
||||
|
@ -127,7 +127,7 @@ fn test_twice_parenthesized_name() {
|
|||
#[test]
|
||||
fn test_twice_parenthesized_argument() {
|
||||
let source_code = r"f(((a + 1)))";
|
||||
let expr = parse_expression(source_code, "<filename>").unwrap();
|
||||
let expr = parse_expression(source_code).unwrap();
|
||||
|
||||
let call = expr.as_call_expr().unwrap();
|
||||
let arguments = &call.arguments;
|
||||
|
|
|
@ -149,7 +149,7 @@ fn f_strings() {
|
|||
|
||||
fn trace_preorder_visitation(source: &str) -> String {
|
||||
let tokens = lex(source, Mode::Module);
|
||||
let parsed = parse_tokens(tokens, source, Mode::Module, "test.py").unwrap();
|
||||
let parsed = parse_tokens(tokens, source, Mode::Module).unwrap();
|
||||
|
||||
let mut visitor = RecordVisitor::default();
|
||||
visitor.visit_mod(&parsed);
|
||||
|
|
|
@ -10,7 +10,7 @@ fn extract_elif_else_range() -> Result<(), ParseError> {
|
|||
elif b:
|
||||
...
|
||||
";
|
||||
let mut stmts = parse_suite(contents, "<filename>")?;
|
||||
let mut stmts = parse_suite(contents)?;
|
||||
let stmt = stmts
|
||||
.pop()
|
||||
.and_then(ruff_python_ast::Stmt::if_stmt)
|
||||
|
@ -24,7 +24,7 @@ elif b:
|
|||
else:
|
||||
...
|
||||
";
|
||||
let mut stmts = parse_suite(contents, "<filename>")?;
|
||||
let mut stmts = parse_suite(contents)?;
|
||||
let stmt = stmts
|
||||
.pop()
|
||||
.and_then(ruff_python_ast::Stmt::if_stmt)
|
||||
|
|
|
@ -160,7 +160,7 @@ fn f_strings() {
|
|||
|
||||
fn trace_visitation(source: &str) -> String {
|
||||
let tokens = lex(source, Mode::Module);
|
||||
let parsed = parse_tokens(tokens, source, Mode::Module, "test.py").unwrap();
|
||||
let parsed = parse_tokens(tokens, source, Mode::Module).unwrap();
|
||||
|
||||
let mut visitor = RecordVisitor::default();
|
||||
walk_module(&mut visitor, &parsed);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue