1- const { builders, utils } = require ( "prettier/doc" ) ;
1+ import { ContentCtx , ElementCstNode } from "@xml-tools/parser" ;
2+ import type { Doc , FastPath , ParserOptions , Printer } from "prettier" ;
3+ import { builders , utils } from "prettier/doc" ;
4+
5+ import { XMLAST } from "./types" ;
6+
27const {
38 concat,
49 dedentToRoot,
@@ -10,12 +15,11 @@ const {
1015 literalline,
1116 softline
1217} = builders ;
13- const { mapDoc, stripTrailingHardline } = utils ;
1418
1519// Replace the string content newlines within a doc tree with literallines so
1620// that all of the indentation lines up appropriately
17- const replaceNewlines = ( doc ) =>
18- mapDoc ( doc , ( currentDoc ) =>
21+ function replaceNewlines ( doc : Doc ) {
22+ return utils . mapDoc ( doc , ( currentDoc ) =>
1923 typeof currentDoc === "string" && currentDoc . includes ( "\n" )
2024 ? concat (
2125 currentDoc
@@ -24,14 +28,15 @@ const replaceNewlines = (doc) =>
2428 )
2529 : currentDoc
2630 ) ;
31+ }
2732
2833// Get the start and end element tags from the current node on the tree
29- const getElementTags = ( path , print ) => {
30- const node = path . getValue ( ) ;
34+ function getElementTags ( path : FastPath < XMLAST > , print : ( path : FastPath < XMLAST > ) => Doc ) {
35+ const node = path . getValue ( ) as ElementCstNode ;
3136 const { OPEN , Name, attribute, START_CLOSE , SLASH_OPEN , END_NAME , END } =
3237 node . children ;
3338
34- const parts = [ OPEN [ 0 ] . image , Name [ 0 ] . image ] ;
39+ const parts : Doc [ ] = [ OPEN [ 0 ] . image , Name [ 0 ] . image ] ;
3540
3641 if ( attribute ) {
3742 parts . push (
@@ -51,7 +56,7 @@ const getElementTags = (path, print) => {
5156
5257// Get the name of the parser that is represented by the given element node,
5358// return null if a matching parser cannot be found
54- const getParser = ( node , opts ) => {
59+ function getParser ( node : ElementCstNode , opts : ParserOptions < XMLAST > ) {
5560 const { Name, attribute } = node . children ;
5661 const parser = Name [ 0 ] . image . toLowerCase ( ) ;
5762
@@ -81,6 +86,7 @@ const getParser = (node, opts) => {
8186 if (
8287 opts . plugins . some (
8388 ( plugin ) =>
89+ typeof plugin !== "string" &&
8490 plugin . parsers &&
8591 Object . prototype . hasOwnProperty . call ( plugin . parsers , parser )
8692 )
@@ -89,26 +95,27 @@ const getParser = (node, opts) => {
8995 }
9096
9197 return null ;
92- } ;
98+ }
9399
94100// Get the source string that will be passed into the embedded parser from the
95101// content of the inside of the element node
96- const getSource = ( content ) =>
97- content . chardata
102+ function getSource ( content : ContentCtx ) {
103+ return content . chardata
98104 . map ( ( node ) => {
99105 const { SEA_WS , TEXT } = node . children ;
100106 const [ { image } ] = SEA_WS || TEXT ;
101107
102108 return {
103- offset : node . location . startOffset ,
109+ offset : node . location ! . startOffset ,
104110 printed : image
105111 } ;
106112 } )
107113 . sort ( ( { offset } ) => offset )
108114 . map ( ( { printed } ) => printed )
109115 . join ( "" ) ;
116+ }
110117
111- const embed = ( path , print , textToDoc , opts ) => {
118+ const embed : Printer < XMLAST > [ "embed" ] = ( path , print , textToDoc , opts ) => {
112119 const node = path . getValue ( ) ;
113120
114121 // If the node isn't an element node, then skip
@@ -140,7 +147,7 @@ const embed = (path, print, textToDoc, opts) => {
140147 literalline ,
141148 dedentToRoot (
142149 replaceNewlines (
143- stripTrailingHardline ( textToDoc ( getSource ( content ) , { parser } ) )
150+ utils . stripTrailingHardline ( textToDoc ( getSource ( content ) , { parser } ) )
144151 )
145152 ) ,
146153 hardline ,
@@ -149,4 +156,4 @@ const embed = (path, print, textToDoc, opts) => {
149156 ) ;
150157} ;
151158
152- module . exports = embed ;
159+ export default embed ;
0 commit comments