From 274a0bb41ed1c1e391ad45ddb5025c5361cf84cc Mon Sep 17 00:00:00 2001 From: Richard Klafter Date: Sat, 25 Jul 2015 13:23:52 -0700 Subject: [PATCH 1/2] fixed bug --- lib/durable-json-lint-with-dependencies.js | 216 +++++++++++---------- lib/durable-json-lint.js | 4 +- src/durable-json-lint.coffee | 1 + 3 files changed, 113 insertions(+), 108 deletions(-) diff --git a/lib/durable-json-lint-with-dependencies.js b/lib/durable-json-lint-with-dependencies.js index dbf4ca2..7f8fba0 100644 --- a/lib/durable-json-lint-with-dependencies.js +++ b/lib/durable-json-lint-with-dependencies.js @@ -3894,112 +3894,112 @@ parseStatement: true, parseSourceElement: true */ })); /* vim: set sw=4 ts=4 et tw=80 : */ -(function() { -var parse = typeof(module)=='undefined' ? window.esprima.parse : require('esprima').parse; -var objectKeys = Object.keys || function (obj) { - var keys = []; - for (var key in obj) keys.push(key); - return keys; -}; -var forEach = function (xs, fn) { - if (xs.forEach) return xs.forEach(fn); - for (var i = 0; i < xs.length; i++) { - fn.call(xs, xs[i], i, xs); - } -}; - -var isArray = Array.isArray || function (xs) { - return Object.prototype.toString.call(xs) === '[object Array]'; -}; - -var falafel = function (src, opts, fn, breadthFirstFn) { - if (typeof opts === 'function') { - breadthFirstFn = fn; - fn = opts; - opts = {}; - } - if (typeof src === 'object') { - opts = src; - src = opts.source; - delete opts.source; - } - src = src || opts.source; - opts.range = true; - if (typeof src !== 'string') { src = String(src); } - if (!breadthFirstFn) { breadthFirstFn = function(){}; } - - var ast = opts.ast || parse(src, opts); - delete opts.ast; - - var result = { - chunks : src.split(''), - toString : function () { return result.chunks.join(''); }, - inspect : function () { return result.toString(); } - }; - var index = 0; - - (function walk (node, parent) { - insertHelpers(node, parent, result.chunks); - breadthFirstFn(node); - insertHelpers2(node, parent, result.chunks); - forEach(objectKeys(node), function (key) { - if (key === 'parent') { return; } - var child = node[key]; - if (isArray(child)) { - forEach(child, function (c) { - if (c && typeof c.type === 'string') { - walk(c, node); - } - }); - } - else if (child && typeof child.type === 'string') { - insertHelpers(child, node, result.chunks); - walk(child, node); - } - }); - fn(node); - })(ast, undefined); - - return result; -}; - -function insertHelpers (node, parent, chunks) { - if (!node.range) return; - - node.parent = parent; - - node.source = function () { - return chunks.slice( - node.range[0], node.range[1] - ).join(''); - }; -} - -function insertHelpers2 (node, parent, chunks) { - if (node.update && typeof node.update === 'object') { - var prev = node.update; - forEach(objectKeys(prev), function (key) { - update[key] = prev[key]; - }); - node.update = update; - } - else { - node.update = update; - } - - function update (s) { - chunks[node.range[0]] = s; - for (var i = node.range[0] + 1; i < node.range[1]; i++) { - chunks[i] = ''; - } - }; -} - -//export -if(typeof(module)==='undefined') { window.falafel = falafel; } -else { module.exports = falafel; } - -})(); +(function() { +var parse = typeof(module)=='undefined' ? window.esprima.parse : require('esprima').parse; +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) keys.push(key); + return keys; +}; +var forEach = function (xs, fn) { + if (xs.forEach) return xs.forEach(fn); + for (var i = 0; i < xs.length; i++) { + fn.call(xs, xs[i], i, xs); + } +}; + +var isArray = Array.isArray || function (xs) { + return Object.prototype.toString.call(xs) === '[object Array]'; +}; + +var falafel = function (src, opts, fn, breadthFirstFn) { + if (typeof opts === 'function') { + breadthFirstFn = fn; + fn = opts; + opts = {}; + } + if (typeof src === 'object') { + opts = src; + src = opts.source; + delete opts.source; + } + src = src || opts.source; + opts.range = true; + if (typeof src !== 'string') { src = String(src); } + if (!breadthFirstFn) { breadthFirstFn = function(){}; } + + var ast = opts.ast || parse(src, opts); + delete opts.ast; + + var result = { + chunks : src.split(''), + toString : function () { return result.chunks.join(''); }, + inspect : function () { return result.toString(); } + }; + var index = 0; + + (function walk (node, parent) { + insertHelpers(node, parent, result.chunks); + breadthFirstFn(node); + insertHelpers2(node, parent, result.chunks); + forEach(objectKeys(node), function (key) { + if (key === 'parent') { return; } + var child = node[key]; + if (isArray(child)) { + forEach(child, function (c) { + if (c && typeof c.type === 'string') { + walk(c, node); + } + }); + } + else if (child && typeof child.type === 'string') { + insertHelpers(child, node, result.chunks); + walk(child, node); + } + }); + fn(node); + })(ast, undefined); + + return result; +}; + +function insertHelpers (node, parent, chunks) { + if (!node.range) return; + + node.parent = parent; + + node.source = function () { + return chunks.slice( + node.range[0], node.range[1] + ).join(''); + }; +} + +function insertHelpers2 (node, parent, chunks) { + if (node.update && typeof node.update === 'object') { + var prev = node.update; + forEach(objectKeys(prev), function (key) { + update[key] = prev[key]; + }); + node.update = update; + } + else { + node.update = update; + } + + function update (s) { + chunks[node.range[0]] = s; + for (var i = node.range[0] + 1; i < node.range[1]; i++) { + chunks[i] = ''; + } + }; +} + +//export +if(typeof(module)==='undefined') { window.falafel = falafel; } +else { module.exports = falafel; } + +})(); (function() { var esprima, falafel, jsonLint; @@ -4023,6 +4023,9 @@ else { module.exports = falafel; } ] }; } + src = src.replace(/[\u0000-\u001f]/g, function(c) { + return JSON.stringify(c).slice(1, -1); + }); wrappedSrc = "(function(){return " + src + '\n})();'; errors = []; try { @@ -4136,7 +4139,6 @@ else { module.exports = falafel; } node.valid = false; return node.correct = ""; default: - console.log(node.type); node.valid = false; return createError(node, "fail", "A \"" + node.type + "\" is an invalid Json element."); } diff --git a/lib/durable-json-lint.js b/lib/durable-json-lint.js index 1439e3d..e8dcd77 100644 --- a/lib/durable-json-lint.js +++ b/lib/durable-json-lint.js @@ -20,6 +20,9 @@ ] }; } + src = src.replace(/[\u0000-\u001f]/g, function(c) { + return JSON.stringify(c).slice(1, -1); + }); wrappedSrc = "(function(){return " + src + '\n})();'; errors = []; try { @@ -133,7 +136,6 @@ node.valid = false; return node.correct = ""; default: - console.log(node.type); node.valid = false; return createError(node, "fail", "A \"" + node.type + "\" is an invalid Json element."); } diff --git a/src/durable-json-lint.coffee b/src/durable-json-lint.coffee index fb14eb0..8283a0c 100644 --- a/src/durable-json-lint.coffee +++ b/src/durable-json-lint.coffee @@ -2,6 +2,7 @@ esprima = if typeof module == 'undefined' then window.esprima else require('espr falafel = if typeof module == 'undefined' then window.falafel else require('free-falafel') jsonLint=(src)-> if !src or /^\s*$/.test(src) then return {json:null, errors:[{lineNumber:1,column:1,description:"An empty string is not valid Json",status:"crash"}]} + src = src.replace(/[\u0000-\u001f]/g, (c)->JSON.stringify(c).slice(1,-1)) #escape troublesome characters wrappedSrc = "(function(){return "+src+'\n})();' errors = [] try From bb1bdf479d5a0420b1a00690e3ee372eaeb46b7e Mon Sep 17 00:00:00 2001 From: Richard Klafter Date: Sat, 25 Jul 2015 13:26:37 -0700 Subject: [PATCH 2/2] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1e23bf9..b08379c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "git" , "url": "https://github.com/freethenation/durable-json-lint.git" }, - "version": "0.0.1", + "version": "0.0.2", "private": false, "dependencies": { "free-falafel": "0.1.6",