Permalink
Browse files

Set correct end source position for nodes

  • Loading branch information...
1 parent 69b685c commit ce2013d8b1fda9587080b0d69a754bfd5fb2e695 @ai ai committed Oct 7, 2016
View
@@ -9,7 +9,8 @@ export default function parse(source, opts) {
let input = new Input(source, opts);
let parser = new Parser(input);
- parser.parts = preprocess(input, liner(tokenizer(input)));
+ parser.tokens = tokenizer(input);
+ parser.parts = preprocess(input, liner(parser.tokens));
parser.loop();
return parser.root;
View
@@ -52,6 +52,17 @@ export default class Parser {
this.pos += 1;
}
+
+ for ( let i = this.tokens.length - 1; i >= 0; i-- ) {
+ if ( this.tokens[i].length > 3 ) {
+ let last = this.tokens[i];
+ this.root.source.end = {
+ line: last[4] || last[2],
+ column: last[5] || last[3]
+ };
+ break;
+ }
+ }
}
comment(part) {
@@ -294,7 +305,15 @@ export default class Parser {
}
node[prop] = value;
- let last = tokens[tokens.length - 1] || altLast;
+ let last;
+ for ( let i = tokens.length - 1; i >= 0; i-- ) {
+ if ( tokens[i].length > 2 ) {
+ last = tokens[i];
+ break;
+ }
+ }
+ if ( !last ) last = altLast;
+
node.source.end = {
line: last[4] || last[2],
column: last[5] || last[3]
View
@@ -389,6 +389,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 21,
+ "column": 21
}
}
}
@@ -133,6 +133,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 8,
+ "column": 22
}
}
}
@@ -431,6 +431,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 24,
+ "column": 19
}
}
}
@@ -147,6 +147,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 6,
+ "column": 12
}
}
}
@@ -100,6 +100,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 10,
+ "column": 19
}
}
}
@@ -35,6 +35,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 1,
+ "column": 2
}
}
}
@@ -60,6 +60,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 2,
+ "column": 22
}
}
}
@@ -260,6 +260,10 @@
"start": {
"line": 1,
"column": 1
+ },
+ "end": {
+ "line": 17,
+ "column": 14
}
}
}
View
@@ -84,6 +84,14 @@ test('keeps last new line', t => {
t.deepEqual(parse('color: black\n').raws.after, '\n');
});
+test('generates correct source maps on trailing spaces', t => {
+ t.deepEqual(parse('a: 1 ').first.source.end.line, 1);
+});
+
+test('sets end position for root', t => {
+ t.deepEqual(parse('a\n b: 1\n').source.end, { line: 2, column: 6 });
+});
+
let tests = fs.readdirSync(path.join(__dirname, 'cases'))
.filter(i => path.extname(i) === '.sss' );

0 comments on commit ce2013d

Please sign in to comment.