From 4cd77ebc99374f526704d8a980cc02e93c8ba65f Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 08:05:36 +0000 Subject: [PATCH 1/2] refactor(test): replace require-style imports with import statements DeepSource reported "Require statement not part of import statement" for `import path = require("path")` in vs-code-adapter.test.ts. Use a named import there and `import * as Mocha` in the test runner, which compiles to the same require() call. nyc ships no type definitions and sonar-project.js is a plain node script, so both keep their require() with a justified eslint-disable comment. Enable @typescript-eslint/no-require-imports, so lint and CI catch this before DeepSource does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- .eslintrc.js | 1 + sonar-project.js | 3 ++- src/test/suite/adapter/vs-code-adapter.test.ts | 4 ++-- src/test/suite/index.ts | 5 +++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index b197a9b..268c3c8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -19,6 +19,7 @@ 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", "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 From 6b6616a28a6cf9dd5d2bd1d2ee1fc526d1e73172 Mon Sep 17 00:00:00 2001 From: Pascal Reitermann Date: Fri, 20 Nov 2026 08:05:49 +0000 Subject: [PATCH 2/2] refactor(sort): build the remaining keys instead of deleting them DeepSource reported "Do not delete dynamically computed property keys" for `delete doc[key]` in the custom sort of YamlUtil.sortYaml(). The loaded document was typed as `any` and mutated while the keywords were dumped. Type the loaded document as unknown, apply the custom sort only to mappings, dump each keyword in the new dumpKeyword() and build the remaining keys as a new object. Characterization tests cover keyword order, a document with only keywords, list and scalar documents. Enable @typescript-eslint/no-dynamic-delete, so lint and CI catch this before DeepSource does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Jdb5is9YsMnteaxHryN26M --- .eslintrc.js | 1 + src/test/suite/util/yaml-util.test.ts | 20 +++++++++++ src/util/yaml-util.ts | 50 +++++++++++++++++---------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 268c3c8..74d2ab9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -20,6 +20,7 @@ module.exports = { "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/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.