+62
-23
@@ -6,4 +6,5 @@ "use strict"; | ||
| var explicitTablePaths = new Set(); | ||
| var currentPath = ""; | ||
| var data = Object.create(null); | ||
| var currentPath = []; | ||
| var ownedContainers = new WeakSet(); | ||
| var data = createTable(); | ||
| var context = data; | ||
@@ -62,6 +63,6 @@ | ||
| var k = keys[i]; | ||
| var intermediatePath = currentPath ? currentPath + "." + keys.slice(0, i + 1).join(".") : keys.slice(0, i + 1).join("."); | ||
| var intermediatePath = makeFullPath(keys.slice(0, i + 1)); | ||
| if (typeof target[k] === "undefined") { | ||
| target[k] = Object.create(null); | ||
| target[k] = createTable(); | ||
| assignedPaths.add(intermediatePath); | ||
@@ -72,3 +73,3 @@ } else if (typeof target[k] !== "object" || target[k] === null || Array.isArray(target[k])) { | ||
| genError("Cannot redefine existing key '" + intermediatePath + "'.", off); | ||
| } else if (explicitTablePaths.has(intermediatePath) && intermediatePath !== (Array.isArray(currentPath) ? currentPath.join(".") : currentPath)) { | ||
| } else if (explicitTablePaths.has(intermediatePath) && intermediatePath !== pathKey(currentPath)) { | ||
| genError("Cannot use dotted keys to extend table '" + intermediatePath + "' defined elsewhere.", off); | ||
@@ -80,3 +81,3 @@ } | ||
| var lastKey = keys[keys.length - 1]; | ||
| var fullPath = currentPath ? currentPath + "." + keys.join(".") : keys.join("."); | ||
| var fullPath = makeFullPath(keys); | ||
@@ -105,3 +106,3 @@ if (typeof target[lastKey] !== "undefined") { | ||
| function reduceInlineTableNode(values) { | ||
| var obj = Object.create(null); | ||
| var obj = createTable(); | ||
| var definedKeys = new Set(); | ||
@@ -119,3 +120,3 @@ | ||
| definedKeys.add(keys.join(".")); | ||
| definedKeys.add(pathKey(keys)); | ||
| } | ||
@@ -129,5 +130,5 @@ | ||
| var k = keys[i]; | ||
| var intermediatePath = keys.slice(0, i + 1).join("."); | ||
| var intermediatePath = pathKey(keys.slice(0, i + 1)); | ||
| if (typeof obj[k] === "undefined") { | ||
| obj[k] = Object.create(null); | ||
| obj[k] = createTable(); | ||
| } else if (typeof obj[k] !== "object" || obj[k] === null || Array.isArray(obj[k])) { | ||
@@ -142,3 +143,3 @@ genError("Cannot redefine existing key '" + intermediatePath + "'.", off); | ||
| if (typeof obj[lastKey] !== "undefined") { | ||
| genError("Cannot redefine existing key '" + keys.join(".") + "'.", off); | ||
| genError("Cannot redefine existing key '" + pathKey(keys) + "'.", off); | ||
| } | ||
@@ -158,3 +159,3 @@ obj[lastKey] = value; | ||
| explicitTablePaths.add(quotedPath); | ||
| context = deepRef(data, path, Object.create(null), off); | ||
| context = deepRef(data, path, createTable(), off); | ||
| currentPath = path; | ||
@@ -172,15 +173,15 @@ } | ||
| // Clear paths that start with this table array path | ||
| // Clear this table array path and paths nested under it. | ||
| assignedPaths.forEach(function(p) { | ||
| if (p.indexOf(quotedPath) === 0) assignedPaths.delete(p); | ||
| if (isSameOrSubPath(p, quotedPath)) assignedPaths.delete(p); | ||
| }); | ||
| valueAssignments.forEach(function(p) { | ||
| if (p.indexOf(quotedPath) === 0) valueAssignments.delete(p); | ||
| if (isSameOrSubPath(p, quotedPath)) valueAssignments.delete(p); | ||
| }); | ||
| assignedPaths.add(quotedPath); | ||
| context = deepRef(data, path, [], off); | ||
| currentPath = quotedPath; | ||
| context = deepRef(data, path, createTableArray(), off); | ||
| currentPath = path; | ||
| if (context instanceof Array) { | ||
| var newObj = Object.create(null); | ||
| var newObj = createTable(); | ||
| context.push(newObj); | ||
@@ -194,3 +195,2 @@ context = newObj; | ||
| function deepRef(start, keys, value, off) { | ||
| var traversedPath = ""; | ||
| var ctx = start; | ||
@@ -200,3 +200,3 @@ | ||
| var key = keys[i]; | ||
| traversedPath = traversedPath ? traversedPath + "." + key : key; | ||
| var traversedPath = pathKey(keys.slice(0, i + 1)); | ||
| if (typeof ctx[key] === "undefined") { | ||
@@ -206,3 +206,3 @@ if (i === keys.length - 1) { | ||
| } else { | ||
| ctx[key] = Object.create(null); | ||
| ctx[key] = createTable(); | ||
| } | ||
@@ -214,4 +214,15 @@ } else if (i !== keys.length - 1 && valueAssignments.has(traversedPath)) { | ||
| ctx = ctx[key]; | ||
| if (ctx instanceof Array && ctx.length && i < keys.length - 1) { | ||
| ctx = ctx[ctx.length - 1]; | ||
| if (i < keys.length - 1) { | ||
| if (!isOwnedContainer(ctx)) { | ||
| genError("Cannot redefine existing key '" + traversedPath + "'.", off); | ||
| } | ||
| if (ctx instanceof Array) { | ||
| if (!ctx.length) { | ||
| genError("Cannot redefine existing key '" + traversedPath + "'.", off); | ||
| } | ||
| ctx = ctx[ctx.length - 1]; | ||
| if (!isOwnedContainer(ctx)) { | ||
| genError("Cannot redefine existing key '" + traversedPath + "'.", off); | ||
| } | ||
| } | ||
| } | ||
@@ -234,2 +245,30 @@ } | ||
| } | ||
| function createTable() { | ||
| var table = Object.create(null); | ||
| ownedContainers.add(table); | ||
| return table; | ||
| } | ||
| function createTableArray() { | ||
| var tableArray = []; | ||
| ownedContainers.add(tableArray); | ||
| return tableArray; | ||
| } | ||
| function isOwnedContainer(value) { | ||
| return value !== null && typeof value === "object" && ownedContainers.has(value); | ||
| } | ||
| function pathKey(keys) { | ||
| return keys.map(quoteDottedString).join("."); | ||
| } | ||
| function makeFullPath(keys) { | ||
| return pathKey(currentPath.concat(keys)); | ||
| } | ||
| function isSameOrSubPath(path, prefix) { | ||
| return path === prefix || path.indexOf(prefix + ".") === 0; | ||
| } | ||
| } | ||
@@ -236,0 +275,0 @@ |
+1
-1
| { | ||
| "name": "toml", | ||
| "version": "4.1.1", | ||
| "version": "4.1.2", | ||
| "description": "TOML parser for Node.js (TOML v1.1.0 compliant)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
124830
0.7%4354
0.76%