Skip to content

Commit

Permalink
feat: Check file names
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
epage committed Jul 19, 2019
1 parent 6da8305 commit ec307df
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 14 deletions.
6 changes: 6 additions & 0 deletions benches/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn process_empty(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -38,6 +39,7 @@ fn process_no_tokens(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -58,6 +60,7 @@ fn process_single_token(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -78,6 +81,7 @@ fn process_sherlock(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -98,6 +102,7 @@ fn process_code(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
true,
false,
typos::report::print_silent,
)
Expand All @@ -118,6 +123,7 @@ fn process_corpus(b: &mut test::Bencher) {
sample_path.path(),
&corrections,
true,
true,
false,
typos::report::print_silent,
)
Expand Down
3 changes: 1 addition & 2 deletions docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Whitelist: A confidence rating is given for how close a word is to one in the wh
| Whole-project | Yes | Yes | Yes | Yes | No |
| Ignores hidden | Yes | Yes | ? | Yes | No |
| Respect gitignore | Yes | Yes | ? | No | No |
| Checks filenames | No ([#24][def-24]) | No | ? | Yes | No |
| Checks filenames | Yes | No | ? | Yes | No |
| API | Rust / [JSON Lines] | Rust | ? | Python | None |
| License | MIT or Apache | AGPL | MIT | GPLv2 | GPLv2 |

Expand All @@ -59,5 +59,4 @@ Whitelist: A confidence rating is given for how close a word is to one in the wh
[def-14]: https://github.com/epage/typos/issues/14
[def-17]: https://github.com/epage/typos/issues/17
[def-18]: https://github.com/epage/typos/issues/18
[def-24]: https://github.com/epage/typos/issues/24
[def-3]: https://github.com/epage/typos/issues/3
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,41 @@ use bstr::ByteSlice;
pub fn process_file(
path: &std::path::Path,
dictionary: &Dictionary,
check_filenames: bool,
ignore_hex: bool,
binary: bool,
report: report::Report,
) -> Result<(), failure::Error> {
if check_filenames {
for part in path.components().filter_map(|c| c.as_os_str().to_str()) {
for ident in tokens::Identifier::parse(part) {
if !ignore_hex && is_hex(ident.token()) {
continue;
}
if let Some(correction) = dictionary.correct_ident(ident) {
let msg = report::FilenameCorrection {
path,
typo: ident.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
}
for word in ident.split() {
if let Some(correction) = dictionary.correct_word(word) {
let msg = report::FilenameCorrection {
path,
typo: word.token(),
correction,
non_exhaustive: (),
};
report(msg.into());
}
}
}
}
}

let mut buffer = Vec::new();
File::open(path)?.read_to_end(&mut buffer)?;
if !binary && buffer.find_byte(b'\0').is_some() {
Expand Down
21 changes: 21 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ struct Options {
/// Paths to check
path: Vec<std::path::PathBuf>,

#[structopt(long, raw(overrides_with = r#""check-filenames""#))]
/// Skip verifying spelling in file names.
no_check_filenames: bool,
#[structopt(
long,
raw(overrides_with = r#""no-check-filenames""#),
raw(hidden = "true")
)]
check_filenames: bool,

#[structopt(long, raw(overrides_with = r#""hex""#))]
/// Don't try to detect that an identifier looks like hex
no_hex: bool,
Expand Down Expand Up @@ -115,6 +125,15 @@ impl Options {
self
}

pub fn check_filenames(&self) -> Option<bool> {
match (self.check_filenames, self.no_check_filenames) {
(true, false) => Some(true),
(false, true) => Some(false),
(false, false) => None,
(_, _) => unreachable!("StructOpt should make this impossible"),
}
}

pub fn ignore_hex(&self) -> Option<bool> {
match (self.no_hex, self.hex) {
(true, false) => Some(false),
Expand Down Expand Up @@ -197,6 +216,7 @@ fn run() -> Result<(), failure::Error> {
let options = Options::from_args().infer();

let dictionary = typos::Dictionary::new();
let check_filenames = options.check_filenames().unwrap_or(true);
let ignore_hex = options.ignore_hex().unwrap_or(true);
let binary = options.binary().unwrap_or(false);

Expand All @@ -222,6 +242,7 @@ fn run() -> Result<(), failure::Error> {
typos::process_file(
entry.path(),
&dictionary,
check_filenames,
ignore_hex,
binary,
options.format.report(),
Expand Down
27 changes: 27 additions & 0 deletions src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::io::{self, Write};
pub enum Message<'m> {
BinaryFile(BinaryFile<'m>),
Correction(Correction<'m>),
FilenameCorrection(FilenameCorrection<'m>),
}

impl<'m> From<BinaryFile<'m>> for Message<'m> {
Expand All @@ -21,6 +22,12 @@ impl<'m> From<Correction<'m>> for Message<'m> {
}
}

impl<'m> From<FilenameCorrection<'m>> for Message<'m> {
fn from(msg: FilenameCorrection<'m>) -> Self {
Message::FilenameCorrection(msg)
}
}

#[derive(Clone, Debug, Serialize)]
pub struct BinaryFile<'m> {
pub path: &'m std::path::Path,
Expand All @@ -41,6 +48,15 @@ pub struct Correction<'m> {
pub(crate) non_exhaustive: (),
}

#[derive(Clone, Debug, Serialize)]
pub struct FilenameCorrection<'m> {
pub path: &'m std::path::Path,
pub typo: &'m str,
pub correction: Cow<'m, str>,
#[serde(skip)]
pub(crate) non_exhaustive: (),
}

pub type Report = fn(msg: Message);

pub fn print_silent(_: Message) {}
Expand All @@ -60,6 +76,9 @@ pub fn print_brief(msg: Message) {
msg.correction
);
}
Message::FilenameCorrection(msg) => {
println!("{}: {} -> {}", msg.path.display(), msg.typo, msg.correction);
}
}
}

Expand All @@ -69,6 +88,14 @@ pub fn print_long(msg: Message) {
println!("Skipping binary file {}", msg.path.display(),);
}
Message::Correction(msg) => print_long_correction(msg),
Message::FilenameCorrection(msg) => {
println!(
"{}: error: `{}` should be `{}`",
msg.path.display(),
msg.typo,
msg.correction
);
}
}
}

Expand Down
47 changes: 35 additions & 12 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ impl<'t> Identifier<'t> {
Self { token, offset }
}

pub fn parse(content: &str) -> impl Iterator<Item = Identifier<'_>> {
lazy_static::lazy_static! {
// Getting false positives for this lint
#[allow(clippy::invalid_regex)]
static ref SPLIT: regex::Regex = regex::Regex::new(r#"\b(\p{Alphabetic}|\d|_|')+\b"#).unwrap();
}
SPLIT
.find_iter(content)
.map(|m| Identifier::new_unchecked(m.as_str(), m.start()))
}

pub fn parse_bytes(content: &[u8]) -> impl Iterator<Item = Identifier<'_>> {
lazy_static::lazy_static! {
// Getting false positives for this lint
Expand Down Expand Up @@ -240,58 +251,70 @@ mod test {

#[test]
fn tokenize_empty_is_empty() {
let input = b"";
let input = "";
let expected: Vec<Identifier> = vec![];
let actual: Vec<_> = Identifier::parse_bytes(input).collect();
let actual: Vec<_> = Identifier::parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = Identifier::parse(input).collect();
assert_eq!(expected, actual);
}

#[test]
fn tokenize_word_is_word() {
let input = b"word";
let input = "word";
let expected: Vec<Identifier> = vec![Identifier::new_unchecked("word", 0)];
let actual: Vec<_> = Identifier::parse_bytes(input).collect();
let actual: Vec<_> = Identifier::parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = Identifier::parse(input).collect();
assert_eq!(expected, actual);
}

#[test]
fn tokenize_space_separated_words() {
let input = b"A B";
let input = "A B";
let expected: Vec<Identifier> = vec![
Identifier::new_unchecked("A", 0),
Identifier::new_unchecked("B", 2),
];
let actual: Vec<_> = Identifier::parse_bytes(input).collect();
let actual: Vec<_> = Identifier::parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = Identifier::parse(input).collect();
assert_eq!(expected, actual);
}

#[test]
fn tokenize_dot_separated_words() {
let input = b"A.B";
let input = "A.B";
let expected: Vec<Identifier> = vec![
Identifier::new_unchecked("A", 0),
Identifier::new_unchecked("B", 2),
];
let actual: Vec<_> = Identifier::parse_bytes(input).collect();
let actual: Vec<_> = Identifier::parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = Identifier::parse(input).collect();
assert_eq!(expected, actual);
}

#[test]
fn tokenize_namespace_separated_words() {
let input = b"A::B";
let input = "A::B";
let expected: Vec<Identifier> = vec![
Identifier::new_unchecked("A", 0),
Identifier::new_unchecked("B", 3),
];
let actual: Vec<_> = Identifier::parse_bytes(input).collect();
let actual: Vec<_> = Identifier::parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = Identifier::parse(input).collect();
assert_eq!(expected, actual);
}

#[test]
fn tokenize_underscore_doesnt_separate() {
let input = b"A_B";
let input = "A_B";
let expected: Vec<Identifier> = vec![Identifier::new_unchecked("A_B", 0)];
let actual: Vec<_> = Identifier::parse_bytes(input).collect();
let actual: Vec<_> = Identifier::parse_bytes(input.as_bytes()).collect();
assert_eq!(expected, actual);
let actual: Vec<_> = Identifier::parse(input).collect();
assert_eq!(expected, actual);
}

Expand Down

0 comments on commit ec307df

Please sign in to comment.