File tree 3 files changed +86
-0
lines changed
crates/ruff_python_formatter
resources/test/fixtures/ruff/expression
3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 33
33
# comment
34
34
''
35
35
)
36
+
37
+ (
38
+ f'{ 1 } ' # comment
39
+ f'{ 2 } '
40
+ )
41
+
42
+ (
43
+ f'{ 1 } '
44
+ f'{ 2 } ' # comment
45
+ )
46
+
47
+ (
48
+ 1 , ( # comment
49
+ f'{ 2 } '
50
+ )
51
+ )
52
+
53
+ (
54
+ (
55
+ f'{ 1 } '
56
+ # comment
57
+ ),
58
+ 2
59
+ )
Original file line number Diff line number Diff line change @@ -70,6 +70,20 @@ fn handle_parenthesized_comment<'a>(
70
70
comment : DecoratedComment < ' a > ,
71
71
locator : & Locator ,
72
72
) -> CommentPlacement < ' a > {
73
+ // As a special-case, ignore comments within f-strings, like:
74
+ // ```python
75
+ // (
76
+ // f'{1}' # comment
77
+ // f'{2}'
78
+ // )
79
+ // ```
80
+ // These can't be parenthesized, as they must fall between two string tokens in an implicit
81
+ // concatenation. But the expression ranges only include the `1` and `2` above, so we also
82
+ // can't lex the contents between them.
83
+ if comment. enclosing_node ( ) . is_expr_f_string ( ) {
84
+ return CommentPlacement :: Default ( comment) ;
85
+ }
86
+
73
87
let Some ( preceding) = comment. preceding_node ( ) else {
74
88
return CommentPlacement :: Default ( comment) ;
75
89
} ;
Original file line number Diff line number Diff line change @@ -39,6 +39,30 @@ result_f = (
39
39
# comment
40
40
''
41
41
)
42
+
43
+ (
44
+ f'{ 1 } ' # comment
45
+ f'{ 2 } '
46
+ )
47
+
48
+ (
49
+ f'{ 1 } '
50
+ f'{ 2 } ' # comment
51
+ )
52
+
53
+ (
54
+ 1, ( # comment
55
+ f'{ 2 } '
56
+ )
57
+ )
58
+
59
+ (
60
+ (
61
+ f'{ 1 } '
62
+ # comment
63
+ ),
64
+ 2
65
+ )
42
66
` ` `
43
67
44
68
## Output
@@ -76,6 +100,30 @@ result_f = (
76
100
# comment
77
101
""
78
102
)
103
+
104
+ (
105
+ f"{ 1 } " # comment
106
+ f"{ 2 } "
107
+ )
108
+
109
+ (
110
+ f"{ 1 } " f"{ 2 } " # comment
111
+ )
112
+
113
+ (
114
+ 1,
115
+ ( # comment
116
+ f"{ 2 } "
117
+ ),
118
+ )
119
+
120
+ (
121
+ (
122
+ f"{ 1 } "
123
+ # comment
124
+ ),
125
+ 2,
126
+ )
79
127
` ` `
80
128
81
129
You can’t perform that action at this time.
0 commit comments