Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Apr 12, 2023
1 parent fae1586 commit 6ecd573
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 117 deletions.
4 changes: 2 additions & 2 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ where
.rules
.enabled(Rule::ModuleImportNotAtTopOfFile)
{
pycodestyle::rules::module_import_not_at_top_of_file(self, stmt);
pycodestyle::rules::module_import_not_at_top_of_file(self, stmt, self.locator);
}

if self.settings.rules.enabled(Rule::GlobalStatement) {
Expand Down Expand Up @@ -1102,7 +1102,7 @@ where
.rules
.enabled(Rule::ModuleImportNotAtTopOfFile)
{
pycodestyle::rules::module_import_not_at_top_of_file(self, stmt);
pycodestyle::rules::module_import_not_at_top_of_file(self, stmt, self.locator);
}

if self.settings.rules.enabled(Rule::GlobalStatement) {
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ruff_text_size::TextRange;
use rustpython_parser::ast::Location;
use rustpython_parser::lexer::LexResult;

use ruff_diagnostics::{Diagnostic, Fix};
Expand Down Expand Up @@ -136,7 +135,9 @@ pub fn check_logical_lines(
}
}
if line.flags().contains(TokenFlags::COMMENT) {
for (range, kind) in whitespace_before_comment(&line.tokens(), locator) {
for (range, kind) in
whitespace_before_comment(&line.tokens(), locator, prev_line.is_none())
{
if settings.rules.enabled(kind.rule()) {
diagnostics.push(Diagnostic {
kind,
Expand All @@ -161,8 +162,7 @@ pub fn check_logical_lines(

// Extract the indentation level.
let Some(start_loc) = line.first_token_location() else { continue; };
let start_line =
locator.slice(TextRange::new(Location::new(start_loc.row(), 0), start_loc));
let start_line = locator.slice(TextRange::new(locator.line_start(start_loc), start_loc));
let indent_level = expand_indent(start_line);
let indent_size = 4;

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ fn diagnostics_to_messages(
let file = once_cell::unsync::Lazy::new(|| {
let mut builder = SourceFileBuilder::new(&path.to_string_lossy());
if settings.show_source {
builder.set_source_text(locator.contents());
builder.set_source_code(&locator.to_source_code())
}

builder.finish()
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/message/grouped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use colored::Colorize;
use ruff_python_ast::source_code::OneIndexed;
use std::fmt::{Display, Formatter};
use std::io::Write;
use std::num::{NonZeroU32, NonZeroUsize};
use std::num::NonZeroUsize;

#[derive(Default)]
pub struct GroupedEmitter {
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub fn add_noqa(
path: &Path,
diagnostics: &[Diagnostic],
contents: &str,
commented_lines: &[usize],
commented_lines: &[TextRange],
noqa_line_for: &IntMap<usize, usize>,
line_ending: LineEnding,
) -> Result<usize> {
Expand All @@ -204,7 +204,7 @@ pub fn add_noqa(
fn add_noqa_inner(
diagnostics: &[Diagnostic],
contents: &str,
commented_lines: &[usize],
commented_lines: &[TextRange],
noqa_line_for: &IntMap<usize, usize>,
line_ending: LineEnding,
) -> (usize, String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ruff_text_size::TextRange;
use rustpython_parser::ast::{Expr, ExprKind};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic};
Expand Down
19 changes: 8 additions & 11 deletions crates/ruff/src/rules/flake8_executable/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruff_text_size::{TextLen, TextSize};
static SHEBANG_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(?P<spaces>\s*)#!(?P<directive>.*)").unwrap());

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum ShebangDirective<'a> {
None,
// whitespace length, start of shebang, end, shebang contents
Expand Down Expand Up @@ -72,31 +72,28 @@ mod tests {

#[test]
fn shebang_extract_match() {
assert!(matches!(
extract_shebang("not a match"),
ShebangDirective::None
));
assert!(matches!(
assert_eq!(extract_shebang("not a match"), ShebangDirective::None);
assert_eq!(
extract_shebang("#!/usr/bin/env python"),
ShebangDirective::Match(
TextSize::from(0),
TextSize::from(2),
TextSize::from(21),
"/usr/bin/env python"
)
));
assert!(matches!(
);
assert_eq!(
extract_shebang(" #!/usr/bin/env python"),
ShebangDirective::Match(
TextSize::from(2),
TextSize::from(4),
TextSize::from(23),
"/usr/bin/env python"
)
));
assert!(matches!(
);
assert_eq!(
extract_shebang("print('test') #!/usr/bin/python"),
ShebangDirective::None
));
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ruff_text_size::TextLen;
use rustpython_parser::ast::{
Constant, Excepthandler, ExcepthandlerKind, ExprKind, Located, Location, Stmt, StmtKind,
Constant, Excepthandler, ExcepthandlerKind, ExprKind, Located, Stmt, StmtKind,
};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
Expand Down Expand Up @@ -97,10 +98,10 @@ pub fn suppressible_exception(
&checker.importer,
checker.locator,
)?;
let try_ending = stmt.location.with_col_offset(3); // size of "try"
let try_ending = stmt.start() + "try".text_len();
let replace_try = Edit::replacement(
format!("with {binding}({exception})"),
stmt.location,
stmt.start(),
try_ending,
);
let handler_line_begin = checker.locator.line_start(handler.start());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ruff_text_size::TextSize;
use rustpython_parser::ast::Location;

use super::{LogicalLine, Whitespace};
Expand Down Expand Up @@ -111,10 +112,7 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine) -> Vec<(Location, Diagno
TokenKind::Lbrace | TokenKind::Lpar | TokenKind::Lsqb => {
if !matches!(line.trailing_whitespace(&token), Whitespace::None) {
let end = token.end();
diagnostics.push((
Location::new(end.row(), end.column()),
WhitespaceAfterOpenBracket.into(),
));
diagnostics.push((end, WhitespaceAfterOpenBracket.into()));
}
}
TokenKind::Rbrace
Expand All @@ -135,10 +133,8 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine) -> Vec<(Location, Diagno
{
if !matches!(last_token, Some(TokenKind::Comma)) {
let start = token.start();
diagnostics.push((
Location::new(start.row(), start.column() - offset),
diagnostic_kind,
));
diagnostics
.push((start - TextSize::try_from(offset).unwrap(), diagnostic_kind));
}
}
}
Expand Down
22 changes: 12 additions & 10 deletions crates/ruff/src/rules/pycodestyle/rules/logical_lines/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bitflags::bitflags;
use ruff_text_size::TextRange;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Location;
use rustpython_parser::lexer::LexResult;
use std::fmt::{Debug, Formatter};
Expand Down Expand Up @@ -248,8 +248,8 @@ impl<'a> LogicalLine<'a> {
Whitespace::leading(self.text_after(token))
}

/// Returns the whitespace and whitespace character-length *before* the `token`
pub fn leading_whitespace(&self, token: &LogicalLineToken<'a>) -> (Whitespace, usize) {
/// Returns the whitespace and whitespace byte-length *before* the `token`
pub fn leading_whitespace(&self, token: &LogicalLineToken<'a>) -> (Whitespace, TextSize) {
Whitespace::trailing(self.text_before(token))
}

Expand Down Expand Up @@ -515,26 +515,28 @@ impl Whitespace {
}
}

fn trailing(content: &str) -> (Self, usize) {
let mut count = 0;
fn trailing(content: &str) -> (Self, TextSize) {
let mut len = TextSize::default();
let mut count = 0usize;

for c in content.chars().rev() {
if c == '\t' {
return (Self::Tab, count + 1);
return (Self::Tab, len + c.text_len());
} else if matches!(c, '\n' | '\r') {
// Indent
return (Self::None, 0);
return (Self::None, TextSize::default());
} else if c.is_whitespace() {
count += 1;
len += c.text_len();
} else {
break;
}
}

match count {
0 => (Self::None, 0),
1 => (Self::Single, count),
_ => (Self::Many, count),
0 => (Self::None, TextSize::default()),
1 => (Self::Single, len),
_ => (Self::Many, len),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustpython_parser::ast::Location;
use ruff_text_size::TextSize;

use super::{LogicalLine, Whitespace};
use ruff_diagnostics::DiagnosticKind;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl Violation for MultipleSpacesAfterOperator {
}

/// E221, E222, E223, E224
pub(crate) fn space_around_operator(line: &LogicalLine) -> Vec<(Location, DiagnosticKind)> {
pub(crate) fn space_around_operator(line: &LogicalLine) -> Vec<(TextSize, DiagnosticKind)> {
let mut diagnostics = vec![];
let mut after_operator = false;

Expand All @@ -135,17 +135,11 @@ pub(crate) fn space_around_operator(line: &LogicalLine) -> Vec<(Location, Diagno
match line.leading_whitespace(&token) {
(Whitespace::Tab, offset) => {
let start = token.start();
diagnostics.push((
Location::new(start.row(), start.column() - offset),
TabBeforeOperator.into(),
));
diagnostics.push((start - offset, TabBeforeOperator.into()));
}
(Whitespace::Many, offset) => {
let start = token.start();
diagnostics.push((
Location::new(start.row(), start.column() - offset),
MultipleSpacesBeforeOperator.into(),
));
diagnostics.push((start - offset, MultipleSpacesBeforeOperator.into()));
}
_ => {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,11 @@ pub(crate) fn whitespace_around_keywords(line: &LogicalLine) -> Vec<(Location, D
match line.leading_whitespace(&token) {
(Whitespace::Tab, offset) => {
let start = token.start();
diagnostics.push((
Location::new(start.row(), start.column() - offset),
TabBeforeKeyword.into(),
));
diagnostics.push((start - offset, TabBeforeKeyword.into()));
}
(Whitespace::Many, offset) => {
let start = token.start();
diagnostics.push((
Location::new(start.row(), start.column() - offset),
MultipleSpacesBeforeKeyword.into(),
));
diagnostics.push((start - offset, MultipleSpacesBeforeKeyword.into()));
}
_ => {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::token_kind::TokenKind;
use ruff_text_size::TextRange;
use rustpython_parser::ast::Location;
use ruff_text_size::{TextRange, TextSize};

/// ## What it does
/// Checks if inline comments are separated by at least two spaces.
Expand Down Expand Up @@ -140,24 +139,24 @@ impl Violation for MultipleLeadingHashesForBlockComment {
pub(crate) fn whitespace_before_comment(
tokens: &LogicalLineTokens,
locator: &Locator,
is_first_row: bool,
) -> Vec<(TextRange, DiagnosticKind)> {
let mut diagnostics = vec![];
let mut prev_end = Location::new(0, 0);
let mut prev_end = TextSize::default();
for token in tokens {
let kind = token.kind();

if let TokenKind::Comment = kind {
let (start, end) = token.range();
let line = locator.slice(TextRange::new(
Location::new(start.row(), 0),
Location::new(start.row(), start.column()),
));

let line = locator.slice(TextRange::new(locator.line_start(start), start));
let text = locator.slice(TextRange::new(start, end));

let is_inline_comment = !line.trim().is_empty();
if is_inline_comment {
if prev_end.row() == start.row() && start.column() < prev_end.column() + 2 {
if start < prev_end + TextSize::from(2)
&& !locator.contains_line_break(TextRange::new(start, prev_end))
{
diagnostics.push((
TextRange::new(prev_end, start),
TooFewSpacesBeforeInlineComment.into(),
Expand All @@ -183,7 +182,7 @@ pub(crate) fn whitespace_before_comment(
.push((TextRange::new(start, end), NoSpaceAfterInlineComment.into()));
}
} else if let Some(bad_prefix) = bad_prefix {
if bad_prefix != '!' || start.row() > 1 {
if bad_prefix != '!' || !is_first_row {
if bad_prefix != '#' {
diagnostics
.push((TextRange::new(start, end), NoSpaceAfterBlockComment.into()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::token_kind::TokenKind;
use ruff_text_size::TextRange;
use ruff_text_size::{TextRange, TextSize};
use rustpython_parser::ast::Location;

use super::LogicalLineTokens;
Expand Down Expand Up @@ -57,10 +57,8 @@ pub(crate) fn whitespace_before_parameters(
&& (pre_pre_kind != Some(TokenKind::Class))
&& token.start() != prev_end
{
let start = Location::new(prev_end.row(), prev_end.column());
let end = token.end();
let end = Location::new(end.row(), end.column() - 1);

let start = prev_end;
let end = token.end() - TextSize::from(1);
let kind: WhitespaceBeforeParameters = WhitespaceBeforeParameters { bracket: kind };

let mut diagnostic = Diagnostic::new(kind, TextRange::new(start, end));
Expand Down
Loading

0 comments on commit 6ecd573

Please sign in to comment.