-
Notifications
You must be signed in to change notification settings - Fork 882
/
blank_lines.rs
1030 lines (922 loc) · 34.5 KB
/
blank_lines.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use itertools::Itertools;
use std::cmp::Ordering;
use std::num::NonZeroU32;
use std::slice::Iter;
use ruff_diagnostics::AlwaysFixableViolation;
use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Edit;
use ruff_diagnostics::Fix;
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::PySourceType;
use ruff_python_codegen::Stylist;
use ruff_python_parser::lexer::LexResult;
use ruff_python_parser::lexer::LexicalError;
use ruff_python_parser::Tok;
use ruff_python_parser::TokenKind;
use ruff_source_file::{Locator, UniversalNewlines};
use ruff_text_size::TextRange;
use ruff_text_size::TextSize;
use crate::checkers::logical_lines::expand_indent;
use crate::line_width::IndentWidth;
use ruff_python_trivia::PythonWhitespace;
/// Number of blank lines around top level classes and functions.
const BLANK_LINES_TOP_LEVEL: u32 = 2;
/// Number of blank lines around methods and nested classes and functions.
const BLANK_LINES_NESTED_LEVEL: u32 = 1;
/// ## What it does
/// Checks for missing blank lines between methods of a class.
///
/// ## Why is this bad?
/// PEP 8 recommends exactly one blank line between methods of a class.
///
/// ## Example
/// ```python
/// class MyClass(object):
/// def func1():
/// pass
/// def func2():
/// pass
/// ```
///
/// Use instead:
/// ```python
/// class MyClass(object):
/// def func1():
/// pass
///
/// def func2():
/// pass
/// ```
///
/// ## Typing stub files (`.pyi`)
/// The typing style guide recommends to not use blank lines between methods except to group
/// them. That's why this rule is not enabled in typing stub files.
///
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines)
/// - [Flake 8 rule](https://www.flake8rules.com/rules/E301.html)
/// - [Typing Style Guide](https://typing.readthedocs.io/en/latest/source/stubs.html#blank-lines)
#[violation]
pub struct BlankLineBetweenMethods;
impl AlwaysFixableViolation for BlankLineBetweenMethods {
#[derive_message_formats]
fn message(&self) -> String {
format!("Expected {BLANK_LINES_NESTED_LEVEL:?} blank line, found 0")
}
fn fix_title(&self) -> String {
"Add missing blank line".to_string()
}
}
/// ## What it does
/// Checks for missing blank lines between top level functions and classes.
///
/// ## Why is this bad?
/// PEP 8 recommends exactly two blank lines between top level functions and classes.
///
/// Note: The rule respects the [`lint.isort.lines-after-imports`] setting when determining
/// the required number of blank lines between top-level `import` statements and function or class definitions
/// for compatibility with isort.
///
/// ## Example
/// ```python
/// def func1():
/// pass
/// def func2():
/// pass
/// ```
///
/// Use instead:
/// ```python
/// def func1():
/// pass
///
///
/// def func2():
/// pass
/// ```
///
/// ## Typing stub files (`.pyi`)
/// The typing style guide recommends to not use blank lines between classes and functions except to group
/// them. That's why this rule is not enabled in typing stub files.
///
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines)
/// - [Flake 8 rule](https://www.flake8rules.com/rules/E302.html)
/// - [Typing Style Guide](https://typing.readthedocs.io/en/latest/source/stubs.html#blank-lines)
#[violation]
pub struct BlankLinesTopLevel {
actual_blank_lines: u32,
expected_blank_lines: u32,
}
impl AlwaysFixableViolation for BlankLinesTopLevel {
#[derive_message_formats]
fn message(&self) -> String {
let BlankLinesTopLevel {
actual_blank_lines,
expected_blank_lines,
} = self;
format!("Expected {expected_blank_lines:?} blank lines, found {actual_blank_lines}")
}
fn fix_title(&self) -> String {
"Add missing blank line(s)".to_string()
}
}
/// ## What it does
/// Checks for extraneous blank lines.
///
/// ## Why is this bad?
/// PEP 8 recommends using blank lines as follows:
/// - No more than two blank lines between top-level statements.
/// - No more than one blank line between non-top-level statements.
///
/// ## Example
/// ```python
/// def func1():
/// pass
///
///
///
/// def func2():
/// pass
/// ```
///
/// Use instead:
/// ```python
/// def func1():
/// pass
///
///
/// def func2():
/// pass
/// ```
///
/// ## Typing stub files (`.pyi`)
/// The rule allows at most one blank line in typing stub files in accordance to the typing style guide recommendation.
///
/// Note: The rule respects the following `isort` settings when determining the maximum number of blank lines allowed between two statements:
/// * [`lint.isort.lines-after-imports`]: For top-level statements directly following an import statement.
/// * [`lint.isort.lines-between-types`]: For `import` statements directly following a `from ... import ...` statement or vice versa.
///
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines)
/// - [Flake 8 rule](https://www.flake8rules.com/rules/E303.html)
/// - [Typing Style Guide](https://typing.readthedocs.io/en/latest/source/stubs.html#blank-lines)
#[violation]
pub struct TooManyBlankLines {
actual_blank_lines: u32,
}
impl AlwaysFixableViolation for TooManyBlankLines {
#[derive_message_formats]
fn message(&self) -> String {
let TooManyBlankLines { actual_blank_lines } = self;
format!("Too many blank lines ({actual_blank_lines})")
}
fn fix_title(&self) -> String {
"Remove extraneous blank line(s)".to_string()
}
}
/// ## What it does
/// Checks for extraneous blank line(s) after function decorators.
///
/// ## Why is this bad?
/// There should be no blank lines between a decorator and the object it is decorating.
///
/// ## Example
/// ```python
/// class User(object):
///
/// @property
///
/// def name(self):
/// pass
/// ```
///
/// Use instead:
/// ```python
/// class User(object):
///
/// @property
/// def name(self):
/// pass
/// ```
///
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines)
/// - [Flake 8 rule](https://www.flake8rules.com/rules/E304.html)
#[violation]
pub struct BlankLineAfterDecorator {
actual_blank_lines: u32,
}
impl AlwaysFixableViolation for BlankLineAfterDecorator {
#[derive_message_formats]
fn message(&self) -> String {
format!(
"Blank lines found after function decorator ({lines})",
lines = self.actual_blank_lines
)
}
fn fix_title(&self) -> String {
"Remove extraneous blank line(s)".to_string()
}
}
/// ## What it does
/// Checks for missing blank lines after the end of function or class.
///
/// ## Why is this bad?
/// PEP 8 recommends using blank lines as follows:
/// - Two blank lines are expected between functions and classes
/// - One blank line is expected between methods of a class.
///
/// ## Example
/// ```python
/// class User(object):
/// pass
/// user = User()
/// ```
///
/// Use instead:
/// ```python
/// class User(object):
/// pass
///
///
/// user = User()
/// ```
///
/// ## Typing stub files (`.pyi`)
/// The typing style guide recommends to not use blank lines between statements except to group
/// them. That's why this rule is not enabled in typing stub files.
///
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines)
/// - [Flake 8 rule](https://www.flake8rules.com/rules/E305.html)
/// - [Typing Style Guide](https://typing.readthedocs.io/en/latest/source/stubs.html#blank-lines)
#[violation]
pub struct BlankLinesAfterFunctionOrClass {
actual_blank_lines: u32,
}
impl AlwaysFixableViolation for BlankLinesAfterFunctionOrClass {
#[derive_message_formats]
fn message(&self) -> String {
let BlankLinesAfterFunctionOrClass {
actual_blank_lines: blank_lines,
} = self;
format!("Expected 2 blank lines after class or function definition, found ({blank_lines})")
}
fn fix_title(&self) -> String {
"Add missing blank line(s)".to_string()
}
}
/// ## What it does
/// Checks for 1 blank line between nested function or class definitions.
///
/// ## Why is this bad?
/// PEP 8 recommends using blank lines as follows:
/// - Two blank lines are expected between functions and classes
/// - One blank line is expected between methods of a class.
///
/// ## Example
/// ```python
/// def outer():
/// def inner():
/// pass
/// def inner2():
/// pass
/// ```
///
/// Use instead:
/// ```python
/// def outer():
/// def inner():
/// pass
///
/// def inner2():
/// pass
/// ```
///
/// ## Typing stub files (`.pyi`)
/// The typing style guide recommends to not use blank lines between classes and functions except to group
/// them. That's why this rule is not enabled in typing stub files.
///
/// ## References
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines)
/// - [Flake 8 rule](https://www.flake8rules.com/rules/E306.html)
/// - [Typing Style Guide](https://typing.readthedocs.io/en/latest/source/stubs.html#blank-lines)
#[violation]
pub struct BlankLinesBeforeNestedDefinition;
impl AlwaysFixableViolation for BlankLinesBeforeNestedDefinition {
#[derive_message_formats]
fn message(&self) -> String {
format!("Expected 1 blank line before a nested definition, found 0")
}
fn fix_title(&self) -> String {
"Add missing blank line".to_string()
}
}
#[derive(Debug)]
struct LogicalLineInfo {
kind: LogicalLineKind,
first_token_range: TextRange,
// The token's kind right before the newline ending the logical line.
last_token: TokenKind,
// The end of the logical line including the newline.
logical_line_end: TextSize,
// `true` if this is not a blank but only consists of a comment.
is_comment_only: bool,
/// `true` if the line is a string only (including trivia tokens) line, which is a docstring if coming right after a class/function definition.
is_docstring: bool,
/// The indentation length in columns. See [`expand_indent`] for the computation of the indent.
indent_length: usize,
/// The number of blank lines preceding the current line.
blank_lines: BlankLines,
/// The maximum number of consecutive blank lines between the current line
/// and the previous non-comment logical line.
/// One of its main uses is to allow a comments to directly precede or follow a class/function definition.
/// As such, `preceding_blank_lines` is used for rules that cannot trigger on comments (all rules except E303),
/// and `blank_lines` is used for the rule that can trigger on comments (E303).
preceding_blank_lines: BlankLines,
}
/// Iterator that processes tokens until a full logical line (or comment line) is "built".
/// It then returns characteristics of that logical line (see `LogicalLineInfo`).
struct LinePreprocessor<'a> {
tokens: Iter<'a, Result<(Tok, TextRange), LexicalError>>,
locator: &'a Locator<'a>,
indent_width: IndentWidth,
/// The start position of the next logical line.
line_start: TextSize,
/// Maximum number of consecutive blank lines between the current line and the previous non-comment logical line.
/// One of its main uses is to allow a comment to directly precede a class/function definition.
max_preceding_blank_lines: BlankLines,
}
impl<'a> LinePreprocessor<'a> {
fn new(
tokens: &'a [LexResult],
locator: &'a Locator,
indent_width: IndentWidth,
) -> LinePreprocessor<'a> {
LinePreprocessor {
tokens: tokens.iter(),
locator,
line_start: TextSize::new(0),
max_preceding_blank_lines: BlankLines::Zero,
indent_width,
}
}
}
impl<'a> Iterator for LinePreprocessor<'a> {
type Item = LogicalLineInfo;
fn next(&mut self) -> Option<LogicalLineInfo> {
let mut line_is_comment_only = true;
let mut is_docstring = false;
// Number of consecutive blank lines directly preceding this logical line.
let mut blank_lines = BlankLines::Zero;
let mut first_logical_line_token: Option<(LogicalLineKind, TextRange)> = None;
let mut last_token: TokenKind = TokenKind::EndOfFile;
let mut parens = 0u32;
while let Some(result) = self.tokens.next() {
let Ok((token, range)) = result else {
continue;
};
if matches!(token, Tok::Indent | Tok::Dedent) {
continue;
}
let token_kind = TokenKind::from_token(token);
let (logical_line_kind, first_token_range) = if let Some(first_token_range) =
first_logical_line_token
{
first_token_range
}
// At the start of the line...
else {
// An empty line
if token_kind == TokenKind::NonLogicalNewline {
blank_lines.add(*range);
self.line_start = range.end();
continue;
}
is_docstring = token_kind == TokenKind::String;
let logical_line_kind = match token_kind {
TokenKind::Class => LogicalLineKind::Class,
TokenKind::Comment => LogicalLineKind::Comment,
TokenKind::At => LogicalLineKind::Decorator,
TokenKind::Def => LogicalLineKind::Function,
// Lookahead to distinguish `async def` from `async with`.
TokenKind::Async
if matches!(self.tokens.as_slice().first(), Some(Ok((Tok::Def, _)))) =>
{
LogicalLineKind::Function
}
TokenKind::Import => LogicalLineKind::Import,
TokenKind::From => LogicalLineKind::FromImport,
_ => LogicalLineKind::Other,
};
first_logical_line_token = Some((logical_line_kind, *range));
(logical_line_kind, *range)
};
if !token_kind.is_trivia() {
line_is_comment_only = false;
}
// A docstring line is composed only of the docstring (TokenKind::String) and trivia tokens.
// (If a comment follows a docstring, we still count the line as a docstring)
if token_kind != TokenKind::String && !token_kind.is_trivia() {
is_docstring = false;
}
match token_kind {
TokenKind::Lbrace | TokenKind::Lpar | TokenKind::Lsqb => {
parens = parens.saturating_add(1);
}
TokenKind::Rbrace | TokenKind::Rpar | TokenKind::Rsqb => {
parens = parens.saturating_sub(1);
}
TokenKind::Newline | TokenKind::NonLogicalNewline if parens == 0 => {
let indent_range = TextRange::new(self.line_start, first_token_range.start());
let indent_length =
expand_indent(self.locator.slice(indent_range), self.indent_width);
self.max_preceding_blank_lines =
self.max_preceding_blank_lines.max(blank_lines);
let logical_line = LogicalLineInfo {
kind: logical_line_kind,
first_token_range,
last_token,
logical_line_end: range.end(),
is_comment_only: line_is_comment_only,
is_docstring,
indent_length,
blank_lines,
preceding_blank_lines: self.max_preceding_blank_lines,
};
// Reset the blank lines after a non-comment only line.
if !line_is_comment_only {
self.max_preceding_blank_lines = BlankLines::Zero;
}
// Set the start for the next logical line.
self.line_start = range.end();
return Some(logical_line);
}
_ => {}
}
last_token = token_kind;
}
None
}
}
#[derive(Clone, Copy, Debug, Default)]
enum BlankLines {
/// No blank lines
#[default]
Zero,
/// One or more blank lines
Many { count: NonZeroU32, range: TextRange },
}
impl BlankLines {
fn add(&mut self, line_range: TextRange) {
match self {
BlankLines::Zero => {
*self = BlankLines::Many {
count: NonZeroU32::MIN,
range: line_range,
}
}
BlankLines::Many { count, range } => {
*count = count.saturating_add(1);
*range = TextRange::new(range.start(), line_range.end());
}
}
}
fn count(&self) -> u32 {
match self {
BlankLines::Zero => 0,
BlankLines::Many { count, .. } => count.get(),
}
}
fn range(&self) -> Option<TextRange> {
match self {
BlankLines::Zero => None,
BlankLines::Many { range, .. } => Some(*range),
}
}
}
impl PartialEq<u32> for BlankLines {
fn eq(&self, other: &u32) -> bool {
self.partial_cmp(other) == Some(Ordering::Equal)
}
}
impl PartialOrd<u32> for BlankLines {
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
self.count().partial_cmp(other)
}
}
impl PartialOrd for BlankLines {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BlankLines {
fn cmp(&self, other: &Self) -> Ordering {
self.count().cmp(&other.count())
}
}
impl PartialEq for BlankLines {
fn eq(&self, other: &Self) -> bool {
self.count() == other.count()
}
}
impl Eq for BlankLines {}
#[derive(Copy, Clone, Debug, Default)]
enum Follows {
#[default]
Other,
Decorator,
Def,
Import,
FromImport,
Docstring,
}
impl Follows {
const fn is_any_import(self) -> bool {
matches!(self, Follows::Import | Follows::FromImport)
}
}
#[derive(Copy, Clone, Debug, Default)]
enum Status {
/// Stores the indent level where the nesting started.
Inside(usize),
/// This is used to rectify a Inside switched to a Outside because of a dedented comment.
CommentAfter(usize),
#[default]
Outside,
}
impl Status {
fn update(&mut self, line: &LogicalLineInfo) {
match *self {
Status::Inside(nesting_indent) => {
if line.indent_length <= nesting_indent {
if line.is_comment_only {
*self = Status::CommentAfter(nesting_indent);
} else {
*self = Status::Outside;
}
}
}
Status::CommentAfter(indent) => {
if !line.is_comment_only {
if line.indent_length > indent {
*self = Status::Inside(indent);
} else {
*self = Status::Outside;
}
}
}
Status::Outside => {
// Nothing to do
}
}
}
}
/// Contains variables used for the linting of blank lines.
#[derive(Debug)]
pub(crate) struct BlankLinesChecker<'a> {
stylist: &'a Stylist<'a>,
locator: &'a Locator<'a>,
indent_width: IndentWidth,
lines_after_imports: isize,
lines_between_types: usize,
source_type: PySourceType,
}
impl<'a> BlankLinesChecker<'a> {
pub(crate) fn new(
locator: &'a Locator<'a>,
stylist: &'a Stylist<'a>,
settings: &crate::settings::LinterSettings,
source_type: PySourceType,
) -> BlankLinesChecker<'a> {
BlankLinesChecker {
stylist,
locator,
indent_width: settings.tab_size,
lines_after_imports: settings.isort.lines_after_imports,
lines_between_types: settings.isort.lines_between_types,
source_type,
}
}
/// E301, E302, E303, E304, E305, E306
pub(crate) fn check_lines(&self, tokens: &[LexResult], diagnostics: &mut Vec<Diagnostic>) {
let mut prev_indent_length: Option<usize> = None;
let mut state = BlankLinesState::default();
let line_preprocessor = LinePreprocessor::new(tokens, self.locator, self.indent_width);
for logical_line in line_preprocessor {
// Reset `follows` after a dedent:
// ```python
// if True:
// import test
// a = 10
// ```
// The `a` statement doesn't follow the `import` statement but the `if` statement.
if let Some(prev_indent_length) = prev_indent_length {
if prev_indent_length > logical_line.indent_length {
state.follows = Follows::Other;
}
}
state.class_status.update(&logical_line);
state.fn_status.update(&logical_line);
self.check_line(&logical_line, &state, prev_indent_length, diagnostics);
match logical_line.kind {
LogicalLineKind::Class => {
if matches!(state.class_status, Status::Outside) {
state.class_status = Status::Inside(logical_line.indent_length);
}
state.follows = Follows::Other;
}
LogicalLineKind::Decorator => {
state.follows = Follows::Decorator;
}
LogicalLineKind::Function => {
if matches!(state.fn_status, Status::Outside) {
state.fn_status = Status::Inside(logical_line.indent_length);
}
state.follows = Follows::Def;
}
LogicalLineKind::Comment => {}
LogicalLineKind::Import => {
state.follows = Follows::Import;
}
LogicalLineKind::FromImport => {
state.follows = Follows::FromImport;
}
LogicalLineKind::Other => {
state.follows = Follows::Other;
}
}
if logical_line.is_docstring {
state.follows = Follows::Docstring;
}
if !logical_line.is_comment_only {
state.is_not_first_logical_line = true;
state.last_non_comment_line_end = logical_line.logical_line_end;
if logical_line.indent_length == 0 {
state.previous_unindented_line_kind = Some(logical_line.kind);
}
}
if !logical_line.is_comment_only {
prev_indent_length = Some(logical_line.indent_length);
}
}
}
#[allow(clippy::nonminimal_bool)]
fn check_line(
&self,
line: &LogicalLineInfo,
state: &BlankLinesState,
prev_indent_length: Option<usize>,
diagnostics: &mut Vec<Diagnostic>,
) {
if line.preceding_blank_lines == 0
// Only applies to methods.
&& matches!(line.kind, LogicalLineKind::Function | LogicalLineKind::Decorator)
// Allow groups of one-liners.
&& !(matches!(state.follows, Follows::Def) && !matches!(line.last_token, TokenKind::Colon))
&& matches!(state.class_status, Status::Inside(_))
// The class/parent method's docstring can directly precede the def.
// Allow following a decorator (if there is an error it will be triggered on the first decorator).
&& !matches!(state.follows, Follows::Docstring | Follows::Decorator)
// Do not trigger when the def follows an if/while/etc...
&& prev_indent_length.is_some_and(|prev_indent_length| prev_indent_length >= line.indent_length)
// Blank lines in stub files are only used for grouping. Don't enforce blank lines.
&& !self.source_type.is_stub()
{
// E301
let mut diagnostic = Diagnostic::new(BlankLineBetweenMethods, line.first_token_range);
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
self.stylist.line_ending().to_string(),
self.locator.line_start(state.last_non_comment_line_end),
)));
diagnostics.push(diagnostic);
}
// Blank lines in stub files are used to group definitions. Don't enforce blank lines.
let max_lines_level = if self.source_type.is_stub() {
1
} else {
if line.indent_length == 0 {
BLANK_LINES_TOP_LEVEL
} else {
BLANK_LINES_NESTED_LEVEL
}
};
let expected_blank_lines_before_definition = if line.indent_length == 0 {
// Mimic the isort rules for the number of blank lines before classes and functions
if state.follows.is_any_import() {
// Fallback to the default if the value is too large for an u32 or if it is negative.
// A negative value means that isort should determine the blank lines automatically.
// `isort` defaults to 2 if before a class or function definition (except in stubs where it is one) and 1 otherwise.
// Defaulting to 2 (or 1 in stubs) here is correct because the variable is only used when testing the
// blank lines before a class or function definition.
u32::try_from(self.lines_after_imports).unwrap_or(max_lines_level)
} else {
max_lines_level
}
} else {
max_lines_level
};
if line.preceding_blank_lines < expected_blank_lines_before_definition
// Allow following a decorator (if there is an error it will be triggered on the first decorator).
&& !matches!(state.follows, Follows::Decorator)
// Allow groups of one-liners.
&& !(matches!(state.follows, Follows::Def) && !matches!(line.last_token, TokenKind::Colon))
// Only trigger on non-indented classes and functions (for example functions within an if are ignored)
&& line.indent_length == 0
// Only apply to functions or classes.
&& line.kind.is_class_function_or_decorator()
// Blank lines in stub files are used to group definitions. Don't enforce blank lines.
&& !self.source_type.is_stub()
// Do not expect blank lines before the first logical line.
&& state.is_not_first_logical_line
{
// E302
let mut diagnostic = Diagnostic::new(
BlankLinesTopLevel {
actual_blank_lines: line.preceding_blank_lines.count(),
expected_blank_lines: expected_blank_lines_before_definition,
},
line.first_token_range,
);
if let Some(blank_lines_range) = line.blank_lines.range() {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
self.stylist
.line_ending()
.repeat(expected_blank_lines_before_definition as usize),
blank_lines_range,
)));
} else {
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
self.stylist
.line_ending()
.repeat(expected_blank_lines_before_definition as usize),
self.locator.line_start(state.last_non_comment_line_end),
)));
}
diagnostics.push(diagnostic);
}
// If between `import` and `from .. import ..` or the other way round,
// allow up to `lines_between_types` newlines for isort compatibility.
// We let `isort` remove extra blank lines when the imports belong
// to different sections.
let max_blank_lines = if matches!(
(line.kind, state.follows),
(LogicalLineKind::Import, Follows::FromImport)
| (LogicalLineKind::FromImport, Follows::Import)
) {
max_lines_level.max(u32::try_from(self.lines_between_types).unwrap_or(u32::MAX))
} else {
expected_blank_lines_before_definition
};
if line.blank_lines > max_blank_lines {
// E303
let mut diagnostic = Diagnostic::new(
TooManyBlankLines {
actual_blank_lines: line.blank_lines.count(),
},
line.first_token_range,
);
if let Some(blank_lines_range) = line.blank_lines.range() {
if max_blank_lines == 0 {
diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion(blank_lines_range)));
} else {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
self.stylist.line_ending().repeat(max_blank_lines as usize),
blank_lines_range,
)));
}
}
diagnostics.push(diagnostic);
}
if matches!(state.follows, Follows::Decorator)
&& !line.is_comment_only
&& line.preceding_blank_lines > 0
{
// E304
let mut diagnostic = Diagnostic::new(
BlankLineAfterDecorator {
actual_blank_lines: line.preceding_blank_lines.count(),
},
line.first_token_range,
);
// Get all the lines between the last decorator line (included) and the current line (included).
// Then remove all blank lines.
let trivia_range = TextRange::new(
state.last_non_comment_line_end,
self.locator.line_start(line.first_token_range.start()),
);
let trivia_text = self.locator.slice(trivia_range);
let mut trivia_without_blank_lines = trivia_text
.universal_newlines()
.filter_map(|line| (!line.trim_whitespace().is_empty()).then_some(line.as_str()))
.join(&self.stylist.line_ending());
let fix = if trivia_without_blank_lines.is_empty() {
Fix::safe_edit(Edit::range_deletion(trivia_range))
} else {
trivia_without_blank_lines.push_str(&self.stylist.line_ending());
Fix::safe_edit(Edit::range_replacement(
trivia_without_blank_lines,
trivia_range,
))
};
diagnostic.set_fix(fix);
diagnostics.push(diagnostic);
}
if line.preceding_blank_lines < BLANK_LINES_TOP_LEVEL
&& state
.previous_unindented_line_kind
.is_some_and(LogicalLineKind::is_class_function_or_decorator)
&& line.indent_length == 0
&& !line.is_comment_only
&& !line.kind.is_class_function_or_decorator()
// Blank lines in stub files are used for grouping, don't enforce blank lines.
&& !self.source_type.is_stub()
{
// E305
let mut diagnostic = Diagnostic::new(
BlankLinesAfterFunctionOrClass {
actual_blank_lines: line.preceding_blank_lines.count(),
},
line.first_token_range,
);
if let Some(blank_lines_range) = line.blank_lines.range() {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
self.stylist
.line_ending()
.repeat(BLANK_LINES_TOP_LEVEL as usize),
blank_lines_range,
)));
} else {
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
self.stylist
.line_ending()
.repeat(BLANK_LINES_TOP_LEVEL as usize),
self.locator.line_start(line.first_token_range.start()),
)));
}
diagnostics.push(diagnostic);
}
if line.preceding_blank_lines == 0
// Only apply to nested functions.
&& matches!(state.fn_status, Status::Inside(_))
&& line.kind.is_class_function_or_decorator()
// Allow following a decorator (if there is an error it will be triggered on the first decorator).
&& !matches!(state.follows, Follows::Decorator)
// The class's docstring can directly precede the first function.
&& !matches!(state.follows, Follows::Docstring)
// Do not trigger when the def/class follows an "indenting token" (if/while/etc...).
&& prev_indent_length.is_some_and(|prev_indent_length| prev_indent_length >= line.indent_length)
// Allow groups of one-liners.
&& !(matches!(state.follows, Follows::Def) && line.last_token != TokenKind::Colon)
// Blank lines in stub files are only used for grouping. Don't enforce blank lines.
&& !self.source_type.is_stub()
{
// E306
let mut diagnostic =
Diagnostic::new(BlankLinesBeforeNestedDefinition, line.first_token_range);
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
self.stylist.line_ending().to_string(),
self.locator.line_start(line.first_token_range.start()),
)));
diagnostics.push(diagnostic);
}
}
}
#[derive(Clone, Debug, Default)]
struct BlankLinesState {
follows: Follows,
fn_status: Status,
class_status: Status,
/// First line that is not a comment.
is_not_first_logical_line: bool,
/// Used for the fix in case a comment separates two non-comment logical lines to make the comment "stick"
/// to the second line instead of the first.