Skip to content
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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = {
// the base rule does not understand typescript, so use the typescript-eslint variant
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/no-dynamic-delete": "error",
"semi": ["error", "never"],
"sort-imports": [
"error",
Expand Down
3 changes: 2 additions & 1 deletion sonar-project.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
// a plain node script without a build step, so it uses require()
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const scanner = require("sonarqube-scanner")
scanner(
{
Expand Down
4 changes: 2 additions & 2 deletions src/test/suite/adapter/vs-code-adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { equal, fail } from "assert"
import path = require("path")
import { resolve } from "path"

import { spy } from "sinon"
import { Uri, window, workspace } from "vscode"
Expand All @@ -8,7 +8,7 @@ import { Severity, VsCodeAdapter } from "../../../adapter/vs-code-adapter"

suite("Test VsCodeAdapter - getActiveDocument()", () => {
test("when text editor is active should open document", async () => {
const uri = Uri.parse(path.resolve("./src/test/files/testGetActiveDocument.yaml"))
const uri = Uri.parse(resolve("./src/test/files/testGetActiveDocument.yaml"))
const doc = await workspace.openTextDocument(uri)
await window.showTextDocument(doc, { preview: false })

Expand Down
5 changes: 3 additions & 2 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { join, resolve} from "path"

import { sync } from "glob"
import Mocha = require("mocha")
import * as Mocha from "mocha"

// eslint-disable-next-line @typescript-eslint/no-var-requires
// nyc ships no type definitions, so it cannot be imported with an import statement
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const NYC = require("nyc")

// Recommended modules, loading them here to speed up NYC init
Expand Down
20 changes: 20 additions & 0 deletions src/test/suite/util/yaml-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,23 @@ suite("Test YamlUtil - injected settings reach every processing step", () => {
equal(new YamlUtil(settings).formatYaml("a:\n b: 1", false), "a:\n b: 1")
})
})

suite("Test YamlUtil - sortYaml() with custom sort keeps the document intact", () => {
const yamlutil = new YamlUtil()

test("should put the keywords in their order before the remaining keys", () => {
equal(yamlutil.sortYaml("data: 1\nkind: x\nb:\n c: 1", 1), "kind: x\ndata: 1\nb:\n c: 1")
})

test("should keep a document that only contains keywords", () => {
equal(yamlutil.sortYaml("kind: x", 1), "kind: x\n")
})

test("should leave a list document unchanged", () => {
equal(yamlutil.sortYaml("- kind: x\n- a: 1", 1), "- kind: x\n- a: 1")
})

test("should leave a scalar document unchanged", () => {
equal(yamlutil.sortYaml("42", 1), "42")
})
})
50 changes: 31 additions & 19 deletions src/util/yaml-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { Settings } from "../settings"
import { sortArraysRecursively } from "./sort-util"
import { removeTrailingCharacters } from "./yaml-text"

/**
* @param value Any loaded yaml value.
* @returns True for a mapping, i.e. an object that is no list.
*/
const isMapping = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value)

export class YamlUtil {
settings: Settings
jsyamladapter: JsYamlAdapter
Expand Down Expand Up @@ -49,35 +56,25 @@ export class YamlUtil {
processor.preprocess()
const unsortedYamlWithoutTabs = processor.text

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const doc = this.jsyamladapter.load(unsortedYamlWithoutTabs) as any
const doc = this.jsyamladapter.load(unsortedYamlWithoutTabs)

if (this.settings.sortArrays) {
sortArraysRecursively(doc)
}

let sortedYaml = ""
if (customSort > 0 && !this.settings.useCustomSortRecursively) {
let remainder = doc
if (customSort > 0 && !this.settings.useCustomSortRecursively && isMapping(doc)) {
// check for the key itself, values like 0, false or null must be ordered too
const keywords = this.settings.getCustomSortKeywords(customSort)

keywords.forEach(key => {
// check for the key itself, values like 0, false or null must be ordered too
if (Object.prototype.hasOwnProperty.call(doc, key)) {
let sortedSubYaml = this.jsyamladapter.dumpValue(doc[key], true, customSort)
if (YamlUtil.isValueMultiline(sortedSubYaml)) {
sortedSubYaml = this.transformMultilineValue(sortedSubYaml)
sortedYaml += `${key}:\n${sortedSubYaml}\n`
} else {
sortedYaml += `${key}: ${sortedSubYaml}\n`
}
// delete key from yaml
delete doc[key]
}
})
.filter((key) => Object.prototype.hasOwnProperty.call(doc, key))
sortedYaml = keywords.map((key) => this.dumpKeyword(key, doc[key], customSort)).join("")
// the remaining keys are sorted as usual; build them anew instead of deleting from the loaded document
remainder = Object.fromEntries(Object.entries(doc).filter(([key]) => !keywords.includes(key)))
}

// either sort whole yaml or sort the rest of the yaml (which can be empty) and add it to the sortedYaml
sortedYaml += this.jsyamladapter.dumpYaml(doc, true, customSort)
sortedYaml += this.jsyamladapter.dumpYaml(remainder, true, customSort)

processor.text = sortedYaml
processor.postprocess()
Expand All @@ -89,6 +86,21 @@ export class YamlUtil {
}
}

/**
* Dumps one top-level keyword of a custom sort together with its value.
* @param key The keyword.
* @param value Its value.
* @param customSort 1 to 3, the custom sort that is applied.
* @returns The yaml lines for the keyword, ending with a line break.
*/
dumpKeyword(key: string, value: unknown, customSort: number): string {
const dumpedValue = this.jsyamladapter.dumpValue(value, true, customSort)
if (YamlUtil.isValueMultiline(dumpedValue)) {
return `${key}:\n${this.transformMultilineValue(dumpedValue)}\n`
}
return `${key}: ${dumpedValue}\n`
}

/**
* Formats a yaml document (without sorting).
* @param {string} yaml Yaml to be formatted.
Expand Down
Loading