Skip to content

Commit

Permalink
fix: fix ending loc of empty comment
Browse files Browse the repository at this point in the history
closes #126
  • Loading branch information
3cp committed Oct 27, 2020
1 parent 38aed7d commit d62d0b8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lexer/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export function skipSingleLineComment(
column: number
): LexerState {
const { index } = parser;
parser.tokenPos = parser.index;
parser.linePos = parser.line;
parser.colPos = parser.column;
while (parser.index < parser.end) {
if (CharTypes[parser.currentChar] & CharFlags.LineTerminator) {
const isCR = parser.currentChar === Chars.CarriageReturn;
Expand Down
59 changes: 59 additions & 0 deletions test/parser/miscellaneous/onComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ describe('Miscellaneous - onComment', () => {
t.equal(onCommentCount, 1);
});

it('should extract single line empty comment', () => {
let onCommentCount = 0;
t.deepEqual(
parseScript('//\n', {
ranges: true,
loc: true,
onComment: (type: string, value: string, start: number, end: number, loc: SourceLocation) => {
t.deepEqual(type, 'SingleLine');
t.deepEqual(value, '');
t.equal(start, 0);
t.equal(end, 2);
t.deepEqual(loc, {
start: { line: 1, column: 0 },
end: { line: 1, column: 2 }
});
onCommentCount++;
}
}),
{
body: [],
sourceType: 'script',
type: 'Program',
start: 0,
end: 3,
range: [0, 3],
loc: {
start: { line: 1, column: 0 },
end: { line: 2, column: 0 }
}
}
);
t.equal(onCommentCount, 1);
});

it('should extract single line comment with trailing new line', () => {
let onCommentCount = 0;
t.deepEqual(
Expand Down Expand Up @@ -164,6 +198,31 @@ describe('Miscellaneous - onComment', () => {
t.equal(onCommentCount, 1);
});

it('should extract empty multiline line comment', () => {
let onCommentCount = 0;
t.deepEqual(
parseScript('/**/', {
onComment: (type: string, value: string, start: number, end: number, loc: SourceLocation) => {
t.deepEqual(type, 'MultiLine');
t.deepEqual(value, '');
t.equal(start, 0);
t.equal(end, 4);
t.deepEqual(loc, {
start: { line: 1, column: 0 },
end: { line: 1, column: 4 }
});
onCommentCount++;
}
}),
{
body: [],
sourceType: 'script',
type: 'Program'
}
);
t.equal(onCommentCount, 1);
});

it('should extract multiline line comment', () => {
let onCommentCount = 0;
t.deepEqual(
Expand Down

0 comments on commit d62d0b8

Please sign in to comment.