From 8f026b1dc2219e9d2e3ceeda5e5bdcc385bb9f4d Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Mon, 6 Jul 2026 13:15:40 +0100 Subject: [PATCH] Allow inline expectation comments in more file formats --- .../internal/InlineExpectationsTestImpl.qll | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/csharp/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll b/csharp/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll index 962ea7aa004..6916c5f6106 100644 --- a/csharp/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll +++ b/csharp/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll @@ -1,13 +1,51 @@ private import csharp as CS +private import semmle.code.asp.AspNet as ASP private import codeql.util.test.InlineExpectationsTest module Impl implements InlineExpectationsTestSig { + private newtype TExpectationComment = + TCSharpComment(CS::SinglelineComment c) or + TXmlComment(CS::XmlComment c) or + TAspComment(ASP::AspComment c) + /** - * A class representing line comments in C# used by the InlineExpectations core code + * A class representing comments that may contain inline expectations. + * Supports C# single-line comments (`//`), XML comments (``), and + * ASP.NET comments (`` and `<%-- --%>`) in their respective file types. */ - class ExpectationComment extends CS::SinglelineComment { - /** Gets the contents of the given comment, _without_ the preceding comment marker (`//`). */ - string getContents() { result = this.getText() } + class ExpectationComment extends TExpectationComment { + CS::SinglelineComment asCSharpComment() { this = TCSharpComment(result) } + + CS::XmlComment asXmlComment() { this = TXmlComment(result) } + + ASP::AspComment asAspComment() { this = TAspComment(result) } + + /** Gets the contents of this comment, _without_ the preceding comment marker. */ + string getContents() { + result = this.asCSharpComment().getText() + or + result = this.asXmlComment().getText() + or + result = this.asAspComment().getBody() + } + + /** Gets the location of this comment. */ + Location getLocation() { + result = this.asCSharpComment().getLocation() + or + result = this.asXmlComment().getLocation() + or + result = this.asAspComment().getLocation() + } + + /** Gets a textual representation of this comment. */ + string toString() { + result = this.asCSharpComment().toString() + or + result = this.asXmlComment().toString() + or + result = this.asAspComment().toString() + } } class Location = CS::Location;