From 231e2e2b45613347101d95efa4a7da1b5676e1ff Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Wed, 4 Sep 2019 12:42:51 +0200 Subject: [PATCH 1/3] Added flag and code to forcestyleliteral --- lib/js-yaml/dumper.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index 86f34794..665aa1e9 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -116,7 +116,7 @@ function State(options) { this.noRefs = options['noRefs'] || false; this.noCompatMode = options['noCompatMode'] || false; this.condenseFlow = options['condenseFlow'] || false; - + this.forceStyleLiteral = options['forceStyleLiteral'] || false; this.implicitTypes = this.schema.compiledImplicit; this.explicitTypes = this.schema.compiledExplicit; @@ -254,7 +254,10 @@ var STYLE_PLAIN = 1, // STYLE_PLAIN or STYLE_SINGLE => no \n are in the string. // STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1). // STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1). -function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType) { +function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType, forceStyleLiteral) { + + + var i; var char; var hasLineBreak = false; @@ -270,7 +273,11 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te for (i = 0; i < string.length; i++) { char = string.charCodeAt(i); if (!isPrintable(char)) { - return STYLE_DOUBLE; + if (forceStyleLiteral) { + return STYLE_LITERAL + } else { + return STYLE_DOUBLE; + } } plain = plain && isPlainSafe(char); } @@ -289,7 +296,11 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te previousLineBreak = i; } } else if (!isPrintable(char)) { - return STYLE_DOUBLE; + if (forceStyleLiteral) { + return STYLE_LITERAL + } else { + return STYLE_DOUBLE; + } } plain = plain && isPlainSafe(char); } @@ -351,7 +362,7 @@ function writeScalar(state, string, level, iskey) { return testImplicitResolving(state, string); } - switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity)) { + switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity, state.forceStyleLiteral)) { case STYLE_PLAIN: return string; case STYLE_SINGLE: From 9980e7193020abbf3ae2e8581a5b322fd5f72bb2 Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Wed, 4 Sep 2019 13:30:45 +0200 Subject: [PATCH 2/3] Fixed lint errors --- lib/js-yaml/dumper.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index 665aa1e9..17181cb4 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -255,9 +255,6 @@ var STYLE_PLAIN = 1, // STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1). // STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1). function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType, forceStyleLiteral) { - - - var i; var char; var hasLineBreak = false; @@ -274,10 +271,9 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te char = string.charCodeAt(i); if (!isPrintable(char)) { if (forceStyleLiteral) { - return STYLE_LITERAL - } else { - return STYLE_DOUBLE; - } + return STYLE_LITERAL; + } + return STYLE_DOUBLE; } plain = plain && isPlainSafe(char); } @@ -298,9 +294,8 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te } else if (!isPrintable(char)) { if (forceStyleLiteral) { return STYLE_LITERAL - } else { - return STYLE_DOUBLE; - } + } + return STYLE_DOUBLE; } plain = plain && isPlainSafe(char); } @@ -362,7 +357,8 @@ function writeScalar(state, string, level, iskey) { return testImplicitResolving(state, string); } - switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity, state.forceStyleLiteral)) { + switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity, + state.forceStyleLiteral)) { case STYLE_PLAIN: return string; case STYLE_SINGLE: From 45c26a85f83810f9f401df9b8518145b86df36e2 Mon Sep 17 00:00:00 2001 From: Birger Moell Date: Wed, 4 Sep 2019 13:35:40 +0200 Subject: [PATCH 3/3] More lint fixes --- lib/js-yaml/dumper.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/js-yaml/dumper.js b/lib/js-yaml/dumper.js index 17181cb4..41119d48 100644 --- a/lib/js-yaml/dumper.js +++ b/lib/js-yaml/dumper.js @@ -272,7 +272,7 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te if (!isPrintable(char)) { if (forceStyleLiteral) { return STYLE_LITERAL; - } + } return STYLE_DOUBLE; } plain = plain && isPlainSafe(char); @@ -293,8 +293,8 @@ function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, te } } else if (!isPrintable(char)) { if (forceStyleLiteral) { - return STYLE_LITERAL - } + return STYLE_LITERAL; + } return STYLE_DOUBLE; } plain = plain && isPlainSafe(char); @@ -357,7 +357,7 @@ function writeScalar(state, string, level, iskey) { return testImplicitResolving(state, string); } - switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity, + switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth, testAmbiguity, state.forceStyleLiteral)) { case STYLE_PLAIN: return string;