Compare commits

..

1 Commits

Author SHA1 Message Date
Charlie Marsh
7fcf4c067c Remove string from comment 2024-01-06 16:19:48 -05:00
40 changed files with 122 additions and 177 deletions

View File

@@ -7,9 +7,6 @@ constant = 5
{v: v * v for v in range(10)}
{(0, "a", v): v * v for v in range(10)} # Tuple with variable
{constant: value.upper() for value in data for constant in data}
{value.attribute: value.upper() for value in data for constant in data}
{constant[value]: value.upper() for value in data for constant in data}
{value[constant]: value.upper() for value in data for constant in data}
# Errors
{"key": value.upper() for value in data}
@@ -18,5 +15,3 @@ constant = 5
{(1, "a"): value.upper() for value in data} # Constant tuple
{constant: value.upper() for value in data}
{constant + constant: value.upper() for value in data}
{constant.attribute: value.upper() for value in data}
{constant[0]: value.upper() for value in data}

View File

@@ -61,7 +61,7 @@ pub(crate) fn check_tokens(
}
}
Tok::FStringMiddle { .. } => Context::String,
Tok::Comment(_) => Context::Comment,
Tok::Comment => Context::Comment,
_ => continue,
};
ruff::rules::ambiguous_unicode_character(

View File

@@ -39,7 +39,7 @@ impl Iterator for DocLines<'_> {
let (tok, range) = self.inner.next()?;
match tok {
Tok::Comment(..) => {
Tok::Comment => {
if at_start_of_line {
break Some(range.start());
}

View File

@@ -174,7 +174,7 @@ impl<'a> Insertion<'a> {
// Once we've seen the colon, we're looking for a newline; otherwise, there's no
// block body (e.g. `if True: pass`).
Awaiting::Newline => match tok {
Tok::Comment(..) => {}
Tok::Comment => {}
Tok::Newline => {
state = Awaiting::Indent;
}
@@ -185,7 +185,7 @@ impl<'a> Insertion<'a> {
},
// Once we've seen the newline, we're looking for the indentation of the block body.
Awaiting::Indent => match tok {
Tok::Comment(..) => {}
Tok::Comment => {}
Tok::NonLogicalNewline => {}
Tok::Indent => {
// This is like:

View File

@@ -33,11 +33,9 @@ pub(crate) struct StateMachine {
impl StateMachine {
pub(crate) fn consume(&mut self, tok: &Tok) -> bool {
match tok {
Tok::NonLogicalNewline
| Tok::Newline
| Tok::Indent
| Tok::Dedent
| Tok::Comment(..) => false,
Tok::NonLogicalNewline | Tok::Newline | Tok::Indent | Tok::Dedent | Tok::Comment => {
false
}
Tok::String { .. } => {
if matches!(

View File

@@ -242,7 +242,7 @@ pub(crate) fn trailing_commas(
.flatten()
.filter_map(|spanned @ (tok, tok_range)| match tok {
// Completely ignore comments -- they just interfere with the logic.
Tok::Comment(_) => None,
Tok::Comment => None,
// F-strings are handled as `String` token type with the complete range
// of the outermost f-string. This means that the expression inside the
// f-string is not checked for trailing commas.

View File

@@ -168,7 +168,7 @@ pub(crate) fn avoidable_escaped_quote(
match tok {
Tok::String {
value,
value: string_contents,
kind,
triple_quoted,
} => {
@@ -176,8 +176,6 @@ pub(crate) fn avoidable_escaped_quote(
continue;
}
let string_contents = locator.slice(&value);
// Check if we're using the preferred quotation style.
if !leading_quote(locator.slice(tok_range)).is_some_and(|text| {
contains_quote(text, quotes_settings.inline_quotes.as_char())
@@ -314,7 +312,7 @@ pub(crate) fn unnecessary_escaped_quote(
match tok {
Tok::String {
value,
value: string_contents,
kind,
triple_quoted,
} => {
@@ -322,8 +320,6 @@ pub(crate) fn unnecessary_escaped_quote(
continue;
}
let string_contents = locator.slice(&value);
let leading = match leading_quote(locator.slice(tok_range)) {
Some("\"") => Quote::Double,
Some("'") => Quote::Single,

View File

@@ -461,7 +461,7 @@ pub(crate) fn check_string_quotes(
// range to the sequence.
sequence.push(fstring_range_builder.finish());
}
Tok::Comment(..) | Tok::NonLogicalNewline => continue,
Tok::Comment | Tok::NonLogicalNewline => continue,
_ => {
// Otherwise, consume the sequence.
if !sequence.is_empty() {

View File

@@ -26,7 +26,7 @@ pub(super) fn trailing_comma(
if count == 1 {
if matches!(
tok,
Tok::NonLogicalNewline | Tok::Indent | Tok::Dedent | Tok::Comment(_)
Tok::NonLogicalNewline | Tok::Indent | Tok::Dedent | Tok::Comment
) {
continue;
} else if matches!(tok, Tok::Comma) {

View File

@@ -239,7 +239,7 @@ pub(crate) fn compound_statements(
semi = Some((range.start(), range.end()));
allow_ellipsis = false;
}
Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {}
Tok::Comment | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {}
_ => {
if let Some((start, end)) = semi {
diagnostics.push(Diagnostic::new(
@@ -347,7 +347,7 @@ fn has_non_trivia_tokens_till<'a>(
}
if !matches!(
tok,
Tok::Newline | Tok::Comment(_) | Tok::EndOfFile | Tok::NonLogicalNewline
Tok::Newline | Tok::Comment | Tok::EndOfFile | Tok::NonLogicalNewline
) {
return true;
}

View File

@@ -48,7 +48,7 @@ fn match_extraneous_parentheses(tokens: &[LexResult], mut i: usize) -> Option<(u
return None;
};
match tok {
Tok::Comment(..) | Tok::NonLogicalNewline => {
Tok::Comment | Tok::NonLogicalNewline => {
i += 1;
}
Tok::Lpar => {
@@ -88,12 +88,7 @@ fn match_extraneous_parentheses(tokens: &[LexResult], mut i: usize) -> Option<(u
let end = i;
// Verify that we're not in an empty tuple.
if (start + 1..i).all(|i| {
matches!(
tokens[i],
Ok((Tok::Comment(..) | Tok::NonLogicalNewline, _))
)
}) {
if (start + 1..i).all(|i| matches!(tokens[i], Ok((Tok::Comment | Tok::NonLogicalNewline, _)))) {
return None;
}
@@ -107,7 +102,7 @@ fn match_extraneous_parentheses(tokens: &[LexResult], mut i: usize) -> Option<(u
return None;
};
match tok {
Tok::Comment(..) | Tok::NonLogicalNewline => {
Tok::Comment | Tok::NonLogicalNewline => {
i += 1;
}
_ => {

View File

@@ -426,7 +426,7 @@ pub(crate) fn f_strings(
if !lexer::lex_starts_at(rest, Mode::Expression, prev_end)
.flatten()
.all(|(token, _)| match token {
Tok::Comment(_) | Tok::Newline | Tok::NonLogicalNewline | Tok::Indent | Tok::Dedent => {
Tok::Comment | Tok::Newline | Tok::NonLogicalNewline | Tok::Indent | Tok::Dedent => {
true
}
Tok::String { value, .. } => value.is_empty(),

View File

@@ -74,10 +74,6 @@ fn is_constant(key: &Expr, names: &FxHashMap<&str, &ast::ExprName>) -> bool {
match key {
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().all(|elt| is_constant(elt, names)),
Expr::Name(ast::ExprName { id, .. }) => !names.contains_key(id.as_str()),
Expr::Attribute(ast::ExprAttribute { value, .. }) => is_constant(value, names),
Expr::Subscript(ast::ExprSubscript { value, slice, .. }) => {
is_constant(value, names) && is_constant(slice, names)
}
Expr::BinOp(ast::ExprBinOp { left, right, .. }) => {
is_constant(left, names) && is_constant(right, names)
}

View File

@@ -1,80 +1,60 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF011.py:15:2: RUF011 Dictionary comprehension uses static key: `"key"`
RUF011.py:12:2: RUF011 Dictionary comprehension uses static key: `"key"`
|
14 | # Errors
15 | {"key": value.upper() for value in data}
11 | # Errors
12 | {"key": value.upper() for value in data}
| ^^^^^ RUF011
16 | {True: value.upper() for value in data}
17 | {0: value.upper() for value in data}
13 | {True: value.upper() for value in data}
14 | {0: value.upper() for value in data}
|
RUF011.py:16:2: RUF011 Dictionary comprehension uses static key: `True`
RUF011.py:13:2: RUF011 Dictionary comprehension uses static key: `True`
|
14 | # Errors
15 | {"key": value.upper() for value in data}
16 | {True: value.upper() for value in data}
11 | # Errors
12 | {"key": value.upper() for value in data}
13 | {True: value.upper() for value in data}
| ^^^^ RUF011
17 | {0: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
14 | {0: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
|
RUF011.py:17:2: RUF011 Dictionary comprehension uses static key: `0`
RUF011.py:14:2: RUF011 Dictionary comprehension uses static key: `0`
|
15 | {"key": value.upper() for value in data}
16 | {True: value.upper() for value in data}
17 | {0: value.upper() for value in data}
12 | {"key": value.upper() for value in data}
13 | {True: value.upper() for value in data}
14 | {0: value.upper() for value in data}
| ^ RUF011
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
19 | {constant: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {constant: value.upper() for value in data}
|
RUF011.py:18:2: RUF011 Dictionary comprehension uses static key: `(1, "a")`
RUF011.py:15:2: RUF011 Dictionary comprehension uses static key: `(1, "a")`
|
16 | {True: value.upper() for value in data}
17 | {0: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
13 | {True: value.upper() for value in data}
14 | {0: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
| ^^^^^^^^ RUF011
19 | {constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
16 | {constant: value.upper() for value in data}
17 | {constant + constant: value.upper() for value in data}
|
RUF011.py:19:2: RUF011 Dictionary comprehension uses static key: `constant`
RUF011.py:16:2: RUF011 Dictionary comprehension uses static key: `constant`
|
17 | {0: value.upper() for value in data}
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
19 | {constant: value.upper() for value in data}
14 | {0: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {constant: value.upper() for value in data}
| ^^^^^^^^ RUF011
20 | {constant + constant: value.upper() for value in data}
21 | {constant.attribute: value.upper() for value in data}
17 | {constant + constant: value.upper() for value in data}
|
RUF011.py:20:2: RUF011 Dictionary comprehension uses static key: `constant + constant`
RUF011.py:17:2: RUF011 Dictionary comprehension uses static key: `constant + constant`
|
18 | {(1, "a"): value.upper() for value in data} # Constant tuple
19 | {constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
15 | {(1, "a"): value.upper() for value in data} # Constant tuple
16 | {constant: value.upper() for value in data}
17 | {constant + constant: value.upper() for value in data}
| ^^^^^^^^^^^^^^^^^^^ RUF011
21 | {constant.attribute: value.upper() for value in data}
22 | {constant[0]: value.upper() for value in data}
|
RUF011.py:21:2: RUF011 Dictionary comprehension uses static key: `constant.attribute`
|
19 | {constant: value.upper() for value in data}
20 | {constant + constant: value.upper() for value in data}
21 | {constant.attribute: value.upper() for value in data}
| ^^^^^^^^^^^^^^^^^^ RUF011
22 | {constant[0]: value.upper() for value in data}
|
RUF011.py:22:2: RUF011 Dictionary comprehension uses static key: `constant[0]`
|
20 | {constant + constant: value.upper() for value in data}
21 | {constant.attribute: value.upper() for value in data}
22 | {constant[0]: value.upper() for value in data}
| ^^^^^^^^^^^ RUF011
|

View File

@@ -411,7 +411,7 @@ impl<'source> Lexer<'source> {
let offset = memchr::memchr2(b'\n', b'\r', bytes).unwrap_or(bytes.len());
self.cursor.skip_bytes(offset);
Tok::Comment(self.token_text().to_string())
Tok::Comment
}
/// Lex a single IPython escape command.
@@ -759,7 +759,7 @@ impl<'source> Lexer<'source> {
};
let tok = Tok::String {
value: TextRange::new(value_start, value_end),
value: self.source[TextRange::new(value_start, value_end)].to_string(),
kind,
triple_quoted,
};

View File

@@ -176,7 +176,7 @@ pub fn locate_cmp_ops(expr: &Expr, source: &str) -> Vec<LocatedCmpOp> {
.flatten()
.skip(1)
.map(|(tok, range)| (tok, range - TextSize::from(1)))
.filter(|(tok, _)| !matches!(tok, Tok::NonLogicalNewline | Tok::Comment(_)))
.filter(|(tok, _)| !matches!(tok, Tok::NonLogicalNewline | Tok::Comment))
.peekable();
let mut ops: Vec<LocatedCmpOp> = vec![];

View File

@@ -1605,8 +1605,8 @@ StringLiteralOrFString: StringType = {
StringLiteral: StringType = {
<location:@L> <string:string> <end_location:@R> =>? {
let (value, kind, triple_quoted) = string;
Ok(parse_string_literal(&source_code[value], kind, triple_quoted, (location..end_location).into())?)
let (source, kind, triple_quoted) = string;
Ok(parse_string_literal(&source, kind, triple_quoted, (location..end_location).into())?)
}
};
@@ -2061,7 +2061,7 @@ extern {
float => token::Tok::Float { value: <f64> },
complex => token::Tok::Complex { real: <f64>, imag: <f64> },
string => token::Tok::String {
value: <TextRange>,
value: <String>,
kind: <StringKind>,
triple_quoted: <bool>
},
@@ -2076,6 +2076,6 @@ extern {
},
"\n" => token::Tok::Newline,
";" => token::Tok::Semi,
// "#" => token::Tok::Comment(_),
// "#" => token::Tok::Comment,
}
}

View File

@@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: 28f158c07e00e286b0a28fb9af14b474f60e5d67d1dd47e5dddc93a4b622c46b
// sha3: 64183bfe2809b4e883c796c3b5125541e4be6bf9022efb1374d4bf2b842236a7
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use ruff_python_ast::{self as ast, Int, IpyEscapeKind};
use crate::{
@@ -54,7 +54,7 @@ mod __parse__Top {
Variant4(Int),
Variant5((IpyEscapeKind, String)),
Variant6(String),
Variant7((TextRange, StringKind, bool)),
Variant7((String, StringKind, bool)),
Variant8(core::option::Option<token::Tok>),
Variant9(Option<Box<ast::Parameter>>),
Variant10(core::option::Option<Option<Box<ast::Parameter>>>),
@@ -18373,6 +18373,16 @@ mod __parse__Top {
_ => __symbol_type_mismatch()
}
}
fn __pop_Variant7<
>(
__symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)>
) -> (TextSize, (String, StringKind, bool), TextSize)
{
match __symbols.pop() {
Some((__l, __Symbol::Variant7(__v), __r)) => (__l, __v, __r),
_ => __symbol_type_mismatch()
}
}
fn __pop_Variant3<
>(
__symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)>
@@ -18383,16 +18393,6 @@ mod __parse__Top {
_ => __symbol_type_mismatch()
}
}
fn __pop_Variant7<
>(
__symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)>
) -> (TextSize, (TextRange, StringKind, bool), TextSize)
{
match __symbols.pop() {
Some((__l, __Symbol::Variant7(__v), __r)) => (__l, __v, __r),
_ => __symbol_type_mismatch()
}
}
fn __pop_Variant67<
>(
__symbols: &mut alloc::vec::Vec<(TextSize,__Symbol<>,TextSize)>
@@ -36363,13 +36363,13 @@ fn __action217<
source_code: &str,
mode: Mode,
(_, location, _): (TextSize, TextSize, TextSize),
(_, string, _): (TextSize, (TextRange, StringKind, bool), TextSize),
(_, string, _): (TextSize, (String, StringKind, bool), TextSize),
(_, end_location, _): (TextSize, TextSize, TextSize),
) -> Result<StringType,__lalrpop_util::ParseError<TextSize,token::Tok,LexicalError>>
{
{
let (value, kind, triple_quoted) = string;
Ok(parse_string_literal(&source_code[value], kind, triple_quoted, (location..end_location).into())?)
let (source, kind, triple_quoted) = string;
Ok(parse_string_literal(&source, kind, triple_quoted, (location..end_location).into())?)
}
}
@@ -52719,7 +52719,7 @@ fn __action937<
>(
source_code: &str,
mode: Mode,
__0: (TextSize, (TextRange, StringKind, bool), TextSize),
__0: (TextSize, (String, StringKind, bool), TextSize),
__1: (TextSize, TextSize, TextSize),
) -> Result<StringType,__lalrpop_util::ParseError<TextSize,token::Tok,LexicalError>>
{
@@ -69997,7 +69997,7 @@ fn __action1494<
>(
source_code: &str,
mode: Mode,
__0: (TextSize, (TextRange, StringKind, bool), TextSize),
__0: (TextSize, (String, StringKind, bool), TextSize),
) -> Result<StringType,__lalrpop_util::ParseError<TextSize,token::Tok,LexicalError>>
{
let __start0 = __0.2;

View File

@@ -10,9 +10,7 @@ expression: comment_until_eol(MAC_EOL)
0..3,
),
(
Comment(
"# Foo",
),
Comment,
5..10,
),
(

View File

@@ -10,9 +10,7 @@ expression: comment_until_eol(UNIX_EOL)
0..3,
),
(
Comment(
"# Foo",
),
Comment,
5..10,
),
(

View File

@@ -10,9 +10,7 @@ expression: comment_until_eol(WINDOWS_EOL)
0..3,
),
(
Comment(
"# Foo",
),
Comment,
5..10,
),
(

View File

@@ -13,7 +13,7 @@ expression: lex_source(source)
),
(
String {
value: 5..5,
value: "",
kind: String,
triple_quoted: false,
},
@@ -37,7 +37,7 @@ expression: lex_source(source)
),
(
String {
value: 16..16,
value: "",
kind: String,
triple_quoted: false,
},

View File

@@ -5,7 +5,7 @@ expression: lex_source(source)
[
(
String {
value: 1..13,
value: "\\N{EN SPACE}",
kind: String,
triple_quoted: false,
},

View File

@@ -19,9 +19,7 @@ expression: lex_source(source)
21..22,
),
(
Comment(
"# comment {",
),
Comment,
23..34,
),
(

View File

@@ -137,7 +137,7 @@ expression: lex_source(source)
),
(
String {
value: 32..32,
value: "",
kind: String,
triple_quoted: false,
},

View File

@@ -10,9 +10,7 @@ expression: lex_source(&source)
0..5,
),
(
Comment(
"#",
),
Comment,
7..8,
),
(

View File

@@ -10,9 +10,7 @@ expression: lex_source(&source)
0..5,
),
(
Comment(
"# foo",
),
Comment,
7..12,
),
(

View File

@@ -10,9 +10,7 @@ expression: lex_source(&source)
0..5,
),
(
Comment(
"# ",
),
Comment,
7..9,
),
(

View File

@@ -10,9 +10,7 @@ expression: lex_source(&source)
0..5,
),
(
Comment(
"# ",
),
Comment,
7..10,
),
(

View File

@@ -4,9 +4,7 @@ expression: lex_source(source)
---
[
(
Comment(
"#Hello",
),
Comment,
0..6,
),
(
@@ -14,9 +12,7 @@ expression: lex_source(source)
6..7,
),
(
Comment(
"#World",
),
Comment,
7..13,
),
(

View File

@@ -13,7 +13,7 @@ expression: lex_source(source)
),
(
String {
value: 7..8,
value: "a",
kind: String,
triple_quoted: false,
},
@@ -25,7 +25,7 @@ expression: lex_source(source)
),
(
String {
value: 15..16,
value: "b",
kind: String,
triple_quoted: false,
},
@@ -41,7 +41,7 @@ expression: lex_source(source)
),
(
String {
value: 24..25,
value: "c",
kind: String,
triple_quoted: false,
},
@@ -49,7 +49,7 @@ expression: lex_source(source)
),
(
String {
value: 34..35,
value: "d",
kind: String,
triple_quoted: false,
},

View File

@@ -5,7 +5,7 @@ expression: lex_source(source)
[
(
String {
value: 1..7,
value: "double",
kind: String,
triple_quoted: false,
},
@@ -13,7 +13,7 @@ expression: lex_source(source)
),
(
String {
value: 10..16,
value: "single",
kind: String,
triple_quoted: false,
},
@@ -21,7 +21,7 @@ expression: lex_source(source)
),
(
String {
value: 19..25,
value: "can\\'t",
kind: String,
triple_quoted: false,
},
@@ -29,7 +29,7 @@ expression: lex_source(source)
),
(
String {
value: 28..32,
value: "\\\\\\\"",
kind: String,
triple_quoted: false,
},
@@ -37,7 +37,7 @@ expression: lex_source(source)
),
(
String {
value: 35..41,
value: "\\t\\r\\n",
kind: String,
triple_quoted: false,
},
@@ -45,7 +45,7 @@ expression: lex_source(source)
),
(
String {
value: 44..46,
value: "\\g",
kind: String,
triple_quoted: false,
},
@@ -53,7 +53,7 @@ expression: lex_source(source)
),
(
String {
value: 50..55,
value: "raw\\'",
kind: RawString,
triple_quoted: false,
},
@@ -61,7 +61,7 @@ expression: lex_source(source)
),
(
String {
value: 58..62,
value: "\\420",
kind: String,
triple_quoted: false,
},
@@ -69,7 +69,7 @@ expression: lex_source(source)
),
(
String {
value: 65..72,
value: "\\200\\0a",
kind: String,
triple_quoted: false,
},

View File

@@ -5,7 +5,7 @@ expression: string_continuation_with_eol(MAC_EOL)
[
(
String {
value: 1..9,
value: "abc\\\rdef",
kind: String,
triple_quoted: false,
},

View File

@@ -5,7 +5,7 @@ expression: string_continuation_with_eol(UNIX_EOL)
[
(
String {
value: 1..9,
value: "abc\\\ndef",
kind: String,
triple_quoted: false,
},

View File

@@ -5,7 +5,7 @@ expression: string_continuation_with_eol(WINDOWS_EOL)
[
(
String {
value: 1..10,
value: "abc\\\r\ndef",
kind: String,
triple_quoted: false,
},

View File

@@ -5,7 +5,7 @@ expression: triple_quoted_eol(MAC_EOL)
[
(
String {
value: 3..18,
value: "\r test string\r ",
kind: String,
triple_quoted: true,
},

View File

@@ -5,7 +5,7 @@ expression: triple_quoted_eol(UNIX_EOL)
[
(
String {
value: 3..18,
value: "\n test string\n ",
kind: String,
triple_quoted: true,
},

View File

@@ -5,7 +5,7 @@ expression: triple_quoted_eol(WINDOWS_EOL)
[
(
String {
value: 3..20,
value: "\r\n test string\r\n ",
kind: String,
triple_quoted: true,
},

View File

@@ -7,7 +7,7 @@
use crate::Mode;
use ruff_python_ast::{Int, IpyEscapeKind};
use ruff_text_size::{TextRange, TextSize};
use ruff_text_size::TextSize;
use std::fmt;
/// The set of tokens the Python source code can be tokenized in.
@@ -37,8 +37,8 @@ pub enum Tok {
},
/// Token value for a string.
String {
/// The range of the string value.
value: TextRange,
/// The string value.
value: String,
/// The kind of string.
kind: StringKind,
/// Whether the string is triple quoted.
@@ -51,8 +51,6 @@ pub enum Tok {
/// part of the expression part and isn't an opening or closing brace.
FStringMiddle {
/// The string value.
// TODO(charlie): This could _maybe_ be a range, but we'd have to move logic into the parser
// to handle some escaping.
value: String,
/// Whether the string is raw or not.
is_raw: bool,
@@ -68,7 +66,7 @@ pub enum Tok {
kind: IpyEscapeKind,
},
/// Token value for a comment. These are filtered out of the token stream prior to parsing.
Comment(String),
Comment,
/// Token value for a newline.
Newline,
/// Token value for a newline that is not a logical line break. These are filtered out of
@@ -243,7 +241,14 @@ impl fmt::Display for Tok {
Int { value } => write!(f, "'{value}'"),
Float { value } => write!(f, "'{value}'"),
Complex { real, imag } => write!(f, "{real}j{imag}"),
String { .. } => write!(f, "String"),
String {
value,
kind,
triple_quoted,
} => {
let quotes = "\"".repeat(if *triple_quoted { 3 } else { 1 });
write!(f, "{kind}{quotes}{value}{quotes}")
}
FStringStart => f.write_str("FStringStart"),
FStringMiddle { value, .. } => f.write_str(value),
FStringEnd => f.write_str("FStringEnd"),
@@ -263,7 +268,7 @@ impl fmt::Display for Tok {
Rsqb => f.write_str("']'"),
Colon => f.write_str("':'"),
Comma => f.write_str("','"),
Comment(value) => f.write_str(value),
Comment => f.write_str("Comment"),
Semi => f.write_str("';'"),
Plus => f.write_str("'+'"),
Minus => f.write_str("'-'"),
@@ -801,7 +806,7 @@ impl TokenKind {
Tok::FStringMiddle { .. } => TokenKind::FStringMiddle,
Tok::FStringEnd => TokenKind::FStringEnd,
Tok::IpyEscapeCommand { .. } => TokenKind::EscapeCommand,
Tok::Comment(_) => TokenKind::Comment,
Tok::Comment => TokenKind::Comment,
Tok::Newline => TokenKind::Newline,
Tok::NonLogicalNewline => TokenKind::NonLogicalNewline,
Tok::Indent => TokenKind::Indent,

View File

@@ -1019,7 +1019,7 @@ mod tests {
let comment_ranges: Vec<_> = lex(self.source, Mode::Module)
.filter_map(|result| {
let (token, range) = result.expect("Input to be a valid python program.");
if matches!(token, Tok::Comment(_)) {
if matches!(token, Tok::Comment) {
Some(range)
} else {
None