use super::ucd;
use crate::lint::{Group, Lint};
use crate::scan::{Hit, located};
pub(in crate::lint) const UNICODE_SPACE: Lint =
Lint::new("unicode-space", Group::Pedantic);
pub(crate) const CRLF: Lint = Lint::new("crlf", Group::Pedantic);
pub(in crate::lint) const TRAILING_WHITESPACE: Lint =
Lint::new("trailing-whitespace", Group::Pedantic);
pub(in crate::lint) const FINAL_NEWLINE: Lint =
Lint::new("final-newline", Group::Pedantic);
pub(in crate::lint) const NOT_NFC: Lint = Lint::new("not-nfc", Group::Pedantic);
pub(in crate::lint) const NFKC_COMPAT: Lint =
Lint::new("nfkc-compat", Group::Pedantic);
pub(in crate::lint) const MIXED_SCRIPT: Lint =
Lint::new("mixed-script", Group::Pedantic);
pub(in crate::lint) const CONFUSABLE: Lint =
Lint::new("confusable", Group::Pedantic);
pub(crate) const TEXT_LINTS: [Lint; 2] = [NOT_NFC, MIXED_SCRIPT];
pub(crate) const CHAR_LINTS: [Lint; 3] =
[UNICODE_SPACE, NFKC_COMPAT, CONFUSABLE];
pub(crate) const LINE_LINTS: [Lint; 3] =
[CRLF, TRAILING_WHITESPACE, FINAL_NEWLINE];
pub(in crate::lint) const CLAIM_ORDER: [Lint; 8] = [
UNICODE_SPACE,
CRLF,
TRAILING_WHITESPACE,
FINAL_NEWLINE,
NFKC_COMPAT,
CONFUSABLE,
NOT_NFC,
MIXED_SCRIPT,
];
const SPACES: [char; 16] = [
'\u{00A0}', '\u{1680}', '\u{2000}', '\u{2001}', '\u{2002}', '\u{2003}',
'\u{2004}', '\u{2005}', '\u{2006}', '\u{2007}', '\u{2008}', '\u{2009}',
'\u{200A}', '\u{202F}', '\u{205F}', '\u{3000}',
];
pub(crate) fn unicode_space(character: char) -> Option<Lint> {
SPACES.contains(&character).then_some(UNICODE_SPACE)
}
pub(crate) fn char_lints(character: char) -> [Option<Lint>; 3] {
if character.is_ascii() {
return [None; 3];
}
if let Some(space) = unicode_space(character) {
return [Some(space), None, None];
}
let compat = ucd::compat(character).then_some(NFKC_COMPAT);
let lookalike = ucd::lookalike(character).then_some(CONFUSABLE);
[None, compat, lookalike]
}
pub(crate) fn text_hits(text: &str) -> Vec<(Lint, Hit)> {
if text.is_ascii() {
return Vec::new();
}
let nfc = ucd::denormal_in(text).into_iter().map(|hit| (NOT_NFC, hit));
let words = ucd::mixed_in(located(text));
let words = words.into_iter().map(|hit| (MIXED_SCRIPT, hit));
let mut found: Vec<(Lint, Hit)> = nfc.chain(words).collect();
found.sort_by_key(|(_, hit)| hit.position.byte);
found
}
pub(crate) fn line_hits(text: &str) -> Vec<(Lint, Hit)> {
let mut walk = Walk::default();
for hit in located(text) {
walk.step(hit);
}
walk.finish()
}
#[derive(Default)]
struct Walk {
run: Option<Hit>,
last: Option<Hit>,
found: Vec<(Lint, Hit)>,
}
impl Walk {
fn step(&mut self, hit: Hit) {
if hit.character == '\n' {
self.end_line();
} else if hit.character.is_whitespace() {
self.run = self.run.or(Some(hit));
} else {
self.run = None;
}
self.last = Some(hit);
}
fn end_line(&mut self) {
let cr = self.last.filter(|hit| hit.character == '\r');
let run = self.run.take();
let trailing = run.filter(|start| cr != Some(*start));
if let Some(start) = trailing {
self.found.push((TRAILING_WHITESPACE, start));
}
if let Some(cr) = cr {
self.found.push((CRLF, cr));
}
}
fn finish(mut self) -> Vec<(Lint, Hit)> {
if let Some(last) = self.last.filter(|hit| hit.character != '\n') {
if let Some(start) = self.run.take() {
self.found.push((TRAILING_WHITESPACE, start));
}
self.found.push((FINAL_NEWLINE, last));
}
self.found
}
}
#[cfg(test)]
mod tests {
use super::{
CHAR_LINTS, CONFUSABLE, CRLF, FINAL_NEWLINE, LINE_LINTS, MIXED_SCRIPT,
NFKC_COMPAT, NOT_NFC, SPACES, TEXT_LINTS, TRAILING_WHITESPACE,
UNICODE_SPACE, char_lints, line_hits, text_hits, unicode_space,
};
use crate::lint::{Group, LINTS, Lint};
#[test]
fn every_pedantic_lint_has_one_detector() {
let pedantic = LINTS.iter().filter(|l| l.group == Group::Pedantic);
for lint in pedantic {
let homes = [&CHAR_LINTS[..], &TEXT_LINTS, &LINE_LINTS]
.iter()
.filter(|walk| walk.contains(lint))
.count();
assert_eq!(homes, 1, "{}", lint.name);
}
}
#[test]
fn char_lints_claim_in_order() {
assert_eq!(char_lints('a'), [None; 3]);
let space = [Some(UNICODE_SPACE), None, None];
assert_eq!(char_lints('\u{a0}'), space);
let both = [None, Some(NFKC_COMPAT), Some(CONFUSABLE)];
assert_eq!(char_lints('\u{ff21}'), both);
assert_eq!(char_lints('\u{b2}'), [None, Some(NFKC_COMPAT), None]);
}
#[test]
fn text_hits_are_in_byte_order() {
let found: Vec<(&str, usize)> = text_hits("\u{3bb}e\u{301} x\u{212b}")
.into_iter()
.map(|(lint, hit)| (lint.name, hit.position.byte))
.collect();
let expected =
vec![(NOT_NFC.name, 2), (MIXED_SCRIPT.name, 2), (NOT_NFC.name, 7)];
assert_eq!(found, expected);
}
type Fired = (&'static str, usize, usize, char);
fn fired(text: &str) -> Vec<Fired> {
line_hits(text)
.into_iter()
.map(|(lint, hit)| {
let at = hit.position;
(lint.name, at.line, at.column, hit.character)
})
.collect()
}
#[test]
fn the_lints_here_are_the_registered_ones() {
for lint in [UNICODE_SPACE, CRLF, TRAILING_WHITESPACE, FINAL_NEWLINE] {
assert_eq!(Lint::named(lint.name), Some(lint), "{}", lint.name);
assert_eq!(lint.group, Group::Pedantic, "{}", lint.name);
}
assert!(!LINE_LINTS.contains(&UNICODE_SPACE));
}
#[test]
fn a_clean_text_fires_nothing() {
assert_eq!(fired("one\ntwo\n"), vec![]);
assert_eq!(fired("\n\n"), vec![]);
}
#[test]
fn an_empty_file_is_not_missing_a_newline() {
assert_eq!(fired(""), vec![]);
}
#[test]
fn crlf_points_at_each_carriage_return() {
let expected = vec![("crlf", 1, 2, '\r'), ("crlf", 2, 3, '\r')];
assert_eq!(fired("a\r\nbc\r\n"), expected);
}
#[test]
fn a_lone_carriage_return_is_not_crlf() {
assert_eq!(fired("a\rb\n"), vec![]);
}
#[test]
fn trailing_whitespace_points_at_the_start_of_the_run_once() {
let expected = vec![("trailing-whitespace", 1, 4, ' ')];
assert_eq!(fired("abc \t \ndef\n"), expected);
}
#[test]
fn a_whitespace_only_line_trails_from_its_first_column() {
assert_eq!(fired("a\n \n"), vec![("trailing-whitespace", 2, 1, ' ')]);
}
#[test]
fn whitespace_inside_a_line_is_not_trailing() {
assert_eq!(fired("a b\n"), vec![]);
}
#[test]
fn a_crlf_line_trails_only_when_whitespace_precedes_the_cr() {
assert_eq!(fired("abc\r\n"), vec![("crlf", 1, 4, '\r')]);
let both =
vec![("trailing-whitespace", 1, 4, ' '), ("crlf", 1, 5, '\r')];
assert_eq!(fired("abc \r\n"), both);
}
#[test]
fn final_newline_points_at_the_last_character() {
assert_eq!(fired("a\nbc"), vec![("final-newline", 2, 2, 'c')]);
}
#[test]
fn an_unterminated_last_line_can_trail_too() {
let expected = vec![
("trailing-whitespace", 1, 2, ' '),
("final-newline", 1, 3, ' '),
];
assert_eq!(fired("a "), expected);
}
#[test]
fn a_trailing_no_break_space_is_trailing_whitespace() {
let expected = vec![("trailing-whitespace", 1, 2, '\u{a0}')];
assert_eq!(fired("a\u{a0}\n"), expected);
}
#[test]
fn unicode_space_fires_on_the_listed_spaces_and_nothing_else() {
for point in ['\u{00A0}', '\u{2000}', '\u{200A}', '\u{202F}'] {
assert_eq!(unicode_space(point), Some(UNICODE_SPACE), "{point:?}");
}
for point in [' ', '\t', '\u{200B}', '\u{2028}', 'a'] {
assert_eq!(unicode_space(point), None, "{point:?}");
}
}
#[test]
fn the_list_is_every_non_ascii_space_separator() {
let not_zs = ['\u{0085}', '\u{2028}', '\u{2029}'];
let whitespace: Vec<char> = (char::from(0x80_u8)..=char::MAX)
.filter(|c| c.is_whitespace() && !not_zs.contains(c))
.collect();
assert_eq!(whitespace, SPACES.to_vec());
}
}