11// source: https://github.com/prettier/prettier/blob/ee2839bacbf6a52d004fa2f0373b732f6f191ccc/tests_config/run_spec.js
2- 'use strict' ;
3-
42const fs = require ( 'fs' ) ;
53const path = require ( 'path' ) ;
4+ const {
5+ read,
6+ mergeDefaultOptions,
7+ prettyprint,
8+ raw,
9+ parse
10+ } = require ( './helpers' ) ;
611
7- const AST_COMPARE = process . env [ 'AST_COMPARE' ] ;
8-
9- const prettier = require ( 'prettier' ) ;
12+ const { AST_COMPARE } = process . env ;
1013
11- function run_spec ( dirname , options ) {
12- fs . readdirSync ( dirname ) . forEach ( filename => {
13- const filepath = dirname + '/' + filename ;
14+ function runSpec ( dirname , options ) {
15+ fs . readdirSync ( dirname ) . forEach ( ( filename ) => {
16+ const filepath = ` ${ dirname } / ${ filename } ` ;
1417 if (
1518 path . extname ( filename ) !== '.snap' &&
1619 fs . lstatSync ( filepath ) . isFile ( ) &&
@@ -45,16 +48,16 @@ function run_spec(dirname, options) {
4548 const output = prettyprint ( input , mergedOptions ) ;
4649 test ( filename , ( ) => {
4750 expect (
48- raw ( source + '~' . repeat ( mergedOptions . printWidth ) + '\n' + output )
51+ raw ( ` ${ source + '~' . repeat ( mergedOptions . printWidth ) } \n ${ output } ` )
4952 ) . toMatchSnapshot ( ) ;
5053 } ) ;
5154
5255 if ( AST_COMPARE ) {
5356 test ( `${ filepath } parse` , ( ) => {
54- const compareOptions = Object . assign ( { } , mergedOptions ) ;
57+ const compareOptions = { ... mergedOptions } ;
5558 delete compareOptions . cursorOffset ;
5659 const astMassaged = parse ( input , compareOptions ) ;
57- let ppastMassaged = undefined ;
60+ let ppastMassaged ;
5861
5962 expect ( ( ) => {
6063 ppastMassaged = parse (
@@ -73,45 +76,4 @@ function run_spec(dirname, options) {
7376 } ) ;
7477}
7578
76- global . run_spec = run_spec ;
77-
78- function parse ( string , opts ) {
79- return prettier . __debug . parse ( string , opts , /* massage */ true ) . ast ;
80- }
81-
82- function prettyprint ( src , options ) {
83- const result = prettier . formatWithCursor ( src , options ) ;
84- if ( options . cursorOffset >= 0 ) {
85- result . formatted =
86- result . formatted . slice ( 0 , result . cursorOffset ) +
87- '<|>' +
88- result . formatted . slice ( result . cursorOffset ) ;
89- }
90- return result . formatted ;
91- }
92-
93- function read ( filename ) {
94- return fs . readFileSync ( filename , 'utf8' ) ;
95- }
96-
97- /**
98- * Wraps a string in a marker object that is used by `./raw-serializer.js` to
99- * directly print that string in a snapshot without escaping all double quotes.
100- * Backticks will still be escaped.
101- */
102- function raw ( string ) {
103- if ( typeof string !== 'string' ) {
104- throw new Error ( 'Raw snapshots have to be strings.' ) ;
105- }
106- return { [ Symbol . for ( 'raw' ) ] : string } ;
107- }
108-
109- function mergeDefaultOptions ( parserConfig ) {
110- return Object . assign (
111- {
112- plugins : [ path . dirname ( __dirname ) ] ,
113- printWidth : 80
114- } ,
115- parserConfig
116- ) ;
117- }
79+ global . run_spec = runSpec ;
0 commit comments