@@ -618,63 +618,27 @@ class Pipeline implements Serializable {
618
618
}
619
619
}
620
620
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)
665
636
}
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
678
642
}
679
643
680
644
private String getJenkinsfileScriptPath () {
0 commit comments