Skip to content

Commit b73914b

Browse files
committed
Account for initial indentation when stringifying values (Fixes #133)
1 parent a1996d8 commit b73914b

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

src/schema/Pair.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ export default class Pair extends Node {
117117
value = doc.schema.createNode(value, true)
118118
}
119119
ctx.implicitKey = false
120+
if (!explicitKey && !this.comment && value instanceof Scalar)
121+
ctx.indentAtStart = str.length + 1
120122
chompKeep = false
121123
const valueStr = doc.schema.stringify(
122124
value,

src/schema/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ export default class Schema {
264264
if (item instanceof Pair) return item.toString(ctx, onComment, onChompKeep)
265265
if (!tagObj) tagObj = this.getTagObject(item)
266266
const props = this.stringifyProps(item, tagObj, ctx)
267+
if (props.length > 0)
268+
ctx.indentAtStart = (ctx.indentAtStart || 0) + props.length + 1
267269
const str =
268270
typeof tagObj.stringify === 'function'
269271
? tagObj.stringify(item, ctx, onComment, onChompKeep)

src/stringify.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import foldFlowLines, {
77
} from './foldFlowLines'
88
import { strOptions } from './tags/options'
99

10+
const getFoldOptions = ({ indentAtStart }) =>
11+
indentAtStart
12+
? Object.assign({ indentAtStart }, strOptions.fold)
13+
: strOptions.fold
14+
1015
export function stringifyNumber({ format, minFractionDigits, tag, value }) {
1116
if (!isFinite(value))
1217
return isNaN(value) ? '.nan' : value < 0 ? '-.inf' : '.inf'
@@ -41,7 +46,8 @@ function lineLengthOverLimit(str, limit) {
4146
return true
4247
}
4348

44-
function doubleQuotedString(value, { implicitKey, indent }) {
49+
function doubleQuotedString(value, ctx) {
50+
const { implicitKey, indent } = ctx
4551
const { jsonEncoding, minMultiLineLength } = strOptions.doubleQuoted
4652
const json = JSON.stringify(value)
4753
if (jsonEncoding) return json
@@ -126,7 +132,7 @@ function doubleQuotedString(value, { implicitKey, indent }) {
126132
str = start ? str + json.slice(start) : json
127133
return implicitKey
128134
? str
129-
: foldFlowLines(str, indent, FOLD_QUOTED, strOptions.fold)
135+
: foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx))
130136
}
131137

132138
function singleQuotedString(value, ctx) {
@@ -141,7 +147,7 @@ function singleQuotedString(value, ctx) {
141147
"'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"
142148
return implicitKey
143149
? res
144-
: foldFlowLines(res, indent, FOLD_FLOW, strOptions.fold)
150+
: foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx))
145151
}
146152

147153
function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
@@ -255,7 +261,7 @@ function plainString(item, ctx, onComment, onChompKeep) {
255261
}
256262
const body = implicitKey
257263
? str
258-
: foldFlowLines(str, indent, FOLD_FLOW, strOptions.fold)
264+
: foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx))
259265
if (
260266
comment &&
261267
!inFlow &&

tests/doc/stringify.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { source } from 'common-tags'
22
import YAML from '../../src/index'
33
import Pair from '../../src/schema/Pair'
4+
import { Type } from '../../src/constants'
45
import { stringifyString } from '../../src/stringify'
56
import { strOptions } from '../../src/tags/options'
67

@@ -89,6 +90,43 @@ blah blah blah blah
8990
blah blah blah blah
9091
blah blah\n`)
9192
})
93+
94+
test('long line in map', () => {
95+
const foo = 'fuzz'.repeat(16)
96+
const doc = new YAML.Document()
97+
doc.contents = YAML.createNode({ foo })
98+
for (const node of doc.contents.items) node.value.type = Type.QUOTE_DOUBLE
99+
expect(
100+
String(doc)
101+
.split('\n')
102+
.map(line => line.length)
103+
).toMatchObject([20, 20, 20, 20, 0])
104+
})
105+
106+
test('long line in sequence', () => {
107+
const foo = 'fuzz'.repeat(16)
108+
const doc = new YAML.Document()
109+
doc.contents = YAML.createNode([foo])
110+
for (const node of doc.contents.items) node.type = Type.QUOTE_DOUBLE
111+
expect(
112+
String(doc)
113+
.split('\n')
114+
.map(line => line.length)
115+
).toMatchObject([20, 20, 20, 17, 0])
116+
})
117+
118+
test('long line in sequence in map', () => {
119+
const foo = 'fuzz'.repeat(16)
120+
const doc = new YAML.Document()
121+
doc.contents = YAML.createNode({ foo: [foo] })
122+
const seq = doc.contents.items[0].value
123+
for (const node of seq.items) node.type = Type.QUOTE_DOUBLE
124+
expect(
125+
String(doc)
126+
.split('\n')
127+
.map(line => line.length)
128+
).toMatchObject([4, 20, 20, 20, 20, 10, 0])
129+
})
92130
})
93131

94132
describe('timestamp-like string (YAML 1.1)', () => {

tests/doc/types.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ description:
224224
expect(canonicalStr.substr(0, 5)).toBe('GIF89')
225225
strOptions.fold.lineWidth = 80
226226
expect(String(doc))
227-
.toBe(`canonical: !!binary "R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlp\\
228-
aWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++SH+Dk1hZ\\
229-
GUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG8\\
230-
4BwwEeECcgggoBADs="
227+
.toBe(`canonical: !!binary "R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J\\
228+
+fn5OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/\\
229+
++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLCAgjoEwnuNAFOhpEMTRiggcz4BN\\
230+
JHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
231231
generic: !!binary |-
232232
R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5OTk6enp56enmlp
233233
aWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++f/++SH+Dk1h

0 commit comments

Comments
 (0)