-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will now lint incorrect quote characters
- Loading branch information
1 parent
db0d1a9
commit 6669a8b
Showing
4 changed files
with
43 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::{ | ||
document::Document, parsing::Quote, Lint, LintKind, Punctuation, Suggestion, Token, TokenKind, | ||
}; | ||
|
||
pub fn wrong_quotes(document: &Document) -> Vec<Lint> { | ||
document | ||
.iter_quote_indices() | ||
.zip(document.iter_quotes()) | ||
.filter_map(|(quote_idx, quote_token)| lint_quote(document, quote_idx, quote_token)) | ||
.collect() | ||
} | ||
|
||
fn lint_quote(document: &Document, quote_idx: usize, quote_token: Token) -> Option<Lint> { | ||
let quote = quote_token.kind.as_quote().unwrap(); | ||
|
||
let twin_loc = quote.twin_loc?; | ||
let is_left = twin_loc > quote_idx; | ||
|
||
let quote_char = *document.get_span_content(quote_token.span).first()?; | ||
|
||
let should_be = if is_left { '“' } else { '”' }; | ||
|
||
if quote_char != should_be { | ||
Some(Lint { | ||
span: quote_token.span, | ||
lint_kind: LintKind::WrongQuotes, | ||
suggestions: vec![Suggestion::ReplaceWith(vec![should_be])], | ||
message: "Use the better-formatted quote character.".to_string(), | ||
}) | ||
} else { | ||
None | ||
} | ||
} |