Replace row/column based Location with byte-offsets. (#3931)

This commit is contained in:
Micha Reiser 2023-04-26 20:11:02 +02:00 committed by GitHub
parent ee91598835
commit cab65b25da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
418 changed files with 6203 additions and 7040 deletions

View file

@ -1,6 +1,5 @@
use std::path::Path;
use rustpython_parser::ast::Location;
use rustpython_parser::lexer::LexResult;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
@ -19,7 +18,7 @@ use ruff::settings::configuration::Configuration;
use ruff::settings::options::Options;
use ruff::settings::{defaults, flags, Settings};
use ruff_diagnostics::Edit;
use ruff_python_ast::source_code::{Indexer, Locator, Stylist};
use ruff_python_ast::source_code::{Indexer, Locator, SourceLocation, Stylist};
const VERSION: &str = env!("CARGO_PKG_VERSION");
@ -63,8 +62,8 @@ pub struct ExpandedFix {
pub struct ExpandedMessage {
pub code: String,
pub message: String,
pub location: Location,
pub end_location: Location,
pub location: SourceLocation,
pub end_location: SourceLocation,
pub fix: Option<ExpandedFix>,
}
@ -180,10 +179,11 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
let stylist = Stylist::from_tokens(&tokens, &locator);
// Extra indices from the code.
let indexer: Indexer = tokens.as_slice().into();
let indexer = Indexer::from_tokens(&tokens, &locator);
// Extract the `# noqa` and `# isort: skip` directives from the source.
let directives = directives::extract_directives(&tokens, directives::Flags::empty());
let directives =
directives::extract_directives(&tokens, directives::Flags::empty(), &locator, &indexer);
// Generate checks.
let LinterResult {
@ -192,7 +192,6 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
} = check_path(
Path::new("<filename>"),
None,
contents,
tokens,
&locator,
&stylist,
@ -203,21 +202,28 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
flags::Autofix::Enabled,
);
let source_code = locator.to_source_code();
let messages: Vec<ExpandedMessage> = diagnostics
.into_iter()
.map(|message| ExpandedMessage {
code: message.kind.rule().noqa_code().to_string(),
message: message.kind.body,
location: message.location,
end_location: message.end_location,
fix: if message.fix.is_empty() {
None
} else {
Some(ExpandedFix {
message: message.kind.suggestion,
edits: message.fix.into_edits(),
})
},
.map(|message| {
let start_location = source_code.source_location(message.start());
let end_location = source_code.source_location(message.end());
ExpandedMessage {
code: message.kind.rule().noqa_code().to_string(),
message: message.kind.body,
location: start_location,
end_location,
fix: if message.fix.is_empty() {
None
} else {
Some(ExpandedFix {
message: message.kind.suggestion,
edits: message.fix.into_edits(),
})
},
}
})
.collect();

View file

@ -1,10 +1,11 @@
#![cfg(target_arch = "wasm32")]
use js_sys;
use rustpython_parser::ast::Location;
use wasm_bindgen_test::*;
use ruff::registry::Rule;
use ruff_python_ast::source_code::{OneIndexed, SourceLocation};
use ruff_wasm::*;
macro_rules! check {
@ -28,8 +29,14 @@ fn empty_config() {
[ExpandedMessage {
code: Rule::IfTuple.noqa_code().to_string(),
message: "If test is a tuple, which is always `True`".to_string(),
location: Location::new(1, 0),
end_location: Location::new(2, 8),
location: SourceLocation {
row: OneIndexed::from_zero_indexed(0),
column: OneIndexed::from_zero_indexed(0)
},
end_location: SourceLocation {
row: OneIndexed::from_zero_indexed(1),
column: OneIndexed::from_zero_indexed(8)
},
fix: None,
}]
);