1
1
/** @import { TSESTree } from '@typescript-eslint/types' */
2
- /** @import { Chunk, Command, Dedent, Handlers, Indent, Newline, NodeWithComments, Sequence , State, TypeAnnotationNodes } from './types' */
2
+ /** @import { Command, Dedent, Handlers, Location, Indent, Newline, NodeWithComments, State, TypeAnnotationNodes } from './types' */
3
3
4
4
/** @type {Newline } */
5
5
const newline = { type : 'Newline' } ;
@@ -11,11 +11,10 @@ const indent = { type: 'Indent' };
11
11
const dedent = { type : 'Dedent' } ;
12
12
13
13
/**
14
- * @param {Command[] } children
15
- * @returns {Sequence }
14
+ * @returns {Command[] }
16
15
*/
17
- function create_sequence ( ... children ) {
18
- return { type : 'Sequence' , children } ;
16
+ function create_sequence ( ) {
17
+ return [ ] ;
19
18
}
20
19
21
20
/**
@@ -30,11 +29,11 @@ function measure(commands, from, to = commands.length) {
30
29
const command = commands [ i ] ;
31
30
if ( typeof command === 'string' ) {
32
31
total += command . length ;
33
- } else if ( command . type === 'Chunk' ) {
34
- total += command . content . length ;
35
- } else if ( command . type === 'Sequence' ) {
36
- // assume this is ', '
37
- total += 2 ;
32
+ } else if ( Array . isArray ( command ) ) {
33
+ total +=
34
+ command . length === 0
35
+ ? 2 // assume this is ', '
36
+ : measure ( command , 0 ) ;
38
37
}
39
38
}
40
39
@@ -66,17 +65,32 @@ export function handle(node, state) {
66
65
}
67
66
}
68
67
68
+ /**
69
+ * @param {number } line
70
+ * @param {number } column
71
+ * @returns {Location }
72
+ */
73
+ function l ( line , column ) {
74
+ return {
75
+ type : 'Location' ,
76
+ line,
77
+ column
78
+ } ;
79
+ }
80
+
69
81
/**
70
82
* @param {string } content
71
83
* @param {TSESTree.Node } node
72
- * @returns {Chunk }
84
+ * @returns {string | Command[] }
73
85
*/
74
86
function c ( content , node ) {
75
- return {
76
- type : 'Chunk' ,
77
- content,
78
- loc : node ?. loc ?? null
79
- } ;
87
+ return node . loc
88
+ ? [
89
+ l ( node . loc . start . line , node . loc . start . column ) ,
90
+ content ,
91
+ l ( node . loc . end . line , node . loc . end . column )
92
+ ]
93
+ : content ;
80
94
}
81
95
82
96
/**
@@ -288,7 +302,7 @@ const handle_body = (nodes, state) => {
288
302
grouped_expression_types . includes ( last_statement . type ) ) &&
289
303
last_statement . type !== statement . type )
290
304
) {
291
- margin . children . push ( '\n' ) ;
305
+ margin . push ( '\n' ) ;
292
306
}
293
307
294
308
let add_newline = false ;
@@ -332,11 +346,11 @@ const handle_var_declaration = (node, state) => {
332
346
333
347
if ( multiline ) {
334
348
state . multiline = true ;
335
- if ( node . declarations . length > 1 ) open . children . push ( indent ) ;
336
- join . children . push ( ',' , newline ) ;
349
+ if ( node . declarations . length > 1 ) open . push ( indent ) ;
350
+ join . push ( ',' , newline ) ;
337
351
if ( node . declarations . length > 1 ) state . commands . push ( dedent ) ;
338
352
} else {
339
- join . children . push ( ', ' ) ;
353
+ join . push ( ', ' ) ;
340
354
}
341
355
} ;
342
356
@@ -408,13 +422,13 @@ function sequence(nodes, state, spaces, fn, separator = ',') {
408
422
if ( multiline ) {
409
423
state . multiline = true ;
410
424
411
- open . children . push ( indent , newline ) ;
412
- join . children . push ( newline ) ;
413
- close . children . push ( dedent , newline ) ;
425
+ open . push ( indent , newline ) ;
426
+ join . push ( newline ) ;
427
+ close . push ( dedent , newline ) ;
414
428
} else {
415
- if ( spaces ) open . children . push ( ' ' ) ;
416
- join . children . push ( ' ' ) ;
417
- if ( spaces ) close . children . push ( ' ' ) ;
429
+ if ( spaces ) open . push ( ' ' ) ;
430
+ join . push ( ' ' ) ;
431
+ if ( spaces ) close . push ( ' ' ) ;
418
432
}
419
433
}
420
434
@@ -710,11 +724,11 @@ const shared = {
710
724
}
711
725
712
726
if ( multiline ) {
713
- open . children . push ( indent , newline ) ;
714
- join . children . push ( ',' , newline ) ;
715
- close . children . push ( dedent , newline ) ;
727
+ open . push ( indent , newline ) ;
728
+ join . push ( ',' , newline ) ;
729
+ close . push ( dedent , newline ) ;
716
730
} else {
717
- join . children . push ( ', ' ) ;
731
+ join . push ( ', ' ) ;
718
732
}
719
733
} ,
720
734
@@ -906,12 +920,12 @@ const handlers = {
906
920
const multiline = child_state . multiline ;
907
921
908
922
if ( multiline ) {
909
- if_true . children . push ( indent , newline , '? ' ) ;
910
- if_false . children . push ( newline , ': ' ) ;
923
+ if_true . push ( indent , newline , '? ' ) ;
924
+ if_false . push ( newline , ': ' ) ;
911
925
state . commands . push ( dedent ) ;
912
926
} else {
913
- if_true . children . push ( ' ? ' ) ;
914
- if_false . children . push ( ' : ' ) ;
927
+ if_true . push ( ' ? ' ) ;
928
+ if_false . push ( ' : ' ) ;
915
929
}
916
930
} ,
917
931
0 commit comments