Skip to content

Commit 2962622

Browse files
committed
fix out of index code error and test
1 parent 11d67ad commit 2962622

File tree

2 files changed

+23
-58
lines changed

2 files changed

+23
-58
lines changed

src/org/ods/component/Pipeline.groovy

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -618,63 +618,27 @@ class Pipeline implements Serializable {
618618
}
619619
}
620620

621-
private String removeCommentedCode(String content) {
622-
def lines = content.split('\n')
623-
def result = new StringBuilder()
624-
boolean inBlockComment = false
625-
626-
for (String line : lines) {
627-
String processedLine = line
628-
629-
// Handle block comments (/* */)
630-
if (inBlockComment) {
631-
def blockCommentEnd = processedLine.indexOf('*/')
632-
if (blockCommentEnd != -1) {
633-
// End of block comment found
634-
if (blockCommentEnd + 2 <= processedLine.length()) {
635-
processedLine = processedLine[(blockCommentEnd + 2)..-1]
636-
} else {
637-
processedLine = ""
638-
}
639-
inBlockComment = false
640-
} else {
641-
// Still inside block comment, skip entire line
642-
continue
643-
}
644-
}
645-
646-
// Check for start of block comment
647-
def blockCommentStart = processedLine.indexOf('/*')
648-
if (blockCommentStart != -1) {
649-
def blockCommentEnd = processedLine.indexOf('*/', blockCommentStart + 2)
650-
if (blockCommentEnd != -1) {
651-
// Complete block comment on same line
652-
String before = blockCommentStart > 0
653-
? processedLine[0..(blockCommentStart - 1)]
654-
: ""
655-
String after = ""
656-
if (blockCommentEnd + 2 < processedLine.length()) {
657-
after = processedLine[(blockCommentEnd + 2)..-1]
658-
}
659-
processedLine = before + after
660-
} else {
661-
// Block comment starts but doesn't end on this line
662-
processedLine = blockCommentStart > 0 ? processedLine[0..(blockCommentStart - 1)] : ""
663-
inBlockComment = true
664-
}
621+
/**
622+
* Removes all commented code from the given string.
623+
* Handles // single-line, /* block , and multi-line comments.
624+
*/
625+
def removeCommentedCode(String content) {
626+
if (!content) {
627+
return content
628+
}
629+
// Remove block comments (including multi-line)
630+
content = content.replaceAll(/(?s)\/\*.*?\*\//, "")
631+
// Remove single-line comments (// ...), but keep code before //
632+
content = content.readLines().collect { line ->
633+
def idx = line.indexOf('//')
634+
if (idx >= 0) {
635+
return line.substring(0, idx)
665636
}
666-
667-
// Handle single-line comments (//)
668-
def singleCommentIndex = processedLine.indexOf('//')
669-
if (singleCommentIndex != -1) {
670-
processedLine = singleCommentIndex > 0 ? processedLine[0..(singleCommentIndex - 1)] : ""
671-
}
672-
673-
// Add processed line to result
674-
result.append(processedLine).append('\n')
675-
}
676-
677-
return result.toString()
637+
return line
638+
}.join('\n')
639+
// Remove lines that are now empty or whitespace only
640+
content = content.readLines().findAll { it.trim() }.join('\n')
641+
return content
678642
}
679643

680644
private String getJenkinsfileScriptPath() {

test/groovy/org/ods/component/PipelineSpec.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class PipelineSpec extends PipelineSpockTestBase {
144144
/* Multi-line
145145
comment */
146146
code3
147-
// Inline comment code4
147+
code4 // Inline comment
148148
"""
149149

150150
when:
@@ -154,10 +154,11 @@ class PipelineSpec extends PipelineSpockTestBase {
154154
!result.contains('Single line comment')
155155
!result.contains('Block comment')
156156
!result.contains('Multi-line')
157-
!result.contains('code4') // code4 is part of inline comment and should be removed
157+
!result.contains('// Inline comment') // Inline comment should be removed
158158
result.contains('code1')
159159
result.contains('code2')
160160
result.contains('code3')
161+
result.contains('code4') // code4 should remain, only the comment after // should be removed
161162
}
162163

163164
def "execute handles multi-repo build setup correctly"() {

0 commit comments

Comments
 (0)