Skip to content

Commit a864999

Browse files
committed
Fix computation of end offset of comment
1 parent 01dcca9 commit a864999

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/nodes/inline-comment.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
if (token[0] === 'word' && token[1].slice(0, 2) === '//') {
99
const first = token;
1010
const bits = [];
11-
let last;
11+
let endOffset;
1212
let remainingInput;
1313

1414
while (token) {
@@ -20,7 +20,12 @@ module.exports = {
2020

2121
// Get remaining input and retokenize
2222
remainingInput = token[1].substring(token[1].indexOf('\n'));
23-
remainingInput += this.input.css.valueOf().substring(this.tokenizer.position());
23+
const untokenizedRemainingInput = this.input.css
24+
.valueOf()
25+
.substring(this.tokenizer.position());
26+
remainingInput += untokenizedRemainingInput;
27+
28+
endOffset = token[3] + untokenizedRemainingInput.length - remainingInput.length;
2429
} else {
2530
// If the tokenizer went to the next line go back
2631
this.tokenizer.back(token);
@@ -29,11 +34,12 @@ module.exports = {
2934
}
3035

3136
bits.push(token[1]);
32-
last = token;
37+
// eslint-disable-next-line prefer-destructuring
38+
endOffset = token[2];
3339
token = this.tokenizer.nextToken({ ignoreUnclosed: true });
3440
}
3541

36-
const newToken = ['comment', bits.join(''), first[2], last[2]];
42+
const newToken = ['comment', bits.join(''), first[2], endOffset];
3743
this.inlineComment(newToken);
3844

3945
// Replace tokenizer to retokenize the rest of the string

test/parser/comments.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ test('inline comments with asterisk are persisted (#135)', (t) => {
180180
t.is(nodeToString(root), less);
181181
});
182182

183-
test.only('handles single quotes in comments (#163)', (t) => {
183+
test('handles single quotes in comments (#163)', (t) => {
184184
const less = `a {\n // '\n color: pink;\n}\n\n/** ' */`;
185185

186186
const root = parse(less);
@@ -203,8 +203,7 @@ test.only('handles single quotes in comments (#163)', (t) => {
203203
t.is(innerCommentNode.source.start.line, 2);
204204
t.is(innerCommentNode.source.start.column, 3);
205205
t.is(innerCommentNode.source.end.line, 2);
206-
// TODO: this test is failing
207-
// t.is(innerCommentNode.source.end.column, 6);
206+
t.is(innerCommentNode.source.end.column, 6);
208207

209208
t.is(declarationNode.source.start.line, 3);
210209
t.is(declarationNode.source.start.column, 3);

0 commit comments

Comments
 (0)