Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Check for newline when reading or updating source. #5

Closed
wants to merge 1 commit into from

2 participants

@alex-seville

If you parse code without newline characters, i.e.

"var a =1;if ( a==1){ a=2; }"

the node range values are correct, but the source() and update() functions assume a newline character follows (node.range[1] + 1).

Instead of automatically adding 1, I added an internal function to check for the newline character and if it exists add 1, otherwise add 0.

@substack
Owner

This fix breaks a lot of tests.

@alex-seville

I'll take a look and push corrections

@alex-seville

Sorry, I think the problem is coming from esprima.
When I parse a line like:
var a=3;if(a==1){a=2;}else if(a==3){a=4;}
it has the wrong node.range values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 3 deletions.
  1. +7 −3 index.js
View
10 index.js
@@ -55,7 +55,7 @@ function insertHelpers (node, parent, chunks) {
node.source = function () {
return chunks.slice(
- node.range[0], node.range[1] + 1
+ node.range[0], lastChar(node,chunks)
).join('');
};
@@ -72,8 +72,12 @@ function insertHelpers (node, parent, chunks) {
function update (s) {
chunks[node.range[0]] = s;
- for (var i = node.range[0] + 1; i < node.range[1] + 1; i++) {
+ for (var i = node.range[0] + 1; i < lastChar(node,chunks); i++) {
chunks[i] = '';
}
- };
+ }
+
+ function lastChar(node,chunks){
+ return node.range[1] + (chunks[node.range[1]] !== '\n' ? 0 : 1);
+ }
}
Something went wrong with that request. Please try again.