From 977c20b67793e33adb3a9613a7ddd6e3331a2fc0 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 12 Mar 2024 16:52:34 +0800 Subject: [PATCH] feat(coverage): add a duplicate comment check (#2688) --- tasks/coverage/parser_babel.snap | 5 ++++- tasks/coverage/parser_typescript.snap | 6 ++++-- tasks/coverage/src/suite.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tasks/coverage/parser_babel.snap b/tasks/coverage/parser_babel.snap index 8b2f6f609be67..1a8f9264dda1a 100644 --- a/tasks/coverage/parser_babel.snap +++ b/tasks/coverage/parser_babel.snap @@ -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" @@ -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 diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index 010e4878d2df0..052782d1ed183 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -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" @@ -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" @@ -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 `￾` @@ -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 diff --git a/tasks/coverage/src/suite.rs b/tasks/coverage/src/suite.rs index 323fda260dbc4..b27ecc5254103 100644 --- a/tasks/coverage/src/suite.rs +++ b/tasks/coverage/src/suite.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashSet, fs::{self, File}, io::{stdout, Read, Write}, panic::UnwindSafe, @@ -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; @@ -35,6 +37,7 @@ pub enum TestResult { CorrectError(String, /* panicked */ bool), RuntimeError(String), CodegenError(/* reason */ &'static str), + DuplicatedComments(String), Snapshot(String), } @@ -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(); @@ -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(()) @@ -418,4 +433,16 @@ pub trait Case: Sized + Sync + Send + UnwindSafe { fn check_semantic(&self, _semantic: &oxc_semantic::Semantic<'_>) -> Option { None } + + fn check_comments(&self, trivias: &Trivias) -> Option { + 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 + } }