• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pascalre / vscode-yaml-sort / 42425937008

07 Nov 2026 11:35PM UTC coverage: 16.225% (+0.09%) from 16.134%
42425937008

Pull #301

github

web-flow
Merge 056fc6674 into 3dc192b78
Pull Request #301: fix: keep comments above their anchor while sorting (closes #221)

1804 of 13869 branches covered (13.01%)

Branch coverage included in aggregate %.

17 of 18 new or added lines in 1 file covered. (94.44%)

541 of 584 relevant lines covered (92.64%)

72.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.54
/src/processor/comment-processor.ts
1
export class CommentProcessor {
1 ✔
2
  store: string[][] = []
76 ✔
3
  inlineStore: string[][] = []
76 ✔
4
  text: string
5
  lines: string[]
6

7
  constructor(text: string) {
8
    this.text = text.trim()
76 ✔
9
    this.lines = this.text.split("\n")
76 ✔
10
  }
11

12
  /**
13
   * Finds all comments in a given yaml. Stores full line comments with their following line
14
   * and inline comments with the code they belong to.
15
   * @param text Yaml document
16
   */
17
  findComments() {
18
    this.lines.forEach((line, index) => {
64 ✔
19
      if (CommentProcessor.isLineComment(line)) {
219 ✔
20
        this.addLineToComments(index)
23 ✔
21
      } else {
22
        this.addInlineCommentToStore(line)
196 ✔
23
      }
24
    })
25
  }
26

27
  static isLineComment(line: string) {
28
    return /^ *#/.test(line)
224 ✔
29
  }
30

31
  addLineToComments(index: number) {
32
    if (index < this.lines.length-1) {
26 ✔
33
      this.store.push([this.lines[index], this.lines[index+1]])
22 ✔
34
    } else {
35
      this.store.push([this.lines[index], "vscode-yaml-sort.lastLine"])
4 ✔
36
    }
37
  }
38

39
  addInlineCommentToStore(line: string) {
40
    const comment = CommentProcessor.getInlineComment(line)
196 ✔
41

42
    if (comment !== undefined) {
196 ✔
43
      this.inlineStore.push([` ${comment}`, line.slice(0, line.length - comment.length).trimEnd()])
5 ✔
44
    }
45
  }
46

47
  static getInlineComment(line: string): string | undefined {
48
    let inDoubleQuotes = false
202 ✔
49
    let inSingleQuotes = false
202 ✔
50

51
    for (let i = 0; i < line.length; i++) {
202 ✔
52
      if (line[i] === "\"" && !inSingleQuotes) {
3,318 ✔
53
        inDoubleQuotes = !inDoubleQuotes
42 ✔
54
      } else if (line[i] === "'" && !inDoubleQuotes) {
3,276 ✔
55
        inSingleQuotes = !inSingleQuotes
4 ✔
56
      } else if (line[i] === "#" && i > 0 && (line[i - 1] === " " || line[i - 1] === "\t") && !inDoubleQuotes && !inSingleQuotes) {
3,272 ✔
57
        return line.slice(i)
7 ✔
58
      }
59
    }
60

61
    return undefined
195 ✔
62
  }
63

64
  postprocess(): string {
65
    this.reverseComments()
57 ✔
66
    this.store.forEach(comment => {
57 ✔
67
      this.applyComment(comment)
19 ✔
68
    })
69
    this.applyInlineComments()
57 ✔
70
    return this.text
57 ✔
71
  }
72

73
  reverseComments() {
74
    this.store.reverse()
58 ✔
75
  }
76

77
  applyInlineComments() {
78
    this.inlineStore.reverse()
57 ✔
79
    this.inlineStore.forEach(comment => {
57 ✔
80
      this.applyInlineComment(comment)
5 ✔
81
    })
82
  }
83

84
  applyInlineComment(comment: string[]) {
85
    const indexOfComment = this.getIndexOfString(comment[1])
5 ✔
86

87
    if (CommentProcessor.isCommentFound(indexOfComment)) {
5 ✔
88
      this.insertInlineIfNotContained(comment)
5 ✔
89
    }
90
  }
91

92
  insertInlineIfNotContained(comment: string[]) {
93
    const endOfLine = this.getEndOfLine(this.getIndexOfString(comment[1]))
5 ✔
94
    const textBefore = this.text.slice(0, endOfLine)
5 ✔
95
    const textAfter = this.text.slice(endOfLine)
5 ✔
96

97
    if (!textBefore.endsWith(comment[0])) {
5 ✔
98
      this.text = `${textBefore}${comment[0]}${textAfter}`
5 ✔
99
    }
100
  }
101

102
  getEndOfLine(index: number): number {
103
    const endOfLine = this.text.indexOf("\n", index)
5 ✔
104

105
    return endOfLine === -1 ? this.text.length : endOfLine
5 !
106
  }
107

108
  applyComment(comment: string[]) {
109
    if (comment[1] === "vscode-yaml-sort.lastLine") {
20 ✔
110
      this.append(comment[0])
3 ✔
111
    } else {
112
      this.insert(comment)
17 ✔
113
    }
114
  }
115

116
  append(line: string) {
117
    this.text = `${this.text}\n${line}`
4 ✔
118
  }
119

120
  insert(comment: string[]) {
121
    const indexOfComment = this.getIndexOfString(comment[1])
19 ✔
122
    if (CommentProcessor.isCommentFound(indexOfComment)) {
19 ✔
123
      this.insertIfNotContained(comment)
18 ✔
124
    }
125
  }
126

127
  insertIfNotContained(comment: string[]) {
128
    const indexOfComment = this.getBeginningOfLine(this.getIndexOfString(comment[1]))
20 ✔
129
    const textAfter = this.text.slice(indexOfComment)
20 ✔
130
    const textBefore = this.text.slice(0, indexOfComment)
20 ✔
131

132
    if (!textBefore.endsWith(`${comment[0]}\n`)) {
20 ✔
133
      this.text = `${textBefore}${comment[0]}\n${textAfter.trimEnd()}`
17 ✔
134
    }
135
  }
136

137
  getBeginningOfLine(index: number): number {
138
    return this.text.lastIndexOf("\n", index) + 1
20 ✔
139
  }
140

141
  getIndexOfString(text: string): number {
142
    if (text === "vscode-yaml-sort.lastLine") {
51 ✔
143
      return this.text.length
1 ✔
144
    } else {
145
      return this.search(text)
50 ✔
146
    }
147
  }
148

149
  search(text: string) {
150
    let result = this.searchExactMatch(text)
53 ✔
151
    if (CommentProcessor.isCommentFound(result)) {
53 ✔
152
      return result
40 ✔
153
    }
154

155
    result = this.searchFuzzyForTrimmedText(text)
13 ✔
156
    if (CommentProcessor.isCommentFound(result)) {
13 ✔
157
      return result
5 ✔
158
    }
159

160
    return this.searchFuzzyForKeyword(text)
8 ✔
161
  }
162

163
  searchExactMatch(text: string): number {
164
    // an empty anchor keeps its previous behavior
165
    if (text === "") {
54 !
NEW
166
      return this.text.lastIndexOf(text)
×
167
    }
168
    // comments must be inserted above the exact matching line (see #221).
169
    // a substring search would also find `mysql5:` inside a deeper indented
170
    // line like `      mysql5:` of a `depends_on` block
171
    let result = -1
54 ✔
172
    let offset = 0
54 ✔
173
    for (const line of this.text.split("\n")) {
54 ✔
174
      if (line === text) {
230 ✔
175
        result = offset
42 ✔
176
      }
177
      offset += line.length + 1
230 ✔
178
    }
179
    return result
54 ✔
180
  }
181

182
  searchFuzzyForTrimmedText(text: string): number {
183
    // like searchExactMatch(), but the line may have more or less
184
    // surrounding whitespaces than the anchor (see #139)
185
    const trimmedText = text.trim()
14 ✔
186
    let result = -1
14 ✔
187
    let offset = 0
14 ✔
188
    for (const line of this.text.split("\n")) {
14 ✔
189
      if (line.trim() === trimmedText) {
50 ✔
190
        result = offset
6 ✔
191
      }
192
      offset += line.length + 1
50 ✔
193
    }
194
    return result
14 ✔
195
  }
196

197
  searchFuzzyForKeyword(text: string): number {
198
    // the value of a line may change while sorting,
199
    // so only search for the keyword in front of the first colon
200
    return this.text.lastIndexOf(text.split(":")[0])
9 ✔
201
  }
202

203
  static isCommentFound(index: number): boolean {
204
    return index !== -1
92 ✔
205
  }
206
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc