diff --git a/.eslintrc.js b/.eslintrc.js index b197a9b..74d2ab9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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", diff --git a/sonar-project.js b/sonar-project.js index 3653ac5..85e076d 100644 --- a/sonar-project.js +++ b/sonar-project.js @@ -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( { diff --git a/src/test/suite/adapter/vs-code-adapter.test.ts b/src/test/suite/adapter/vs-code-adapter.test.ts index 264acbd..59cb4c3 100644 --- a/src/test/suite/adapter/vs-code-adapter.test.ts +++ b/src/test/suite/adapter/vs-code-adapter.test.ts @@ -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" @@ -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 }) diff --git a/src/test/suite/index.ts b/src/test/suite/index.ts index aabb6a5..70d8e7b 100644 --- a/src/test/suite/index.ts +++ b/src/test/suite/index.ts @@ -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 diff --git a/src/test/suite/util/yaml-util.test.ts b/src/test/suite/util/yaml-util.test.ts index 6401a85..6e3530f 100644 --- a/src/test/suite/util/yaml-util.test.ts +++ b/src/test/suite/util/yaml-util.test.ts @@ -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") + }) +}) diff --git a/src/util/yaml-util.ts b/src/util/yaml-util.ts index 5a155bc..ed50e86 100644 --- a/src/util/yaml-util.ts +++ b/src/util/yaml-util.ts @@ -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 => + typeof value === "object" && value !== null && !Array.isArray(value) + export class YamlUtil { settings: Settings jsyamladapter: JsYamlAdapter @@ -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() @@ -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.