Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to the Vale preprocessor. #681

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading