Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix: issues with C++ and TypeScript multiline comments

Elijah Potter9bde7339a773
diff --git a/Cargo.lock b/Cargo.lock
index 2c0ff9275..105af732e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -614,6 +614,7 @@ name = "harper-tree-sitter"
version = "0.8.0"
dependencies = [
"harper-core",
+ "paste",
"tree-sitter",
"tree-sitter-c",
"tree-sitter-c-sharp",
@@ -1186,9 +1187,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
-version = "1.0.202"
+version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"
+checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
dependencies = [
"serde_derive",
]
@@ -1206,9 +1207,9 @@ dependencies = [
[[package]]
name = "serde_derive"
-version = "1.0.202"
+version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"
+checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [
"proc-macro2",
"quote",
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index ddf58b99d..6fd172fc2 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -7,90 +7,87 @@ use harper_core::{remove_overlaps, Document, FullDictionary, LintGroup, LintGrou
use harper_tree_sitter::TreeSitterParser;
#[derive(Debug, Parser)]
-struct Args {
- /// The file you wish to grammar check.
- file: PathBuf,
- /// Whether to merely print out the number of errors encountered, without
- /// further details.
- #[arg(short, long)]
- count: bool
+enum Args {
+ /// Lint a provided document.
+ Lint {
+ /// The file you wish to grammar check.
+ file: PathBuf,
+ /// Whether to merely print out the number of errors encountered,
+ /// without further details.
+ #[arg(short, long)]
+ count: bool
+ },
+ /// Parse a provided document and print the detected symbols.
+ Parse {
+ /// The file you wish to parse.
+ file: PathBuf
+ }
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
- let source = std::fs::read_to_string(&args.file)?;
+ match args {
+ Args::Lint { file, count } => {
+ let (doc, source) = load_file(&file)?;
- let parser = TreeSitterParser::new_from_language_id(
- filename_to_filetype(&args.file).ok_or(format_err!("Could not detect filetype."))?
- )
- .ok_or(format_err!("Could not detect language ID."))?;
+ let mut linter = LintGroup::new(
+ LintGroupConfig::default(),
+ FullDictionary::create_from_curated()
+ );
+ let mut lints = linter.lint(&doc);
- let doc = Document::new(&source, Box::new(parser));
+ if count {
+ println!("{}", lints.len());
+ return Ok(());
+ }
- let mut linter = LintGroup::new(
- LintGroupConfig::default(),
- FullDictionary::create_from_curated()
- );
- let mut lints = linter.lint(&doc);
+ if lints.is_empty() {
+ println!("No lints found");
+ return Ok(());
+ }
- if args.count {
- println!("{}", lints.len());
- return Ok(());
- }
+ remove_overlaps(&mut lints);
- if lints.is_empty() {
- println!("No lints found");
- return Ok(());
- }
+ let primary_color = Color::Magenta;
- remove_overlaps(&mut lints);
+ let filename = file
+ .file_name()
+ .map(|s| s.to_string_lossy().into())
+ .unwrap_or("<file>".to_string());
- let primary_color = Color::Magenta;
+ let mut report_builder = Report::build(ReportKind::Advice, &filename, 0);
- let filename = args
- .file
- .file_name()
- .map(|s| s.to_string_lossy().into())
- .unwrap_or("<file>".to_string());
+ for lint in lints {
+ report_builder = report_builder.with_label(
+ Label::new((&filename, lint.span.into()))
+ .with_message(lint.message)
+ .with_color(primary_color)
+ );
+ }
- let mut report_builder = Report::build(ReportKind::Advice, &filename, 0);
+ let report = report_builder.finish();
+ report.print((&filename, Source::from(source)))?;
- for lint in lints {
- report_builder = report_builder.with_label(
- Label::new((&filename, lint.span.into()))
- .with_message(lint.message)
- .with_color(primary_color)
- );
- }
+ std::process::exit(1);
+ }
+ Args::Parse { file } => {
+ let (doc, _) = load_file(&file)?;
- let report = report_builder.finish();
- report.print((&filename, Source::from(source)))?;
+ for token in doc.tokens() {
+ dbg!(token);
+ }
- std::process::exit(1);
+ Ok(())
+ }
+ }
}
-/// Convert a provided path to a corresponding Language Server Protocol file
-/// type.
-///
-/// Note to contributors: try to keep this in sync with
-/// [`TreeSitterParser::new_from_language_id`]
-fn filename_to_filetype(path: &Path) -> Option<&'static str> {
- Some(match path.extension()?.to_str()? {
- "rs" => "rust",
- "ts" => "typescript",
- "tsx" => "typescriptreact",
- "js" => "javascript",
- "jsx" => "javascriptreact",
- "go" => "go",
- "c" => "c",
- "cpp" => "cpp",
- "h" => "cpp",
- "rb" => "ruby",
- "swift" => "swift",
- "cs" => "csharp",
- "toml" => "toml",
- "lua" => "lua",
- _ => return None
- })
+fn load_file(file: &Path) -> anyhow::Result<(Document, String)> {
+ let source = std::fs::read_to_string(file)?;
+
+ let parser = TreeSitterParser::new_from_filename(file)
+ .ok_or(format_err!("Could not detect language ID."))?;
+
+ Ok((Document::new(&source, Box::new(parser)), source))
}
diff --git a/harper-tree-sitter/Cargo.toml b/harper-tree-sitter/Cargo.toml
index e8fb59583..3406eb547 100644
--- a/harper-tree-sitter/Cargo.toml
+++ b/harper-tree-sitter/Cargo.toml
@@ -18,3 +18,7 @@ tree-sitter-swift = "=0.4.0"
tree-sitter-c-sharp = "0.20.0"
tree-sitter-toml = "0.20.0"
tree-sitter-lua = "0.0.19"
+
+[dev-dependencies]
+paste = "1.0.15"
+
diff --git a/harper-tree-sitter/src/tree_sitter_parser.rs b/harper-tree-sitter/src/tree_sitter_parser.rs
index 47cfb39b8..d8115b14d 100644
--- a/harper-tree-sitter/src/tree_sitter_parser.rs
+++ b/harper-tree-sitter/src/tree_sitter_parser.rs
@@ -1,12 +1,13 @@
use std::collections::HashSet;
+use std::path::Path;
use harper_core::parsers::Parser;
-use harper_core::{FullDictionary, Span, Token};
+use harper_core::{FullDictionary, Span, Token, TokenKind};
use tree_sitter::{Language, Node, Tree, TreeCursor};
use super::comment_parsers::{Go, Unit};
-/// A Harper parser that wraps various [`super::comment_parsers`] that
+/// A Harper parser that wraps various comment parsers that
/// exclusively parses comments in any language supported by [`tree_sitter`].
pub struct TreeSitterParser {
language: Language,
@@ -44,6 +45,36 @@ impl TreeSitterParser {
})
}
+ /// Infer the programming language from a provided filename.
+ pub fn new_from_filename(filename: &Path) -> Option<Self> {
+ Self::new_from_language_id(Self::filename_to_filetype(filename)?)
+ }
+
+ /// Convert a provided path to a corresponding Language Server Protocol file
+ /// type.
+ ///
+ /// Note to contributors: try to keep this in sync with
+ /// [`Self::new_from_language_id`]
+ fn filename_to_filetype(path: &Path) -> Option<&'static str> {
+ Some(match path.extension()?.to_str()? {
+ "rs" => "rust",
+ "ts" => "typescript",
+ "tsx" => "typescriptreact",
+ "js" => "javascript",
+ "jsx" => "javascriptreact",
+ "go" => "go",
+ "c" => "c",
+ "cpp" => "cpp",
+ "h" => "cpp",
+ "rb" => "ruby",
+ "swift" => "swift",
+ "cs" => "csharp",
+ "toml" => "toml",
+ "lua" => "lua",
+ _ => return None
+ })
+ }
+
fn parse_root(&self, text: &str) -> Option<Tree> {
let mut parser = tree_sitter::Parser::new();
parser.set_language(self.language).unwrap();
@@ -127,6 +158,11 @@ impl Parser for TreeSitterParser {
byte_spans_to_char_spans(&mut comments_spans, &text);
+ dbg!(comments_spans
+ .iter()
+ .map(|span| span.get_content_string(text.chars().collect::<Vec<_>>().as_slice()))
+ .collect::<Vec<_>>());
+
let mut tokens = Vec::new();
for (s_index, span) in comments_spans.iter().enumerate() {
@@ -144,6 +180,13 @@ impl Parser for TreeSitterParser {
}
}
+ // Same goes for newlines within the comment
+ for t in new_tokens.iter_mut() {
+ if let TokenKind::Newline(n) = &mut t.kind {
+ *n = 1;
+ }
+ }
+
tokens.append(&mut new_tokens);
}
diff --git a/harper-tree-sitter/tests/language_support.rs b/harper-tree-sitter/tests/language_support.rs
new file mode 100644
index 000000000..d730f1e75
--- /dev/null
+++ b/harper-tree-sitter/tests/language_support.rs
@@ -0,0 +1,38 @@
+use std::path::Path;
+
+use harper_core::{Document, FullDictionary, LintGroup, LintGroupConfig, Linter};
+use harper_tree_sitter::TreeSitterParser;
+
+/// Creates a unit test checking that the linting of a source file in
+/// `language_support_sources` produces the expected number of lints.
+macro_rules! create_test {
+ ($filename:ident.$ext:ident, $correct_expected:expr) => {
+ paste::paste! {
+ #[test]
+ fn [<lints_$ext _ $filename _correctly>](){
+ let filename = concat!(stringify!($filename), ".", stringify!($ext));
+ let source = include_str!(
+ concat!(
+ "./language_support_sources/",
+ concat!(
+ stringify!($filename), ".", stringify!($ext))
+ )
+ );
+
+ let parser = TreeSitterParser::new_from_filename(Path::new(filename)).unwrap();
+ let document = Document::new(&source, Box::new(parser));
+
+ let mut linter = LintGroup::new(
+ LintGroupConfig::default(),
+ FullDictionary::create_from_curated()
+ );
+ let lints = linter.lint(&document);
+
+ assert_eq!(lints.len(), $correct_expected);
+ }
+ }
+ };
+}
+
+create_test!(multiline_comments.cpp, 3);
+create_test!(multiline_comments.ts, 3);
diff --git a/harper-tree-sitter/tests/language_support_sources/multiline_comments.cpp b/harper-tree-sitter/tests/language_support_sources/multiline_comments.cpp
new file mode 100644
index 000000000..5eeeffcb0
--- /dev/null
+++ b/harper-tree-sitter/tests/language_support_sources/multiline_comments.cpp
@@ -0,0 +1,12 @@
+/// This is an example of an problematic comment.
+/// It should produce one error.
+int test() {}
+
+/***
+ * This is an example of a possible error:
+ * these subsequent lines should not be considered a new sentence and should
+ * produce no errors.
+ */
+int arbitrary() {}
+
+/// Let's aadd a cuple spelling errors for good measure.
diff --git a/harper-tree-sitter/tests/language_support_sources/multiline_comments.ts b/harper-tree-sitter/tests/language_support_sources/multiline_comments.ts
new file mode 100644
index 000000000..51d87c35e
--- /dev/null
+++ b/harper-tree-sitter/tests/language_support_sources/multiline_comments.ts
@@ -0,0 +1,12 @@
+/// This is an example of an problematic comment.
+/// It should produce one error.
+function test() {}
+
+/***
+ * This is an example of a possible error:
+ * these subsequent lines should not be considered a new sentence and should
+ * produce no errors.
+ */
+function arbitrary() {}
+
+/// Let's aadd a cuple spelling errors for good measure.