@@ -16,15 +16,14 @@ import {
1616 isAstUnstable ,
1717 isUnstable ,
1818} from "./failed-format-tests.js" ;
19+ import { format , parse } from "./run-prettier.js" ;
1920
2021const { __dirname } = createEsmUtils ( import . meta) ;
2122
2223const { FULL_TEST } = process . env ;
2324const BOM = "\uFEFF" ;
2425
2526const CURSOR_PLACEHOLDER = "<|>" ;
26- const RANGE_START_PLACEHOLDER = "<<<PRETTIER_RANGE_START>>>" ;
27- const RANGE_END_PLACEHOLDER = "<<<PRETTIER_RANGE_END>>>" ;
2827
2928const testsWithAstChanges = new Map (
3029 [
@@ -370,83 +369,4 @@ function shouldSkipEolTest(code, options) {
370369 return false ;
371370}
372371
373- async function parse ( source , options ) {
374- const prettier = await getPrettier ( ) ;
375-
376- const { ast } = await prettier . __debug . parse (
377- source ,
378- { ...options , plugins : await getPlugins ( ) } ,
379- { massage : true } ,
380- ) ;
381- return ast ;
382- }
383-
384- const indexProperties = [
385- {
386- property : "cursorOffset" ,
387- placeholder : CURSOR_PLACEHOLDER ,
388- } ,
389- {
390- property : "rangeStart" ,
391- placeholder : RANGE_START_PLACEHOLDER ,
392- } ,
393- {
394- property : "rangeEnd" ,
395- placeholder : RANGE_END_PLACEHOLDER ,
396- } ,
397- ] ;
398- function replacePlaceholders ( originalText , originalOptions ) {
399- const indexes = indexProperties
400- . map ( ( { property, placeholder } ) => {
401- const value = originalText . indexOf ( placeholder ) ;
402- return value === - 1 ? undefined : { property, value, placeholder } ;
403- } )
404- . filter ( Boolean )
405- . sort ( ( a , b ) => a . value - b . value ) ;
406-
407- const options = { ...originalOptions } ;
408- let text = originalText ;
409- let offset = 0 ;
410- for ( const { property, value, placeholder } of indexes ) {
411- text = text . replace ( placeholder , "" ) ;
412- options [ property ] = value + offset ;
413- offset -= placeholder . length ;
414- }
415- return { text, options } ;
416- }
417-
418- const insertCursor = ( text , cursorOffset ) =>
419- cursorOffset >= 0
420- ? text . slice ( 0 , cursorOffset ) +
421- CURSOR_PLACEHOLDER +
422- text . slice ( cursorOffset )
423- : text ;
424- async function format ( originalText , originalOptions ) {
425- const { text : input , options } = replacePlaceholders (
426- originalText ,
427- originalOptions ,
428- ) ;
429- const inputWithCursor = insertCursor ( input , options . cursorOffset ) ;
430- const prettier = await getPrettier ( ) ;
431-
432- const { formatted : output , cursorOffset } = await prettier . formatWithCursor (
433- input ,
434- { ...options , plugins : await getPlugins ( ) } ,
435- ) ;
436- const outputWithCursor = insertCursor ( output , cursorOffset ) ;
437- const eolVisualizedOutput = visualizeEndOfLine ( outputWithCursor ) ;
438-
439- const changed = outputWithCursor !== inputWithCursor ;
440-
441- return {
442- changed,
443- options,
444- input,
445- inputWithCursor,
446- output,
447- outputWithCursor,
448- eolVisualizedOutput,
449- } ;
450- }
451-
452372export default runFormatTest ;
0 commit comments