Files
angular/packages/compiler/test/render3/view/parse_template_options_spec.ts
P4 6755f5354c fix(compiler): return full spans for Comment nodes (#50855)
Change sourceSpan for Comment nodes to cover the whole comment
instead of just the opening token.

The primary motivation for this is the interaction between ESLint and
`@angular-eslint`. ESLint can detect unused `eslint-disable` directives
in comments and automatically remove them when running with `--fix`.
This is based on ranges computed from AST spans, and as a result
does not work inside Angular templates - right now all comments
claim to be 4 characters long so only the opening `<!--` is removed.

PR Close #50855
2023-07-28 14:39:18 -07:00

52 lines
2.1 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ParseSourceSpan} from '../../../src/parse_util';
import {Comment} from '../../../src/render3/r3_ast';
import {parseTemplate} from '../../../src/render3/view/template';
describe('collectCommentNodes', () => {
it('should include an array of HTML comment nodes on the returned R3 AST', () => {
const html = `
<!-- eslint-disable-next-line -->
<div *ngFor="let item of items">
{{item.name}}
</div>
<div>
<p>
<!-- some nested comment -->
<span>Text</span>
</p>
</div>
`;
const templateNoCommentsOption = parseTemplate(html, '', {});
expect(templateNoCommentsOption.commentNodes).toBeUndefined();
const templateCommentsOptionDisabled = parseTemplate(html, '', {collectCommentNodes: false});
expect(templateCommentsOptionDisabled.commentNodes).toBeUndefined();
const templateCommentsOptionEnabled = parseTemplate(html, '', {collectCommentNodes: true});
expect(templateCommentsOptionEnabled.commentNodes!.length).toEqual(2);
expect(templateCommentsOptionEnabled.commentNodes![0]).toBeInstanceOf(Comment);
expect(templateCommentsOptionEnabled.commentNodes![0].value)
.toEqual('eslint-disable-next-line');
expect(templateCommentsOptionEnabled.commentNodes![0].sourceSpan)
.toBeInstanceOf(ParseSourceSpan);
expect(templateCommentsOptionEnabled.commentNodes![0].sourceSpan.toString())
.toEqual('<!-- eslint-disable-next-line -->');
expect(templateCommentsOptionEnabled.commentNodes![1]).toBeInstanceOf(Comment);
expect(templateCommentsOptionEnabled.commentNodes![1].value).toEqual('some nested comment');
expect(templateCommentsOptionEnabled.commentNodes![1].sourceSpan)
.toBeInstanceOf(ParseSourceSpan);
expect(templateCommentsOptionEnabled.commentNodes![1].sourceSpan.toString())
.toEqual('<!-- some nested comment -->');
});
});