@@ -19,69 +19,67 @@ test('attachComments', async function (t) {
1919 } )
2020
2121 await t . test ( 'should support an empty document' , async function ( ) {
22- assert . equal ( recast . print ( attachComments ( ...parse ( '' ) ) ) . code , '' )
22+ const [ tree , comments ] = parse ( '' )
23+ attachComments ( tree , comments )
24+ assert . equal ( recast . print ( tree ) . code , '' )
2325 } )
2426
2527 await t . test ( 'should support no comments' , async function ( ) {
26- assert . equal ( recast . print ( attachComments ( ...parse ( 'a + 1' ) ) ) . code , 'a + 1;' )
28+ const [ tree , comments ] = parse ( 'a + 1' )
29+ attachComments ( tree , comments )
30+ assert . equal ( recast . print ( tree ) . code , 'a + 1;' )
2731 } )
2832
2933 await t . test ( 'should support a single block comment' , async function ( ) {
30- assert . equal (
31- recast . print ( attachComments ( ...parse ( '/* ! */' ) ) ) . code ,
32- '/* ! */\n'
33- )
34+ const [ tree , comments ] = parse ( '/* ! */' )
35+ attachComments ( tree , comments )
36+ assert . equal ( recast . print ( tree ) . code , '/* ! */\n' )
3437 } )
3538
3639 await t . test ( 'should support a single line comment' , async function ( ) {
37- assert . equal ( recast . print ( attachComments ( ...parse ( '// !' ) ) ) . code , '// !\n' )
40+ const [ tree , comments ] = parse ( '// !' )
41+ attachComments ( tree , comments )
42+ assert . equal ( recast . print ( tree ) . code , '// !\n' )
3843 } )
3944
4045 await t . test ( 'should support some comments' , async function ( ) {
46+ const [ tree , comments ] = parse (
47+ '/* 1 */ function a (/* 2 */b) { return b + 1 }'
48+ )
49+ attachComments ( tree , comments )
4150 assert . equal (
42- recast . print (
43- attachComments (
44- ...parse ( '/* 1 */ function a (/* 2 */b) { return b + 1 }' )
45- )
46- ) . code ,
51+ recast . print ( tree ) . code ,
4752 '/* 1 */\nfunction a(\n /* 2 */\n b\n) {\n return b + 1;\n}'
4853 )
4954 } )
5055
5156 await t . test ( 'should support a bunch of block comments' , async function ( ) {
57+ const [ tree , comments ] = parse (
58+ '/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }'
59+ )
60+ attachComments ( tree , comments )
5261 assert . equal (
53- recast . print (
54- attachComments (
55- ...parse (
56- '/* 1 */ function /* 2 */ a /* 3 */ (/* 4 */b) /* 5 */ { /* 6 */ return /* 7 */ b + /* 8 */ 1 /* 9 */ }'
57- )
58- )
59- ) . code ,
62+ recast . print ( tree ) . code ,
6063 '/* 1 */\nfunction /* 2 */\na(\n /* 3 */\n /* 4 */\n b\n) /* 5 */\n{\n /* 6 */\n return (\n /* 7 */\n b + /* 8 */\n 1\n );\n}/* 9 */'
6164 )
6265 } )
6366
6467 await t . test ( 'should support some more comments' , async function ( ) {
68+ const [ tree , comments ] = parse ( '/* 1 */ a /* 2 */ = /* 3 */ { /* 4 */ }' )
69+ attachComments ( tree , comments )
6570 // Recast parses `4` as “dangling”:
6671 // <https://github.com/benjamn/recast/blob/dd7c5ec/lib/comments.ts#L255-L256>
6772 // But apprently doesn’t serialize it?
68- assert . equal (
69- recast . print (
70- attachComments ( ...parse ( '/* 1 */ a /* 2 */ = /* 3 */ { /* 4 */ }' ) )
71- ) . code ,
72- '/* 1 */\na = /* 2 */\n/* 3 */\n{};'
73- )
73+ assert . equal ( recast . print ( tree ) . code , '/* 1 */\na = /* 2 */\n/* 3 */\n{};' )
7474 } )
7575
7676 await t . test ( 'should support a bunch of line comments' , async function ( ) {
77+ const [ tree , comments ] = parse (
78+ '// 1\nfunction // 2\na // 3\n(// 4\nb) // 5\n { // 6\n return b + // 7\n 1 // 8\n }'
79+ )
80+ attachComments ( tree , comments )
7781 assert . equal (
78- recast . print (
79- attachComments (
80- ...parse (
81- '// 1\nfunction // 2\na // 3\n(// 4\nb) // 5\n { // 6\n return b + // 7\n 1 // 8\n }'
82- )
83- )
84- ) . code ,
82+ recast . print ( tree ) . code ,
8583 '// 1\nfunction // 2\na(\n // 3\n // 4\n b\n) // 5\n{\n // 6\n return b + // 7\n 1;\n}// 8'
8684 )
8785 } )
@@ -100,8 +98,9 @@ test('attachComments', async function (t) {
10098 } )
10199
102100 removePositions ( tree )
101+ attachComments ( tree , comments )
103102
104- assert . equal ( recast . print ( attachComments ( tree , comments ) ) . code , 'a + 1;' )
103+ assert . equal ( recast . print ( tree ) . code , 'a + 1;' )
105104 }
106105 )
107106
@@ -116,7 +115,9 @@ test('attachComments', async function (t) {
116115 onComment : comments
117116 } )
118117
119- assert . equal ( recast . print ( attachComments ( tree ) ) . code , '1 + 1;' )
118+ attachComments ( tree )
119+
120+ assert . equal ( recast . print ( tree ) . code , '1 + 1;' )
120121 } )
121122
122123 await t . test (
@@ -134,7 +135,9 @@ test('attachComments', async function (t) {
134135
135136 removePositions ( comments )
136137
137- assert . equal ( recast . print ( attachComments ( tree , comments ) ) . code , 'a + 1;' )
138+ attachComments ( tree , comments )
139+
140+ assert . equal ( recast . print ( tree ) . code , 'a + 1;' )
138141 }
139142 )
140143
@@ -152,10 +155,9 @@ test('attachComments', async function (t) {
152155
153156 removePositions ( tree )
154157
155- assert . equal (
156- recast . print ( attachComments ( tree , comments ) ) . code ,
157- '/* 1 */\na + /* 2 */\n/* 3 */\n1;'
158- )
158+ attachComments ( tree , comments )
159+
160+ assert . equal ( recast . print ( tree ) . code , '/* 1 */\na + /* 2 */\n/* 3 */\n1;' )
159161 } )
160162
161163 await t . test ( 'should use `loc`s' , async function ( ) {
@@ -171,11 +173,9 @@ test('attachComments', async function (t) {
171173 } )
172174
173175 removePositions ( tree )
176+ attachComments ( tree , comments )
174177
175- assert . equal (
176- recast . print ( attachComments ( tree , comments ) ) . code ,
177- '/* 1 */\na + /* 2 */\n/* 3 */\n1;'
178- )
178+ assert . equal ( recast . print ( tree ) . code , '/* 1 */\na + /* 2 */\n/* 3 */\n1;' )
179179 } )
180180} )
181181
0 commit comments