mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
Report precise location for invalid conversion flag (#7809)
## Summary This PR updates the parser definition to use the precise location when reporting an invalid f-string conversion flag error. Taking the following example code: ```python f"{foo!x}" ``` On earlier version, ``` Error: f-string: invalid conversion character at byte offset 6 ``` Now, ``` Error: f-string: invalid conversion character at byte offset 7 ``` This becomes more useful when there's whitespace between `!` and the flag value although that is not valid but we can't detect that now. ## Test Plan As mentioned above.
This commit is contained in:
parent
adb6580270
commit
17fba99ed4
2 changed files with 15 additions and 4 deletions
|
@ -1662,14 +1662,14 @@ FStringFormatSpec: ast::Expr = {
|
||||||
};
|
};
|
||||||
|
|
||||||
FStringConversion: (TextSize, ast::ConversionFlag) = {
|
FStringConversion: (TextSize, ast::ConversionFlag) = {
|
||||||
<location:@L> "!" <s:name> =>? {
|
<location:@L> "!" <name_location:@L> <s:name> =>? {
|
||||||
let conversion = match s.as_str() {
|
let conversion = match s.as_str() {
|
||||||
"s" => ast::ConversionFlag::Str,
|
"s" => ast::ConversionFlag::Str,
|
||||||
"r" => ast::ConversionFlag::Repr,
|
"r" => ast::ConversionFlag::Repr,
|
||||||
"a" => ast::ConversionFlag::Ascii,
|
"a" => ast::ConversionFlag::Ascii,
|
||||||
_ => Err(LexicalError {
|
_ => Err(LexicalError {
|
||||||
error: LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag),
|
error: LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag),
|
||||||
location,
|
location: name_location,
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
Ok((location, conversion))
|
Ok((location, conversion))
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// auto-generated: "lalrpop 0.20.0"
|
// auto-generated: "lalrpop 0.20.0"
|
||||||
// sha3: 1a0e7fb63b805f132cd3ab1d4c27182a01180a7196bacc2b93eae088dd07c79a
|
// sha3: 02b10a353d68a76918de8edbf7f2eaf4710077920b96ce46f0e4855e2ee189bb
|
||||||
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
|
||||||
use ruff_python_ast::{self as ast, Int, IpyEscapeKind};
|
use ruff_python_ast::{self as ast, Int, IpyEscapeKind};
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -36287,6 +36287,7 @@ fn __action221<
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
(_, location, _): (TextSize, TextSize, TextSize),
|
(_, location, _): (TextSize, TextSize, TextSize),
|
||||||
(_, _, _): (TextSize, token::Tok, TextSize),
|
(_, _, _): (TextSize, token::Tok, TextSize),
|
||||||
|
(_, name_location, _): (TextSize, TextSize, TextSize),
|
||||||
(_, s, _): (TextSize, String, TextSize),
|
(_, s, _): (TextSize, String, TextSize),
|
||||||
) -> Result<(TextSize, ast::ConversionFlag),__lalrpop_util::ParseError<TextSize,token::Tok,LexicalError>>
|
) -> Result<(TextSize, ast::ConversionFlag),__lalrpop_util::ParseError<TextSize,token::Tok,LexicalError>>
|
||||||
{
|
{
|
||||||
|
@ -36297,7 +36298,7 @@ fn __action221<
|
||||||
"a" => ast::ConversionFlag::Ascii,
|
"a" => ast::ConversionFlag::Ascii,
|
||||||
_ => Err(LexicalError {
|
_ => Err(LexicalError {
|
||||||
error: LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag),
|
error: LexicalErrorType::FStringError(FStringErrorType::InvalidConversionFlag),
|
||||||
location,
|
location: name_location,
|
||||||
})?
|
})?
|
||||||
};
|
};
|
||||||
Ok((location, conversion))
|
Ok((location, conversion))
|
||||||
|
@ -48263,6 +48264,8 @@ fn __action800<
|
||||||
{
|
{
|
||||||
let __start0 = __0.0;
|
let __start0 = __0.0;
|
||||||
let __end0 = __0.0;
|
let __end0 = __0.0;
|
||||||
|
let __start1 = __0.2;
|
||||||
|
let __end1 = __1.0;
|
||||||
let __temp0 = __action412(
|
let __temp0 = __action412(
|
||||||
source_code,
|
source_code,
|
||||||
mode,
|
mode,
|
||||||
|
@ -48270,11 +48273,19 @@ fn __action800<
|
||||||
&__end0,
|
&__end0,
|
||||||
);
|
);
|
||||||
let __temp0 = (__start0, __temp0, __end0);
|
let __temp0 = (__start0, __temp0, __end0);
|
||||||
|
let __temp1 = __action412(
|
||||||
|
source_code,
|
||||||
|
mode,
|
||||||
|
&__start1,
|
||||||
|
&__end1,
|
||||||
|
);
|
||||||
|
let __temp1 = (__start1, __temp1, __end1);
|
||||||
__action221(
|
__action221(
|
||||||
source_code,
|
source_code,
|
||||||
mode,
|
mode,
|
||||||
__temp0,
|
__temp0,
|
||||||
__0,
|
__0,
|
||||||
|
__temp1,
|
||||||
__1,
|
__1,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue