Tim 2 weeks ago committed by GitHub
commit 5315301e05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': minor
---
Add ability to comment out attributes [RFC](https://github.com/sveltejs/rfcs/pull/43)

@ -221,8 +221,15 @@ export default function element(parser) {
const read = is_top_level_script_or_style ? read_static_attribute : read_attribute;
/** @type {ReturnType<typeof read>} */
let attribute;
while ((attribute = read(parser))) {
// {/* comment */}
if (attribute.type === 'CommentAttribute') {
parser.allow_whitespace();
continue;
}
// animate and transition can only be specified once per element so no need
// to check here, use can be used multiple times, same for the on directive
// finally let already has error handling in case of duplicate variable names
@ -496,7 +503,7 @@ function read_static_attribute(parser) {
/**
* @param {Parser} parser
* @returns {AST.Attribute | AST.SpreadAttribute | AST.Directive | AST.AttachTag | null}
* @returns {AST.Attribute | AST.SpreadAttribute | AST.Directive | AST.AttachTag | AST.CommentAttribute | null}
*/
function read_attribute(parser) {
const start = parser.index;
@ -543,6 +550,19 @@ function read_attribute(parser) {
};
return spread;
} else if (parser.eat('/*')) {
const data = parser.read_until(/\*\/\}/);
parser.eat('*/}', true);
/** @type {AST.CommentAttribute} */
const comment = {
type: 'CommentAttribute',
data: data,
start,
end: parser.index
};
return comment;
} else {
const id = parser.read_identifier();

@ -144,6 +144,13 @@ export namespace AST {
data: string;
}
/** An Attribute comment `{/* ... */}` */
export interface CommentAttribute extends BaseNode {
type: 'CommentAttribute';
/** the contents of the comment */
data: string;
}
/** A `{@const ...}` tag */
export interface ConstTag extends BaseNode {
type: 'ConstTag';
@ -631,6 +638,7 @@ export namespace AST {
| Directive
| AST.AttachTag
| AST.Comment
| AST.CommentAttribute
| Block;
export type SvelteNode = Node | TemplateNode | AST.Fragment | _CSS.Node | Script;

@ -0,0 +1,3 @@
<div {/* comment */} {/*
Second comment Multi-line
*/}></div>

@ -0,0 +1,24 @@
{
"css": null,
"js": [],
"start": 0,
"end": 62,
"type": "Root",
"fragment": {
"type": "Fragment",
"nodes": [
{
"type": "RegularElement",
"start": 0,
"end": 62,
"name": "div",
"attributes": [],
"fragment": {
"type": "Fragment",
"nodes": []
}
}
]
},
"options": null
}

@ -1277,6 +1277,13 @@ declare module 'svelte/compiler' {
data: string;
}
/** An Attribute comment `{/* ... */}` */
export interface CommentAttribute extends BaseNode {
type: 'CommentAttribute';
/** the contents of the comment */
data: string;
}
/** A `{@const ...}` tag */
export interface ConstTag extends BaseNode {
type: 'ConstTag';
@ -1610,6 +1617,7 @@ declare module 'svelte/compiler' {
| Directive
| AST.AttachTag
| AST.Comment
| AST.CommentAttribute
| Block;
export type SvelteNode = Node | TemplateNode | AST.Fragment | _CSS.Node | Script;

Loading…
Cancel
Save