esrap 1.4.0 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/handlers.js +85 -42
- package/src/index.js +14 -30
- package/src/types.d.ts +5 -13
- package/types/index.d.ts.map +1 -1
package/package.json
CHANGED
package/src/handlers.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** @import { TSESTree } from '@typescript-eslint/types' */
|
|
2
|
-
/** @import {
|
|
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
|
-
* @
|
|
15
|
-
* @returns {Sequence}
|
|
14
|
+
* @returns {Command[]}
|
|
16
15
|
*/
|
|
17
|
-
function create_sequence(
|
|
18
|
-
return
|
|
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
|
|
34
|
-
total +=
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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 {
|
|
84
|
+
* @returns {string | Command[]}
|
|
73
85
|
*/
|
|
74
86
|
function c(content, node) {
|
|
75
|
-
return
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
/**
|
|
@@ -101,7 +115,26 @@ function prepend_comments(comments, state, newlines) {
|
|
|
101
115
|
* @param {'\'' | '"'} char
|
|
102
116
|
*/
|
|
103
117
|
function quote(string, char) {
|
|
104
|
-
|
|
118
|
+
let out = char;
|
|
119
|
+
let escaped = false;
|
|
120
|
+
|
|
121
|
+
for (const c of string) {
|
|
122
|
+
if (escaped) {
|
|
123
|
+
out += c;
|
|
124
|
+
escaped = false;
|
|
125
|
+
} else if (c === '\\') {
|
|
126
|
+
out += '\\\\';
|
|
127
|
+
escaped = true;
|
|
128
|
+
} else if (c === char) {
|
|
129
|
+
out += '\\' + c;
|
|
130
|
+
} else if (c === '\n') {
|
|
131
|
+
out += '\\n';
|
|
132
|
+
} else {
|
|
133
|
+
out += c;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return out + char;
|
|
105
138
|
}
|
|
106
139
|
|
|
107
140
|
const OPERATOR_PRECEDENCE = {
|
|
@@ -288,7 +321,7 @@ const handle_body = (nodes, state) => {
|
|
|
288
321
|
grouped_expression_types.includes(last_statement.type)) &&
|
|
289
322
|
last_statement.type !== statement.type)
|
|
290
323
|
) {
|
|
291
|
-
margin.
|
|
324
|
+
margin.push('\n');
|
|
292
325
|
}
|
|
293
326
|
|
|
294
327
|
let add_newline = false;
|
|
@@ -332,11 +365,11 @@ const handle_var_declaration = (node, state) => {
|
|
|
332
365
|
|
|
333
366
|
if (multiline) {
|
|
334
367
|
state.multiline = true;
|
|
335
|
-
if (node.declarations.length > 1) open.
|
|
336
|
-
join.
|
|
368
|
+
if (node.declarations.length > 1) open.push(indent);
|
|
369
|
+
join.push(',', newline);
|
|
337
370
|
if (node.declarations.length > 1) state.commands.push(dedent);
|
|
338
371
|
} else {
|
|
339
|
-
join.
|
|
372
|
+
join.push(', ');
|
|
340
373
|
}
|
|
341
374
|
};
|
|
342
375
|
|
|
@@ -408,13 +441,13 @@ function sequence(nodes, state, spaces, fn, separator = ',') {
|
|
|
408
441
|
if (multiline) {
|
|
409
442
|
state.multiline = true;
|
|
410
443
|
|
|
411
|
-
open.
|
|
412
|
-
join.
|
|
413
|
-
close.
|
|
444
|
+
open.push(indent, newline);
|
|
445
|
+
join.push(newline);
|
|
446
|
+
close.push(dedent, newline);
|
|
414
447
|
} else {
|
|
415
|
-
if (spaces) open.
|
|
416
|
-
join.
|
|
417
|
-
if (spaces) close.
|
|
448
|
+
if (spaces) open.push(' ');
|
|
449
|
+
join.push(' ');
|
|
450
|
+
if (spaces) close.push(' ');
|
|
418
451
|
}
|
|
419
452
|
}
|
|
420
453
|
|
|
@@ -623,16 +656,26 @@ const shared = {
|
|
|
623
656
|
* @param {State} state
|
|
624
657
|
*/
|
|
625
658
|
'BlockStatement|ClassBody': (node, state) => {
|
|
626
|
-
if (node.
|
|
627
|
-
|
|
628
|
-
|
|
659
|
+
if (node.loc) {
|
|
660
|
+
const { line, column } = node.loc.start;
|
|
661
|
+
state.commands.push(l(line, column), '{', l(line, column + 1));
|
|
662
|
+
} else {
|
|
663
|
+
state.commands.push('{');
|
|
629
664
|
}
|
|
630
665
|
|
|
631
|
-
|
|
666
|
+
if (node.body.length > 0) {
|
|
667
|
+
state.multiline = true;
|
|
668
|
+
state.commands.push(indent, newline);
|
|
669
|
+
handle_body(node.body, state);
|
|
670
|
+
state.commands.push(dedent, newline);
|
|
671
|
+
}
|
|
632
672
|
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
673
|
+
if (node.loc) {
|
|
674
|
+
const { line, column } = node.loc.end;
|
|
675
|
+
state.commands.push(l(line, column - 1), '}', l(line, column));
|
|
676
|
+
} else {
|
|
677
|
+
state.commands.push('}');
|
|
678
|
+
}
|
|
636
679
|
},
|
|
637
680
|
|
|
638
681
|
/**
|
|
@@ -710,11 +753,11 @@ const shared = {
|
|
|
710
753
|
}
|
|
711
754
|
|
|
712
755
|
if (multiline) {
|
|
713
|
-
open.
|
|
714
|
-
join.
|
|
715
|
-
close.
|
|
756
|
+
open.push(indent, newline);
|
|
757
|
+
join.push(',', newline);
|
|
758
|
+
close.push(dedent, newline);
|
|
716
759
|
} else {
|
|
717
|
-
join.
|
|
760
|
+
join.push(', ');
|
|
718
761
|
}
|
|
719
762
|
},
|
|
720
763
|
|
|
@@ -906,12 +949,12 @@ const handlers = {
|
|
|
906
949
|
const multiline = child_state.multiline;
|
|
907
950
|
|
|
908
951
|
if (multiline) {
|
|
909
|
-
if_true.
|
|
910
|
-
if_false.
|
|
952
|
+
if_true.push(indent, newline, '? ');
|
|
953
|
+
if_false.push(newline, ': ');
|
|
911
954
|
state.commands.push(dedent);
|
|
912
955
|
} else {
|
|
913
|
-
if_true.
|
|
914
|
-
if_false.
|
|
956
|
+
if_true.push(' ? ');
|
|
957
|
+
if_false.push(' : ');
|
|
915
958
|
}
|
|
916
959
|
},
|
|
917
960
|
|
package/src/index.js
CHANGED
|
@@ -79,30 +79,21 @@ export function print(node, opts = {}) {
|
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
current_column,
|
|
89
|
-
0, // source index is always zero
|
|
90
|
-
loc.start.line - 1,
|
|
91
|
-
loc.start.column
|
|
92
|
-
]);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
append(command.content);
|
|
96
|
-
|
|
97
|
-
if (loc) {
|
|
98
|
-
current_line.push([
|
|
99
|
-
current_column,
|
|
100
|
-
0, // source index is always zero
|
|
101
|
-
loc.end.line - 1,
|
|
102
|
-
loc.end.column
|
|
103
|
-
]);
|
|
104
|
-
}
|
|
82
|
+
if (Array.isArray(command)) {
|
|
83
|
+
for (let i = 0; i < command.length; i += 1) {
|
|
84
|
+
run(command[i]);
|
|
85
|
+
}
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
105
88
|
|
|
89
|
+
switch (command.type) {
|
|
90
|
+
case 'Location':
|
|
91
|
+
current_line.push([
|
|
92
|
+
current_column,
|
|
93
|
+
0, // source index is always zero
|
|
94
|
+
command.line - 1,
|
|
95
|
+
command.column
|
|
96
|
+
]);
|
|
106
97
|
break;
|
|
107
98
|
|
|
108
99
|
case 'Newline':
|
|
@@ -117,13 +108,6 @@ export function print(node, opts = {}) {
|
|
|
117
108
|
newline = newline.slice(0, -indent.length);
|
|
118
109
|
break;
|
|
119
110
|
|
|
120
|
-
case 'Sequence':
|
|
121
|
-
for (let i = 0; i < command.children.length; i += 1) {
|
|
122
|
-
run(command.children[i]);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
break;
|
|
126
|
-
|
|
127
111
|
case 'Comment':
|
|
128
112
|
if (command.comment.type === 'Line') {
|
|
129
113
|
append(`//${command.comment.value}`);
|
package/src/types.d.ts
CHANGED
|
@@ -38,13 +38,10 @@ export interface State {
|
|
|
38
38
|
quote: "'" | '"';
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
export interface
|
|
42
|
-
type: '
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
start: { line: number; column: number };
|
|
46
|
-
end: { line: number; column: number };
|
|
47
|
-
};
|
|
41
|
+
export interface Location {
|
|
42
|
+
type: 'Location';
|
|
43
|
+
line: number;
|
|
44
|
+
column: number;
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
export interface Newline {
|
|
@@ -64,17 +61,12 @@ export interface IndentChange {
|
|
|
64
61
|
offset: number;
|
|
65
62
|
}
|
|
66
63
|
|
|
67
|
-
export interface Sequence {
|
|
68
|
-
type: 'Sequence';
|
|
69
|
-
children: Command[];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
64
|
export interface CommentChunk {
|
|
73
65
|
type: 'Comment';
|
|
74
66
|
comment: TSESTree.Comment;
|
|
75
67
|
}
|
|
76
68
|
|
|
77
|
-
export type Command = string |
|
|
69
|
+
export type Command = string | Location | Newline | Indent | Dedent | CommentChunk | Command[];
|
|
78
70
|
|
|
79
71
|
export interface PrintOptions {
|
|
80
72
|
sourceMapSource?: string;
|