fix: Use UTF-32 char count for line length (#223) (#224)

This commit is contained in:
Suguru Yamamoto 2022-09-19 01:45:41 +09:00 committed by GitHub
parent cf6a23b83c
commit 0d0c8730fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View file

@ -4,3 +4,6 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
""" """
_ = "---------------------------------------------------------------------------AAAAAAA"
_ = "---------------------------------------------------------------------------亜亜亜亜亜亜亜"

View file

@ -35,7 +35,7 @@ pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings)
// Enforce line length. // Enforce line length.
if enforce_line_too_long { if enforce_line_too_long {
let line_length = line.len(); let line_length = line.chars().count();
if should_enforce_line_length(line, line_length, settings.line_length) { if should_enforce_line_length(line, line_length, settings.line_length) {
let check = Check::new( let check = Check::new(
CheckKind::LineTooLong(line_length, settings.line_length), CheckKind::LineTooLong(line_length, settings.line_length),
@ -53,3 +53,28 @@ pub fn check_lines(checks: &mut Vec<Check>, contents: &str, settings: &Settings)
} }
checks.extend(line_checks); checks.extend(line_checks);
} }
#[cfg(test)]
mod tests {
use super::check_lines;
use super::*;
use std::collections::BTreeSet;
#[test]
fn e501_non_ascii_char() {
let line = "'\u{4e9c}' * 2"; // 7 in UTF-32, 9 in UTF-8.
let check_with_max_line_length = |line_length: usize| {
let mut checks: Vec<Check> = vec![];
let settings = Settings {
line_length,
exclude: vec![],
extend_exclude: vec![],
select: BTreeSet::from_iter(vec![CheckCode::E501]),
};
check_lines(&mut checks, line, &settings);
return checks;
};
assert!(!check_with_max_line_length(6).is_empty());
assert!(check_with_max_line_length(7).is_empty());
}
}