test(compiler): check `fullStart` source-span (#42062)

The tests were checking that the source-span of parsed HTML nodes were
accurate, but they were not checking the span when it includes the
"leading trivia", which are given by the `fullStart` rather than `start`
location.

PR Close #42062
This commit is contained in:
Pete Bacon Darwin 2021-05-20 13:18:33 +01:00 committed by atscott
parent 8a54896a91
commit 973f9b8d19
2 changed files with 21 additions and 16 deletions

View File

@ -87,6 +87,10 @@ class _Humanizer implements html.Visitor {
private _appendContext(ast: html.Node, input: any[]): any[] {
if (!this.includeSourceSpan) return input;
input.push(ast.sourceSpan.toString());
if (ast.sourceSpan.fullStart.offset !== ast.sourceSpan.start.offset) {
input.push(ast.sourceSpan.fullStart.file.content.substring(
ast.sourceSpan.fullStart.offset, ast.sourceSpan.end.offset));
}
return input;
}
}

View File

@ -798,22 +798,23 @@ import {humanizeDom, humanizeDomSourceSpans, humanizeLineColumn, humanizeNodes}
]);
});
it('should set the end source span excluding trailing whitespace whitespace', () => {
expect(humanizeDomSourceSpans(
parser.parse('<input type="text" />\n\n\n <span>\n</span>', 'TestComp', {
leadingTriviaChars: [' ', '\n', '\r', '\t'],
})))
.toEqual([
[
html.Element, 'input', 0, '<input type="text" />', '<input type="text" />',
'<input type="text" />'
],
[html.Attribute, 'type', 'text', ['text'], 'type="text"'],
[html.Text, '\n\n\n ', 0, ['\n\n\n '], ''],
[html.Element, 'span', 0, '<span>\n</span>', '<span>', '</span>'],
[html.Text, '\n', 1, ['\n'], ''],
]);
});
it('should not include leading trivia from the following node of an element in the end source',
() => {
expect(humanizeDomSourceSpans(
parser.parse('<input type="text" />\n\n\n <span>\n</span>', 'TestComp', {
leadingTriviaChars: [' ', '\n', '\r', '\t'],
})))
.toEqual([
[
html.Element, 'input', 0, '<input type="text" />', '<input type="text" />',
'<input type="text" />'
],
[html.Attribute, 'type', 'text', ['text'], 'type="text"'],
[html.Text, '\n\n\n ', 0, ['\n\n\n '], '', '\n\n\n '],
[html.Element, 'span', 0, '<span>\n</span>', '<span>', '</span>'],
[html.Text, '\n', 1, ['\n'], '', '\n'],
]);
});
it('should not set the end source span for elements that are implicitly closed', () => {
expect(humanizeDomSourceSpans(parser.parse('<div><p></div>', 'TestComp'))).toEqual([