/
stringifyString.ts
342 lines (329 loc) · 10.2 KB
/
stringifyString.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { Scalar } from '../nodes/Scalar.js'
import {
foldFlowLines,
FoldOptions,
FOLD_BLOCK,
FOLD_FLOW,
FOLD_QUOTED
} from './foldFlowLines.js'
import type { StringifyContext } from './stringify.js'
interface StringifyScalar extends Scalar {
value: string
}
const getFoldOptions = (ctx: StringifyContext): FoldOptions => ({
indentAtStart: ctx.indentAtStart,
lineWidth: ctx.options.lineWidth,
minContentWidth: ctx.options.minContentWidth
})
// Also checks for lines starting with %, as parsing the output as YAML 1.1 will
// presume that's starting a new document.
const containsDocumentMarker = (str: string) => /^(%|---|\.\.\.)/m.test(str)
function lineLengthOverLimit(
str: string,
lineWidth: number,
indentLength: number
) {
if (!lineWidth || lineWidth < 0) return false
const limit = lineWidth - indentLength
const strLen = str.length
if (strLen <= limit) return false
for (let i = 0, start = 0; i < strLen; ++i) {
if (str[i] === '\n') {
if (i - start > limit) return true
start = i + 1
if (strLen - start <= limit) return false
}
}
return true
}
function doubleQuotedString(value: string, ctx: StringifyContext) {
const json = JSON.stringify(value)
if (ctx.options.doubleQuotedAsJSON) return json
const { implicitKey } = ctx
const minMultiLineLength = ctx.options.doubleQuotedMinMultiLineLength
const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '')
let str = ''
let start = 0
for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
// space before newline needs to be escaped to not be folded
str += json.slice(start, i) + '\\ '
i += 1
start = i
ch = '\\'
}
if (ch === '\\')
switch (json[i + 1]) {
case 'u':
{
str += json.slice(start, i)
const code = json.substr(i + 2, 4)
switch (code) {
case '0000':
str += '\\0'
break
case '0007':
str += '\\a'
break
case '000b':
str += '\\v'
break
case '001b':
str += '\\e'
break
case '0085':
str += '\\N'
break
case '00a0':
str += '\\_'
break
case '2028':
str += '\\L'
break
case '2029':
str += '\\P'
break
default:
if (code.substr(0, 2) === '00') str += '\\x' + code.substr(2)
else str += json.substr(i, 6)
}
i += 5
start = i + 1
}
break
case 'n':
if (
implicitKey ||
json[i + 2] === '"' ||
json.length < minMultiLineLength
) {
i += 1
} else {
// folding will eat first newline
str += json.slice(start, i) + '\n\n'
while (
json[i + 2] === '\\' &&
json[i + 3] === 'n' &&
json[i + 4] !== '"'
) {
str += '\n'
i += 2
}
str += indent
// space after newline needs to be escaped to not be folded
if (json[i + 2] === ' ') str += '\\'
i += 1
start = i + 1
}
break
default:
i += 1
}
}
str = start ? str + json.slice(start) : json
return implicitKey
? str
: foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx))
}
function singleQuotedString(value: string, ctx: StringifyContext) {
if (ctx.implicitKey) {
if (/\n/.test(value)) return doubleQuotedString(value, ctx)
} else {
// single quoted string can't have leading or trailing whitespace around newline
if (/[ \t]\n|\n[ \t]/.test(value)) return doubleQuotedString(value, ctx)
}
const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '')
const res =
"'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"
return ctx.implicitKey
? res
: foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx))
}
function blockString(
{ comment, type, value }: StringifyScalar,
ctx: StringifyContext,
onComment?: () => void,
onChompKeep?: () => void
) {
// 1. Block can't end in whitespace unless the last line is non-empty.
// 2. Strings consisting of only whitespace are best rendered explicitly.
if (/\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
return doubleQuotedString(value, ctx)
}
const indent =
ctx.indent ||
(ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '')
const indentSize = indent ? '2' : '1' // root is at -1
const literal =
type === Scalar.BLOCK_FOLDED
? false
: type === Scalar.BLOCK_LITERAL
? true
: !lineLengthOverLimit(value, ctx.options.lineWidth, indent.length)
let header = literal ? '|' : '>'
if (!value) return header + '\n'
let wsStart = ''
let wsEnd = ''
value = value
.replace(/[\n\t ]*$/, ws => {
const n = ws.indexOf('\n')
if (n === -1) {
header += '-' // strip
} else if (value === ws || n !== ws.length - 1) {
header += '+' // keep
if (onChompKeep) onChompKeep()
}
wsEnd = ws.replace(/\n$/, '')
return ''
})
.replace(/^[\n ]*/, ws => {
if (ws.indexOf(' ') !== -1) header += indentSize
const m = ws.match(/ +$/)
if (m) {
wsStart = ws.slice(0, -m[0].length)
return m[0]
} else {
wsStart = ws
return ''
}
})
if (wsEnd) wsEnd = wsEnd.replace(/\n+(?!\n|$)/g, `$&${indent}`)
if (wsStart) wsStart = wsStart.replace(/\n+/g, `$&${indent}`)
if (comment) {
header += ' #' + comment.replace(/ ?[\r\n]+/g, ' ')
if (onComment) onComment()
}
if (!value) return `${header}${indentSize}\n${indent}${wsEnd}`
if (literal) {
value = value.replace(/\n+/g, `$&${indent}`)
return `${header}\n${indent}${wsStart}${value}${wsEnd}`
}
value = value
.replace(/\n+/g, '\n$&')
.replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
// ^ ind.line ^ empty ^ capture next empty lines only at end of indent
.replace(/\n+/g, `$&${indent}`)
const body = foldFlowLines(
`${wsStart}${value}${wsEnd}`,
indent,
FOLD_BLOCK,
getFoldOptions(ctx)
)
return `${header}\n${indent}${body}`
}
function plainString(
item: StringifyScalar,
ctx: StringifyContext,
onComment?: () => void,
onChompKeep?: () => void
) {
const { type, value } = item
const { actualString, implicitKey, indent, inFlow } = ctx
if (
(implicitKey && /[\n[\]{},]/.test(value)) ||
(inFlow && /[[\]{},]/.test(value))
) {
return doubleQuotedString(value, ctx)
}
if (
!value ||
/^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(
value
)
) {
const hasDouble = value.indexOf('"') !== -1
const hasSingle = value.indexOf("'") !== -1
let quotedString
if (hasDouble && !hasSingle) {
quotedString = singleQuotedString
} else if (hasSingle && !hasDouble) {
quotedString = doubleQuotedString
} else if (ctx.options.singleQuote) {
quotedString = singleQuotedString
} else {
quotedString = doubleQuotedString
}
// not allowed:
// - empty string, '-' or '?'
// - start with an indicator character (except [?:-]) or /[?-] /
// - '\n ', ': ' or ' \n' anywhere
// - '#' not preceded by a non-space char
// - end with ' ' or ':'
return implicitKey || inFlow || value.indexOf('\n') === -1
? quotedString(value, ctx)
: blockString(item, ctx, onComment, onChompKeep)
}
if (
!implicitKey &&
!inFlow &&
type !== Scalar.PLAIN &&
value.indexOf('\n') !== -1
) {
// Where allowed & type not set explicitly, prefer block style for multiline strings
return blockString(item, ctx, onComment, onChompKeep)
}
if (indent === '' && containsDocumentMarker(value)) {
ctx.forceBlockIndent = true
return blockString(item, ctx, onComment, onChompKeep)
}
const str = value.replace(/\n+/g, `$&\n${indent}`)
// Verify that output will be parsed as a string, as e.g. plain numbers and
// booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
// and others in v1.1.
if (actualString) {
for (const tag of ctx.doc.schema.tags) {
if (
tag.default &&
tag.tag !== 'tag:yaml.org,2002:str' &&
tag.test?.test(str)
)
return doubleQuotedString(value, ctx)
}
}
return implicitKey
? str
: foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx))
}
export function stringifyString(
item: Scalar,
ctx: StringifyContext,
onComment?: () => void,
onChompKeep?: () => void
) {
const { implicitKey, inFlow } = ctx
const ss: Scalar<string> =
typeof item.value === 'string'
? (item as Scalar<string>)
: Object.assign({}, item, { value: String(item.value) })
let { type } = item
if (type !== Scalar.QUOTE_DOUBLE) {
// force double quotes on control characters & unpaired surrogates
if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(ss.value))
type = Scalar.QUOTE_DOUBLE
}
const _stringify = (_type: Scalar.Type | undefined) => {
switch (_type) {
case Scalar.BLOCK_FOLDED:
case Scalar.BLOCK_LITERAL:
return implicitKey || inFlow
? doubleQuotedString(ss.value, ctx) // blocks are not valid inside flow containers
: blockString(ss, ctx, onComment, onChompKeep)
case Scalar.QUOTE_DOUBLE:
return doubleQuotedString(ss.value, ctx)
case Scalar.QUOTE_SINGLE:
return singleQuotedString(ss.value, ctx)
case Scalar.PLAIN:
return plainString(ss, ctx, onComment, onChompKeep)
default:
return null
}
}
let res = _stringify(type)
if (res === null) {
const { defaultKeyType, defaultStringType } = ctx.options
const t = (implicitKey && defaultKeyType) || defaultStringType
res = _stringify(t)
if (res === null) throw new Error(`Unsupported default string type ${t}`)
}
return res
}