Skip to content

Commit

Permalink
feat(coverage): add a duplicate comment check (#2688)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Mar 12, 2024
1 parent cda9c93 commit 977c20b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parser_babel Summary:
AST Parsed : 2090/2096 (99.71%)
Positive Passed: 2083/2096 (99.38%)
Positive Passed: 2080/2096 (99.24%)
Negative Passed: 1362/1500 (90.80%)
Expect Syntax Error: "annex-b/disabled/1.1-html-comments-close/input.js"
Expect Syntax Error: "annex-b/disabled/3.1-sloppy-labeled-functions/input.js"
Expand Down Expand Up @@ -140,6 +140,9 @@ Expect Syntax Error: "typescript/types/read-only-3/input.ts"
Expect Syntax Error: "typescript/types/read-only-4/input.ts"
Expect Syntax Error: "typescript/types/tuple-optional-invalid/input.ts"
Expect Syntax Error: "typescript/types/tuple-required-after-labeled-optional/input.ts"
Duplicated comments " 3 ": "comments/basic/nested-parentheses/input.js"
Duplicated comments " 1 ": "comments/basic/object-method/input.js"
Duplicated comments " 3 ": "comments/basic/sequence-expression/input.js"
Expect to Parse: "core/opts/allowNewTargetOutsideFunction-true/input.js"

× Unexpected new.target expression
Expand Down
6 changes: 4 additions & 2 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parser_typescript Summary:
AST Parsed : 5240/5243 (99.94%)
Positive Passed: 5233/5243 (99.81%)
Positive Passed: 5231/5243 (99.77%)
Negative Passed: 1062/4879 (21.77%)
Expect Syntax Error: "compiler/ClassDeclaration10.ts"
Expect Syntax Error: "compiler/ClassDeclaration11.ts"
Expand Down Expand Up @@ -2697,7 +2697,7 @@ Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardOfFormTypeOfEq
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardOfFormTypeOfNotEqualHasNoEffect.ts"
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardOfFormTypeOfOther.ts"
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardsDefeat.ts"
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardsInIfStatement.ts"
Duplicated comments " change value of x": "conformance/expressions/typeGuards/typeGuardsInIfStatement.ts"
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardsWithAny.ts"
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardsWithInstanceOf.ts"
Expect Syntax Error: "conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts"
Expand Down Expand Up @@ -3819,6 +3819,7 @@ Expect Syntax Error: "conformance/types/unknown/unknownControlFlow.ts"
Expect Syntax Error: "conformance/types/unknown/unknownType1.ts"
Expect Syntax Error: "conformance/types/unknown/unknownType2.ts"
Expect Syntax Error: "conformance/types/witness/witness.ts"
Duplicated comments "comment2": "compiler/awaitExpressionInnerCommentEmit.ts"
Expect to Parse: "compiler/bom-utf16be.ts"

× Invalid Character `￾`
Expand Down Expand Up @@ -3853,6 +3854,7 @@ Expect to Parse: "compiler/emitBundleWithShebangAndPrologueDirectives1.ts"
· ─
7 │ "use strict"
╰────
Duplicated comments " must be identifier?": "compiler/jsxNestedWithinTernaryParsesCorrectly.tsx"
Expect to Parse: "compiler/withStatementInternalComments.ts"

× 'with' statements are not allowed
Expand Down
27 changes: 27 additions & 0 deletions tasks/coverage/src/suite.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::HashSet,
fs::{self, File},
io::{stdout, Read, Write},
panic::UnwindSafe,
Expand All @@ -12,6 +13,7 @@ use encoding_rs::UTF_16LE;
use encoding_rs_io::DecodeReaderBytesBuilder;
use futures::future::join_all;
use oxc_allocator::Allocator;
use oxc_ast::Trivias;
use oxc_diagnostics::{miette::NamedSource, GraphicalReportHandler, GraphicalTheme};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
Expand All @@ -35,6 +37,7 @@ pub enum TestResult {
CorrectError(String, /* panicked */ bool),
RuntimeError(String),
CodegenError(/* reason */ &'static str),
DuplicatedComments(String),
Snapshot(String),
}

Expand Down Expand Up @@ -315,6 +318,9 @@ pub trait Case: Sized + Sync + Send + UnwindSafe {
let parser_ret = Parser::new(&allocator, source_text, source_type)
.allow_return_outside_function(self.allow_return_outside_function())
.parse();
if let Some(res) = self.check_comments(&parser_ret.trivias) {
return res;
}

// Make sure serialization doesn't crash for ast and hir, also for code coverage.
let _json = parser_ret.program.to_json();
Expand Down Expand Up @@ -390,6 +396,15 @@ pub trait Case: Sized + Sync + Send + UnwindSafe {
TestResult::Snapshot(snapshot) => {
writer.write_all(snapshot.as_bytes())?;
}
TestResult::DuplicatedComments(comment) => {
writer.write_all(
format!(
"Duplicated comments \"{comment}\": {:?}\n",
normalize_path(self.path())
)
.as_bytes(),
)?;
}
TestResult::Passed | TestResult::ToBeRun | TestResult::CorrectError(..) => {}
}
Ok(())
Expand Down Expand Up @@ -418,4 +433,16 @@ pub trait Case: Sized + Sync + Send + UnwindSafe {
fn check_semantic(&self, _semantic: &oxc_semantic::Semantic<'_>) -> Option<TestResult> {
None
}

fn check_comments(&self, trivias: &Trivias) -> Option<TestResult> {
let mut uniq: HashSet<(u32, u32)> = HashSet::new();
for (start, end, _) in &trivias.comments {
if !uniq.insert((*start, *end)) {
return Some(TestResult::DuplicatedComments(
self.code()[*start as usize..*end as usize].to_string(),
));
}
}
None
}
}

0 comments on commit 977c20b

Please sign in to comment.