@decimalturn/toml-patch 3.1.1-dev → 3.1.2-dev
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dev/comment-alignment.js +1 -1
- package/dist/dev/comment-ownership.js +1 -1
- package/dist/dev/cst-source.js +12 -2
- package/dist/dev/cst-source.js.map +1 -1
- package/dist/dev/cst.js +1 -1
- package/dist/dev/cursor.js +1 -1
- package/dist/dev/date-format.js +1 -1
- package/dist/dev/decode-utf8.js +1 -1
- package/dist/dev/diff.js +1 -1
- package/dist/dev/escape-preference.js +1 -1
- package/dist/dev/find-by-path.js +1 -1
- package/dist/dev/formatter.js +5 -2
- package/dist/dev/formatter.js.map +1 -1
- package/dist/dev/generate.js +1 -1
- package/dist/dev/inline-comma-space.js +1 -1
- package/dist/dev/inline-format.js +1 -1
- package/dist/dev/inline-layout.js +1 -1
- package/dist/dev/line-ending-backslash.js +1 -1
- package/dist/dev/location.js +1 -1
- package/dist/dev/parse-error.js +1 -1
- package/dist/dev/parse-js.js +8 -4
- package/dist/dev/parse-js.js.map +1 -1
- package/dist/dev/parse-string.js +1 -1
- package/dist/dev/parse-toml.js +47 -10
- package/dist/dev/parse-toml.js.map +1 -1
- package/dist/dev/patch.js +5 -2
- package/dist/dev/patch.js.map +1 -1
- package/dist/dev/to-js.js +1 -1
- package/dist/dev/to-toml.js +308 -3
- package/dist/dev/to-toml.js.map +1 -1
- package/dist/dev/tokenizer.js +1 -1
- package/dist/dev/toml-document.js +7 -2
- package/dist/dev/toml-document.js.map +1 -1
- package/dist/dev/toml-format.js +1 -1
- package/dist/dev/toml-patch.js +14 -4
- package/dist/dev/toml-patch.js.map +1 -1
- package/dist/dev/traverse.js +1 -1
- package/dist/dev/truncate.js +1 -1
- package/dist/dev/update-order.js +1 -1
- package/dist/dev/utils.js +1 -1
- package/dist/dev/writer.js +5 -5
- package/dist/dev/writer.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { hasItems, isComment, isInlineArray, isInlineItem, isInlineTable, isKeyValue, isTable, isTableArray } from "./cst.js";
|
|
3
3
|
import { getSpan } from "./location.js";
|
|
4
4
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { hasItems, isComment, isDocument, isInlineArray, isInlineItem, isInlineTable, isKeyValue, isString, isTable, isTableArray } from "./cst.js";
|
|
3
3
|
import { clonePosition } from "./location.js";
|
|
4
4
|
import { last } from "./utils.js";
|
package/dist/dev/cst-source.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { hasItems, isInlineItem, isKeyValue, isTable, isTableArray } from "./cst.js";
|
|
3
3
|
|
|
4
4
|
//#region src/cst-source.ts
|
|
@@ -9,6 +9,16 @@ const originalChildren = /* @__PURE__ */ new WeakMap();
|
|
|
9
9
|
function positionToOffset(lineStarts, position) {
|
|
10
10
|
return (lineStarts[position.line - 1] ?? lineStarts[lineStarts.length - 1]) + position.column;
|
|
11
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Attaches source positions to every node reachable from the given roots using
|
|
14
|
+
* a single `lineStarts` scan. This prepares a CST parsed without source
|
|
15
|
+
* attachment (the default, since attachment is only needed for patching) for
|
|
16
|
+
* the `range` and node-source lookups used by the writer and stringifier.
|
|
17
|
+
*/
|
|
18
|
+
function attachSources(roots, source) {
|
|
19
|
+
const attach = createSourceAttacher(source);
|
|
20
|
+
for (const root of roots) attach(root);
|
|
21
|
+
}
|
|
12
22
|
function createSourceAttacher(source) {
|
|
13
23
|
const lineStarts = [0];
|
|
14
24
|
for (let index = 0; index < source.length; index++) if (source.charCodeAt(index) === 10) lineStarts.push(index + 1);
|
|
@@ -104,5 +114,5 @@ function originalFirstChildStartedAfterOpener(node) {
|
|
|
104
114
|
}
|
|
105
115
|
|
|
106
116
|
//#endregion
|
|
107
|
-
export { createSourceAttacher, getNodeSource, linkParents, markMutation, markTreeDirty, originalFirstChildStartedAfterOpener, sourceStructureUnchanged };
|
|
117
|
+
export { attachSources, createSourceAttacher, getNodeSource, linkParents, markMutation, markTreeDirty, originalFirstChildStartedAfterOpener, sourceStructureUnchanged };
|
|
108
118
|
//# sourceMappingURL=cst-source.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cst-source.js","names":[],"sources":["../../src/cst-source.ts"],"sourcesContent":["import {\n TreeNode,\n hasItems,\n isInlineItem,\n isKeyValue,\n isTable,\n isTableArray\n} from './cst';\nimport { Position } from './location';\n\nconst nodeSources = new WeakMap<TreeNode, string>();\nconst nodeParents = new WeakMap<TreeNode, TreeNode>();\nconst linkedRoots = new WeakSet<TreeNode>();\nconst originalChildren = new WeakMap<TreeNode, TreeNode[]>();\n\nfunction positionToOffset(lineStarts: number[], position: Position): number {\n return (lineStarts[position.line - 1] ?? lineStarts[lineStarts.length - 1]) + position.column;\n}\n\nexport function attachSource(node: TreeNode, source: string): void {\n createSourceAttacher(source)(node);\n}\n\nexport function createSourceAttacher(source: string): (node: TreeNode) => void {\n const lineStarts = [0];\n for (let index = 0; index < source.length; index++) {\n if (source.charCodeAt(index) === 0x0a) lineStarts.push(index + 1);\n }\n\n const attach = (root: TreeNode): void => {\n const stack: TreeNode[] = [root];\n while (stack.length > 0) {\n const current = stack.pop()!;\n const range = [\n positionToOffset(lineStarts, current.loc.start),\n positionToOffset(lineStarts, current.loc.end)\n ] as const;\n\n Object.defineProperty(current, 'range', {\n configurable: true,\n enumerable: false,\n value: range\n });\n nodeSources.set(current, source);\n // Snapshot: childrenOf returns node.items by reference for some node types,\n // so a later in-place splice would also mutate the stored \"original\" list.\n originalChildren.set(current, childrenOf(current).slice());\n\n if (isKeyValue(current)) {\n stack.push(current.value);\n stack.push(current.key);\n } else if (isInlineItem(current)) {\n stack.push(current.item);\n } else if (isTable(current) || isTableArray(current)) {\n stack.push(current.key);\n const items = current.items as TreeNode[];\n for (let index = items.length - 1; index >= 0; index--) stack.push(items[index]);\n } else if (hasItems(current)) {\n const items = current.items as TreeNode[];\n for (let index = items.length - 1; index >= 0; index--) stack.push(items[index]);\n }\n }\n };\n\n return attach;\n}\n\nexport function getNodeSource(node: TreeNode): string | undefined {\n return nodeSources.get(node);\n}\n\nexport function sourceStructureUnchanged(node: TreeNode): boolean {\n const original = originalChildren.get(node);\n if (!original) return false;\n const current = childrenOf(node);\n return original.length === current.length &&\n original.every((child, index) => child === current[index]);\n}\n\nfunction childrenOf(node: TreeNode): TreeNode[] {\n if (isKeyValue(node)) return [node.key, node.value];\n if (isInlineItem(node)) return [node.item];\n if (isTable(node) || isTableArray(node)) {\n return [node.key, ...(node.items as TreeNode[])];\n }\n return hasItems(node) ? node.items as TreeNode[] : [];\n}\n\nexport function linkParents(root: TreeNode): void {\n if (linkedRoots.has(root)) return;\n\n const stack: TreeNode[] = [root];\n while (stack.length > 0) {\n const parent = stack.pop()!;\n for (const child of childrenOf(parent)) {\n nodeParents.set(child, parent);\n stack.push(child);\n }\n }\n linkedRoots.add(root);\n}\n\nfunction markSubtreeDirty(node: TreeNode): void {\n const stack: TreeNode[] = [node];\n while (stack.length > 0) {\n const current = stack.pop()!;\n setDirty(current);\n for (const child of childrenOf(current)) stack.push(child);\n }\n}\n\nexport function markTreeDirty(node: TreeNode): void {\n markSubtreeDirty(node);\n}\n\nfunction setDirty(node: TreeNode): void {\n Object.defineProperty(node, 'dirty', {\n configurable: true,\n enumerable: false,\n value: true,\n writable: true\n });\n}\n\nexport function markMutation(root: TreeNode, parent: TreeNode, changed?: TreeNode): void {\n linkParents(root);\n if (changed) {\n nodeParents.set(changed, parent);\n markSubtreeDirty(changed);\n }\n\n let current: TreeNode | undefined = parent;\n while (current) {\n setDirty(current);\n current = nodeParents.get(current);\n }\n}\n\nexport function originalFirstChildStartedAfterOpener(node: TreeNode): boolean {\n const original = originalChildren.get(node);\n return !!original?.[0] && original[0].loc.start.line > node.loc.start.line;\n}"],"mappings":";;;;AAUA,MAAM,8BAAc,IAAI,QAA0B;AAClD,MAAM,8BAAc,IAAI,QAA4B;AACpD,MAAM,8BAAc,IAAI,QAAkB;AAC1C,MAAM,mCAAmB,IAAI,QAA8B;AAE3D,SAAS,iBAAiB,YAAsB,UAA4B;CAC1E,QAAQ,WAAW,SAAS,OAAO,MAAM,WAAW,WAAW,SAAS,MAAM,SAAS;AACzF;
|
|
1
|
+
{"version":3,"file":"cst-source.js","names":[],"sources":["../../src/cst-source.ts"],"sourcesContent":["import {\n TreeNode,\n hasItems,\n isInlineItem,\n isKeyValue,\n isTable,\n isTableArray\n} from './cst';\nimport { Position } from './location';\n\nconst nodeSources = new WeakMap<TreeNode, string>();\nconst nodeParents = new WeakMap<TreeNode, TreeNode>();\nconst linkedRoots = new WeakSet<TreeNode>();\nconst originalChildren = new WeakMap<TreeNode, TreeNode[]>();\n\nfunction positionToOffset(lineStarts: number[], position: Position): number {\n return (lineStarts[position.line - 1] ?? lineStarts[lineStarts.length - 1]) + position.column;\n}\n\nexport function attachSource(node: TreeNode, source: string): void {\n createSourceAttacher(source)(node);\n}\n\n/**\n * Attaches source positions to every node reachable from the given roots using\n * a single `lineStarts` scan. This prepares a CST parsed without source\n * attachment (the default, since attachment is only needed for patching) for\n * the `range` and node-source lookups used by the writer and stringifier.\n */\nexport function attachSources(roots: Iterable<TreeNode>, source: string): void {\n const attach = createSourceAttacher(source);\n for (const root of roots) attach(root);\n}\n\nexport function createSourceAttacher(source: string): (node: TreeNode) => void {\n const lineStarts = [0];\n for (let index = 0; index < source.length; index++) {\n if (source.charCodeAt(index) === 0x0a) lineStarts.push(index + 1);\n }\n\n const attach = (root: TreeNode): void => {\n const stack: TreeNode[] = [root];\n while (stack.length > 0) {\n const current = stack.pop()!;\n const range = [\n positionToOffset(lineStarts, current.loc.start),\n positionToOffset(lineStarts, current.loc.end)\n ] as const;\n\n Object.defineProperty(current, 'range', {\n configurable: true,\n enumerable: false,\n value: range\n });\n nodeSources.set(current, source);\n // Snapshot: childrenOf returns node.items by reference for some node types,\n // so a later in-place splice would also mutate the stored \"original\" list.\n originalChildren.set(current, childrenOf(current).slice());\n\n if (isKeyValue(current)) {\n stack.push(current.value);\n stack.push(current.key);\n } else if (isInlineItem(current)) {\n stack.push(current.item);\n } else if (isTable(current) || isTableArray(current)) {\n stack.push(current.key);\n const items = current.items as TreeNode[];\n for (let index = items.length - 1; index >= 0; index--) stack.push(items[index]);\n } else if (hasItems(current)) {\n const items = current.items as TreeNode[];\n for (let index = items.length - 1; index >= 0; index--) stack.push(items[index]);\n }\n }\n };\n\n return attach;\n}\n\nexport function getNodeSource(node: TreeNode): string | undefined {\n return nodeSources.get(node);\n}\n\nexport function sourceStructureUnchanged(node: TreeNode): boolean {\n const original = originalChildren.get(node);\n if (!original) return false;\n const current = childrenOf(node);\n return original.length === current.length &&\n original.every((child, index) => child === current[index]);\n}\n\nfunction childrenOf(node: TreeNode): TreeNode[] {\n if (isKeyValue(node)) return [node.key, node.value];\n if (isInlineItem(node)) return [node.item];\n if (isTable(node) || isTableArray(node)) {\n return [node.key, ...(node.items as TreeNode[])];\n }\n return hasItems(node) ? node.items as TreeNode[] : [];\n}\n\nexport function linkParents(root: TreeNode): void {\n if (linkedRoots.has(root)) return;\n\n const stack: TreeNode[] = [root];\n while (stack.length > 0) {\n const parent = stack.pop()!;\n for (const child of childrenOf(parent)) {\n nodeParents.set(child, parent);\n stack.push(child);\n }\n }\n linkedRoots.add(root);\n}\n\nfunction markSubtreeDirty(node: TreeNode): void {\n const stack: TreeNode[] = [node];\n while (stack.length > 0) {\n const current = stack.pop()!;\n setDirty(current);\n for (const child of childrenOf(current)) stack.push(child);\n }\n}\n\nexport function markTreeDirty(node: TreeNode): void {\n markSubtreeDirty(node);\n}\n\nfunction setDirty(node: TreeNode): void {\n Object.defineProperty(node, 'dirty', {\n configurable: true,\n enumerable: false,\n value: true,\n writable: true\n });\n}\n\nexport function markMutation(root: TreeNode, parent: TreeNode, changed?: TreeNode): void {\n linkParents(root);\n if (changed) {\n nodeParents.set(changed, parent);\n markSubtreeDirty(changed);\n }\n\n let current: TreeNode | undefined = parent;\n while (current) {\n setDirty(current);\n current = nodeParents.get(current);\n }\n}\n\nexport function originalFirstChildStartedAfterOpener(node: TreeNode): boolean {\n const original = originalChildren.get(node);\n return !!original?.[0] && original[0].loc.start.line > node.loc.start.line;\n}"],"mappings":";;;;AAUA,MAAM,8BAAc,IAAI,QAA0B;AAClD,MAAM,8BAAc,IAAI,QAA4B;AACpD,MAAM,8BAAc,IAAI,QAAkB;AAC1C,MAAM,mCAAmB,IAAI,QAA8B;AAE3D,SAAS,iBAAiB,YAAsB,UAA4B;CAC1E,QAAQ,WAAW,SAAS,OAAO,MAAM,WAAW,WAAW,SAAS,MAAM,SAAS;AACzF;;;;;;;AAYA,SAAgB,cAAc,OAA2B,QAAsB;CAC7E,MAAM,SAAS,qBAAqB,MAAM;CAC1C,KAAK,MAAM,QAAQ,OAAO,OAAO,IAAI;AACvC;AAEA,SAAgB,qBAAqB,QAA0C;CAC7E,MAAM,aAAa,CAAC,CAAC;CACrB,KAAK,IAAI,QAAQ,GAAG,QAAQ,OAAO,QAAQ,SACzC,IAAI,OAAO,WAAW,KAAK,MAAM,IAAM,WAAW,KAAK,QAAQ,CAAC;CAGlE,MAAM,UAAU,SAAyB;EACvC,MAAM,QAAoB,CAAC,IAAI;EAC/B,OAAO,MAAM,SAAS,GAAG;GACvB,MAAM,UAAU,MAAM,IAAI;GAC1B,MAAM,QAAQ,CACZ,iBAAiB,YAAY,QAAQ,IAAI,KAAK,GAC9C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turn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
//#region src/cst.ts
|
|
3
3
|
function isDocument(node) {
|
|
4
4
|
return node.type === "Document";
|
package/dist/dev/cursor.js
CHANGED
package/dist/dev/date-format.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
//#region src/date-format.ts
|
|
3
3
|
/**
|
|
4
4
|
* Central module for TOML date/time format handling and custom date classes.
|
package/dist/dev/decode-utf8.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
//#region src/decode-utf8.ts
|
|
3
3
|
/**
|
|
4
4
|
* Decodes UTF-8 bytes in fatal mode, preserving any leading BOM.
|
package/dist/dev/diff.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { datesEqual, isObject, merge, stableStringify } from "./utils.js";
|
|
3
3
|
|
|
4
4
|
//#region src/diff.ts
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
//#region src/escape-preference.ts
|
|
3
3
|
/**
|
|
4
4
|
* @file Escape-style helpers used to preserve escape sequences as preferred encoding (e.g. \u263A)
|
package/dist/dev/find-by-path.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { hasItems, isDocument, isInlineItem, isKeyValue, isTable, isTableArray } from "./cst.js";
|
|
3
3
|
import { arraysEqual, stableStringify } from "./utils.js";
|
|
4
4
|
|
package/dist/dev/formatter.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { isInlineArray, isInlineItem, isInlineTable, isKeyValue, isTable, isTableArray } from "./cst.js";
|
|
3
3
|
import { getCommaSpace } from "./inline-comma-space.js";
|
|
4
4
|
import { getInlineContainerLayout, hasStructuralMultilineRows } from "./inline-format.js";
|
|
5
|
-
import { applyWrites, insert, remove, shiftNode } from "./writer.js";
|
|
5
|
+
import { applyWrites, insert, markStringifyRoot, remove, shiftNode } from "./writer.js";
|
|
6
6
|
import { generateDocument, generateTable, generateTableArray } from "./generate.js";
|
|
7
7
|
|
|
8
8
|
//#region src/formatter.ts
|
|
@@ -41,6 +41,7 @@ function formatTopLevel(document, format) {
|
|
|
41
41
|
}
|
|
42
42
|
function formatTable(key_value, bracketSpacing) {
|
|
43
43
|
const table = generateTable(key_value.key.value);
|
|
44
|
+
markStringifyRoot(table);
|
|
44
45
|
for (const item of key_value.value.items) insert(table, table, item.item, void 0, void 0, void 0, void 0, true);
|
|
45
46
|
applyWrites(table);
|
|
46
47
|
normalizeGeneratedInlineRows(table, 1, bracketSpacing);
|
|
@@ -48,6 +49,7 @@ function formatTable(key_value, bracketSpacing) {
|
|
|
48
49
|
}
|
|
49
50
|
function formatTableArray(key_value, bracketSpacing) {
|
|
50
51
|
const root = generateDocument();
|
|
52
|
+
markStringifyRoot(root);
|
|
51
53
|
for (const inline_array_item of key_value.value.items) {
|
|
52
54
|
const table_array = generateTableArray(key_value.key.value);
|
|
53
55
|
insert(root, root, table_array);
|
|
@@ -262,6 +264,7 @@ function processTableForNestedInlines(table, additionalTables, format) {
|
|
|
262
264
|
}
|
|
263
265
|
for (const { item, nestedTableKey } of toExtract) {
|
|
264
266
|
const separateTable = generateTable(nestedTableKey);
|
|
267
|
+
markStringifyRoot(separateTable);
|
|
265
268
|
const inlineTable = item.value;
|
|
266
269
|
for (const inlineItem of inlineTable.items) insert(separateTable, separateTable, inlineItem.item);
|
|
267
270
|
applyWrites(separateTable);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter.js","names":[],"sources":["../../src/formatter.ts"],"sourcesContent":["import {\n KeyValue,\n Table,\n InlineTable,\n TableArray,\n InlineArray,\n isInlineTable,\n isInlineArray,\n isKeyValue,\n isInlineItem,\n isTable,\n isTableArray,\n Document,\n Block,\n TreeNode\n} from './cst';\nimport { generateTable, generateDocument, generateTableArray } from './generate';\nimport { insert, remove, applyWrites, shiftNode } from './writer';\nimport { TomlFormat } from './toml-format';\nimport { getInlineContainerLayout, hasStructuralMultilineRows } from './inline-format';\nimport { getCommaSpace } from './inline-comma-space';\n\n// Helper function to detect if an InlineArray originally had trailing commas\nexport function arrayHadTrailingCommas(node: TreeNode): boolean {\n if (!isInlineArray(node)) return false;\n if (node.items.length === 0) return false;\n // Check if the last item has a trailing comma\n const lastItem = node.items[node.items.length - 1];\n return lastItem.comma === true;\n}\n\n// Helper function to detect if an InlineTable originally had trailing commas\nexport function tableHadTrailingCommas(node: TreeNode): boolean {\n if (!isInlineTable(node)) return false;\n if (node.items.length === 0) return false;\n // Check if the last item has a trailing comma\n const lastItem = node.items[node.items.length - 1];\n return lastItem.comma === true;\n}\n\nexport function formatTopLevel(document: Document, format: TomlFormat): Document {\n\n // If inlineTableStart is 0, convert all top-level tables to inline tables\n if (format.inlineTableStart === 0) {\n return document;\n }\n\n const move_to_top_level = document.items.filter(item => {\n if (!isKeyValue(item)) return false;\n\n const is_inline_table = isInlineTable(item.value);\n const is_inline_array =\n isInlineArray(item.value) &&\n item.value.items.length > 0 &&\n item.value.items.every(i => isInlineTable(i.item));\n\n // Only move to top level if the depth is less than inlineTableStart\n if (is_inline_table || is_inline_array) {\n const depth = calculateTableDepth(item.key.value);\n return format.inlineTableStart === undefined || depth < format.inlineTableStart;\n }\n\n return false;\n }) as KeyValue[];\n\n move_to_top_level.forEach(node => {\n remove(document, document, node);\n\n if (isInlineTable(node.value)) {\n // Fast path: empty inline tables don't need the full formatTable\n // machinery (inline table copy, insert loop, applyWrites).\n if ((node.value as InlineTable).items.length === 0) {\n insert(document, document, generateTable(node.key.value));\n } else {\n insert(document, document, formatTable(node, format.bracketSpacing));\n }\n } else {\n formatTableArray(node, format.bracketSpacing).forEach(table_array => {\n insert(document, document, table_array);\n });\n }\n });\n\n applyWrites(document);\n return document;\n}\n\nfunction formatTable(key_value: KeyValue, bracketSpacing: boolean): Table {\n const table = generateTable(key_value.key.value);\n\n for (const item of (key_value.value as InlineTable).items) {\n insert(table, table, item.item, undefined, undefined, undefined, undefined, true);\n }\n\n applyWrites(table);\n normalizeGeneratedInlineRows(table, 1, bracketSpacing);\n return table;\n}\n\nfunction formatTableArray(key_value: KeyValue, bracketSpacing: boolean): TableArray[] {\n const root = generateDocument();\n\n for (const inline_array_item of (key_value.value as InlineArray).items) {\n const table_array = generateTableArray(key_value.key.value);\n insert(root, root, table_array);\n\n for (const inline_table_item of (inline_array_item.item as InlineTable).items) {\n insert(root, table_array, inline_table_item.item, undefined, undefined, undefined, undefined, true);\n }\n }\n\n applyWrites(root);\n for (const item of root.items) {\n if (isTable(item) || isTableArray(item)) normalizeGeneratedInlineRows(item, 1, bracketSpacing);\n }\n return root.items as TableArray[];\n}\n\n/**\n * Whether an inline container lays its items out one per row. A container\n * regenerated through a TOML round-trip carries no layout flag, so fall back to\n * the physical rows: a row on a later line than the previous item's end means\n * multiline. A compact container whose items merely SPAN several lines (because\n * one of them is a multiline value) stays compact — its closing bracket still\n * follows its last item.\n */\nfunction isMultilineInlineContainer(container: InlineArray | InlineTable): boolean {\n const layout = getInlineContainerLayout(container);\n return layout !== undefined ? layout : hasStructuralMultilineRows(container);\n}\n\n/**\n * Row indent for a container that is an array ELEMENT (no key of its own).\n * Keyless rows keep the historical minimum of two columns, matching\n * positionGeneratedNestedInlineTables so element rows and their closing\n * bracket stay aligned the same way no matter which path laid them out.\n */\nfunction elementIndentWidth(indentWidth: number): number {\n return Math.max(indentWidth, 2);\n}\n\n/**\n * Lays out a generated inline container so that its rows and closing bracket\n * line up with the key (or, for an array element, the element) that owns it:\n *\n * - a multiline container puts one row per line at `anchorColumn + indentWidth`\n * and its closing bracket at `anchorColumn`;\n * - a nested multiline container inside a row does the same, using that row's\n * key column as its own anchor.\n *\n * `anchorColumn` is the column of the owning key's start. It only has to be\n * correct for containers the generator built: containers parsed from an\n * existing document keep their own formatting, so callers must restrict this\n * to freshly generated/replaced subtrees.\n *\n * Returns whether a nested container's end position moved.\n */\nexport function normalizeInlineContainerRows(\n container: InlineArray | InlineTable,\n anchorColumn: number,\n indentWidth: number,\n bracketSpacing = true\n): boolean {\n const multiline = isMultilineInlineContainer(container);\n let nestedEndChanged = false;\n if (multiline) {\n const rowIndent = anchorColumn + indentWidth;\n for (const item of container.items) {\n shiftNode(item, { lines: 0, columns: rowIndent - item.loc.start.column });\n if (isInlineItem(item)) {\n if (isInlineArray(item.item) || isInlineTable(item.item)) {\n if (normalizeInlineContainerRows(item.item, item.item.loc.start.column, elementIndentWidth(indentWidth), bracketSpacing)) {\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n } else if (isKeyValue(item.item) &&\n (isInlineArray(item.item.value) || isInlineTable(item.item.value))) {\n // The nested container's rows hang off the ROW's key, so it has to\n // be laid out from the key's own column — not from the enclosing\n // container's indent (fuzz3 seed 18515).\n if (normalizeInlineContainerRows(item.item.value, item.item.loc.start.column, indentWidth, bracketSpacing)) {\n item.item.loc.end = { ...item.item.value.loc.end };\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n }\n }\n }\n container.loc.end.column = anchorColumn + 1;\n // The container's own closing bracket moved, so the enclosing item has to\n // re-sync its end from it (its comma is written at that end).\n nestedEndChanged = true;\n }\n\n for (const item of container.items) {\n if (!isInlineItem(item)) continue;\n if (isInlineArray(item.item) || isInlineTable(item.item)) {\n if (normalizeInlineContainerRows(item.item, item.item.loc.start.column, elementIndentWidth(indentWidth), bracketSpacing)) {\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n } else if (isKeyValue(item.item) &&\n (isInlineArray(item.item.value) || isInlineTable(item.item.value))) {\n const value = item.item.value;\n const anchorColumn = item.item.loc.start.column;\n const valueMultiline = isMultilineInlineContainer(value);\n if (normalizeInlineContainerRows(value, anchorColumn, indentWidth, bracketSpacing)) {\n // A MULTILINE nested value closes on its own row, level with the row's\n // key. A compact one closes right after its last item — that end was\n // already computed by its own pass, so snapping it to the key column\n // would place the closing bracket before the item it follows\n // (fuzz3 seed 14739).\n if (valueMultiline) value.loc.end.column = anchorColumn + 1;\n item.item.loc.end = { ...value.loc.end };\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n }\n }\n // A nested container that grew pushes its following siblings along: the\n // siblings were positioned against its OLD end, so the ones still sharing its\n // end row must be re-anchored to its new end before the container's own\n // closing bracket is placed from the last item.\n const commaSpace = getCommaSpace(container) ?? 2;\n for (let index = 1; index < container.items.length; index++) {\n const previous = container.items[index - 1];\n const next = container.items[index];\n if (previous.loc.end.line !== next.loc.start.line) continue;\n const targetColumn = previous.loc.end.column + (previous.comma ? commaSpace : 1);\n shiftNode(next, { lines: 0, columns: targetColumn - next.loc.start.column });\n }\n if (nestedEndChanged && !multiline && container.items.length > 0) {\n const lastItem = container.items[container.items.length - 1];\n container.loc.end = {\n line: lastItem.loc.end.line,\n column: lastItem.loc.end.column + (lastItem.comma ? 1 : 0) + (bracketSpacing ? 2 : 1)\n };\n }\n return nestedEndChanged;\n}\n\nexport function normalizeGeneratedInlineRows(\n block: Table | TableArray | KeyValue,\n indentWidth: number,\n bracketSpacing = true\n): void {\n // A root-level key-value is normalized like the rows of a table: its own key\n // column is the anchor the nested container hangs off.\n const rows: Block[] = isKeyValue(block) ? [block] : block.items;\n for (const item of rows) {\n if (!isKeyValue(item)) continue;\n if (isInlineArray(item.value) || isInlineTable(item.value)) {\n normalizeInlineContainerRows(item.value, item.loc.start.column, indentWidth, bracketSpacing);\n }\n }\n}\n\nexport function normalizeGeneratedInlineContainerRows(\n container: InlineArray | InlineTable,\n indentWidth: number\n): void {\n const normalize = (\n current: InlineArray | InlineTable,\n normalizeCurrent: boolean\n ): void => {\n if (normalizeCurrent) {\n delete (current as { range?: [number, number] }).range;\n const multiline = getInlineContainerLayout(current) === true ||\n hasStructuralMultilineRows(current);\n if (multiline) {\n const rowIndent = current.loc.start.column + indentWidth;\n for (const item of current.items) {\n shiftNode(item, { lines: 0, columns: rowIndent - item.loc.start.column });\n }\n current.loc.end.column = current.loc.start.column + 1;\n }\n }\n\n for (const item of current.items) {\n if (!isInlineItem(item)) continue;\n if (isInlineArray(item.item) || isInlineTable(item.item)) {\n normalize(item.item, isInlineArray(current) && isInlineTable(item.item));\n } else if (isKeyValue(item.item) &&\n (isInlineArray(item.item.value) || isInlineTable(item.item.value))) {\n normalize(item.item.value, false);\n }\n }\n };\n\n normalize(container, false);\n}\n\n/**\n * Updates a table's location end position after removing inline table items.\n * When inline table content is removed from a parent table, the parent table's \n * end position needs to be adjusted to reflect where the content actually ends.\n * \n * @param table - The table whose end position should be updated\n */\nexport function postInlineItemRemovalAdjustment(table: Table | TableArray): void {\n if (table.items.length > 0) {\n const lastItem = table.items[table.items.length - 1];\n table.loc.end.line = lastItem.loc.end.line;\n table.loc.end.column = lastItem.loc.end.column;\n } else {\n // If no items left, table ends at the header line\n table.loc.end.line = table.key.loc.end.line;\n table.loc.end.column = table.key.loc.end.column;\n }\n}\n\n/**\n * Calculates the nesting depth of a table based on its key path.\n * Root level tables (e.g., [table]) have depth 0.\n * First level nested tables (e.g., [table.nested]) have depth 1.\n * \n * @param keyPath - Array representing the table key path (e.g., ['table', 'nested'])\n * @returns The nesting depth (0 for root level, 1+ for nested levels)\n */\nexport function calculateTableDepth(keyPath: string[]): number {\n return Math.max(0, keyPath.length - 1);\n}\n\n/**\n * Converts nested inline tables to separate table sections based on the inlineTableStart depth setting.\n * This function recursively processes all tables in the document and extracts inline tables that are\n * at a depth less than the inlineTableStart threshold.\n */\nexport function formatNestedTablesMultiline(document: Document, format: TomlFormat): Document {\n // If inlineTableStart is undefined, use the default behavior (no conversion)\n // If inlineTableStart is 0, all should be inline (no conversion)\n if (format.inlineTableStart === undefined || format.inlineTableStart === 0) {\n return document;\n }\n\n const additionalTables: { table: Table; parent: Table | TableArray }[] = [];\n \n // Process all existing tables for nested inline tables\n for (const item of document.items) {\n if (isKeyValue(item) && isInlineTable(item.value)) {\n // This is a top-level inline table (depth 0)\n const depth = calculateTableDepth(item.key.value);\n if (depth < format.inlineTableStart) {\n // Convert to a separate table\n const table = formatTable(item, format.bracketSpacing);\n remove(document, document, item);\n insert(document, document, table);\n \n processTableForNestedInlines(table, additionalTables, format);\n }\n } else if (item.type === 'Table') {\n processTableForNestedInlines(item as Table, additionalTables, format);\n } else if (isTableArray(item)) {\n processTableForNestedInlines(item, additionalTables, format);\n }\n }\n \n // Insert each extracted table immediately after its parent block, preserving\n // the parent-child positional relationship. A flat append at the document\n // end (the previous behaviour) misplaced a sub-table of an earlier entry —\n // e.g. `[tp6.k41]` (a sub-table of `[[tp6]]` entry 0) landed after `[[tp6]]`\n // entry 1, reassigning `k41` to the wrong entry. `additionalTables` is in\n // depth-first pre-order (a child is pushed before its own children), so\n // inserting in order with a per-parent counter keeps every sub-table after\n // its parent and before the parent's next sibling.\n const insertedAfter = new Map<Table | TableArray, number>();\n for (const { table, parent } of additionalTables) {\n const index = document.items.indexOf(parent);\n const offset = (insertedAfter.get(parent) ?? 0) + 1;\n insertedAfter.set(parent, offset);\n insert(document, document, table, index + offset);\n }\n\n applyWrites(document);\n return document;\n}\n\n/**\n * Recursively processes a table for nested inline tables and extracts them as separate tables\n * when they are at a depth less than the inlineTableStart threshold.\n */\nfunction processTableForNestedInlines(table: Table | TableArray, additionalTables: { table: Table; parent: Table | TableArray }[], format: TomlFormat): void {\n // Collect all inline tables that need extraction, then process in forward order\n // to preserve the original key order in the output.\n const toExtract: { item: KeyValue; nestedTableKey: string[] }[] = [];\n for (let i = 0; i < table.items.length; i++) {\n const item = table.items[i];\n if (isKeyValue(item) && isInlineTable(item.value)) {\n const nestedTableKey = [...table.key.item.value, ...item.key.value];\n const depth = calculateTableDepth(nestedTableKey);\n if (depth < (format.inlineTableStart ?? 1)) {\n toExtract.push({ item, nestedTableKey });\n }\n }\n }\n\n // Process in forward order. Each item reference captured above is stable\n // even after previous iterations call remove() — the KeyValue node itself\n // stays valid regardless of index shifts.\n for (const { item, nestedTableKey } of toExtract) {\n const separateTable = generateTable(nestedTableKey);\n const inlineTable = item.value as InlineTable;\n\n for (const inlineItem of inlineTable.items) {\n insert(separateTable, separateTable, inlineItem.item);\n }\n // Finalize the separateTable's internal offsets and recalculate its span\n // so it can be safely inserted into the document later.\n applyWrites(separateTable);\n\n remove(table, table, item);\n postInlineItemRemovalAdjustment(table);\n\n additionalTables.push({ table: separateTable, parent: table });\n\n processTableForNestedInlines(separateTable, additionalTables, format);\n }\n // Apply removal offsets accumulated on this table so remaining items\n // (if any) are shifted to the correct positions.\n if (toExtract.length > 0) {\n applyWrites(table);\n }\n}\n\nexport function formatEmptyLines(document: Document): Document {\n let shift = 0;\n let previous = 0;\n for (const item of document.items) {\n if (previous === 0 && item.loc.start.line > 1) {\n // Remove leading newlines\n shift = 1 - item.loc.start.line;\n } else if (item.loc.start.line + shift > previous + 2) {\n shift += previous + 2 - (item.loc.start.line + shift);\n }\n\n shiftNode(item, {\n lines: shift,\n columns: 0\n });\n previous = item.loc.end.line;\n }\n\n return document;\n}\n"],"mappings":";;;;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,YAAY,aAAa;EAEzB,OAAO,OAAO,OAAO,IAAI;EACzB,gCAAgC,KAAK;EAErC,iBAAiB,KAAK;GAAE,OAAO;GAAe,QAAQ;EAAM,CAAC;EAE7D,6BAA6B,eAAe,kBAAkB,MAAM;CACtE;CAGA,IAAI,UAAU,SAAS,GACrB,YAAY,KAAK;AAErB;AAEA,SAAgB,iBAAiB,UAA8B;CAC7D,IAAI,QAAQ;CACZ,IAAI,WAAW;CACf,KAAK,MAAM,QAAQ,SAAS,OAAO;EACjC,IAAI,aAAa,KAAK,KAAK,IAAI,MAAM,OAAO,GAE1C,QAAQ,IAAI,KAAK,IAAI,MAAM;OACtB,IAAI,KAAK,IAAI,MAAM,OAAO,QAAQ,WAAW,GAClD,SAAS,WAAW,KAAK,KAAK,IAAI,MAAM,OAAO;EAGjD,UAAU,MAAM;GACd,OAAO;GACP,SAAS;EACX,CAAC;EACD,WAAW,KAAK,IAAI,IAAI;CAC1B;CAEA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"formatter.js","names":[],"sources":["../../src/formatter.ts"],"sourcesContent":["import {\n KeyValue,\n Table,\n InlineTable,\n TableArray,\n InlineArray,\n isInlineTable,\n isInlineArray,\n isKeyValue,\n isInlineItem,\n isTable,\n isTableArray,\n Document,\n Block,\n TreeNode\n} from './cst';\nimport { generateTable, generateDocument, generateTableArray } from './generate';\nimport { insert, remove, applyWrites, shiftNode, markStringifyRoot } from './writer';\nimport { TomlFormat } from './toml-format';\nimport { getInlineContainerLayout, hasStructuralMultilineRows } from './inline-format';\nimport { getCommaSpace } from './inline-comma-space';\n\n// Helper function to detect if an InlineArray originally had trailing commas\nexport function arrayHadTrailingCommas(node: TreeNode): boolean {\n if (!isInlineArray(node)) return false;\n if (node.items.length === 0) return false;\n // Check if the last item has a trailing comma\n const lastItem = node.items[node.items.length - 1];\n return lastItem.comma === true;\n}\n\n// Helper function to detect if an InlineTable originally had trailing commas\nexport function tableHadTrailingCommas(node: TreeNode): boolean {\n if (!isInlineTable(node)) return false;\n if (node.items.length === 0) return false;\n // Check if the last item has a trailing comma\n const lastItem = node.items[node.items.length - 1];\n return lastItem.comma === true;\n}\n\nexport function formatTopLevel(document: Document, format: TomlFormat): Document {\n\n // If inlineTableStart is 0, convert all top-level tables to inline tables\n if (format.inlineTableStart === 0) {\n return document;\n }\n\n const move_to_top_level = document.items.filter(item => {\n if (!isKeyValue(item)) return false;\n\n const is_inline_table = isInlineTable(item.value);\n const is_inline_array =\n isInlineArray(item.value) &&\n item.value.items.length > 0 &&\n item.value.items.every(i => isInlineTable(i.item));\n\n // Only move to top level if the depth is less than inlineTableStart\n if (is_inline_table || is_inline_array) {\n const depth = calculateTableDepth(item.key.value);\n return format.inlineTableStart === undefined || depth < format.inlineTableStart;\n }\n\n return false;\n }) as KeyValue[];\n\n move_to_top_level.forEach(node => {\n remove(document, document, node);\n\n if (isInlineTable(node.value)) {\n // Fast path: empty inline tables don't need the full formatTable\n // machinery (inline table copy, insert loop, applyWrites).\n if ((node.value as InlineTable).items.length === 0) {\n insert(document, document, generateTable(node.key.value));\n } else {\n insert(document, document, formatTable(node, format.bracketSpacing));\n }\n } else {\n formatTableArray(node, format.bracketSpacing).forEach(table_array => {\n insert(document, document, table_array);\n });\n }\n });\n\n applyWrites(document);\n return document;\n}\n\nfunction formatTable(key_value: KeyValue, bracketSpacing: boolean): Table {\n const table = generateTable(key_value.key.value);\n markStringifyRoot(table);\n\n for (const item of (key_value.value as InlineTable).items) {\n insert(table, table, item.item, undefined, undefined, undefined, undefined, true);\n }\n\n applyWrites(table);\n normalizeGeneratedInlineRows(table, 1, bracketSpacing);\n return table;\n}\n\nfunction formatTableArray(key_value: KeyValue, bracketSpacing: boolean): TableArray[] {\n const root = generateDocument();\n markStringifyRoot(root);\n\n for (const inline_array_item of (key_value.value as InlineArray).items) {\n const table_array = generateTableArray(key_value.key.value);\n insert(root, root, table_array);\n\n for (const inline_table_item of (inline_array_item.item as InlineTable).items) {\n insert(root, table_array, inline_table_item.item, undefined, undefined, undefined, undefined, true);\n }\n }\n\n applyWrites(root);\n for (const item of root.items) {\n if (isTable(item) || isTableArray(item)) normalizeGeneratedInlineRows(item, 1, bracketSpacing);\n }\n return root.items as TableArray[];\n}\n\n/**\n * Whether an inline container lays its items out one per row. A container\n * regenerated through a TOML round-trip carries no layout flag, so fall back to\n * the physical rows: a row on a later line than the previous item's end means\n * multiline. A compact container whose items merely SPAN several lines (because\n * one of them is a multiline value) stays compact — its closing bracket still\n * follows its last item.\n */\nfunction isMultilineInlineContainer(container: InlineArray | InlineTable): boolean {\n const layout = getInlineContainerLayout(container);\n return layout !== undefined ? layout : hasStructuralMultilineRows(container);\n}\n\n/**\n * Row indent for a container that is an array ELEMENT (no key of its own).\n * Keyless rows keep the historical minimum of two columns, matching\n * positionGeneratedNestedInlineTables so element rows and their closing\n * bracket stay aligned the same way no matter which path laid them out.\n */\nfunction elementIndentWidth(indentWidth: number): number {\n return Math.max(indentWidth, 2);\n}\n\n/**\n * Lays out a generated inline container so that its rows and closing bracket\n * line up with the key (or, for an array element, the element) that owns it:\n *\n * - a multiline container puts one row per line at `anchorColumn + indentWidth`\n * and its closing bracket at `anchorColumn`;\n * - a nested multiline container inside a row does the same, using that row's\n * key column as its own anchor.\n *\n * `anchorColumn` is the column of the owning key's start. It only has to be\n * correct for containers the generator built: containers parsed from an\n * existing document keep their own formatting, so callers must restrict this\n * to freshly generated/replaced subtrees.\n *\n * Returns whether a nested container's end position moved.\n */\nexport function normalizeInlineContainerRows(\n container: InlineArray | InlineTable,\n anchorColumn: number,\n indentWidth: number,\n bracketSpacing = true\n): boolean {\n const multiline = isMultilineInlineContainer(container);\n let nestedEndChanged = false;\n if (multiline) {\n const rowIndent = anchorColumn + indentWidth;\n for (const item of container.items) {\n shiftNode(item, { lines: 0, columns: rowIndent - item.loc.start.column });\n if (isInlineItem(item)) {\n if (isInlineArray(item.item) || isInlineTable(item.item)) {\n if (normalizeInlineContainerRows(item.item, item.item.loc.start.column, elementIndentWidth(indentWidth), bracketSpacing)) {\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n } else if (isKeyValue(item.item) &&\n (isInlineArray(item.item.value) || isInlineTable(item.item.value))) {\n // The nested container's rows hang off the ROW's key, so it has to\n // be laid out from the key's own column — not from the enclosing\n // container's indent (fuzz3 seed 18515).\n if (normalizeInlineContainerRows(item.item.value, item.item.loc.start.column, indentWidth, bracketSpacing)) {\n item.item.loc.end = { ...item.item.value.loc.end };\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n }\n }\n }\n container.loc.end.column = anchorColumn + 1;\n // The container's own closing bracket moved, so the enclosing item has to\n // re-sync its end from it (its comma is written at that end).\n nestedEndChanged = true;\n }\n\n for (const item of container.items) {\n if (!isInlineItem(item)) continue;\n if (isInlineArray(item.item) || isInlineTable(item.item)) {\n if (normalizeInlineContainerRows(item.item, item.item.loc.start.column, elementIndentWidth(indentWidth), bracketSpacing)) {\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n } else if (isKeyValue(item.item) &&\n (isInlineArray(item.item.value) || isInlineTable(item.item.value))) {\n const value = item.item.value;\n const anchorColumn = item.item.loc.start.column;\n const valueMultiline = isMultilineInlineContainer(value);\n if (normalizeInlineContainerRows(value, anchorColumn, indentWidth, bracketSpacing)) {\n // A MULTILINE nested value closes on its own row, level with the row's\n // key. A compact one closes right after its last item — that end was\n // already computed by its own pass, so snapping it to the key column\n // would place the closing bracket before the item it follows\n // (fuzz3 seed 14739).\n if (valueMultiline) value.loc.end.column = anchorColumn + 1;\n item.item.loc.end = { ...value.loc.end };\n item.loc.end = { ...item.item.loc.end };\n nestedEndChanged = true;\n }\n }\n }\n // A nested container that grew pushes its following siblings along: the\n // siblings were positioned against its OLD end, so the ones still sharing its\n // end row must be re-anchored to its new end before the container's own\n // closing bracket is placed from the last item.\n const commaSpace = getCommaSpace(container) ?? 2;\n for (let index = 1; index < container.items.length; index++) {\n const previous = container.items[index - 1];\n const next = container.items[index];\n if (previous.loc.end.line !== next.loc.start.line) continue;\n const targetColumn = previous.loc.end.column + (previous.comma ? commaSpace : 1);\n shiftNode(next, { lines: 0, columns: targetColumn - next.loc.start.column });\n }\n if (nestedEndChanged && !multiline && container.items.length > 0) {\n const lastItem = container.items[container.items.length - 1];\n container.loc.end = {\n line: lastItem.loc.end.line,\n column: lastItem.loc.end.column + (lastItem.comma ? 1 : 0) + (bracketSpacing ? 2 : 1)\n };\n }\n return nestedEndChanged;\n}\n\nexport function normalizeGeneratedInlineRows(\n block: Table | TableArray | KeyValue,\n indentWidth: number,\n bracketSpacing = true\n): void {\n // A root-level key-value is normalized like the rows of a table: its own key\n // column is the anchor the nested container hangs off.\n const rows: Block[] = isKeyValue(block) ? [block] : block.items;\n for (const item of rows) {\n if (!isKeyValue(item)) continue;\n if (isInlineArray(item.value) || isInlineTable(item.value)) {\n normalizeInlineContainerRows(item.value, item.loc.start.column, indentWidth, bracketSpacing);\n }\n }\n}\n\nexport function normalizeGeneratedInlineContainerRows(\n container: InlineArray | InlineTable,\n indentWidth: number\n): void {\n const normalize = (\n current: InlineArray | InlineTable,\n normalizeCurrent: boolean\n ): void => {\n if (normalizeCurrent) {\n delete (current as { range?: [number, number] }).range;\n const multiline = getInlineContainerLayout(current) === true ||\n hasStructuralMultilineRows(current);\n if (multiline) {\n const rowIndent = current.loc.start.column + indentWidth;\n for (const item of current.items) {\n shiftNode(item, { lines: 0, columns: rowIndent - item.loc.start.column });\n }\n current.loc.end.column = current.loc.start.column + 1;\n }\n }\n\n for (const item of current.items) {\n if (!isInlineItem(item)) continue;\n if (isInlineArray(item.item) || isInlineTable(item.item)) {\n normalize(item.item, isInlineArray(current) && isInlineTable(item.item));\n } else if (isKeyValue(item.item) &&\n (isInlineArray(item.item.value) || isInlineTable(item.item.value))) {\n normalize(item.item.value, false);\n }\n }\n };\n\n normalize(container, false);\n}\n\n/**\n * Updates a table's location end position after removing inline table items.\n * When inline table content is removed from a parent table, the parent table's \n * end position needs to be adjusted to reflect where the content actually ends.\n * \n * @param table - The table whose end position should be updated\n */\nexport function postInlineItemRemovalAdjustment(table: Table | TableArray): void {\n if (table.items.length > 0) {\n const lastItem = table.items[table.items.length - 1];\n table.loc.end.line = lastItem.loc.end.line;\n table.loc.end.column = lastItem.loc.end.column;\n } else {\n // If no items left, table ends at the header line\n table.loc.end.line = table.key.loc.end.line;\n table.loc.end.column = table.key.loc.end.column;\n }\n}\n\n/**\n * Calculates the nesting depth of a table based on its key path.\n * Root level tables (e.g., [table]) have depth 0.\n * First level nested tables (e.g., [table.nested]) have depth 1.\n * \n * @param keyPath - Array representing the table key path (e.g., ['table', 'nested'])\n * @returns The nesting depth (0 for root level, 1+ for nested levels)\n */\nexport function calculateTableDepth(keyPath: string[]): number {\n return Math.max(0, keyPath.length - 1);\n}\n\n/**\n * Converts nested inline tables to separate table sections based on the inlineTableStart depth setting.\n * This function recursively processes all tables in the document and extracts inline tables that are\n * at a depth less than the inlineTableStart threshold.\n */\nexport function formatNestedTablesMultiline(document: Document, format: TomlFormat): Document {\n // If inlineTableStart is undefined, use the default behavior (no conversion)\n // If inlineTableStart is 0, all should be inline (no conversion)\n if (format.inlineTableStart === undefined || format.inlineTableStart === 0) {\n return document;\n }\n\n const additionalTables: { table: Table; parent: Table | TableArray }[] = [];\n \n // Process all existing tables for nested inline tables\n for (const item of document.items) {\n if (isKeyValue(item) && isInlineTable(item.value)) {\n // This is a top-level inline table (depth 0)\n const depth = calculateTableDepth(item.key.value);\n if (depth < format.inlineTableStart) {\n // Convert to a separate table\n const table = formatTable(item, format.bracketSpacing);\n remove(document, document, item);\n insert(document, document, table);\n \n processTableForNestedInlines(table, additionalTables, format);\n }\n } else if (item.type === 'Table') {\n processTableForNestedInlines(item as Table, additionalTables, format);\n } else if (isTableArray(item)) {\n processTableForNestedInlines(item, additionalTables, format);\n }\n }\n \n // Insert each extracted table immediately after its parent block, preserving\n // the parent-child positional relationship. A flat append at the document\n // end (the previous behaviour) misplaced a sub-table of an earlier entry —\n // e.g. `[tp6.k41]` (a sub-table of `[[tp6]]` entry 0) landed after `[[tp6]]`\n // entry 1, reassigning `k41` to the wrong entry. `additionalTables` is in\n // depth-first pre-order (a child is pushed before its own children), so\n // inserting in order with a per-parent counter keeps every sub-table after\n // its parent and before the parent's next sibling.\n const insertedAfter = new Map<Table | TableArray, number>();\n for (const { table, parent } of additionalTables) {\n const index = document.items.indexOf(parent);\n const offset = (insertedAfter.get(parent) ?? 0) + 1;\n insertedAfter.set(parent, offset);\n insert(document, document, table, index + offset);\n }\n\n applyWrites(document);\n return document;\n}\n\n/**\n * Recursively processes a table for nested inline tables and extracts them as separate tables\n * when they are at a depth less than the inlineTableStart threshold.\n */\nfunction processTableForNestedInlines(table: Table | TableArray, additionalTables: { table: Table; parent: Table | TableArray }[], format: TomlFormat): void {\n // Collect all inline tables that need extraction, then process in forward order\n // to preserve the original key order in the output.\n const toExtract: { item: KeyValue; nestedTableKey: string[] }[] = [];\n for (let i = 0; i < table.items.length; i++) {\n const item = table.items[i];\n if (isKeyValue(item) && isInlineTable(item.value)) {\n const nestedTableKey = [...table.key.item.value, ...item.key.value];\n const depth = calculateTableDepth(nestedTableKey);\n if (depth < (format.inlineTableStart ?? 1)) {\n toExtract.push({ item, nestedTableKey });\n }\n }\n }\n\n // Process in forward order. Each item reference captured above is stable\n // even after previous iterations call remove() — the KeyValue node itself\n // stays valid regardless of index shifts.\n for (const { item, nestedTableKey } of toExtract) {\n const separateTable = generateTable(nestedTableKey);\n markStringifyRoot(separateTable);\n const inlineTable = item.value as InlineTable;\n\n for (const inlineItem of inlineTable.items) {\n insert(separateTable, separateTable, inlineItem.item);\n }\n // Finalize the separateTable's internal offsets and recalculate its span\n // so it can be safely inserted into the document later.\n applyWrites(separateTable);\n\n remove(table, table, item);\n postInlineItemRemovalAdjustment(table);\n\n additionalTables.push({ table: separateTable, parent: table });\n\n processTableForNestedInlines(separateTable, additionalTables, format);\n }\n // Apply removal offsets accumulated on this table so remaining items\n // (if any) are shifted to the correct positions.\n if (toExtract.length > 0) {\n applyWrites(table);\n }\n}\n\nexport function formatEmptyLines(document: Document): Document {\n let shift = 0;\n let previous = 0;\n for (const item of document.items) {\n if (previous === 0 && item.loc.start.line > 1) {\n // Remove leading newlines\n shift = 1 - item.loc.start.line;\n } else if (item.loc.start.line + shift > previous + 2) {\n shift += previous + 2 - (item.loc.start.line + shift);\n }\n\n shiftNode(item, {\n lines: shift,\n columns: 0\n });\n previous = item.loc.end.line;\n }\n\n return document;\n}\n"],"mappings":";;;;;;;;AAuBA,SAAgB,uBAAuB,MAAyB;CAC9D,IAAI,CAAC,cAAc,IAAI,GAAG,OAAO;CACjC,IAAI,KAAK,MAAM,WAAW,GAAG,OAAO;CAGpC,OADiB,KAAK,MAAM,KAAK,MAAM,SAAS,EACjC,CAAC,UAAU;AAC5B;AAGA,SAAgB,uBAAuB,MAAyB;CAC9D,IAAI,CAAC,cAAc,IAAI,GAAG,OAAO;CACjC,IAAI,KAAK,MAAM,WAAW,GAAG,OAAO;CAGpC,OADiB,KAAK,MAAM,KAAK,MAAM,SAAS,EACjC,CAAC,UAAU;AAC5B;AAEA,SAAgB,eAAe,UAAoB,QAA8B;CAG/E,IAAI,OAAO,qBAAqB,GAC9B,OAAO;CAqBT,AAlB0B,SAAS,MAAM,QAAO,SAAQ;EACtD,IAAI,CAAC,WAAW,IAAI,GAAG,OAAO;EAE9B,MAAM,kBAAkB,cAAc,KAAK,KAAK;EAChD,MAAM,kBACJ,cAAc,KAAK,KAAK,KACxB,KAAK,MAAM,MAAM,SAAS,KAC1B,KAAK,MAAM,MAAM,OAAM,MAAK,cAAc,EAAE,IAAI,CAAC;EAGnD,IAAI,mBAAmB,iBAAiB;GACtC,MAAM,QAAQ,oBAAoB,KAAK,IAAI,KAAK;GAChD,OAAO,OAAO,qBAAqB,UAAa,QAAQ,OAAO;EACjE;EAEA,OAAO;CACT,CAEgB,CAAC,CAAC,SAAQ,SAAQ;EAChC,OAAO,UAAU,UAAU,IAA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AAK,KAAK,GAAG;GACjD,MAAM,iBAAiB,CAAC,GAAG,MAAM,IAAI,KAAK,OAAO,GAAG,KAAK,IAAI,KAAK;GAElE,IADc,oBAAoB,cAC1B,KAAK,OAAO,oBAAoB,IACtC,UAAU,KAAK;IAAE;IAAM;GAAe,CAAC;EAE3C;CACF;CAKA,KAAK,MAAM,EAAE,MAAM,oBAAoB,WAAW;EAChD,MAAM,gBAAgB,cAAc,cAAc;EAClD,kBAAkB,aAAa;EAC/B,MAAM,cAAc,KAAK;EAEzB,KAAK,MAAM,cAAc,YAAY,OACnC,OAAO,eAAe,eAAe,WAAW,IAAI;EAItD,YAAY,aAAa;EAEzB,OAAO,OAAO,OAAO,IAAI;EACzB,gCAAgC,KAAK;EAErC,iBAAiB,KAAK;GAAE,OAAO;GAAe,QAAQ;EAAM,CAAC;EAE7D,6BAA6B,eAAe,kBAAkB,MAAM;CACtE;CAGA,IAAI,UAAU,SAAS,GACrB,YAAY,KAAK;AAErB;AAEA,SAAgB,iBAAiB,UAA8B;CAC7D,IAAI,QAAQ;CACZ,IAAI,WAAW;CACf,KAAK,MAAM,QAAQ,SAAS,OAAO;EACjC,IAAI,aAAa,KAAK,KAAK,IAAI,MAAM,OAAO,GAE1C,QAAQ,IAAI,KAAK,IAAI,MAAM;OACtB,IAAI,KAAK,IAAI,MAAM,OAAO,QAAQ,WAAW,GAClD,SAAS,WAAW,KAAK,KAAK,IAAI,MAAM,OAAO;EAGjD,UAAU,MAAM;GACd,OAAO;GACP,SAAS;EACX,CAAC;EACD,WAAW,KAAK,IAAI,IAAI;CAC1B;CAEA,OAAO;AACT"}
|
package/dist/dev/generate.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { cloneLocation, clonePosition, zero } from "./location.js";
|
|
3
3
|
import { IS_BARE_KEY } from "./tokenizer.js";
|
|
4
4
|
import { LocalDate, LocalTime } from "./date-format.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
//#region src/inline-comma-space.ts
|
|
3
3
|
const commaSpace = /* @__PURE__ */ new WeakMap();
|
|
4
4
|
function setCommaSpace(node, gap) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { hasItems, isInlineArray, isInlineItem, isInlineTable, isKeyValue } from "./cst.js";
|
|
3
3
|
|
|
4
4
|
//#region src/inline-format.ts
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { isInlineArray, isInlineItem, isInlineTable } from "./cst.js";
|
|
3
3
|
import { clonePosition } from "./location.js";
|
|
4
4
|
import { hasStructuralMultilineRows, markGeneratedNestedTable, markGeneratedNestedTableHost, markInlineContainerPositioned } from "./inline-format.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
//#region src/line-ending-backslash.ts
|
|
3
3
|
function detectFirstStructuralNewline(value) {
|
|
4
4
|
const match = value.match(/\r\n|\n/);
|
package/dist/dev/location.js
CHANGED
package/dist/dev/parse-error.js
CHANGED
package/dist/dev/parse-js.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { isInlineArray, isInlineTable, isKeyValue } from "./cst.js";
|
|
3
3
|
import { isBigInt, isBoolean, isDate, isFloat, isInteger, isObject, isString, isTemporal } from "./utils.js";
|
|
4
4
|
import { TomlFormat } from "./toml-format.js";
|
|
@@ -23,9 +23,11 @@ function parseJS(value, format = TomlFormat.default(), depth = 0, parentIsMultil
|
|
|
23
23
|
for (const item of walkObject(value, format, depth, parentIsMultiline)) insert(document, document, item);
|
|
24
24
|
applyWrites(document);
|
|
25
25
|
formatTopLevel(document, format);
|
|
26
|
-
formatNestedTablesMultiline(document, format);
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
if ((format.inlineTableStart ?? 1) > 1) formatNestedTablesMultiline(document, format);
|
|
27
|
+
if (format.multilineTable !== "auto" || format.multilineArray !== "auto") {
|
|
28
|
+
for (const item of document.items) if (item.type === "Table" || item.type === "TableArray") normalizeGeneratedInlineRows(item, format.indentWidth, format.bracketSpacing);
|
|
29
|
+
else if (isKeyValue(item) && (isInlineArray(item.value) || isInlineTable(item.value))) normalizeGeneratedInlineRows(item, format.indentWidth, format.bracketSpacing);
|
|
30
|
+
}
|
|
29
31
|
return formatEmptyLines(document);
|
|
30
32
|
}
|
|
31
33
|
function* walkObject(object, format, depth = 0, parentIsMultiline = false) {
|
|
@@ -56,6 +58,7 @@ function walkInlineArray(value, format, depth, parentIsMultiline) {
|
|
|
56
58
|
const multiline = !(value.length === 0 && depth > 0) && resolveInlineContainerLayout("array", depth, parentIsMultiline, format);
|
|
57
59
|
setInlineContainerLayout(inline_array, multiline);
|
|
58
60
|
setRootIndentWidth(inline_array, format.indentWidth);
|
|
61
|
+
markStringifyRoot(inline_array);
|
|
59
62
|
for (const element of value) {
|
|
60
63
|
const item = walkValue(element, format, depth + 1, multiline);
|
|
61
64
|
const inline_array_item = generateInlineItem(item);
|
|
@@ -75,6 +78,7 @@ function walkInlineTable(value, format, depth, parentIsMultiline) {
|
|
|
75
78
|
const multiline = !(Object.keys(value).length === 0 && depth > 0) && resolveInlineContainerLayout("table", depth, parentIsMultiline, format);
|
|
76
79
|
setInlineContainerLayout(inline_table, multiline);
|
|
77
80
|
setRootIndentWidth(inline_table, format.indentWidth);
|
|
81
|
+
markStringifyRoot(inline_table);
|
|
78
82
|
for (const item of walkObject(value, format, depth + 1, multiline)) {
|
|
79
83
|
const inline_table_item = generateInlineItem(item);
|
|
80
84
|
insert(inline_table, inline_table, inline_table_item);
|
package/dist/dev/parse-js.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-js.js","names":[],"sources":["../../src/parse-js.ts"],"sourcesContent":["import { Value, KeyValue, Document, InlineArray, InlineTable, isKeyValue, isInlineArray, isInlineTable } from './cst';\nimport {\n generateDocument,\n generateKeyValue,\n generateInlineItem,\n generateString,\n generateInteger,\n generateFloat,\n generateBoolean,\n generateDateTime,\n generateTemporalDateTime,\n generateInlineArray,\n generateInlineTable\n} from './generate';\nimport { TomlFormat } from './toml-format';\nimport { formatTopLevel, formatEmptyLines, formatNestedTablesMultiline, normalizeGeneratedInlineRows } from './formatter';\nimport { isObject, isString, isBigInt, isInteger, isFloat, isBoolean, isDate, isTemporal } from './utils';\nimport { insert, applyWrites, applyBracketSpacing, applyTrailingComma, markStringifyRoot, setRootIndentWidth } from './writer';\nimport { prepareInsertedNestedInlineContainer } from './inline-layout';\nimport { getInlineContainerLayout, markInlineContainerPositioned, resolveInlineContainerLayout, setInlineContainerLayout } from './inline-format';\n\n/**\n * Parses a JavaScript object into a CST Document, applying formatting options from TomlFormat.\n * @param value - The JavaScript object to parse.\n * @param format - The formatting options to apply.\n * @returns The resulting CST Document.\n */\nexport default function parseJS(\n value: any,\n format: TomlFormat = TomlFormat.default(),\n depth = 0,\n parentIsMultiline = false\n): Document {\n value = toJSON(value);\n\n const document = generateDocument();\n // Enable stringify fast paths in the writer — no comments, no removals.\n markStringifyRoot(document);\n setRootIndentWidth(document, format.indentWidth);\n for (const item of walkObject(value, format, depth, parentIsMultiline)) {\n insert(document, document, item);\n }\n applyWrites(document);\n\n // Heuristics:\n // 1. Top-level objects/arrays should be tables/table arrays\n // 2. Convert nested inline tables to separate tables based on preferNestedTablesMultiline\n formatTopLevel(document, format);\n formatNestedTablesMultiline(document, format);\n for (const item of document.items) {\n if (item.type === 'Table' || item.type === 'TableArray') {\n normalizeGeneratedInlineRows(item, format.indentWidth, format.bracketSpacing);\n } else if (isKeyValue(item) && (isInlineArray(item.value) || isInlineTable(item.value))) {\n // Root key-values stay key-values when `inlineTableStart` is 0, so they\n // never reach formatTopLevel/formatNestedTablesMultiline. Their nested\n // multiline containers still need the same row/key alignment as the rows\n // of a converted table (fuzz3 seed 18515).\n normalizeGeneratedInlineRows(item, format.indentWidth, format.bracketSpacing);\n }\n }\n\n return formatEmptyLines(document);\n}\n\nfunction* walkObject(\n object: any,\n format: TomlFormat,\n depth = 0,\n parentIsMultiline = false\n): IterableIterator<KeyValue> {\n for (const key of Object.keys(object)) {\n const rawValue = object[key];\n if (rawValue === undefined) continue;\n const value = toJSON(rawValue);\n if (value === undefined) continue;\n yield generateKeyValue(\n [key],\n walkValue(value, format, depth, parentIsMultiline),\n depth > 0 && !parentIsMultiline\n );\n }\n}\n\nfunction walkValue(\n value: any,\n format: TomlFormat,\n depth: number,\n parentIsMultiline: boolean\n): Value {\n const minimumDecimals = format.minimumDecimals ?? 0;\n\n if (value === null) {\n throw new Error('\"null\" values are not supported');\n }\n if (value === undefined) {\n throw new Error('\"undefined\" values are not supported inside arrays');\n }\n\n if (isString(value)) {\n return generateString(value);\n } else if (isBigInt(value)) {\n return generateInteger(value);\n } else if (isInteger(value)) {\n return minimumDecimals > 0 ? generateFloat(value, minimumDecimals) : generateInteger(value);\n } else if (isFloat(value)) {\n return generateFloat(value, Math.max(minimumDecimals, 1));\n } else if (isBoolean(value)) {\n return generateBoolean(value);\n } else if (isTemporal(value)) {\n return generateTemporalDateTime(value, format.truncateZeroTimeInDates);\n } else if (isDate(value)) {\n return generateDateTime(value, format.truncateZeroTimeInDates);\n } else if (Array.isArray(value)) {\n return walkInlineArray(value, format, depth, parentIsMultiline);\n } else {\n return walkInlineTable(value, format, depth, parentIsMultiline);\n }\n}\n\nfunction walkInlineArray(\n value: Array<any>,\n format: TomlFormat,\n depth: number,\n parentIsMultiline: boolean\n): InlineArray {\n const inline_array = generateInlineArray();\n const multiline = !(value.length === 0 && depth > 0) &&\n resolveInlineContainerLayout('array', depth, parentIsMultiline, format);\n setInlineContainerLayout(inline_array, multiline);\n setRootIndentWidth(inline_array, format.indentWidth);\n for (const element of value) {\n const item = walkValue(element, format, depth + 1, multiline);\n const inline_array_item = generateInlineItem(item);\n\n if (!multiline && (item.type === 'InlineArray' || item.type === 'InlineTable') &&\n getInlineContainerLayout(item) === true) {\n markInlineContainerPositioned(item);\n }\n prepareInsertedNestedInlineContainer(inline_array, inline_array_item, format.indentWidth);\n insert(inline_array, inline_array, inline_array_item);\n }\n applyBracketSpacing(inline_array, inline_array, format.bracketSpacing);\n applyTrailingComma(inline_array, inline_array, format.trailingComma);\n applyWrites(inline_array);\n\n return inline_array;\n}\n\nfunction walkInlineTable(\n value: object,\n format: TomlFormat,\n depth: number,\n parentIsMultiline: boolean\n): InlineTable | Value {\n value = toJSON(value);\n if (!isObject(value)) return walkValue(value, format, depth, parentIsMultiline);\n\n const inline_table = generateInlineTable();\n const multiline = !(Object.keys(value).length === 0 && depth > 0) &&\n resolveInlineContainerLayout('table', depth, parentIsMultiline, format);\n setInlineContainerLayout(inline_table, multiline);\n setRootIndentWidth(inline_table, format.indentWidth);\n for (const item of walkObject(value, format, depth + 1, multiline)) {\n const inline_table_item = generateInlineItem(item);\n\n insert(inline_table, inline_table, inline_table_item);\n }\n applyBracketSpacing(inline_table, inline_table, format.bracketSpacing);\n applyTrailingComma(inline_table, inline_table, format.trailingComma);\n applyWrites(inline_table);\n\n return inline_table;\n}\n\n/**\n * Handles custom object serialization by checking for and using toJSON methods\n * \n * @param value - The value to potentially convert\n * @returns The result of value.toJSON() if available, otherwise the original value\n */\nfunction toJSON(value: any): any {\n // Skip null/undefined values\n if (!value) {\n return value;\n }\n \n // Skip Date objects (they have special handling)\n if (isDate(value)) {\n return value;\n }\n\n // Skip Temporal objects (they represent themselves, don't call toJSON())\n if (isTemporal(value)) {\n return value;\n }\n \n // Use object's custom toJSON method if available\n if (typeof value.toJSON === 'function') {\n return value.toJSON();\n }\n \n // Otherwise return unmodified\n return value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA2BA,SAAwB,QACtB,OACA,SAAqB,WAAW,QAAQ,GACxC,QAAQ,GACR,oBAAoB,OACV;CACV,QAAQ,OAAO,KAAK;CAEpB,MAAM,WAAW,iBAAiB;CAElC,kBAAkB,QAAQ;CAC1B,mBAAmB,UAAU,OAAO,WAAW;CAC/C,KAAK,MAAM,QAAQ,WAAW,OAAO,QAAQ,OAAO,iBAAiB,GACnE,OAAO,UAAU,UAAU,IAAI;CAEjC,YAAY,QAAQ;CAKpB,eAAe,UAAU,MAAM;CAC/B,4BAA4B,UAAU,MAAM;CAC5C,KAAK,MAAM,QAAQ,SAAS,OAC1B,IAAI,KAAK,SAAS,WAAW,KAAK,SAAS,cACzC,6BAA6B,MAAM,OAAO,aAAa,OAAO,cAAc;MACvE,IAAI,WAAW,IAAI,MAAM,cAAc,KAAK,KAAK,KAAK,cAAc,KAAK,KAAK,IAKnF,6BAA6B,MAAM,OAAO,aAAa,OAAO,cAAc;CAIhF,OAAO,iBAAiB,QAAQ;AAClC;AAEA,UAAU,WACR,QACA,QACA,QAAQ,GACR,oBAAoB,OACQ;CAC5B,KAAK,MAAM,OAAO,OAAO,KAAK,MAAM,GAAG;EACrC,MAAM,WAAW,OAAO;EACxB,IAAI,aAAa,QAAW;EAC5B,MAAM,QAAQ,OAAO,QAAQ;EAC7B,IAAI,UAAU,QAAW;EACzB,MAAM,iBACJ,CAAC,GAAG,GACJ,UAAU,OAAO,QAAQ,OA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MAAM,MACrC,8BAA8B,IAAI;EAEpC,qCAAqC,cAAc,mBAAmB,OAAO,WAAW;EACxF,OAAO,cAAc,cAAc,iBAAiB;CACtD;CACA,oBAAoB,cAAc,cAAc,OAAO,cAAc;CACrE,mBAAmB,cAAc,cAAc,OAAO,aAAa;CACnE,YAAY,YAAY;CAExB,OAAO;AACT;AAEA,SAAS,gBACP,OACA,QACA,OACA,mBACqB;CACrB,QAAQ,OAAO,KAAK;CACpB,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO,UAAU,OAAO,QAAQ,OAAO,iBAAiB;CAE9E,MAAM,eAAe,oBAAoB;CACzC,MAAM,YAAY,EAAE,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW,KAAK,QAAQ,MAC7D,6BAA6B,SAAS,OAAO,mBAAmB,MAAM;CACxE,yBAAyB,cAAc,SAAS;CAChD,mBAAmB,cAAc,OAAO,WAAW;CACnD,KAAK,MAAM,QAAQ,WAAW,OAAO,QAAQ,QAAQ,GAAG,SAAS,GAAG;EAClE,MAAM,oBAAoB,mBAAmB,IAAI;EAEjD,OAAO,cAAc,cAAc,iBAAiB;CACtD;CACA,oBAAoB,cAAc,cAAc,OAAO,cAAc;CACrE,mBAAmB,cAAc,cAAc,OAAO,aAAa;CACnE,YAAY,YAAY;CAExB,OAAO;AACT;;;;;;;AAQA,SAAS,OAAO,OAAiB;CAE/B,IAAI,CAAC,OACH,OAAO;CAIT,IAAI,OAAO,KAAK,GACd,OAAO;CAIT,IAAI,WAAW,KAAK,GAClB,OAAO;CAIT,IAAI,OAAO,MAAM,WAAW,YAC1B,OAAO,MAAM,OAAO;CAItB,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"parse-js.js","names":[],"sources":["../../src/parse-js.ts"],"sourcesContent":["import { Value, KeyValue, Document, InlineArray, InlineTable, isKeyValue, isInlineArray, isInlineTable } from './cst';\nimport {\n generateDocument,\n generateKeyValue,\n generateInlineItem,\n generateString,\n generateInteger,\n generateFloat,\n generateBoolean,\n generateDateTime,\n generateTemporalDateTime,\n generateInlineArray,\n generateInlineTable\n} from './generate';\nimport { TomlFormat } from './toml-format';\nimport { formatTopLevel, formatEmptyLines, formatNestedTablesMultiline, normalizeGeneratedInlineRows } from './formatter';\nimport { isObject, isString, isBigInt, isInteger, isFloat, isBoolean, isDate, isTemporal } from './utils';\nimport { insert, applyWrites, applyBracketSpacing, applyTrailingComma, markStringifyRoot, setRootIndentWidth } from './writer';\nimport { prepareInsertedNestedInlineContainer } from './inline-layout';\nimport { getInlineContainerLayout, markInlineContainerPositioned, resolveInlineContainerLayout, setInlineContainerLayout } from './inline-format';\n\n/**\n * Parses a JavaScript object into a CST Document, applying formatting options from TomlFormat.\n * @param value - The JavaScript object to parse.\n * @param format - The formatting options to apply.\n * @returns The resulting CST Document.\n */\nexport default function parseJS(\n value: any,\n format: TomlFormat = TomlFormat.default(),\n depth = 0,\n parentIsMultiline = false\n): Document {\n value = toJSON(value);\n\n const document = generateDocument();\n // Enable stringify fast paths in the writer — no comments, no removals.\n markStringifyRoot(document);\n setRootIndentWidth(document, format.indentWidth);\n for (const item of walkObject(value, format, depth, parentIsMultiline)) {\n insert(document, document, item);\n }\n applyWrites(document);\n\n // Heuristics:\n // 1. Top-level objects/arrays should be tables/table arrays\n // 2. Convert nested inline tables to separate tables based on preferNestedTablesMultiline\n formatTopLevel(document, format);\n\n // `formatNestedTablesMultiline` only extracts tables when inlineTableStart > 1\n // (depth-0 tables were already handled by formatTopLevel), and\n // `normalizeGeneratedInlineRows` only moves rows when a multiline container\n // was generated. Skip both full-tree passes when the format cannot trigger\n // them, which is the common default-format case.\n if ((format.inlineTableStart ?? 1) > 1) {\n formatNestedTablesMultiline(document, format);\n }\n if (format.multilineTable !== 'auto' || format.multilineArray !== 'auto') {\n for (const item of document.items) {\n if (item.type === 'Table' || item.type === 'TableArray') {\n normalizeGeneratedInlineRows(item, format.indentWidth, format.bracketSpacing);\n } else if (isKeyValue(item) && (isInlineArray(item.value) || isInlineTable(item.value))) {\n // Root key-values stay key-values when `inlineTableStart` is 0, so they\n // never reach formatTopLevel/formatNestedTablesMultiline. Their nested\n // multiline containers still need the same row/key alignment as the rows\n // of a converted table (fuzz3 seed 18515).\n normalizeGeneratedInlineRows(item, format.indentWidth, format.bracketSpacing);\n }\n }\n }\n\n return formatEmptyLines(document);\n}\n\nfunction* walkObject(\n object: any,\n format: TomlFormat,\n depth = 0,\n parentIsMultiline = false\n): IterableIterator<KeyValue> {\n for (const key of Object.keys(object)) {\n const rawValue = object[key];\n if (rawValue === undefined) continue;\n const value = toJSON(rawValue);\n if (value === undefined) continue;\n yield generateKeyValue(\n [key],\n walkValue(value, format, depth, parentIsMultiline),\n depth > 0 && !parentIsMultiline\n );\n }\n}\n\nfunction walkValue(\n value: any,\n format: TomlFormat,\n depth: number,\n parentIsMultiline: boolean\n): Value {\n const minimumDecimals = format.minimumDecimals ?? 0;\n\n if (value === null) {\n throw new Error('\"null\" values are not supported');\n }\n if (value === undefined) {\n throw new Error('\"undefined\" values are not supported inside arrays');\n }\n\n if (isString(value)) {\n return generateString(value);\n } else if (isBigInt(value)) {\n return generateInteger(value);\n } else if (isInteger(value)) {\n return minimumDecimals > 0 ? generateFloat(value, minimumDecimals) : generateInteger(value);\n } else if (isFloat(value)) {\n return generateFloat(value, Math.max(minimumDecimals, 1));\n } else if (isBoolean(value)) {\n return generateBoolean(value);\n } else if (isTemporal(value)) {\n return generateTemporalDateTime(value, format.truncateZeroTimeInDates);\n } else if (isDate(value)) {\n return generateDateTime(value, format.truncateZeroTimeInDates);\n } else if (Array.isArray(value)) {\n return walkInlineArray(value, format, depth, parentIsMultiline);\n } else {\n return walkInlineTable(value, format, depth, parentIsMultiline);\n }\n}\n\nfunction walkInlineArray(\n value: Array<any>,\n format: TomlFormat,\n depth: number,\n parentIsMultiline: boolean\n): InlineArray {\n const inline_array = generateInlineArray();\n const multiline = !(value.length === 0 && depth > 0) &&\n resolveInlineContainerLayout('array', depth, parentIsMultiline, format);\n setInlineContainerLayout(inline_array, multiline);\n setRootIndentWidth(inline_array, format.indentWidth);\n markStringifyRoot(inline_array);\n for (const element of value) {\n const item = walkValue(element, format, depth + 1, multiline);\n const inline_array_item = generateInlineItem(item);\n\n if (!multiline && (item.type === 'InlineArray' || item.type === 'InlineTable') &&\n getInlineContainerLayout(item) === true) {\n markInlineContainerPositioned(item);\n }\n prepareInsertedNestedInlineContainer(inline_array, inline_array_item, format.indentWidth);\n insert(inline_array, inline_array, inline_array_item);\n }\n applyBracketSpacing(inline_array, inline_array, format.bracketSpacing);\n applyTrailingComma(inline_array, inline_array, format.trailingComma);\n applyWrites(inline_array);\n\n return inline_array;\n}\n\nfunction walkInlineTable(\n value: object,\n format: TomlFormat,\n depth: number,\n parentIsMultiline: boolean\n): InlineTable | Value {\n value = toJSON(value);\n if (!isObject(value)) return walkValue(value, format, depth, parentIsMultiline);\n\n const inline_table = generateInlineTable();\n const multiline = !(Object.keys(value).length === 0 && depth > 0) &&\n resolveInlineContainerLayout('table', depth, parentIsMultiline, format);\n setInlineContainerLayout(inline_table, multiline);\n setRootIndentWidth(inline_table, format.indentWidth);\n markStringifyRoot(inline_table);\n for (const item of walkObject(value, format, depth + 1, multiline)) {\n const inline_table_item = generateInlineItem(item);\n\n insert(inline_table, inline_table, inline_table_item);\n }\n applyBracketSpacing(inline_table, inline_table, format.bracketSpacing);\n applyTrailingComma(inline_table, inline_table, format.trailingComma);\n applyWrites(inline_table);\n\n return inline_table;\n}\n\n/**\n * Handles custom object serialization by checking for and using toJSON methods\n * \n * @param value - The value to potentially convert\n * @returns The result of value.toJSON() if available, otherwise the original value\n */\nfunction toJSON(value: any): any {\n // Skip null/undefined values\n if (!value) {\n return value;\n }\n \n // Skip Date objects (they have special handling)\n if (isDate(value)) {\n return value;\n }\n\n // Skip Temporal objects (they represent themselves, don't call toJSON())\n if (isTemporal(value)) {\n return value;\n }\n \n // Use object's custom toJSON method if available\n if (typeof value.toJSON === 'function') {\n return value.toJSON();\n }\n \n // Otherw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,YAAY,YAAY;CAExB,OAAO;AACT;AAEA,SAAS,gBACP,OACA,QACA,OACA,mBACqB;CACrB,QAAQ,OAAO,KAAK;CACpB,IAAI,CAAC,SAAS,KAAK,GAAG,OAAO,UAAU,OAAO,QAAQ,OAAO,iBAAiB;CAE9E,MAAM,eAAe,oBAAoB;CACzC,MAAM,YAAY,EAAE,OAAO,KAAK,KAAK,CAAC,CAAC,WAAW,KAAK,QAAQ,MAC7D,6BAA6B,SAAS,OAAO,mBAAmB,MAAM;CACxE,yBAAyB,cAAc,SAAS;CAChD,mBAAmB,cAAc,OAAO,WAAW;CACnD,kBAAkB,YAAY;CAC9B,KAAK,MAAM,QAAQ,WAAW,OAAO,QAAQ,QAAQ,GAAG,SAAS,GAAG;EAClE,MAAM,oBAAoB,mBAAmB,IAAI;EAEjD,OAAO,cAAc,cAAc,iBAAiB;CACtD;CACA,oBAAoB,cAAc,cAAc,OAAO,cAAc;CACrE,mBAAmB,cAAc,cAAc,OAAO,aAAa;CACnE,YAAY,YAAY;CAExB,OAAO;AACT;;;;;;;AAQA,SAAS,OAAO,OAAiB;CAE/B,IAAI,CAAC,OACH,OAAO;CAIT,IAAI,OAAO,KAAK,GACd,OAAO;CAIT,IAAI,WAAW,KAAK,GAClB,OAAO;CAIT,IAAI,OAAO,MAAM,WAAW,YAC1B,OAAO,MAAM,OAAO;CAItB,OAAO;AACT"}
|
package/dist/dev/parse-string.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { DOUBLE_QUOTE, SINGLE_QUOTE } from "./tokenizer.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse-string.ts
|
package/dist/dev/parse-toml.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
//! @decimalturn/toml-patch v3.1.
|
|
1
|
+
//! @decimalturn/toml-patch v3.1.2 - https://github.com/DecimalTurn/toml-patch - @license: MIT
|
|
2
2
|
import { clearCachedLines, cloneLocation, clonePosition, getLine } from "./location.js";
|
|
3
3
|
import ParseError from "./parse-error.js";
|
|
4
4
|
import { DOUBLE_QUOTE, SINGLE_QUOTE, tokenize } from "./tokenizer.js";
|
|
5
5
|
import { parseString } from "./parse-string.js";
|
|
6
6
|
import Cursor from "./cursor.js";
|
|
7
7
|
import { setCommaSpace } from "./inline-comma-space.js";
|
|
8
|
-
import { createSourceAttacher } from "./cst-source.js";
|
|
9
8
|
import { DateFormatHelper, LocalDate, LocalDateTime, LocalTime, OffsetDateTime } from "./date-format.js";
|
|
10
9
|
|
|
11
10
|
//#region src/parse-toml.ts
|
|
@@ -110,14 +109,10 @@ function validatePrefixedInt(raw, input, loc, prefix, validDigits, name, invalid
|
|
|
110
109
|
}
|
|
111
110
|
function* parseTOML(input, newlineState) {
|
|
112
111
|
const cursor = new Cursor(tokenize(input, newlineState));
|
|
113
|
-
const attachSource = createSourceAttacher(input);
|
|
114
112
|
try {
|
|
115
113
|
while (!cursor.next().done) {
|
|
116
114
|
const blocks = walkBlock(cursor, input);
|
|
117
|
-
for (const block of blocks)
|
|
118
|
-
attachSource(block);
|
|
119
|
-
yield block;
|
|
120
|
-
}
|
|
115
|
+
for (const block of blocks) yield block;
|
|
121
116
|
}
|
|
122
117
|
} finally {
|
|
123
118
|
clearCachedLines();
|
|
@@ -125,23 +120,65 @@ function* parseTOML(input, newlineState) {
|
|
|
125
120
|
}
|
|
126
121
|
/**
|
|
127
122
|
* Continues parsing TOML from a remaining string and appends the results to an existing CST.
|
|
128
|
-
*
|
|
123
|
+
*
|
|
129
124
|
* @param existingAst - The existing CST to append to
|
|
130
125
|
* @param remainingString - The remaining TOML string to parse
|
|
126
|
+
* @param startPosition - Absolute source position of the first character of
|
|
127
|
+
* `remainingString`. When provided, the newly parsed blocks' locations are
|
|
128
|
+
* rebased from suffix-relative to absolute positions.
|
|
131
129
|
* @returns A new complete CST with both the existing and newly parsed items
|
|
132
130
|
*/
|
|
133
|
-
function* continueParsingTOML(existingAst, remainingString) {
|
|
131
|
+
function* continueParsingTOML(existingAst, remainingString, startPosition) {
|
|
134
132
|
for (const item of existingAst) yield item;
|
|
135
133
|
const cursor = new Cursor(tokenize(remainingString));
|
|
136
134
|
try {
|
|
137
135
|
while (!cursor.next().done) {
|
|
138
136
|
const blocks = walkBlock(cursor, remainingString);
|
|
139
|
-
for (const block of blocks)
|
|
137
|
+
for (const block of blocks) {
|
|
138
|
+
if (startPosition) rebaseBlock(block, startPosition);
|
|
139
|
+
yield block;
|
|
140
|
+
}
|
|
140
141
|
}
|
|
141
142
|
} finally {
|
|
142
143
|
clearCachedLines();
|
|
143
144
|
}
|
|
144
145
|
}
|
|
146
|
+
/**
|
|
147
|
+
* Offsets a suffix-relative position to its absolute position in the full
|
|
148
|
+
* source. The first line of the suffix is shifted by `start.column` while
|
|
149
|
+
* subsequent lines are only shifted by `start.line - 1`.
|
|
150
|
+
*/
|
|
151
|
+
function offsetPosition(position, start) {
|
|
152
|
+
return {
|
|
153
|
+
line: position.line + start.line - 1,
|
|
154
|
+
column: position.line === 1 ? position.column + start.column : position.column
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Rebases every node location in a block parsed from a suffix back to absolute
|
|
159
|
+
* positions, so source attachment and the source-aware writer see consistent
|
|
160
|
+
* offsets after an incremental `update()`.
|
|
161
|
+
*/
|
|
162
|
+
function rebaseBlock(block, start) {
|
|
163
|
+
const stack = [block];
|
|
164
|
+
while (stack.length > 0) {
|
|
165
|
+
const node = stack.pop();
|
|
166
|
+
node.loc.start = offsetPosition(node.loc.start, start);
|
|
167
|
+
node.loc.end = offsetPosition(node.loc.end, start);
|
|
168
|
+
if (node.type === "KeyValue") {
|
|
169
|
+
const keyValue = node;
|
|
170
|
+
stack.push(keyValue.value, keyValue.key);
|
|
171
|
+
} else if (node.type === "TableKey" || node.type === "TableArrayKey" || node.type === "InlineItem") stack.push(node.item);
|
|
172
|
+
else if (node.type === "Table" || node.type === "TableArray") {
|
|
173
|
+
const table = node;
|
|
174
|
+
stack.push(table.key);
|
|
175
|
+
for (let index = table.items.length - 1; index >= 0; index--) stack.push(table.items[index]);
|
|
176
|
+
} else if (node.type === "InlineTable" || node.type === "InlineArray") {
|
|
177
|
+
const items = node.items;
|
|
178
|
+
for (let index = items.length - 1; index >= 0; index--) stack.push(items[index]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
145
182
|
function comment(cursor) {
|
|
146
183
|
return {
|
|
147
184
|
type: "Comment",
|