Skip to content

Commit

Permalink
Fix noArrayIndent on nested arrays (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximgavrilov committed Dec 1, 2020
1 parent d8ba402 commit fe72979
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/js-yaml/dumper.js
Expand Up @@ -539,7 +539,7 @@ function writeBlockSequence(state, level, object, compact) {

for (index = 0, length = object.length; index < length; index += 1) {
// Write only valid elements.
if (writeNode(state, level + 1, object[index], true, true)) {
if (writeNode(state, level + 1, object[index], true, true, false, true)) {
if (!compact || index !== 0) {
_result += generateNextLine(state, level);
}
Expand Down Expand Up @@ -712,7 +712,7 @@ function detectType(state, object, explicit) {
// Serializes `object` and writes it to global `result`.
// Returns true on success, or false on invalid object.
//
function writeNode(state, level, object, block, compact, iskey) {
function writeNode(state, level, object, block, compact, iskey, isblockseq) {
state.tag = null;
state.dump = object;

Expand Down Expand Up @@ -758,14 +758,17 @@ function writeNode(state, level, object, block, compact, iskey) {
}
}
} else if (type === '[object Array]') {
var arrayLevel = (state.noArrayIndent && (level > 0)) ? level - 1 : level;
if (block && (state.dump.length !== 0)) {
writeBlockSequence(state, arrayLevel, state.dump, compact);
if (state.noArrayIndent && !isblockseq && level > 0) {
writeBlockSequence(state, level - 1, state.dump, compact);
} else {
writeBlockSequence(state, level, state.dump, compact);
}
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + state.dump;
}
} else {
writeFlowSequence(state, arrayLevel, state.dump);
writeFlowSequence(state, level, state.dump);
if (duplicate) {
state.dump = '&ref_' + duplicateIndex + ' ' + state.dump;
}
Expand Down
6 changes: 6 additions & 0 deletions test/issues/0432.js
Expand Up @@ -16,3 +16,9 @@ test('should not indent arrays an extra level when disabled', function () {
var expected = 'array:\n- a\n- b\n';
assert.strictEqual(output, expected);
});

test('should always indent nested arrays', function () {
var output = yaml.safeDump({ array: [ 'a', [ 'b', 'c' ], 'd' ] }, { noArrayIndent: true });
var expected = 'array:\n- a\n- - b\n - c\n- d\n';
assert.strictEqual(output, expected);
});

0 comments on commit fe72979

Please sign in to comment.