Skip to content

Commit 1df9147

Browse files
committed
Fix span.start for Messages/Terms with Comments, fixes #255
Move the message.span.start logic from the wrapper to the point where the comment is hooked up to the message. As the message already has the span when it gets there, the wrapper refuses to do it again.
1 parent 6aee471 commit 1df9147

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

fluent-syntax/src/parser.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function withSpan(fn) {
1414
return fn.call(this, ps, ...args);
1515
}
1616

17-
let start = ps.getIndex();
17+
const start = ps.getIndex();
1818
const node = fn.call(this, ps, ...args);
1919

2020
// Don't re-add the span if the node already has it. This may happen when
@@ -23,13 +23,6 @@ function withSpan(fn) {
2323
return node;
2424
}
2525

26-
// Spans of Messages should include the attached Comment.
27-
if (node.type === "Message") {
28-
if (node.comment !== null) {
29-
start = node.comment.span.start;
30-
}
31-
}
32-
3326
const end = ps.getIndex();
3427
node.addSpan(start, end);
3528
return node;
@@ -80,6 +73,9 @@ export default class FluentParser {
8073
if (lastComment) {
8174
if (entry.type === "Message" || entry.type === "Term") {
8275
entry.comment = lastComment;
76+
if (this.withSpans) {
77+
entry.span.start = entry.comment.span.start;
78+
}
8379
} else {
8480
entries.push(lastComment);
8581
}

fluent-syntax/test/fixtures_structure/whitespace_leading.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"span": {
4747
"type": "Span",
48-
"start": 21,
48+
"start": 0,
4949
"end": 46
5050
}
5151
},
@@ -93,7 +93,7 @@
9393
},
9494
"span": {
9595
"type": "Span",
96-
"start": 65,
96+
"start": 48,
9797
"end": 84
9898
}
9999
},

fluent-syntax/test/fixtures_structure/whitespace_trailing.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"span": {
4747
"type": "Span",
48-
"start": 27,
48+
"start": 0,
4949
"end": 53
5050
}
5151
},
@@ -93,7 +93,7 @@
9393
},
9494
"span": {
9595
"type": "Span",
96-
"start": 77,
96+
"start": 55,
9797
"end": 96
9898
}
9999
},

0 commit comments

Comments
 (0)