Skip to content

Commit

Permalink
fix: fix range on HTMLClose comment on first line
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Oct 25, 2020
1 parent 287b77c commit c445b90
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/lexer/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,15 @@ export function skipSingleLineComment(
column: endColumn
}
};
parser.onComment(
CommentTypes[type & 0xff],
source.slice(index, end),
// For Single, start before "//",
// For HTMLOpen, start before "<!--",
// For HTMLClose, start before "\n-->"
index - (type === CommentType.Single ? 2 : 4),
end,
loc
);
// For Single, start before "//",
// For HTMLOpen, start before "<!--",
// For HTMLClose, start before "\n-->"
let start = index - (type === CommentType.Single ? 2 : 4);
// HTMLClose would start with "-->" on first line.
if (start < 0) {
start = 0;
}
parser.onComment(CommentTypes[type & 0xff], source.slice(index, end), start, end, loc);
}
return state | LexerState.NewLine;
}
Expand Down
23 changes: 23 additions & 0 deletions test/parser/miscellaneous/onComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,27 @@ describe('Miscellaneous - onComment', () => {
}
]);
});

it('should extract htmlclose comment on first line', () => {
const arr: any[] = [];
parseScript('--> comment #2\n', {
ranges: true,
loc: true,
onComment: arr,
webcompat: true
});
t.deepEqual(arr, [
{
type: 'HTMLClose',
value: ' comment #2',
start: 0,
end: 14,
range: [0, 14],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 14 }
}
}
]);
});
});

0 comments on commit c445b90

Please sign in to comment.