@@ -147,37 +147,81 @@ describe('Parser', () => {
147
147
) ;
148
148
} ) ;
149
149
150
- it ( 'Add comments from type in AST' , ( ) => {
151
- const ast = parse ( `
150
+ it ( 'Add single comments in AST' , ( ) => {
151
+ const ast = parse ( dedent `
152
152
#This comment has a \u0A0A multi-byte character.
153
153
type alpha{ field(arg: string):string }
154
154
` ) ;
155
155
156
- expect ( ast ) . to . have . nested . property (
157
- 'comments[0].value' ,
158
- 'This comment has a \u0A0A multi-byte character.' ,
159
- ) ;
160
- expect ( ast . comments ) . to . have . length ( 1 ) ;
156
+ expect ( toJSONDeep ( ast . comments ) ) . to . deep . equal ( [
157
+ {
158
+ kind : 'Comment' ,
159
+ loc : { start : 0 , end : 43 } ,
160
+ value : 'This comment has a ਊ multi-byte character.' ,
161
+ } ,
162
+ ] ) ;
163
+ } ) ;
164
+
165
+ it ( 'Ignore comments that comes when we peek for a token in AST' , ( ) => {
166
+ const ast = parse ( dedent `
167
+ type #This is a comment that gets ignored
168
+ alpha{ field(arg: string):string }
169
+ ` ) ;
170
+
171
+ expect ( toJSONDeep ( ast . comments ) ) . to . deep . equal ( [
172
+ {
173
+ kind : 'Comment' ,
174
+ loc : {
175
+ end : 41 ,
176
+ start : 5 ,
177
+ } ,
178
+ value : 'This is a comment that gets ignored' ,
179
+ } ,
180
+ ] ) ;
181
+ } ) ;
182
+
183
+ it ( 'Add empty comments from in AST' , ( ) => {
184
+ const ast = parse ( dedent `
185
+ #
186
+ type alpha{ field(arg: string):string }
187
+ ` ) ;
188
+
189
+ expect ( toJSONDeep ( ast . comments ) ) . to . deep . equal ( [
190
+ {
191
+ kind : 'Comment' ,
192
+ loc : { start : 0 , end : 1 } ,
193
+ value : '' ,
194
+ } ,
195
+ ] ) ;
161
196
} ) ;
162
197
163
198
it ( 'Add multiple comments in AST' , ( ) => {
164
- const ast = parse ( `
199
+ const ast = parse ( dedent `
200
+ #This is top comment
165
201
type alpha{
166
202
#This comment is demo comment.
167
203
field(arg: string):string
168
204
#This is another demo comment having # inside
169
205
}
170
206
` ) ;
171
207
172
- expect ( ast ) . to . have . nested . property (
173
- 'comments[0].value' ,
174
- 'This comment is demo comment.' ,
175
- ) ;
176
- expect ( ast ) . to . have . nested . property (
177
- 'comments[1].value' ,
178
- 'This is another demo comment having # inside' ,
179
- ) ;
180
- expect ( ast . comments ) . to . have . length ( 2 ) ;
208
+ expect ( toJSONDeep ( ast . comments ) ) . to . deep . equal ( [
209
+ {
210
+ kind : 'Comment' ,
211
+ loc : { start : 0 , end : 20 } ,
212
+ value : 'This is top comment' ,
213
+ } ,
214
+ {
215
+ kind : 'Comment' ,
216
+ loc : { start : 35 , end : 65 } ,
217
+ value : 'This comment is demo comment.' ,
218
+ } ,
219
+ {
220
+ kind : 'Comment' ,
221
+ loc : { start : 97 , end : 142 } ,
222
+ value : 'This is another demo comment having # inside' ,
223
+ } ,
224
+ ] ) ;
181
225
} ) ;
182
226
183
227
it ( 'parses kitchen sink' , ( ) => {
0 commit comments