convert `TemplateComment` back to `Comment`

pull/16515/head
ComputerGuy 1 month ago
parent e2f4ad359f
commit 719860a27c

@ -190,7 +190,7 @@ export function convert(source, ast) {
ClassDirective(node) {
return { ...node, type: 'Class' };
},
TemplateComment(node) {
Comment(node) {
return {
...node,
ignores: extract_svelte_ignore(node.start, node.data, false)

@ -1362,7 +1362,7 @@ const template = {
);
}
},
TemplateComment(node, { state }) {
Comment(node, { state }) {
const migrated = migrate_svelte_ignore(node.data);
if (migrated !== node.data) {
state.str.overwrite(node.start + '<!--'.length, node.end - '-->'.length, migrated);

@ -59,7 +59,7 @@ export default function element(parser) {
parser.eat('-->', true);
parser.append({
type: 'TemplateComment',
type: 'Comment',
start,
end: parser.index,
data
@ -302,7 +302,7 @@ export default function element(parser) {
if (is_top_level_script_or_style) {
parser.eat('>', true);
/** @type {AST.TemplateComment | null} */
/** @type {AST.Comment | null} */
let prev_comment = null;
for (let i = current.fragment.nodes.length - 1; i >= 0; i--) {
const node = current.fragment.nodes[i];
@ -311,7 +311,7 @@ export default function element(parser) {
break;
}
if (node.type === 'TemplateComment') {
if (node.type === 'Comment') {
prev_comment = node;
break;
} else if (node.type !== 'Text' || node.data.trim()) {

@ -89,13 +89,13 @@ const visitors = {
/** @type {string[]} */
const ignores = [];
if (parent?.type === 'Fragment' && node.type !== 'TemplateComment' && node.type !== 'Text') {
if (parent?.type === 'Fragment' && node.type !== 'Comment' && node.type !== 'Text') {
const idx = parent.nodes.indexOf(/** @type {any} */ (node));
for (let i = idx - 1; i >= 0; i--) {
const prev = parent.nodes[i];
if (prev.type === 'TemplateComment') {
if (prev.type === 'Comment') {
ignores.push(
...extract_svelte_ignore(
prev.start + 4 /* '<!--'.length */,

@ -71,7 +71,7 @@ export function SnippetBlock(node, context) {
(node) =>
node.type !== 'SnippetBlock' &&
(node.type !== 'Text' || node.data.trim()) &&
node.type !== 'TemplateComment'
node.type !== 'Comment'
)
) {
e.snippet_conflict(node);

@ -34,7 +34,7 @@ export function Text(node, context) {
for (const child of parent.nodes) {
if (child === node) break;
if (child.type === 'TemplateComment') {
if (child.type === 'Comment') {
is_ignored ||= extract_svelte_ignore(
child.start + 4,
child.data,

@ -510,7 +510,7 @@ export function check_element(node, context) {
}
case 'figure': {
const children = node.fragment.nodes.filter((node) => {
if (node.type === 'TemplateComment') return false;
if (node.type === 'Comment') return false;
if (node.type === 'Text') return regex_not_whitespace.test(node.data);
return true;
});

@ -127,14 +127,14 @@ export function visit_component(node, context) {
context.visit(attribute, attribute.type === 'LetDirective' ? default_state : context.state);
}
/** @type {AST.TemplateComment[]} */
/** @type {AST.Comment[]} */
let comments = [];
/** @type {Record<string, AST.Fragment['nodes']>} */
const nodes = { default: [] };
for (const child of node.fragment.nodes) {
if (child.type === 'TemplateComment') {
if (child.type === 'Comment') {
comments.push(child);
continue;
}

@ -98,7 +98,7 @@ export function validate_element(node, context) {
} else if (
parent.body.nodes.filter(
(n) =>
n.type !== 'TemplateComment' &&
n.type !== 'Comment' &&
n.type !== 'ConstTag' &&
(n.type !== 'Text' || n.data.trim() !== '')
).length > 1

@ -19,6 +19,7 @@ import { BlockStatement } from './visitors/BlockStatement.js';
import { BreakStatement } from './visitors/BreakStatement.js';
import { CallExpression } from './visitors/CallExpression.js';
import { ClassBody } from './visitors/ClassBody.js';
import { Comment } from './visitors/Comment.js';
import { Component } from './visitors/Component.js';
import { ConstTag } from './visitors/ConstTag.js';
import { DebugTag } from './visitors/DebugTag.js';
@ -52,7 +53,6 @@ import { SvelteBoundary } from './visitors/SvelteBoundary.js';
import { SvelteHead } from './visitors/SvelteHead.js';
import { SvelteSelf } from './visitors/SvelteSelf.js';
import { SvelteWindow } from './visitors/SvelteWindow.js';
import { TemplateComment } from './visitors/TemplateComment.js';
import { TitleElement } from './visitors/TitleElement.js';
import { TransitionDirective } from './visitors/TransitionDirective.js';
import { UpdateExpression } from './visitors/UpdateExpression.js';
@ -96,6 +96,7 @@ const visitors = {
BreakStatement,
CallExpression,
ClassBody,
Comment,
Component,
ConstTag,
DebugTag,
@ -129,7 +130,6 @@ const visitors = {
SvelteHead,
SvelteSelf,
SvelteWindow,
TemplateComment,
TitleElement,
TransitionDirective,
UpdateExpression,

@ -2,10 +2,10 @@
/** @import { ComponentContext } from '../types' */
/**
* @param {AST.TemplateComment} node
* @param {AST.Comment} node
* @param {ComponentContext} context
*/
export function TemplateComment(node, context) {
export function Comment(node, context) {
// We'll only get here if comments are not filtered out, which they are unless preserveComments is true
context.state.template.push_comment(node.data);
}

@ -28,7 +28,7 @@ export const empty_comment = b.literal(EMPTY_COMMENT);
* @param {ComponentContext} context
*/
export function process_children(nodes, { visit, state }) {
/** @type {Array<AST.Text | AST.TemplateComment | AST.ExpressionTag>} */
/** @type {Array<AST.Text | AST.Comment | AST.ExpressionTag>} */
let sequence = [];
function flush() {
@ -41,9 +41,9 @@ export function process_children(nodes, { visit, state }) {
for (let i = 0; i < sequence.length; i++) {
const node = sequence[i];
if (node.type === 'Text' || node.type === 'TemplateComment') {
if (node.type === 'Text' || node.type === 'Comment') {
quasi.value.cooked +=
node.type === 'TemplateComment' ? `<!--${node.data}-->` : escape_html(node.data);
node.type === 'Comment' ? `<!--${node.data}-->` : escape_html(node.data);
} else {
const evaluated = state.scope.evaluate(node.expression);
@ -68,7 +68,7 @@ export function process_children(nodes, { visit, state }) {
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
if (node.type === 'Text' || node.type === 'TemplateComment' || node.type === 'ExpressionTag') {
if (node.type === 'Text' || node.type === 'Comment' || node.type === 'ExpressionTag') {
sequence.push(node);
} else {
if (sequence.length > 0) {

@ -165,7 +165,7 @@ export function clean_nodes(
const regular = [];
for (const node of nodes) {
if (node.type === 'TemplateComment' && !preserve_comments) {
if (node.type === 'Comment' && !preserve_comments) {
continue;
}
@ -285,7 +285,7 @@ export function clean_nodes(
// and would still call node.replaceWith() on the script tag, it would be a no-op because the script tag has no parent.
if (trimmed.length === 1 && first.type === 'RegularElement' && first.name === 'script') {
trimmed.push({
type: 'TemplateComment',
type: 'Comment',
data: '',
start: -1,
end: -1

@ -15,7 +15,7 @@ export namespace _CSS {
end: number;
styles: string;
/** Possible comment atop the style tag */
comment: AST.TemplateComment | null;
comment: AST.Comment | null;
};
}

@ -210,7 +210,7 @@ export interface LegacyWindow extends BaseElement {
}
export interface LegacyComment extends BaseNode {
type: 'TemplateComment';
type: 'Comment';
/** the contents of the comment */
data: string;
/** any svelte-ignore directives — <!-- svelte-ignore a b c --> would result in ['a', 'b', 'c'] */

@ -43,7 +43,7 @@ export namespace AST {
export interface Fragment {
type: 'Fragment';
nodes: Array<Text | Tag | ElementLike | Block | TemplateComment>;
nodes: Array<Text | Tag | ElementLike | Block | Comment>;
/** @internal */
metadata: {
/**
@ -144,8 +144,9 @@ export namespace AST {
}
/** An HTML comment */
export interface TemplateComment extends BaseNode {
type: 'TemplateComment';
// TODO rename to disambiguate
export interface Comment extends BaseNode {
type: 'Comment';
/** the contents of the comment */
data: string;
}
@ -613,7 +614,7 @@ export namespace AST {
| AST.SpreadAttribute
| Directive
| AST.AttachTag
| AST.TemplateComment
| AST.Comment
| Block;
export type SvelteNode = Node | TemplateNode | AST.Fragment | _CSS.Node | Script;

@ -5,7 +5,7 @@
"end": 31,
"children": [
{
"type": "TemplateComment",
"type": "Comment",
"start": 0,
"end": 31,
"data": " svelte-ignore foo, bar ",

@ -5,7 +5,7 @@
"end": 18,
"children": [
{
"type": "TemplateComment",
"type": "Comment",
"start": 0,
"end": 18,
"data": " a comment ",

@ -21,7 +21,7 @@
"data": "\n"
},
{
"type": "TemplateComment",
"type": "Comment",
"start": 45,
"end": 69,
"data": " prettier-ignore ",
@ -83,7 +83,7 @@
"data": "\n"
},
{
"type": "TemplateComment",
"type": "Comment",
"start": 162,
"end": 186,
"data": " prettier-ignore ",

@ -8,7 +8,7 @@
"type": "Fragment",
"nodes": [
{
"type": "TemplateComment",
"type": "Comment",
"start": 0,
"end": 27,
"data": "should not error out"

@ -1145,7 +1145,7 @@ declare module 'svelte/compiler' {
export interface Fragment {
type: 'Fragment';
nodes: Array<Text | Tag | ElementLike | Block | TemplateComment>;
nodes: Array<Text | Tag | ElementLike | Block | Comment>;
}
export interface Root extends BaseNode {
@ -1220,8 +1220,9 @@ declare module 'svelte/compiler' {
}
/** An HTML comment */
export interface TemplateComment extends BaseNode {
type: 'TemplateComment';
// TODO rename to disambiguate
export interface Comment extends BaseNode {
type: 'Comment';
/** the contents of the comment */
data: string;
}
@ -1553,7 +1554,7 @@ declare module 'svelte/compiler' {
| AST.SpreadAttribute
| Directive
| AST.AttachTag
| AST.TemplateComment
| AST.Comment
| Block;
export type SvelteNode = Node | TemplateNode | AST.Fragment | _CSS.Node | Script;
@ -1608,7 +1609,7 @@ declare module 'svelte/compiler' {
end: number;
styles: string;
/** Possible comment atop the style tag */
comment: AST.TemplateComment | null;
comment: AST.Comment | null;
};
}

Loading…
Cancel
Save