Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

it seems to work now with this start.pos === end.pos fix

  • Loading branch information...
commit 45240c16a209a89bced59c7c1f89f860f6d186f5 1 parent 204d3c8
@substack authored
Showing with 29 additions and 19 deletions.
  1. +1 −1  ex.js
  2. +27 −17 index.js
  3. +1 −1  stub.json
View
2  ex.js
@@ -5,7 +5,7 @@ var parse = require('uglify-js').parser.parse;
var src = fs.readFileSync(__dirname + '/stub.json', 'utf8');
var s = jsup(src)
.set([ 'a', 2 ], 3)
-// .set([ 'c' ], 'lul')
+ .set([ 'c' ], 'lul')
.stringify()
;
console.log(s);
View
44 index.js
@@ -16,30 +16,40 @@ var jsup = module.exports = function (src) {
}, ann);
var res = traverse(obj).get(path);
- var start = cur.node.start.pos - 2;
- var end = cur.node.end.pos - 1;
-
if (cur.node.name === 'array') {
- if (cur.node.value[0][key]) {
- var c = cur.node.value[0][key][0];
- start = c.start.pos - 2;
- end = c.end.pos - 1;
+ if (!cur.node.value[0][key]) {
+ throw new Error('array node does not exist')
+ }
+
+ var c = cur.node.value[0][key];
+ }
+ else if (cur.node.name === 'object') {
+ var xs = cur.node.value[0];
+ var c = null;
+
+ for (var i = 0; i < xs.length; i++) {
+ if (xs[i][0] === key) {
+ c = xs[i][1];
+ break;
+ }
}
- else throw new Error('node does not exist')
- var before = src.slice(0, start);
- var after = src.slice(end);
- src = before + JSON.stringify(value) + after;
+ if (!c) throw new Error('object node does not exist');
+ }
+ else throw new Error('unexpected node type: ' + cur.node.name)
+
+ var start = c[0].start.pos - 2;
+
+ if (c[0].start.pos === c[0].end.pos) {
+ var end = start + c[1].toString().length;
}
else {
- var before = src.slice(0, start);
- var after = src.slice(end);
- src = before + JSON.stringify(res) + after;
+ var end = c[0].end.pos - 1;
}
-console.log('before=<' + before + '>');
-console.log('after=<' + after + '>');
-console.log('src=<' + src + '>');
+ var before = src.slice(0, start);
+ var after = src.slice(end);
+ src = before + JSON.stringify(value) + after;
ann = jsup.annotate(src, obj);
View
2  stub.json
@@ -1,6 +1,6 @@
{
"a" : [ 1, 2, 0 ] ,
"b" : [ 3, 4, { "c" : [ 5, 6 ] } ],
- "c" : "moo",
+ "c" : 444444,
"d" : null
}
Please sign in to comment.
Something went wrong with that request. Please try again.