(
node: Node,
options: StringifyOptions = {},
indentLevel: number = 0,
offset: number = 0, // the starting offset of the current node in the stringify text.
boundOffset: number = 0, // the starting offset of the current node's bound in the stringify text.
genTabs: ReturnType<typeof getGenTabsFn> = getGenTabsFn(options.tabWidth || 2),
)
| 228 | |
| 229 | // TODO support pretty format |
| 230 | stringifyNode( |
| 231 | node: Node, |
| 232 | options: StringifyOptions = {}, |
| 233 | indentLevel: number = 0, |
| 234 | offset: number = 0, // the starting offset of the current node in the stringify text. |
| 235 | boundOffset: number = 0, // the starting offset of the current node's bound in the stringify text. |
| 236 | genTabs: ReturnType<typeof getGenTabsFn> = getGenTabsFn(options.tabWidth || 2), |
| 237 | ): string { |
| 238 | if (!isIterable(node)) { |
| 239 | const stringified = getRawValue(node)!; |
| 240 | |
| 241 | if (!options.pure) { |
| 242 | node.length = stringified.length; |
| 243 | node.offset = offset; |
| 244 | node.boundOffset = boundOffset ?? node.offset; |
| 245 | computeAndSetBoundLength(node); |
| 246 | } |
| 247 | |
| 248 | return stringified; |
| 249 | } |
| 250 | |
| 251 | // for array or object, the bound width is equal to the node width |
| 252 | if (!options.pure) { |
| 253 | node.boundOffset = boundOffset ?? 0; |
| 254 | node.offset = offset; |
| 255 | } |
| 256 | |
| 257 | const isFormat = options?.format === true; |
| 258 | const isObject = node.type === "object"; |
| 259 | let stringified = isObject ? "{" : "["; |
| 260 | |
| 261 | const keys = getChildrenKeys(node); |
| 262 | |
| 263 | if (isObject) { |
| 264 | if (options?.sort === "asc") { |
| 265 | keys.sort(); |
| 266 | } else if (options?.sort === "desc") { |
| 267 | keys.sort().reverse(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | for (let i = 0; i < keys.length; i++) { |
| 272 | const key = keys[i]; |
| 273 | const child = this.getChild(node, key)!; |
| 274 | |
| 275 | if (isFormat) { |
| 276 | stringified += "\n" + genTabs(indentLevel + 1); |
| 277 | } |
| 278 | |
| 279 | const childBoundOffset = offset + stringified.length; |
| 280 | if (isObject) { |
| 281 | const keyText = escape(key); |
| 282 | stringified += `"${keyText}":${isFormat ? " " : ""}`; |
| 283 | } |
| 284 | const childOffset = offset + stringified.length; |
| 285 | |
| 286 | stringified += this.stringifyNode(child, options, indentLevel + 1, childOffset, childBoundOffset, genTabs); |
| 287 |
no test coverage detected