Skip to content

Commit

Permalink
Merge pull request #886 from epage/olt
Browse files Browse the repository at this point in the history
fix(config): Apply extend-ignore-re to file names
  • Loading branch information
epage authored Dec 27, 2023
2 parents 5bd389d + bf66cbd commit e32ec88
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
35 changes: 30 additions & 5 deletions crates/typos-cli/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FileChecker for Typos {
) -> Result<(), std::io::Error> {
if policy.check_filenames {
if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
for typo in typos::check_str(file_name, policy.tokenizer, policy.dict) {
for typo in check_str(file_name, policy) {
let msg = report::Typo {
context: Some(report::PathContext { path }.into()),
buffer: std::borrow::Cow::Borrowed(file_name.as_bytes()),
Expand Down Expand Up @@ -112,7 +112,7 @@ impl FileChecker for FixTypos {
if policy.check_filenames {
if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
let mut fixes = Vec::new();
for typo in typos::check_str(file_name, policy.tokenizer, policy.dict) {
for typo in check_str(file_name, policy) {
if is_fixable(&typo) {
fixes.push(typo.into_owned());
} else {
Expand Down Expand Up @@ -190,7 +190,7 @@ impl FileChecker for DiffTypos {
if policy.check_filenames {
if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
let mut fixes = Vec::new();
for typo in typos::check_str(file_name, policy.tokenizer, policy.dict) {
for typo in check_str(file_name, policy) {
if is_fixable(&typo) {
fixes.push(typo.into_owned());
} else {
Expand Down Expand Up @@ -256,9 +256,16 @@ impl FileChecker for Identifiers {
policy: &crate::policy::Policy,
reporter: &dyn report::Report,
) -> Result<(), std::io::Error> {
let mut ignores: Option<Ignores> = None;
if policy.check_filenames {
if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
for word in policy.tokenizer.parse_str(file_name) {
if ignores
.get_or_insert_with(|| Ignores::new(file_name.as_bytes(), policy.ignore))
.is_ignored(word.span())
{
continue;
}
let msg = report::Parse {
context: Some(report::PathContext { path }.into()),
kind: report::ParseKind::Identifier,
Expand All @@ -275,7 +282,6 @@ impl FileChecker for Identifiers {
let msg = report::BinaryFile { path };
reporter.report(msg.into())?;
} else {
let mut ignores: Option<Ignores> = None;
for word in policy.tokenizer.parse_bytes(&buffer) {
if ignores
.get_or_insert_with(|| Ignores::new(&buffer, policy.ignore))
Expand Down Expand Up @@ -312,13 +318,20 @@ impl FileChecker for Words {
policy: &crate::policy::Policy,
reporter: &dyn report::Report,
) -> Result<(), std::io::Error> {
let mut ignores: Option<Ignores> = None;
if policy.check_filenames {
if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
for word in policy
.tokenizer
.parse_str(file_name)
.flat_map(|i| i.split())
{
if ignores
.get_or_insert_with(|| Ignores::new(file_name.as_bytes(), policy.ignore))
.is_ignored(word.span())
{
continue;
}
let msg = report::Parse {
context: Some(report::PathContext { path }.into()),
kind: report::ParseKind::Word,
Expand All @@ -335,7 +348,6 @@ impl FileChecker for Words {
let msg = report::BinaryFile { path };
reporter.report(msg.into())?;
} else {
let mut ignores: Option<Ignores> = None;
for word in policy
.tokenizer
.parse_bytes(&buffer)
Expand Down Expand Up @@ -536,6 +548,19 @@ fn write_file(
Ok(())
}

fn check_str<'a>(
buffer: &'a str,
policy: &'a crate::policy::Policy<'a, 'a, 'a>,
) -> impl Iterator<Item = typos::Typo<'a>> {
let mut ignores: Option<Ignores> = None;

typos::check_str(buffer, policy.tokenizer, policy.dict).filter(move |typo| {
!ignores
.get_or_insert_with(|| Ignores::new(buffer.as_bytes(), policy.ignore))
.is_ignored(typo.span())
})
}

fn check_bytes<'a>(
buffer: &'a [u8],
policy: &'a crate::policy::Policy<'a, 'a, 'a>,
Expand Down
2 changes: 1 addition & 1 deletion crates/typos-cli/tests/cmd/extend-ignore-re.in/_typos.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[default]
extend-ignore-re = ["`.*`"]
extend-ignore-re = ["`.*`", "olt-manager"]

[default.extend-identifiers]
hello = "goodbye"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
One olt two
One manager two
One olt-manager two
7 changes: 7 additions & 0 deletions crates/typos-cli/tests/cmd/extend-ignore-re.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin.name = "typos"
args = "-j1"
stdin = ""
stdout = """
error: `hello` should be `goodbye`
Expand All @@ -7,6 +8,12 @@ error: `hello` should be `goodbye`
1 | hello `hello`
| ^^^^^
|
error: `olt` should be `old`
--> ./olt-manager.php:1:5
|
1 | One olt two
| ^^^
|
"""
stderr = ""
status.code = 2

0 comments on commit e32ec88

Please sign in to comment.