@@ -19,69 +19,67 @@ test('attachComments', async function (t) {
19
19
} )
20
20
21
21
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 , '' )
23
25
} )
24
26
25
27
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;' )
27
31
} )
28
32
29
33
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' )
34
37
} )
35
38
36
39
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' )
38
43
} )
39
44
40
45
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 )
41
50
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 ,
47
52
'/* 1 */\nfunction a(\n /* 2 */\n b\n) {\n return b + 1;\n}'
48
53
)
49
54
} )
50
55
51
56
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 )
52
61
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 ,
60
63
'/* 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 */'
61
64
)
62
65
} )
63
66
64
67
await t . test ( 'should support some more comments' , async function ( ) {
68
+ const [ tree , comments ] = parse ( '/* 1 */ a /* 2 */ = /* 3 */ { /* 4 */ }' )
69
+ attachComments ( tree , comments )
65
70
// Recast parses `4` as “dangling”:
66
71
// <https://github.com/benjamn/recast/blob/dd7c5ec/lib/comments.ts#L255-L256>
67
72
// 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{};' )
74
74
} )
75
75
76
76
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 )
77
81
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 ,
85
83
'// 1\nfunction // 2\na(\n // 3\n // 4\n b\n) // 5\n{\n // 6\n return b + // 7\n 1;\n}// 8'
86
84
)
87
85
} )
@@ -100,8 +98,9 @@ test('attachComments', async function (t) {
100
98
} )
101
99
102
100
removePositions ( tree )
101
+ attachComments ( tree , comments )
103
102
104
- assert . equal ( recast . print ( attachComments ( tree , comments ) ) . code , 'a + 1;' )
103
+ assert . equal ( recast . print ( tree ) . code , 'a + 1;' )
105
104
}
106
105
)
107
106
@@ -116,7 +115,9 @@ test('attachComments', async function (t) {
116
115
onComment : comments
117
116
} )
118
117
119
- assert . equal ( recast . print ( attachComments ( tree ) ) . code , '1 + 1;' )
118
+ attachComments ( tree )
119
+
120
+ assert . equal ( recast . print ( tree ) . code , '1 + 1;' )
120
121
} )
121
122
122
123
await t . test (
@@ -134,7 +135,9 @@ test('attachComments', async function (t) {
134
135
135
136
removePositions ( comments )
136
137
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;' )
138
141
}
139
142
)
140
143
@@ -152,10 +155,9 @@ test('attachComments', async function (t) {
152
155
153
156
removePositions ( tree )
154
157
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;' )
159
161
} )
160
162
161
163
await t . test ( 'should use `loc`s' , async function ( ) {
@@ -171,11 +173,9 @@ test('attachComments', async function (t) {
171
173
} )
172
174
173
175
removePositions ( tree )
176
+ attachComments ( tree , comments )
174
177
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;' )
179
179
} )
180
180
} )
181
181
0 commit comments