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" ;
81import { builders } from "prettier/doc" ;
9-
102import embed from "./embed" ;
11-
12- import { ContentCstNodeExt , XMLAst , XMLOptions } from "./types" ;
3+ import type { ContentCstNode , Doc , IToken , Path , Printer } from "./types" ;
134
145const { fill, group, hardline, indent, join, line, literalline, softline } =
156 builders ;
@@ -42,13 +33,20 @@ function isWhitespaceIgnorable(node: ContentCstNode) {
4233 return ! CData && ! reference && ! hasIgnoreRanges ( Comment ) ;
4334}
4435
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 ( ) ;
4745
4846 return {
4947 offset : node . startOffset ,
50- startLine : node . startLine ! ,
51- endLine : node . endLine ! ,
48+ startLine : node . startLine ,
49+ endLine : node . endLine ,
5250 printed : node . image
5351 } ;
5452}
@@ -59,9 +57,9 @@ function replaceNewlinesWithLiteralLines(content: string) {
5957 . map ( ( value , idx ) => ( idx % 2 === 0 ? value : literalline ) ) ;
6058}
6159
62- const printer : Printer < XMLAst > = {
60+ const printer : Printer = {
6361 embed,
64- print ( path , opts : XMLOptions , print ) {
62+ print ( path , opts , print ) {
6563 const node = path . getValue ( ) ;
6664
6765 switch ( node . name ) {
@@ -79,22 +77,25 @@ const printer: Printer<XMLAst> = {
7977 . map ( ( value , index ) => ( index % 2 === 0 ? value : literalline ) ) ;
8078 }
8179 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 ( ) ;
8685
8786 if ( children . CData ) {
88- response = response . concat ( path . map ( printIToken , "CData" ) ) ;
87+ response = response . concat ( childrenPath . map ( printIToken , "CData" ) ) ;
8988 }
9089
9190 if ( children . Comment ) {
92- response = response . concat ( path . map ( printIToken , "Comment" ) ) ;
91+ response = response . concat (
92+ childrenPath . map ( printIToken , "Comment" )
93+ ) ;
9394 }
9495
9596 if ( children . chardata ) {
9697 response = response . concat (
97- path . map (
98+ childrenPath . map (
9899 ( charDataPath ) => ( {
99100 offset : charDataPath . getValue ( ) . location ! . startOffset ,
100101 printed : print ( charDataPath )
@@ -106,7 +107,7 @@ const printer: Printer<XMLAst> = {
106107
107108 if ( children . element ) {
108109 response = response . concat (
109- path . map (
110+ childrenPath . map (
110111 ( elementPath ) => ( {
111112 offset : elementPath . getValue ( ) . location ! . startOffset ,
112113 printed : print ( elementPath )
@@ -118,15 +119,14 @@ const printer: Printer<XMLAst> = {
118119
119120 if ( children . PROCESSING_INSTRUCTION ) {
120121 response = response . concat (
121- path . map ( printIToken , "PROCESSING_INSTRUCTION" )
122+ childrenPath . map ( printIToken , "PROCESSING_INSTRUCTION" )
122123 ) ;
123124 }
124125
125126 if ( children . reference ) {
126127 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 ( ) ;
130130
131131 return {
132132 offset : referenceNode . location ! . startOffset ,
@@ -199,17 +199,17 @@ const printer: Printer<XMLAst> = {
199199 }
200200 case "document" : {
201201 const { docTypeDecl, element, misc, prolog } = node . children ;
202- const parts : { offset : number ; printed : Doc } [ ] = [ ] ;
202+ const fragments : Fragment [ ] = [ ] ;
203203
204204 if ( docTypeDecl ) {
205- parts . push ( {
205+ fragments . push ( {
206206 offset : docTypeDecl [ 0 ] . location ! . startOffset ,
207207 printed : path . call ( print , "children" , "docTypeDecl" , 0 )
208208 } ) ;
209209 }
210210
211211 if ( prolog ) {
212- parts . push ( {
212+ fragments . push ( {
213213 offset : prolog [ 0 ] . location ! . startOffset ,
214214 printed : path . call ( print , "children" , "prolog" , 0 )
215215 } ) ;
@@ -218,12 +218,12 @@ const printer: Printer<XMLAst> = {
218218 if ( misc ) {
219219 misc . forEach ( ( node ) => {
220220 if ( node . children . PROCESSING_INSTRUCTION ) {
221- parts . push ( {
221+ fragments . push ( {
222222 offset : node . location ! . startOffset ,
223223 printed : node . children . PROCESSING_INSTRUCTION [ 0 ] . image
224224 } ) ;
225225 } else if ( node . children . Comment ) {
226- parts . push ( {
226+ fragments . push ( {
227227 offset : node . location ! . startOffset ,
228228 printed : node . children . Comment [ 0 ] . image
229229 } ) ;
@@ -232,18 +232,18 @@ const printer: Printer<XMLAst> = {
232232 }
233233
234234 if ( element ) {
235- parts . push ( {
235+ fragments . push ( {
236236 offset : element [ 0 ] . location ! . startOffset ,
237237 printed : path . call ( print , "children" , "element" , 0 )
238238 } ) ;
239239 }
240240
241- parts . sort ( ( left , right ) => left . offset - right . offset ) ;
241+ fragments . sort ( ( left , right ) => left . offset - right . offset ) ;
242242
243243 return [
244244 join (
245245 hardline ,
246- parts . map ( ( { printed } ) => printed )
246+ fragments . map ( ( { printed } ) => printed )
247247 ) ,
248248 hardline
249249 ] ;
@@ -297,24 +297,22 @@ const printer: Printer<XMLAst> = {
297297 opts . xmlWhitespaceSensitivity === "ignore" &&
298298 isWhitespaceIgnorable ( content [ 0 ] )
299299 ) {
300- const fragments = path . call (
300+ const nodePath = path as Path < typeof node > ;
301+
302+ const fragments = nodePath . call (
301303 ( 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 [ ] = [ ] ;
310306
311307 if ( children . Comment ) {
312- response = response . concat ( path . map ( printIToken , "Comment" ) ) ;
308+ response = response . concat (
309+ childrenPath . map ( printIToken , "Comment" )
310+ ) ;
313311 }
314312
315313 if ( children . chardata ) {
316- path . each ( ( charDataPath ) => {
317- const chardata = charDataPath . getValue ( ) as ChardataCstNode ;
314+ childrenPath . each ( ( charDataPath ) => {
315+ const chardata = charDataPath . getValue ( ) ;
318316 if ( ! chardata . children . TEXT ) {
319317 return ;
320318 }
@@ -348,7 +346,7 @@ const printer: Printer<XMLAst> = {
348346
349347 if ( children . element ) {
350348 response = response . concat (
351- path . map ( ( elementPath ) => {
349+ childrenPath . map ( ( elementPath ) => {
352350 const location = elementPath . getValue ( ) . location ! ;
353351
354352 return {
@@ -363,7 +361,7 @@ const printer: Printer<XMLAst> = {
363361
364362 if ( children . PROCESSING_INSTRUCTION ) {
365363 response = response . concat (
366- path . map ( printIToken , "PROCESSING_INSTRUCTION" )
364+ childrenPath . map ( printIToken , "PROCESSING_INSTRUCTION" )
367365 ) ;
368366 }
369367
@@ -398,17 +396,17 @@ const printer: Printer<XMLAst> = {
398396 }
399397
400398 const docs : Doc [ ] = [ ] ;
401- let lastLine : number = fragments [ 0 ] . startLine ;
399+ let lastLine : number = fragments [ 0 ] . startLine ! ;
402400
403401 fragments . forEach ( ( node ) => {
404- if ( node . startLine - lastLine >= 2 ) {
402+ if ( node . startLine ! - lastLine >= 2 ) {
405403 docs . push ( hardline , hardline ) ;
406404 } else {
407405 docs . push ( hardline ) ;
408406 }
409407
410408 docs . push ( node . printed ) ;
411- lastLine = node . endLine ;
409+ lastLine = node . endLine ! ;
412410 } ) ;
413411
414412 return group ( [ openTag , indent ( docs ) , hardline , closeTag ] ) ;
0 commit comments