diff --git a/package-lock.json b/package-lock.json index 073fd87..9300d55 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3443,6 +3443,15 @@ "node": ">=8" } }, + "node_modules/istanbul-lib-processinfo/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/istanbul-lib-report": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/istanbul-lib-report/-/istanbul-lib-report-3.0.0.tgz", @@ -5672,15 +5681,6 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, - "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "dev": true, - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/v8-compile-cache-lib": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", diff --git a/src/adapter/vs-code-adapter.ts b/src/adapter/vs-code-adapter.ts index 5d952a1..96b3f76 100644 --- a/src/adapter/vs-code-adapter.ts +++ b/src/adapter/vs-code-adapter.ts @@ -33,9 +33,13 @@ export class VsCodeAdapter { if (severity === Severity.ERROR) { window.showErrorMessage(message) } else { - if (this.settings.getNotifySuccess()) { - window.showInformationMessage(message) - } + this.notify(message) + } + } + + notify(message: string) { + if (this.settings.getNotifySuccess()) { + window.showInformationMessage(message) } } diff --git a/src/util/comment-util.ts b/src/processor/comment-processor.ts similarity index 81% rename from src/util/comment-util.ts rename to src/processor/comment-processor.ts index b65c4d1..b3b3106 100644 --- a/src/util/comment-util.ts +++ b/src/processor/comment-processor.ts @@ -1,18 +1,11 @@ -import { JsYamlAdapter } from "../adapter/js-yaml-adapter" -import { Settings } from "../settings" - -export class CommentUtil { - settings: Settings - jsyamladapter: JsYamlAdapter +export class CommentProcessor { comments: string[][] = [] text: string lines: string[] - constructor(text: string, settings = new Settings()) { + constructor(text: string) { this.text = text.trim() this.lines = this.text.split("\n") - this.settings = settings - this.jsyamladapter = new JsYamlAdapter(settings) } /** @@ -21,7 +14,7 @@ export class CommentUtil { */ findComments() { this.lines.forEach((line, index) => { - if (CommentUtil.isLineComment(line)) { + if (CommentProcessor.isLineComment(line)) { this.addLineToComments(index) } }) @@ -66,7 +59,7 @@ export class CommentUtil { insert(comment: string[]) { const indexOfComment = this.getIndexOfString(comment[1]) - if (CommentUtil.isCommentFound(indexOfComment)) { + if (CommentProcessor.isCommentFound(indexOfComment)) { const textAfter = this.text.slice(indexOfComment) const textBefore = this.text.slice(0, indexOfComment) this.text = `${textBefore}${comment[0]}\n${textAfter.trimEnd()}` @@ -83,12 +76,12 @@ export class CommentUtil { search(text: string) { let result = this.searchExactMatch(text) - if (CommentUtil.isCommentFound(result)) { + if (CommentProcessor.isCommentFound(result)) { return result } result = this.searchFuzzyForTrimmedText(text) - if (CommentUtil.isCommentFound(result)) { + if (CommentProcessor.isCommentFound(result)) { return result } diff --git a/src/processor/helm-processor.ts b/src/processor/helm-processor.ts new file mode 100644 index 0000000..49b4bbd --- /dev/null +++ b/src/processor/helm-processor.ts @@ -0,0 +1,38 @@ +export class HelmProcessor { + store: Map = new Map() + text: string + + constructor(text: string) { + this.text = text + } + + preprocess() { + const helmValues = this.findHelmValues() + if (helmValues) { + for (const value of helmValues) { + this.replaceValueWithSubstitue(value) + } + } + } + + findHelmValues() { + return this.text.match(/{{.*}}/g) + } + + replaceValueWithSubstitue(value: string) { + const substitue = `vscode-yaml-sort.helm.${this.store.size}` + this.store.set(substitue, value) + this.text = this.text.replace(value, substitue) + } + + postprocess() { + this.store.forEach((value: string, key: string) => { + this.replaceSubstituteWithValue(key, value) + }) + } + + replaceSubstituteWithValue(substitue: string, value: string) { + const match = new RegExp("('|\")?" + substitue + "('|\")?") + this.text = this.text.replace(match, value) + } +} diff --git a/src/processor/processor.ts b/src/processor/processor.ts new file mode 100644 index 0000000..57c4073 --- /dev/null +++ b/src/processor/processor.ts @@ -0,0 +1,31 @@ +import { CommentProcessor } from "./comment-processor" +import { HelmProcessor } from "./helm-processor" + +export class Processor { + text: string + helmprocessor!: HelmProcessor + commentprocessor!: CommentProcessor + + constructor(text: string) { + this.text = text + } + + preprocess() { + this.helmprocessor = new HelmProcessor(this.text) + this.helmprocessor.preprocess() + this.text = this.helmprocessor.text + + this.commentprocessor = new CommentProcessor(this.text) + this.commentprocessor.findComments() + this.text = this.commentprocessor.text + } + + postprocess(text: string) { + this.commentprocessor.applyComments(text) + this.text = this.commentprocessor.text + + this.helmprocessor.text = this.text + this.helmprocessor.postprocess() + this.text = this.helmprocessor.text + } +} \ No newline at end of file diff --git a/src/test/suite/util/comment-util.test.ts b/src/test/suite/processor/comment-processor.test.ts similarity index 51% rename from src/test/suite/util/comment-util.test.ts rename to src/test/suite/processor/comment-processor.test.ts index b93fc13..e7cd841 100644 --- a/src/test/suite/util/comment-util.test.ts +++ b/src/test/suite/processor/comment-processor.test.ts @@ -1,7 +1,7 @@ import { deepStrictEqual, equal } from "assert" -import { CommentUtil } from "../../../util/comment-util" +import { CommentProcessor } from "../../../processor/comment-processor" -suite("Test CommentUtil - findComments()", () => { +suite("Test CommentProcessor - findComments()", () => { test("should add all comments to array 'comments'", () => { const text = '#foo\n' + @@ -12,38 +12,38 @@ suite("Test CommentUtil - findComments()", () => { ' # foo\n' + 'amet, consetetur\n' + '# baz' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - commentutil.findComments() + commentprocessor.findComments() - equal(commentutil.comments.length, 5) - deepStrictEqual(commentutil.comments[0], ["#foo", "#bar"]) - deepStrictEqual(commentutil.comments[1], ["#bar", "lorem ipsum"]) - deepStrictEqual(commentutil.comments[2], ["#foo", "dolor sit"]) - deepStrictEqual(commentutil.comments[3], [" # foo", "amet, consetetur"]) - deepStrictEqual(commentutil.comments[4], ["# baz", "vscode-yaml-sort.lastLine"]) + equal(commentprocessor.comments.length, 5) + deepStrictEqual(commentprocessor.comments[0], ["#foo", "#bar"]) + deepStrictEqual(commentprocessor.comments[1], ["#bar", "lorem ipsum"]) + deepStrictEqual(commentprocessor.comments[2], ["#foo", "dolor sit"]) + deepStrictEqual(commentprocessor.comments[3], [" # foo", "amet, consetetur"]) + deepStrictEqual(commentprocessor.comments[4], ["# baz", "vscode-yaml-sort.lastLine"]) }) }) -suite("Test CommentUtil - isLineComment()", () => { +suite("Test CommentProcessor - isLineComment()", () => { test("when input is '# comment' should return true", () => { - equal(CommentUtil.isLineComment("# comment"), true) + equal(CommentProcessor.isLineComment("# comment"), true) }) test("when input is ' # comment' should return true", () => { - equal(CommentUtil.isLineComment(" # comment"), true) + equal(CommentProcessor.isLineComment(" # comment"), true) }) test("when input is 'text # comment' should return false", () => { - equal(CommentUtil.isLineComment("text # comment"), false) + equal(CommentProcessor.isLineComment("text # comment"), false) }) test("when input is 'text' should return false", () => { - equal(CommentUtil.isLineComment("text"), false) + equal(CommentProcessor.isLineComment("text"), false) }) test("when input is an empty string should return false", () => { - equal(CommentUtil.isLineComment(""), false) + equal(CommentProcessor.isLineComment(""), false) }) }) -suite("Test CommentUtil - addLineToComments()", () => { +suite("Test CommentProcessor - addLineToComments()", () => { test("should add comments to array 'comments'", () => { const text = '#foo\n' + @@ -51,36 +51,36 @@ suite("Test CommentUtil - addLineToComments()", () => { 'lorem ipsum\n' + 'dolor sit\n' + '#foo' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - equal(commentutil.lines.length, 5) + equal(commentprocessor.lines.length, 5) - commentutil.addLineToComments(0) + commentprocessor.addLineToComments(0) const expected: string[][] = [] expected.push(["#foo", "#bar"]) - deepStrictEqual(commentutil.comments, expected) + deepStrictEqual(commentprocessor.comments, expected) - commentutil.addLineToComments(1) + commentprocessor.addLineToComments(1) expected.push(["#bar", "lorem ipsum"]) - deepStrictEqual(commentutil.comments, expected) + deepStrictEqual(commentprocessor.comments, expected) - commentutil.addLineToComments(4) + commentprocessor.addLineToComments(4) expected.push(["#foo", "vscode-yaml-sort.lastLine"]) - deepStrictEqual(commentutil.comments, expected) + deepStrictEqual(commentprocessor.comments, expected) }) }) -suite("Test CommentUtil - applyComments()", () => { +suite("Test CommentProcessor - applyComments()", () => { test("should apply all comments to a text", () => { const text = "lorem ipsum\n" + "dolor sit\n" + "amet, consetetur" - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - commentutil.comments.push(["#foo", "#bar"]) - commentutil.comments.push(["#bar", "lorem ipsum"]) - commentutil.comments.push(["#foo", "vscode-yaml-sort.lastLine"]) + commentprocessor.comments.push(["#foo", "#bar"]) + commentprocessor.comments.push(["#bar", "lorem ipsum"]) + commentprocessor.comments.push(["#foo", "vscode-yaml-sort.lastLine"]) const expected = "#foo\n" + @@ -90,36 +90,36 @@ suite("Test CommentUtil - applyComments()", () => { "amet, consetetur\n" + "#foo" - equal(commentutil.applyComments(text), expected) + equal(commentprocessor.applyComments(text), expected) }) }) -suite("Test CommentUtil - reverseComments()", () => { +suite("Test CommentProcessor - reverseComments()", () => { test("should reverse entrys in comments", () => { - const commentutil = new CommentUtil("") - commentutil.comments.push(["key1", "value1"]) - commentutil.comments.push(["key2", "value2"]) - commentutil.comments.push(["key3", "value3"]) + const commentprocessor = new CommentProcessor("") + commentprocessor.comments.push(["key1", "value1"]) + commentprocessor.comments.push(["key2", "value2"]) + commentprocessor.comments.push(["key3", "value3"]) const expected: string[][] = [] expected.push(["key3", "value3"]) expected.push(["key2", "value2"]) expected.push(["key1", "value1"]) - commentutil.reverseComments() + commentprocessor.reverseComments() - deepStrictEqual(commentutil.comments, expected) + deepStrictEqual(commentprocessor.comments, expected) }) }) -suite("Test CommentUtil - applyComment()", () => { +suite("Test CommentProcessor - applyComment()", () => { test("should apply a comment at correct position", () => { const text = "lorem ipsum\n" + "dolor sit\n" + "amet, consetetur" - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - commentutil.applyComment(["#foo", "dolor sit"]) + commentprocessor.applyComment(["#foo", "dolor sit"]) const expected = "lorem ipsum\n" + @@ -127,90 +127,90 @@ suite("Test CommentUtil - applyComment()", () => { "dolor sit\n" + "amet, consetetur" - equal(commentutil.text, expected) + equal(commentprocessor.text, expected) }) }) -suite("Test CommentUtil - append()", () => { +suite("Test CommentProcessor - append()", () => { test("should append newline and text to a given text", () => { const text = "lorem ipsum" - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - commentutil.append("# comment") + commentprocessor.append("# comment") const expected = "lorem ipsum\n" + "# comment" - equal(commentutil.text, expected) + equal(commentprocessor.text, expected) }) }) -suite("Test CommentUtil - insert()", () => { +suite("Test CommentProcessor - insert()", () => { test("when comment is found should insert", () => { const text = 'lorem ipsum\n' + 'dolor sit\n' + 'amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) const comment = ['#foo', 'dolor sit'] - commentutil.insert(comment) + commentprocessor.insert(comment) const expected = 'lorem ipsum\n' + '#foo\n' + 'dolor sit\n' + 'amet, consetetur' - equal(commentutil.text, expected) + equal(commentprocessor.text, expected) }) test("when comment is not found should do nothing", () => { const text = 'lorem ipsum\n' + 'dolor sit\n' + 'amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) const comment = ['#foo', 'do not find'] - commentutil.insert(comment) + commentprocessor.insert(comment) const expected = 'lorem ipsum\n' + 'dolor sit\n' + 'amet, consetetur' - equal(commentutil.text, expected) + equal(commentprocessor.text, expected) }) }) -suite("Test CommentUtil - getIndexOfString()", () => { +suite("Test CommentProcessor - getIndexOfString()", () => { test("should return index of line", () => { const line = 'dolor sit' const text = 'lorem ipsum\n' + `${line}\n` + 'amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - equal(commentutil.getIndexOfString(line), 12) - equal(commentutil.getIndexOfString("vscode-yaml-sort.lastLine"), commentutil.text.length) + equal(commentprocessor.getIndexOfString(line), 12) + equal(commentprocessor.getIndexOfString("vscode-yaml-sort.lastLine"), commentprocessor.text.length) }) }) -suite("Test CommentUtil - search()", () => { +suite("Test CommentProcessor - search()", () => { test("should return last index of text", () => { const text = 'foo: lorem ipsum\n' + 'bar: dolor sit\n' + 'baz: amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - equal(commentutil.search('foo: lorem ipsum'), 0) - equal(commentutil.search(' bar: dolor sit '), 17) - equal(commentutil.search('baz: "amet, consetetur"'), 32) + equal(commentprocessor.search('foo: lorem ipsum'), 0) + equal(commentprocessor.search(' bar: dolor sit '), 17) + equal(commentprocessor.search('baz: "amet, consetetur"'), 32) }) }) -suite("Test CommentUtil - searchExactMatch()", () => { +suite("Test CommentProcessor - searchExactMatch()", () => { test("should return last index of text", () => { const line = 'bar: dolor sit' const text = @@ -218,46 +218,46 @@ suite("Test CommentUtil - searchExactMatch()", () => { `${line}\n` + `${line}\n` + 'baz: amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - equal(commentutil.searchExactMatch(line), 32) + equal(commentprocessor.searchExactMatch(line), 32) }) }) -suite("Test CommentUtil - searchFuzzyForTrimmedText()", () => { +suite("Test CommentProcessor - searchFuzzyForTrimmedText()", () => { test("should return last index of keyword", () => { const line = ' bar: dolor sit ' const text = 'foo: lorem ipsum\n' + `${line.trim()}\n` + 'baz: amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - equal(commentutil.searchFuzzyForTrimmedText(line), 17) + equal(commentprocessor.searchFuzzyForTrimmedText(line), 17) }) }) -suite("Test CommentUtil - searchFuzzyForKeyword()", () => { +suite("Test CommentProcessor - searchFuzzyForKeyword()", () => { test("should return last index of keyword", () => { const line = 'bar: dolor sit' const text = 'foo: lorem ipsum\n' + `${line}\n` + 'baz: amet, consetetur' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - equal(commentutil.searchFuzzyForKeyword(line), 17) + equal(commentprocessor.searchFuzzyForKeyword(line), 17) }) }) -suite("Test CommentUtil - isCommentFound()", () => { +suite("Test CommentProcessor - isCommentFound()", () => { test("when index is -1 should return true", () => { - equal(CommentUtil.isCommentFound(-1), false) - equal(CommentUtil.isCommentFound(0), true) + equal(CommentProcessor.isCommentFound(-1), false) + equal(CommentProcessor.isCommentFound(0), true) }) }) -suite("Test CommentUtil - Issue 45", () => { +suite("Test CommentProcessor - Issue 45", () => { // https://github.com/pascalre/vscode-yaml-sort/issues/45#issuecomment-1329161613 test("should fuzzy find comments", () => { const text = @@ -272,9 +272,9 @@ suite("Test CommentUtil - Issue 45", () => { ' jobSize: 20\n' + ' schema: test\n' + ' #end comment' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - commentutil.findComments() + commentprocessor.findComments() const textWithoutComments = "params:\n" + " chunkSize: 1000\n" + @@ -284,7 +284,7 @@ suite("Test CommentUtil - Issue 45", () => { " logon: 'test'\n" + " schema: 'test'\n" + " variableMetadata: 'env\\config_dev.yaml'" - commentutil.applyComments(textWithoutComments) + commentprocessor.applyComments(textWithoutComments) const expected = "#begin comment\n" + @@ -299,7 +299,7 @@ suite("Test CommentUtil - Issue 45", () => { " variableMetadata: 'env\\config_dev.yaml'\n" + " #end comment" - equal(commentutil.text, expected) + equal(commentprocessor.text, expected) }) @@ -307,16 +307,16 @@ suite("Test CommentUtil - Issue 45", () => { const text = 'schema: test\n' + '#end comment\n' - const commentutil = new CommentUtil(text) + const commentprocessor = new CommentProcessor(text) - commentutil.findComments() + commentprocessor.findComments() const textWithoutComments = 'schema: test' - commentutil.applyComments(textWithoutComments) + commentprocessor.applyComments(textWithoutComments) const expected = 'schema: test\n' + '#end comment' - equal(commentutil.text, expected) + equal(commentprocessor.text, expected) }) }) \ No newline at end of file diff --git a/src/test/suite/processor/helm-processor.test.ts b/src/test/suite/processor/helm-processor.test.ts new file mode 100644 index 0000000..d55ca37 --- /dev/null +++ b/src/test/suite/processor/helm-processor.test.ts @@ -0,0 +1,123 @@ +import { equal, match } from "assert" +import { HelmProcessor } from "../../../processor/helm-processor" + +suite("Test HelmProcessor - preprocess()", () => { + test("should replace all helm values", () => { + const text = + "apiVersion: keycloak.org/v1alpha1\n" + + "kind: KeycloakClient\n" + + "metadata:\n" + + "labels:\n" + + "app: clientsso\n" + + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" + const helmprocessor = new HelmProcessor(text) + + helmprocessor.preprocess() + + equal(helmprocessor.store.size, 2) + equal(helmprocessor.store.get("vscode-yaml-sort.helm.0"), "{{ .Values.ui.environment.keycloakClientId }}") + equal(helmprocessor.store.get("vscode-yaml-sort.helm.1"), "{{ required 'Keycloak namespace required' .Values.keycloak.namespace }}") + }) +}) + +suite("Test HelmProcessor - findHelmValues()", () => { + test("should return all matches of helm values", () => { + const text = + "apiVersion: keycloak.org/v1alpha1\n" + + "kind: KeycloakClient\n" + + "metadata:\n" + + "labels:\n" + + "app: clientsso\n" + + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" + const helmprocessor = new HelmProcessor(text) + const helmValues = helmprocessor.findHelmValues() + + equal(helmValues?.length, 2) + equal(helmValues?.at(0), "{{ .Values.ui.environment.keycloakClientId }}") + equal(helmValues?.at(1), "{{ required 'Keycloak namespace required' .Values.keycloak.namespace }}") + }) +}) + +suite("Test HelmProcessor - replaceValueWithSubstitue()", () => { + test("should replace first match", () => { + const text = + "foo: bar\n" + + "bar: baz\n" + + "baz: foo2" + const helmprocessor = new HelmProcessor(text) + + equal(helmprocessor.store.size, 0) + + helmprocessor.replaceValueWithSubstitue("bar") + + equal(helmprocessor.store.size, 1) + match(helmprocessor.text, /foo: vscode-yaml-sort.helm.0/) + + helmprocessor.replaceValueWithSubstitue("baz") + helmprocessor.replaceValueWithSubstitue("foo2") + + equal(helmprocessor.store.size, 3) + + const expected = + "foo: vscode-yaml-sort.helm.0\n" + + "bar: vscode-yaml-sort.helm.1\n" + + "baz: vscode-yaml-sort.helm.2" + + equal(helmprocessor.text, expected) + }) + test("should keep trailing newline character", () => { + const text = "foo: bar\n" + const helmprocessor = new HelmProcessor(text) + + helmprocessor.replaceValueWithSubstitue("bar") + + const expected = "foo: vscode-yaml-sort.helm.0\n" + + equal(helmprocessor.text, expected) + }) +}) + +suite("Test HelmProcessor - postprocess()", () => { + test("text should be the same before preprocessing and after postprocessing", () => { + const text = + "apiVersion: keycloak.org/v1alpha1\n" + + "kind: KeycloakClient\n" + + "metadata:\n" + + "labels:\n" + + "app: clientsso\n" + + "name: {{ .Values.ui.environment.keycloakClientId }}\n" + + "namespace: {{ required 'Keycloak namespace required' .Values.keycloak.namespace }}\n" + const helmprocessor = new HelmProcessor(text) + + helmprocessor.preprocess() + + match(helmprocessor.text, /vscode-yaml-sort.helm/) + + helmprocessor.postprocess() + + equal(helmprocessor.text, text) + }) +}) + +suite("Test HelmProcessor - replaceSubstituteWithValue()", () => { + test("should replace substitute with optional quotation marks", () => { + const text = + "key1: 'vscode-yaml-sort.helm.0'\n" + + "key2: \"vscode-yaml-sort.helm.1\"\n" + + "key3: vscode-yaml-sort.helm.2" + const helmprocessor = new HelmProcessor(text) + + helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.0", "test0") + helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.1", "test1") + helmprocessor.replaceSubstituteWithValue("vscode-yaml-sort.helm.2", "test2") + + const expected = + "key1: test0\n" + + "key2: test1\n" + + "key3: test2" + + equal(helmprocessor.text, expected) + }) +}) diff --git a/src/test/suite/processor/processor.test.ts b/src/test/suite/processor/processor.test.ts new file mode 100644 index 0000000..6bee873 --- /dev/null +++ b/src/test/suite/processor/processor.test.ts @@ -0,0 +1,23 @@ +import { equal, notEqual } from "assert" +import { Processor } from "../../../processor/processor" + +suite("Test Processor - preprocess()", () => { + test("text should be the same after pre- and postprocessing", () => { + const text = + 'foo: {{ .Value }}\n' + + '# comment\n' + + 'bar: baz' + const processor = new Processor(text) + + processor.preprocess() + + notEqual(processor.text, text) + + const text2 = + 'foo: {{ .Value }}\n' + + 'bar: baz' + processor.postprocess(text2) + + equal(processor.text, text) + }) +}) diff --git a/src/util/yaml-util.ts b/src/util/yaml-util.ts index 5c97fc5..4981aaa 100644 --- a/src/util/yaml-util.ts +++ b/src/util/yaml-util.ts @@ -2,7 +2,9 @@ import { addNewLineBeforeKeywordsUpToLevelN, prependWhitespacesOnEachLine, repla import { Settings } from "../settings" import { JsYamlAdapter } from "../adapter/js-yaml-adapter" import { Severity, VsCodeAdapter } from "../adapter/vs-code-adapter" -import { CommentUtil } from "./comment-util" +import { CommentProcessor } from "../processor/comment-processor" +import { HelmProcessor } from "../processor/helm-processor" +import { Processor } from "../processor/processor" export class YamlUtil { settings: Settings @@ -35,9 +37,12 @@ export class YamlUtil { sortYaml(unsortedYaml: string, customSort = 0): string | null { try { - const unsortedYamlWithoutTabs = replaceTabsWithSpaces(unsortedYaml, this.settings.getIndent()) - const commentutil = new CommentUtil(unsortedYamlWithoutTabs) - commentutil.findComments() + let unsortedYamlWithoutTabs = replaceTabsWithSpaces(unsortedYaml, this.settings.getIndent()) + + const processor = new Processor(unsortedYamlWithoutTabs) + processor.preprocess() + unsortedYamlWithoutTabs = processor.text + // eslint-disable-next-line @typescript-eslint/no-explicit-any const doc = this.jsyamladapter.load(unsortedYamlWithoutTabs) as any let sortedYaml = "" @@ -67,7 +72,8 @@ export class YamlUtil { sortedYaml = addNewLineBeforeKeywordsUpToLevelN(this.settings.getEmptyLinesUntilLevel(), this.settings.getIndent(), sortedYaml) } - sortedYaml = commentutil.applyComments(sortedYaml) + processor.postprocess(sortedYaml) + sortedYaml = processor.text return sortedYaml } catch (e) { @@ -85,16 +91,15 @@ export class YamlUtil { */ formatYaml(yaml: string, useLeadingDashes: boolean): string | null { try { - const commentutil = new CommentUtil(yaml) - commentutil.findComments() - let doc = new JsYamlAdapter().dumpYaml(this.jsyamladapter.load(yaml) as string, false, 0) - doc = commentutil.applyComments(doc) + const processor = new Processor(yaml) + processor.preprocess() + let doc = new JsYamlAdapter().dumpYaml(this.jsyamladapter.load(processor.text) as string, false, 0) + processor.postprocess(doc) + doc = processor.text if (useLeadingDashes) { doc = `---\n${doc}` } - if (this.settings.getNotifySuccess()) { - new VsCodeAdapter().showMessage(Severity.INFO, "Yaml formatted successfully") - } + new VsCodeAdapter().showMessage(Severity.INFO, "Yaml formatted successfully") return doc } catch (e) { if (e instanceof Error) {