1+ "use strict" ;
2+
13const { TEST_STANDALONE } = process . env ;
24
3- const fs = require ( 'fs' ) ;
4- const path = require ( ' path' ) ;
5+ const fs = require ( "fs" ) ;
6+ const path = require ( " path" ) ;
57const prettier = ! TEST_STANDALONE
6- ? require ( './require_prettier' )
7- : require ( './require_standalone' ) ;
8- const checkParsers = require ( ' ./utils/check-parsers' ) ;
9- const createSnapshot = require ( ' ./utils/create-snapshot' ) ;
10- const visualizeEndOfLine = require ( ' ./utils/visualize-end-of-line' ) ;
11- const consistentEndOfLine = require ( ' ./utils/consistent-end-of-line' ) ;
12- const stringifyOptionsForTitle = require ( ' ./utils/stringify-options-for-title' ) ;
8+ ? require ( "./require-prettier" )
9+ : require ( "./require-standalone" ) ;
10+ const checkParsers = require ( " ./utils/check-parsers" ) ;
11+ const createSnapshot = require ( " ./utils/create-snapshot" ) ;
12+ const visualizeEndOfLine = require ( " ./utils/visualize-end-of-line" ) ;
13+ const consistentEndOfLine = require ( " ./utils/consistent-end-of-line" ) ;
14+ const stringifyOptionsForTitle = require ( " ./utils/stringify-options-for-title" ) ;
1315
1416const { FULL_TEST } = process . env ;
15- const BOM = ' \uFEFF' ;
17+ const BOM = " \uFEFF" ;
1618
17- const CURSOR_PLACEHOLDER = ' <|>' ;
18- const RANGE_START_PLACEHOLDER = ' <<<PRETTIER_RANGE_START>>>' ;
19- const RANGE_END_PLACEHOLDER = ' <<<PRETTIER_RANGE_END>>>' ;
19+ const CURSOR_PLACEHOLDER = " <|>" ;
20+ const RANGE_START_PLACEHOLDER = " <<<PRETTIER_RANGE_START>>>" ;
21+ const RANGE_END_PLACEHOLDER = " <<<PRETTIER_RANGE_END>>>" ;
2022
2123// Here we add files that will not be the same when formating a second time.
2224const unstableTests = new Map (
2325 [ ] . map ( ( fixture ) => {
2426 const [ file , isUnstable = ( ) => true ] = Array . isArray ( fixture )
2527 ? fixture
2628 : [ fixture ] ;
27- return [ path . join ( __dirname , ' ../tests/' , file ) , isUnstable ] ;
29+ return [ path . join ( __dirname , " ../format/" , file ) , isUnstable ] ;
2830 } )
2931) ;
3032
@@ -34,36 +36,36 @@ const unstableAstTests = new Map(
3436 const [ file , isAstUnstable = ( ) => true ] = Array . isArray ( fixture )
3537 ? fixture
3638 : [ fixture ] ;
37- return [ path . join ( __dirname , ' ../tests/' , file ) , isAstUnstable ] ;
39+ return [ path . join ( __dirname , " ../format/" , file ) , isAstUnstable ] ;
3840 } )
3941) ;
4042
4143const testsWithAstChanges = new Map (
4244 [
4345 [
44- ' ExplicitVariableTypes/ExplicitVariableTypes.sol' ,
46+ " ExplicitVariableTypes/ExplicitVariableTypes.sol" ,
4547 ( options ) =>
4648 options . explicitTypes === undefined ||
47- options . explicitTypes !== ' preserve'
49+ options . explicitTypes !== " preserve" ,
4850 ] ,
49- ' Parentheses/AddNoParentheses.sol' ,
50- ' Parentheses/SubNoParentheses.sol' ,
51- ' Parentheses/MulNoParentheses.sol' ,
52- ' Parentheses/DivNoParentheses.sol' ,
53- ' Parentheses/ModNoParentheses.sol' ,
54- ' Parentheses/ExpNoParentheses.sol' ,
55- ' Parentheses/ShiftLNoParentheses.sol' ,
56- ' Parentheses/ShiftRNoParentheses.sol' ,
57- ' Parentheses/BitAndNoParentheses.sol' ,
58- ' Parentheses/BitOrNoParentheses.sol' ,
59- ' Parentheses/BitXorNoParentheses.sol' ,
60- ' Parentheses/LogicNoParentheses.sol' ,
61- ' HexLiteral/HexLiteral.sol'
51+ " Parentheses/AddNoParentheses.sol" ,
52+ " Parentheses/SubNoParentheses.sol" ,
53+ " Parentheses/MulNoParentheses.sol" ,
54+ " Parentheses/DivNoParentheses.sol" ,
55+ " Parentheses/ModNoParentheses.sol" ,
56+ " Parentheses/ExpNoParentheses.sol" ,
57+ " Parentheses/ShiftLNoParentheses.sol" ,
58+ " Parentheses/ShiftRNoParentheses.sol" ,
59+ " Parentheses/BitAndNoParentheses.sol" ,
60+ " Parentheses/BitOrNoParentheses.sol" ,
61+ " Parentheses/BitXorNoParentheses.sol" ,
62+ " Parentheses/LogicNoParentheses.sol" ,
63+ " HexLiteral/HexLiteral.sol" ,
6264 ] . map ( ( fixture ) => {
6365 const [ file , compareBytecode = ( ) => true ] = Array . isArray ( fixture )
6466 ? fixture
6567 : [ fixture ] ;
66- return [ path . join ( __dirname , ' ../tests/' , file ) , compareBytecode ] ;
68+ return [ path . join ( __dirname , " ../format/" , file ) , compareBytecode ] ;
6769 } )
6870) ;
6971
@@ -114,23 +116,23 @@ const shouldThrowOnFormat = (filename, options) => {
114116
115117const isTestDirectory = ( dirname , name ) =>
116118 ( dirname + path . sep ) . startsWith (
117- path . join ( __dirname , ' ../tests' , name ) + path . sep
119+ path . join ( __dirname , " ../format" , name ) + path . sep
118120 ) ;
119121
120122function runSpec ( fixtures , parsers , options ) {
121123 let { dirname, snippets = [ ] } =
122- typeof fixtures === ' string' ? { dirname : fixtures } : fixtures ;
124+ typeof fixtures === " string" ? { dirname : fixtures } : fixtures ;
123125
124126 // `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
125127 const IS_PARSER_INFERENCE_TESTS = isTestDirectory (
126128 dirname ,
127- ' misc/parser-inference'
129+ " misc/parser-inference"
128130 ) ;
129131
130132 // `IS_ERROR_TESTS` mean to watch errors like:
131133 // - syntax parser hasn't supported yet
132134 // - syntax errors that should throws
133- const IS_ERROR_TESTS = isTestDirectory ( dirname , ' misc/errors' ) ;
135+ const IS_ERROR_TESTS = isTestDirectory ( dirname , " misc/errors" ) ;
134136 if ( IS_ERROR_TESTS ) {
135137 options = { errors : true , ...options } ;
136138 }
@@ -140,10 +142,10 @@ function runSpec(fixtures, parsers, options) {
140142 }
141143
142144 snippets = snippets . map ( ( test , index ) => {
143- test = typeof test === ' string' ? { code : test } : test ;
145+ test = typeof test === " string" ? { code : test } : test ;
144146 return {
145147 ...test ,
146- name : `snippet: ${ test . name || `#${ index } ` } `
148+ name : `snippet: ${ test . name || `#${ index } ` } ` ,
147149 } ;
148150 } ) ;
149151
@@ -153,22 +155,22 @@ function runSpec(fixtures, parsers, options) {
153155 const basename = file . name ;
154156 const filename = path . join ( dirname , basename ) ;
155157 if (
156- path . extname ( basename ) === ' .snap' ||
158+ path . extname ( basename ) === " .snap" ||
157159 ! file . isFile ( ) ||
158- basename [ 0 ] === '.' ||
159- basename === ' jsfmt.spec.js' ||
160+ basename [ 0 ] === "." ||
161+ basename === " jsfmt.spec.js" ||
160162 // VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
161- basename === ' debug.log'
163+ basename === " debug.log"
162164 ) {
163165 return ;
164166 }
165167
166- const text = fs . readFileSync ( filename , ' utf8' ) ;
168+ const text = fs . readFileSync ( filename , " utf8" ) ;
167169
168170 return {
169171 name : basename ,
170172 filename,
171- code : text
173+ code : text ,
172174 } ;
173175 } )
174176 . filter ( Boolean ) ;
@@ -188,16 +190,16 @@ function runSpec(fixtures, parsers, options) {
188190
189191 for ( const { name, filename, code, output } of [ ...files , ...snippets ] ) {
190192 const title = `${ name } ${
191- stringifiedOptions ? ` - ${ stringifiedOptions } ` : ''
193+ stringifiedOptions ? ` - ${ stringifiedOptions } ` : ""
192194 } `;
193195
194196 describe ( title , ( ) => {
195197 const formatOptions = {
196- plugins : [ path . dirname ( __dirname ) ] ,
198+ plugins : [ path . dirname ( path . join ( __dirname , ".." ) ) ] ,
197199 printWidth : 80 ,
198200 ...options ,
199201 filepath : filename ,
200- parser
202+ parser,
201203 } ;
202204 const mainParserFormatResult = shouldThrowOnFormat ( name , formatOptions )
203205 ? { options : formatOptions , error : true }
@@ -212,7 +214,7 @@ function runSpec(fixtures, parsers, options) {
212214 output,
213215 parser : currentParser ,
214216 mainParserFormatResult,
215- mainParserFormatOptions : formatOptions
217+ mainParserFormatOptions : formatOptions ,
216218 } ) ;
217219 }
218220 } ) ;
@@ -227,11 +229,11 @@ function runTest({
227229 output,
228230 parser,
229231 mainParserFormatResult,
230- mainParserFormatOptions
232+ mainParserFormatOptions,
231233} ) {
232234 let formatOptions = mainParserFormatOptions ;
233235 let formatResult = mainParserFormatResult ;
234- let formatTestTitle = ' format' ;
236+ let formatTestTitle = " format" ;
235237
236238 // Verify parsers or error tests
237239 if (
@@ -261,7 +263,7 @@ function runTest({
261263 ) ;
262264
263265 // The result is assert to equals to `output`
264- if ( typeof output === ' string' ) {
266+ if ( typeof output === " string" ) {
265267 expect ( formatResult . eolVisualizedOutput ) . toEqual (
266268 visualizeEndOfLine ( output )
267269 ) ;
@@ -273,7 +275,7 @@ function runTest({
273275 createSnapshot ( formatResult , {
274276 parsers,
275277 formatOptions,
276- CURSOR_PLACEHOLDER
278+ CURSOR_PLACEHOLDER ,
277279 } )
278280 ) . toMatchSnapshot ( ) ;
279281 } ) ;
@@ -320,13 +322,15 @@ function runTest({
320322 }
321323
322324 if ( ! shouldSkipEolTest ( code , formatResult . options ) ) {
323- for ( const eol of [ ' \r\n' , '\r' ] ) {
325+ for ( const eol of [ " \r\n" , "\r" ] ) {
324326 test ( `[${ parser } ] EOL ${ JSON . stringify ( eol ) } ` , ( ) => {
325- const output = format ( code . replace ( / \n / g, eol ) , formatOptions )
326- . eolVisualizedOutput ;
327+ const output = format (
328+ code . replace ( / \n / g, eol ) ,
329+ formatOptions
330+ ) . eolVisualizedOutput ;
327331 // Only if `endOfLine: "auto"` the result will be different
328332 const expected =
329- formatOptions . endOfLine === ' auto'
333+ formatOptions . endOfLine === " auto"
330334 ? visualizeEndOfLine (
331335 // All `code` use `LF`, so the `eol` of result is always `LF`
332336 formatResult . outputWithCursor . replace ( / \n / g, eol )
@@ -352,7 +356,7 @@ function runTest({
352356 // - A worker process has failed to exit gracefully and has been force
353357 // exited. This is likely caused by tests leaking due to improper
354358 // teardown. Try running with --detectOpenHandles to find leaks.
355- const compileContract = require ( ' ./utils/compile-contract' ) ;
359+ const compileContract = require ( " ./utils/compile-contract" ) ;
356360 const output = compileContract ( filename , formatResult . output ) ;
357361 const expected = compileContract ( filename , formatResult . input ) ;
358362 expect ( output ) . toEqual ( expected ) ;
@@ -361,7 +365,7 @@ function runTest({
361365}
362366
363367function shouldSkipEolTest ( code , options ) {
364- if ( code . includes ( '\r' ) ) {
368+ if ( code . includes ( "\r" ) ) {
365369 return true ;
366370 }
367371 const { requirePragma, rangeStart, rangeEnd } = options ;
@@ -370,8 +374,8 @@ function shouldSkipEolTest(code, options) {
370374 }
371375
372376 if (
373- typeof rangeStart === ' number' &&
374- typeof rangeEnd === ' number' &&
377+ typeof rangeStart === " number" &&
378+ typeof rangeEnd === " number" &&
375379 rangeStart >= rangeEnd
376380 ) {
377381 return true ;
@@ -385,17 +389,17 @@ function parse(source, options) {
385389
386390const indexProperties = [
387391 {
388- property : ' cursorOffset' ,
389- placeholder : CURSOR_PLACEHOLDER
392+ property : " cursorOffset" ,
393+ placeholder : CURSOR_PLACEHOLDER ,
390394 } ,
391395 {
392- property : ' rangeStart' ,
393- placeholder : RANGE_START_PLACEHOLDER
396+ property : " rangeStart" ,
397+ placeholder : RANGE_START_PLACEHOLDER ,
394398 } ,
395399 {
396- property : ' rangeEnd' ,
397- placeholder : RANGE_END_PLACEHOLDER
398- }
400+ property : " rangeEnd" ,
401+ placeholder : RANGE_END_PLACEHOLDER ,
402+ } ,
399403] ;
400404function replacePlaceholders ( originalText , originalOptions ) {
401405 const indexes = indexProperties
@@ -410,7 +414,7 @@ function replacePlaceholders(originalText, originalOptions) {
410414 let text = originalText ;
411415 let offset = 0 ;
412416 for ( const { property, value, placeholder } of indexes ) {
413- text = text . replace ( placeholder , '' ) ;
417+ text = text . replace ( placeholder , "" ) ;
414418 options [ property ] = value + offset ;
415419 offset -= placeholder . length ;
416420 }
@@ -446,7 +450,7 @@ function format(originalText, originalOptions) {
446450 inputWithCursor,
447451 output,
448452 outputWithCursor,
449- eolVisualizedOutput
453+ eolVisualizedOutput,
450454 } ;
451455}
452456
0 commit comments