@@ -14,7 +14,7 @@ import {
14
14
isTypeScript ,
15
15
printRaw ,
16
16
} from './print/node-helpers' ;
17
- import { ElementNode , Node , ScriptNode , StyleNode } from './print/nodes' ;
17
+ import { CommentNode , ElementNode , Node , ScriptNode , StyleNode } from './print/nodes' ;
18
18
19
19
const {
20
20
builders : { concat, hardline, softline, indent, dedent, literalline } ,
@@ -193,11 +193,16 @@ function embedTag(
193
193
const node : ScriptNode | StyleNode | ElementNode = path . getNode ( ) ;
194
194
const content =
195
195
tag === 'template' ? printRaw ( node as ElementNode , text ) : getSnippedContent ( node ) ;
196
- const previousComment = getLeadingComment ( path ) ;
196
+ const previousComments =
197
+ node . type === 'Script' || node . type === 'Style'
198
+ ? node . comments
199
+ : [ getLeadingComment ( path ) ]
200
+ . filter ( Boolean )
201
+ . map ( ( comment ) => ( { comment : comment as CommentNode , emptyLineAfter : false } ) ) ;
197
202
198
203
const canFormat =
199
204
isNodeSupportedLanguage ( node ) &&
200
- ! isIgnoreDirective ( previousComment ) &&
205
+ ! isIgnoreDirective ( previousComments [ previousComments . length - 1 ] ?. comment ) &&
201
206
( tag !== 'template' ||
202
207
options . plugins . some (
203
208
( plugin ) => typeof plugin !== 'string' && plugin . parsers && plugin . parsers . pug ,
@@ -223,16 +228,21 @@ function embedTag(
223
228
] ) ;
224
229
let result = groupConcat ( [ openingTag , body , '</' , tag , '>' ] ) ;
225
230
231
+ const comments = [ ] ;
232
+ for ( const comment of previousComments ) {
233
+ comments . push ( '<!--' , comment . comment . data , '-->' ) ;
234
+ comments . push ( hardline ) ;
235
+ if ( comment . emptyLineAfter ) {
236
+ comments . push ( hardline ) ;
237
+ }
238
+ }
239
+
226
240
if ( isTopLevel && options . svelteSortOrder !== 'none' ) {
227
241
// top level embedded nodes have been moved from their normal position in the
228
242
// node tree. if there is a comment referring to it, it must be recreated at
229
243
// the new position.
230
- if ( previousComment ) {
231
- result = concat ( [ '<!--' , previousComment . data , '-->' , hardline , result , hardline ] ) ;
232
- } else {
233
- result = concat ( [ result , hardline ] ) ;
234
- }
244
+ return concat ( [ ...comments , result , hardline ] ) ;
245
+ } else {
246
+ return comments . length ? concat ( [ ...comments , result ] ) : result ;
235
247
}
236
-
237
- return result ;
238
248
}
0 commit comments