1
- import type {
2
- ChardataCstNode ,
3
- ContentCstNode ,
4
- ReferenceCstNode
5
- } from "@xml-tools/parser" ;
6
- import type { IToken } from "chevrotain" ;
7
- import type { AstPath , Doc , Printer } from "prettier" ;
8
1
import { builders } from "prettier/doc" ;
9
-
10
2
import embed from "./embed" ;
11
-
12
- import { ContentCstNodeExt , XMLAst , XMLOptions } from "./types" ;
3
+ import type { ContentCstNode , Doc , IToken , Path , Printer } from "./types" ;
13
4
14
5
const { fill, group, hardline, indent, join, line, literalline, softline } =
15
6
builders ;
@@ -42,13 +33,20 @@ function isWhitespaceIgnorable(node: ContentCstNode) {
42
33
return ! CData && ! reference && ! hasIgnoreRanges ( Comment ) ;
43
34
}
44
35
45
- function printIToken ( path : AstPath < XMLAst > ) {
46
- const node = path . getValue ( ) as any as IToken ;
36
+ type Fragment = {
37
+ offset : number ;
38
+ printed : Doc ;
39
+ startLine ?: number ;
40
+ endLine ?: number ;
41
+ } ;
42
+
43
+ function printIToken ( path : Path < IToken > ) : Fragment {
44
+ const node = path . getValue ( ) ;
47
45
48
46
return {
49
47
offset : node . startOffset ,
50
- startLine : node . startLine ! ,
51
- endLine : node . endLine ! ,
48
+ startLine : node . startLine ,
49
+ endLine : node . endLine ,
52
50
printed : node . image
53
51
} ;
54
52
}
@@ -59,9 +57,9 @@ function replaceNewlinesWithLiteralLines(content: string) {
59
57
. map ( ( value , idx ) => ( idx % 2 === 0 ? value : literalline ) ) ;
60
58
}
61
59
62
- const printer : Printer < XMLAst > = {
60
+ const printer : Printer = {
63
61
embed,
64
- print ( path , opts : XMLOptions , print ) {
62
+ print ( path , opts , print ) {
65
63
const node = path . getValue ( ) ;
66
64
67
65
switch ( node . name ) {
@@ -79,22 +77,25 @@ const printer: Printer<XMLAst> = {
79
77
. map ( ( value , index ) => ( index % 2 === 0 ? value : literalline ) ) ;
80
78
}
81
79
case "content" : {
82
- let fragments = path . call ( ( childrenPath ) => {
83
- let response : { offset : number ; printed : Doc } [ ] = [ ] ;
84
- const children =
85
- childrenPath . getValue ( ) as any as ContentCstNodeExt [ "children" ] ;
80
+ const nodePath = path as Path < typeof node > ;
81
+
82
+ let fragments = nodePath . call ( ( childrenPath ) => {
83
+ let response : Fragment [ ] = [ ] ;
84
+ const children = childrenPath . getValue ( ) ;
86
85
87
86
if ( children . CData ) {
88
- response = response . concat ( path . map ( printIToken , "CData" ) ) ;
87
+ response = response . concat ( childrenPath . map ( printIToken , "CData" ) ) ;
89
88
}
90
89
91
90
if ( children . Comment ) {
92
- response = response . concat ( path . map ( printIToken , "Comment" ) ) ;
91
+ response = response . concat (
92
+ childrenPath . map ( printIToken , "Comment" )
93
+ ) ;
93
94
}
94
95
95
96
if ( children . chardata ) {
96
97
response = response . concat (
97
- path . map (
98
+ childrenPath . map (
98
99
( charDataPath ) => ( {
99
100
offset : charDataPath . getValue ( ) . location ! . startOffset ,
100
101
printed : print ( charDataPath )
@@ -106,7 +107,7 @@ const printer: Printer<XMLAst> = {
106
107
107
108
if ( children . element ) {
108
109
response = response . concat (
109
- path . map (
110
+ childrenPath . map (
110
111
( elementPath ) => ( {
111
112
offset : elementPath . getValue ( ) . location ! . startOffset ,
112
113
printed : print ( elementPath )
@@ -118,15 +119,14 @@ const printer: Printer<XMLAst> = {
118
119
119
120
if ( children . PROCESSING_INSTRUCTION ) {
120
121
response = response . concat (
121
- path . map ( printIToken , "PROCESSING_INSTRUCTION" )
122
+ childrenPath . map ( printIToken , "PROCESSING_INSTRUCTION" )
122
123
) ;
123
124
}
124
125
125
126
if ( children . reference ) {
126
127
response = response . concat (
127
- path . map ( ( referencePath ) => {
128
- const referenceNode =
129
- referencePath . getValue ( ) as any as ReferenceCstNode ;
128
+ childrenPath . map ( ( referencePath ) => {
129
+ const referenceNode = referencePath . getValue ( ) ;
130
130
131
131
return {
132
132
offset : referenceNode . location ! . startOffset ,
@@ -199,17 +199,17 @@ const printer: Printer<XMLAst> = {
199
199
}
200
200
case "document" : {
201
201
const { docTypeDecl, element, misc, prolog } = node . children ;
202
- const parts : { offset : number ; printed : Doc } [ ] = [ ] ;
202
+ const fragments : Fragment [ ] = [ ] ;
203
203
204
204
if ( docTypeDecl ) {
205
- parts . push ( {
205
+ fragments . push ( {
206
206
offset : docTypeDecl [ 0 ] . location ! . startOffset ,
207
207
printed : path . call ( print , "children" , "docTypeDecl" , 0 )
208
208
} ) ;
209
209
}
210
210
211
211
if ( prolog ) {
212
- parts . push ( {
212
+ fragments . push ( {
213
213
offset : prolog [ 0 ] . location ! . startOffset ,
214
214
printed : path . call ( print , "children" , "prolog" , 0 )
215
215
} ) ;
@@ -218,12 +218,12 @@ const printer: Printer<XMLAst> = {
218
218
if ( misc ) {
219
219
misc . forEach ( ( node ) => {
220
220
if ( node . children . PROCESSING_INSTRUCTION ) {
221
- parts . push ( {
221
+ fragments . push ( {
222
222
offset : node . location ! . startOffset ,
223
223
printed : node . children . PROCESSING_INSTRUCTION [ 0 ] . image
224
224
} ) ;
225
225
} else if ( node . children . Comment ) {
226
- parts . push ( {
226
+ fragments . push ( {
227
227
offset : node . location ! . startOffset ,
228
228
printed : node . children . Comment [ 0 ] . image
229
229
} ) ;
@@ -232,18 +232,18 @@ const printer: Printer<XMLAst> = {
232
232
}
233
233
234
234
if ( element ) {
235
- parts . push ( {
235
+ fragments . push ( {
236
236
offset : element [ 0 ] . location ! . startOffset ,
237
237
printed : path . call ( print , "children" , "element" , 0 )
238
238
} ) ;
239
239
}
240
240
241
- parts . sort ( ( left , right ) => left . offset - right . offset ) ;
241
+ fragments . sort ( ( left , right ) => left . offset - right . offset ) ;
242
242
243
243
return [
244
244
join (
245
245
hardline ,
246
- parts . map ( ( { printed } ) => printed )
246
+ fragments . map ( ( { printed } ) => printed )
247
247
) ,
248
248
hardline
249
249
] ;
@@ -297,24 +297,22 @@ const printer: Printer<XMLAst> = {
297
297
opts . xmlWhitespaceSensitivity === "ignore" &&
298
298
isWhitespaceIgnorable ( content [ 0 ] )
299
299
) {
300
- const fragments = path . call (
300
+ const nodePath = path as Path < typeof node > ;
301
+
302
+ const fragments = nodePath . call (
301
303
( childrenPath ) => {
302
- const children =
303
- childrenPath . getValue ( ) as any as ContentCstNodeExt [ "children" ] ;
304
- let response : {
305
- offset : number ;
306
- startLine : number ;
307
- endLine : number ;
308
- printed : Doc ;
309
- } [ ] = [ ] ;
304
+ const children = childrenPath . getValue ( ) ;
305
+ let response : Fragment [ ] = [ ] ;
310
306
311
307
if ( children . Comment ) {
312
- response = response . concat ( path . map ( printIToken , "Comment" ) ) ;
308
+ response = response . concat (
309
+ childrenPath . map ( printIToken , "Comment" )
310
+ ) ;
313
311
}
314
312
315
313
if ( children . chardata ) {
316
- path . each ( ( charDataPath ) => {
317
- const chardata = charDataPath . getValue ( ) as ChardataCstNode ;
314
+ childrenPath . each ( ( charDataPath ) => {
315
+ const chardata = charDataPath . getValue ( ) ;
318
316
if ( ! chardata . children . TEXT ) {
319
317
return ;
320
318
}
@@ -348,7 +346,7 @@ const printer: Printer<XMLAst> = {
348
346
349
347
if ( children . element ) {
350
348
response = response . concat (
351
- path . map ( ( elementPath ) => {
349
+ childrenPath . map ( ( elementPath ) => {
352
350
const location = elementPath . getValue ( ) . location ! ;
353
351
354
352
return {
@@ -363,7 +361,7 @@ const printer: Printer<XMLAst> = {
363
361
364
362
if ( children . PROCESSING_INSTRUCTION ) {
365
363
response = response . concat (
366
- path . map ( printIToken , "PROCESSING_INSTRUCTION" )
364
+ childrenPath . map ( printIToken , "PROCESSING_INSTRUCTION" )
367
365
) ;
368
366
}
369
367
@@ -398,17 +396,17 @@ const printer: Printer<XMLAst> = {
398
396
}
399
397
400
398
const docs : Doc [ ] = [ ] ;
401
- let lastLine : number = fragments [ 0 ] . startLine ;
399
+ let lastLine : number = fragments [ 0 ] . startLine ! ;
402
400
403
401
fragments . forEach ( ( node ) => {
404
- if ( node . startLine - lastLine >= 2 ) {
402
+ if ( node . startLine ! - lastLine >= 2 ) {
405
403
docs . push ( hardline , hardline ) ;
406
404
} else {
407
405
docs . push ( hardline ) ;
408
406
}
409
407
410
408
docs . push ( node . printed ) ;
411
- lastLine = node . endLine ;
409
+ lastLine = node . endLine ! ;
412
410
} ) ;
413
411
414
412
return group ( [ openTag , indent ( docs ) , hardline , closeTag ] ) ;
0 commit comments