summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-29 09:48:01 +0300
committerPaul Buetow <paul@buetow.org>2026-05-29 09:48:01 +0300
commit6226a1e62b0e7730a5ac1763f03e3fc948d2f894 (patch)
tree3e74b7dee01d77ed19b008863ca17196f28e9bd9
parent09bc71c8744974aa5501336430b438fcc1d235c4 (diff)
refactor(showcase): clarify stripComments precedence (xq)
-rw-r--r--internal/showcase/code_extractor.go12
-rw-r--r--internal/showcase/code_extractor_test.go56
2 files changed, 64 insertions, 4 deletions
diff --git a/internal/showcase/code_extractor.go b/internal/showcase/code_extractor.go
index 3b0e8bc..92ee1e4 100644
--- a/internal/showcase/code_extractor.go
+++ b/internal/showcase/code_extractor.go
@@ -482,10 +482,7 @@ func stripComments(code string) string {
if trimmed == "" {
// Keep empty lines for readability
result = append(result, line)
- } else if strings.HasPrefix(trimmed, "//") ||
- strings.HasPrefix(trimmed, "#") && !strings.HasPrefix(trimmed, "#include") && !strings.HasPrefix(trimmed, "#define") ||
- strings.HasPrefix(trimmed, "<!--") ||
- strings.HasPrefix(trimmed, "*") && len(trimmed) > 1 && trimmed[1] == ' ' {
+ } else if isCommentLine(trimmed) {
// Skip comment lines
continue
} else if strings.HasPrefix(trimmed, "\"\"\"") || strings.HasPrefix(trimmed, "'''") {
@@ -522,6 +519,13 @@ func stripComments(code string) string {
return strings.Join(result, "\n")
}
+func isCommentLine(trimmed string) bool {
+ return strings.HasPrefix(trimmed, "//") ||
+ (strings.HasPrefix(trimmed, "#") && !strings.HasPrefix(trimmed, "#include") && !strings.HasPrefix(trimmed, "#define")) ||
+ strings.HasPrefix(trimmed, "<!--") ||
+ (strings.HasPrefix(trimmed, "*") && len(trimmed) > 1 && trimmed[1] == ' ')
+}
+
// removeCommonIndentation removes common leading whitespace from all lines
func removeCommonIndentation(lines []string) []string {
if len(lines) == 0 {
diff --git a/internal/showcase/code_extractor_test.go b/internal/showcase/code_extractor_test.go
new file mode 100644
index 0000000..d091f66
--- /dev/null
+++ b/internal/showcase/code_extractor_test.go
@@ -0,0 +1,56 @@
+package showcase
+
+import "testing"
+
+func TestIsCommentLine(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ trimmed string
+ want bool
+ }{
+ {name: "slash comment", trimmed: "// comment", want: true},
+ {name: "hash comment", trimmed: "# comment", want: true},
+ {name: "include directive", trimmed: "#include <stdio.h>", want: false},
+ {name: "define directive", trimmed: "#define FOO 1", want: false},
+ {name: "html comment", trimmed: "<!-- comment -->", want: true},
+ {name: "block comment inner line", trimmed: "* comment", want: true},
+ {name: "single asterisk", trimmed: "*", want: false},
+ {name: "code line", trimmed: "fmt.Println(\"ok\")", want: false},
+ }
+
+ for _, tc := range tests {
+ tc := tc
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ got := isCommentLine(tc.trimmed)
+ if got != tc.want {
+ t.Fatalf("isCommentLine(%q) = %v, want %v", tc.trimmed, got, tc.want)
+ }
+ })
+ }
+}
+
+func TestStripComments_PreservesPreprocessorDirectives(t *testing.T) {
+ t.Parallel()
+
+ code := `#include <stdio.h>
+#define VALUE 1
+# this is a comment
+int main() {
+ return VALUE; // trailing comment
+}`
+
+ got := stripComments(code)
+ want := `#include <stdio.h>
+#define VALUE 1
+int main() {
+ return VALUE;
+}`
+
+ if got != want {
+ t.Fatalf("stripComments() = %q, want %q", got, want)
+ }
+}