11import getPrettier from "./get-prettier.js" ;
22import getPlugins from "./get-plugins.js" ;
3- import {
4- CURSOR_PLACEHOLDER ,
5- RANGE_START_PLACEHOLDER ,
6- RANGE_END_PLACEHOLDER ,
7- } from "./constants.js" ;
3+ import { CURSOR_PLACEHOLDER } from "./constants.js" ;
84import visualizeEndOfLine from "./utils/visualize-end-of-line.js" ;
5+ import { replacePlaceholders } from "./replace-placeholders.js" ;
96
10- async function parse ( source , options ) {
7+ async function parse ( input , options ) {
118 const prettier = await getPrettier ( ) ;
129
1310 const { ast } = await prettier . __debug . parse (
14- source ,
15- { ... options , plugins : await getPlugins ( ) } ,
11+ input ,
12+ await loadPlugins ( options ) ,
1613 { massage : true } ,
1714 ) ;
1815 return ast ;
@@ -28,7 +25,7 @@ async function format(originalText, originalOptions) {
2825
2926 const { formatted : output , cursorOffset } = await prettier . formatWithCursor (
3027 input ,
31- { ... options , plugins : await getPlugins ( ) } ,
28+ await loadPlugins ( options ) ,
3229 ) ;
3330 const outputWithCursor = insertCursor ( output , cursorOffset ) ;
3431 const eolVisualizedOutput = visualizeEndOfLine ( outputWithCursor ) ;
@@ -53,39 +50,8 @@ const insertCursor = (text, cursorOffset) =>
5350 text . slice ( cursorOffset )
5451 : text ;
5552
56- const indexProperties = [
57- {
58- property : "cursorOffset" ,
59- placeholder : CURSOR_PLACEHOLDER ,
60- } ,
61- {
62- property : "rangeStart" ,
63- placeholder : RANGE_START_PLACEHOLDER ,
64- } ,
65- {
66- property : "rangeEnd" ,
67- placeholder : RANGE_END_PLACEHOLDER ,
68- } ,
69- ] ;
70-
71- function replacePlaceholders ( originalText , originalOptions ) {
72- const indexes = indexProperties
73- . map ( ( { property, placeholder } ) => {
74- const value = originalText . indexOf ( placeholder ) ;
75- return value === - 1 ? undefined : { property, value, placeholder } ;
76- } )
77- . filter ( Boolean )
78- . sort ( ( a , b ) => a . value - b . value ) ;
79-
80- const options = { ...originalOptions } ;
81- let text = originalText ;
82- let offset = 0 ;
83- for ( const { property, value, placeholder } of indexes ) {
84- text = text . replace ( placeholder , "" ) ;
85- options [ property ] = value + offset ;
86- offset -= placeholder . length ;
87- }
88- return { text, options } ;
53+ async function loadPlugins ( options ) {
54+ return { ...options , plugins : await getPlugins ( ) } ;
8955}
9056
9157export { format , parse } ;
0 commit comments