Skip to content

Commit

Permalink
Fixes to the Vale preprocessor. (#681)
Browse files Browse the repository at this point in the history
* Fix: capture  lines.

Signed-off-by: dblock <dblock@amazon.com>

* Keep x-deprecation-message.

Signed-off-by: dblock <dblock@amazon.com>

---------

Signed-off-by: dblock <dblock@amazon.com>
  • Loading branch information
dblock authored Nov 19, 2024
1 parent 4d58d8f commit cd3c64d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
29 changes: 16 additions & 13 deletions tools/src/prepare-for-vale/KeepDescriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,30 @@ export default class KeepDescriptions {
const contents = fs.readFileSync(filename, 'utf-8')
var writer = fs.openSync(filename, 'w+')

var inside_description = false
var inside_text = false
contents.split(/\r?\n/).forEach((line) => {
// TODO: keep x-deprecation-message
if (line.match(/^[\s]+(description: \|)/)) {
inside_description = true
} else if (line.match(/^[\s]+(description:)[\s]+/)) {
fs.writeSync(writer, this.prune(line).replace("description:", " "))
} else if (inside_description && line.match(/^[\s]*[\w]*:/)) {
inside_description = false
} else if (inside_description) {
fs.writeSync(writer, this.prune(line))
if (line.match(/^[\s]+((description|x-deprecation-message): \|)/)) {
inside_text = true
} else if (line.match(/^[\s]+((description|x-deprecation-message):)[\s]+/)) {
fs.writeSync(writer, this.prune_vars(this.prune(line, /(description|x-deprecation-message):/, ' ')))
} else if (inside_text && line.match(/^[\s]*[\w\\$]*:/)) {
inside_text = false
} else if (inside_text) {
fs.writeSync(writer, this.prune_vars(line))
}
if (line.length > 0) {
fs.writeSync(writer, "\n")
}
})
}

prune(line: string): string {
return line.replace(/([`])(?:(?=(\\?))\2.)*?\1/g, (match) => {
return Array(match.length + 1).join('*')
prune_vars(line: string): string {
return this.prune(line, /([`])(?:(?=(\\?))\2.)*?\1/g, '*')
}

prune(line: string, regex: RegExp, char: string): string {
return line.replace(regex, (match) => {
return Array(match.length + 1).join(char)
})
}
}
15 changes: 15 additions & 0 deletions tools/tests/prepare-for-vale/fixtures/spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@
Line one
Line with backticks: that includes *******.
Line with two backticks: that includes ******* and *******.



Line one.



Deprecation message.
Line one.



Line one.
Line two.
Line one.
15 changes: 15 additions & 0 deletions tools/tests/prepare-for-vale/fixtures/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ components:
Line one
Line with backticks: that includes `x.y.z`.
Line with two backticks: that includes `x.y.z` and `z.y.x`.
ObjectWithDescriptionAndRef:
type: object
description: |-
Line one.
$ref: '_common.yaml#/components/schemas/HumanReadableByteCount'
ObjectWithDeprecationMessageOneLine:
type: object
x-deprecation-message: Deprecation message.
description: Line one.
ObjectWithDeprecationMessageTwoLines:
type: object
x-deprecation-message: |-
Line one.
Line two.
description: Line one.

0 comments on commit cd3c64d

Please sign in to comment.