esrap 2.0.0 → 2.1.0
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/README.md +1 -0
- package/package.json +1 -1
- package/src/context.js +15 -1
- package/src/index.js +7 -2
- package/src/languages/ts/index.js +20 -23
- package/types/index.d.ts +1 -0
- package/types/index.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -107,6 +107,7 @@ The `context` API has several methods:
|
|
|
107
107
|
- `context.write(data: string, node?: BaseNode)` — add a string. If `node` is provided and has a standard `loc` property (with `start` and `end` properties each with a `line` and `column`), a sourcemap mapping will be created
|
|
108
108
|
- `context.indent()` — increase the indentation level, typically before adding a newline
|
|
109
109
|
- `context.newline()` — self-explanatory
|
|
110
|
+
- `context.space()` — adds a space character, if it doesn't immediately follow a newline
|
|
110
111
|
- `context.margin()` — causes the next newline to be repeated (consecutive newlines are otherwise merged into one)
|
|
111
112
|
- `context.dedent()` — decrease the indentation level (again, typically before adding a newline)
|
|
112
113
|
- `context.visit(node: BaseNode)` — calls the visitor corresponding to `node.type`
|
package/package.json
CHANGED
package/src/context.js
CHANGED
|
@@ -5,10 +5,12 @@ export const margin = 0;
|
|
|
5
5
|
export const newline = 1;
|
|
6
6
|
export const indent = 2;
|
|
7
7
|
export const dedent = 3;
|
|
8
|
+
export const space = 4;
|
|
8
9
|
|
|
9
10
|
export class Context {
|
|
10
11
|
#visitors;
|
|
11
12
|
#commands;
|
|
13
|
+
#has_newline = false;
|
|
12
14
|
|
|
13
15
|
multiline = false;
|
|
14
16
|
|
|
@@ -35,15 +37,23 @@ export class Context {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
newline() {
|
|
38
|
-
this
|
|
40
|
+
this.#has_newline = true;
|
|
39
41
|
this.#commands.push(newline);
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
space() {
|
|
45
|
+
this.#commands.push(space);
|
|
46
|
+
}
|
|
47
|
+
|
|
42
48
|
/**
|
|
43
49
|
* @param {Context} context
|
|
44
50
|
*/
|
|
45
51
|
append(context) {
|
|
46
52
|
this.#commands.push(context.#commands);
|
|
53
|
+
|
|
54
|
+
if (this.#has_newline) {
|
|
55
|
+
this.multiline = true;
|
|
56
|
+
}
|
|
47
57
|
}
|
|
48
58
|
|
|
49
59
|
/**
|
|
@@ -59,6 +69,10 @@ export class Context {
|
|
|
59
69
|
} else {
|
|
60
70
|
this.#commands.push(content);
|
|
61
71
|
}
|
|
72
|
+
|
|
73
|
+
if (this.#has_newline) {
|
|
74
|
+
this.multiline = true;
|
|
75
|
+
}
|
|
62
76
|
}
|
|
63
77
|
|
|
64
78
|
/**
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** @import { BaseNode, Command, Visitors, PrintOptions } from './types' */
|
|
2
2
|
import { encode } from '@jridgewell/sourcemap-codec';
|
|
3
|
-
import { Context, dedent, indent, margin, newline } from './context.js';
|
|
3
|
+
import { Context, dedent, indent, margin, newline, space } from './context.js';
|
|
4
4
|
|
|
5
5
|
/** @type {(str: string) => string} str */
|
|
6
6
|
let btoa = () => {
|
|
@@ -91,6 +91,7 @@ export function print(node, visitors, opts = {}) {
|
|
|
91
91
|
|
|
92
92
|
let needs_newline = false;
|
|
93
93
|
let needs_margin = false;
|
|
94
|
+
let needs_space = false;
|
|
94
95
|
|
|
95
96
|
/** @param {Command} command */
|
|
96
97
|
function run(command) {
|
|
@@ -106,6 +107,8 @@ export function print(node, visitors, opts = {}) {
|
|
|
106
107
|
needs_newline = true;
|
|
107
108
|
} else if (command === margin) {
|
|
108
109
|
needs_margin = true;
|
|
110
|
+
} else if (command === space) {
|
|
111
|
+
needs_space = true;
|
|
109
112
|
} else if (command === indent) {
|
|
110
113
|
current_newline += indent_str;
|
|
111
114
|
} else if (command === dedent) {
|
|
@@ -117,9 +120,11 @@ export function print(node, visitors, opts = {}) {
|
|
|
117
120
|
|
|
118
121
|
if (needs_newline) {
|
|
119
122
|
append(needs_margin ? '\n' + current_newline : current_newline);
|
|
123
|
+
} else if (needs_space) {
|
|
124
|
+
append(' ');
|
|
120
125
|
}
|
|
121
126
|
|
|
122
|
-
needs_margin = needs_newline = false;
|
|
127
|
+
needs_margin = needs_newline = needs_space = false;
|
|
123
128
|
|
|
124
129
|
if (typeof command === 'string') {
|
|
125
130
|
append(command);
|
|
@@ -139,7 +139,6 @@ export default (options = {}) => {
|
|
|
139
139
|
* @param {Context} context
|
|
140
140
|
* @param {{ line: number, column: number } | null} prev
|
|
141
141
|
* @param {{ line: number, column: number } | null} next
|
|
142
|
-
* @returns {boolean} true if final comment is a line comment
|
|
143
142
|
*/
|
|
144
143
|
function flush_trailing_comments(context, prev, next) {
|
|
145
144
|
while (comment_index < comments.length) {
|
|
@@ -157,14 +156,14 @@ export default (options = {}) => {
|
|
|
157
156
|
comment_index += 1;
|
|
158
157
|
|
|
159
158
|
if (comment.type === 'Line') {
|
|
160
|
-
|
|
159
|
+
context.newline();
|
|
160
|
+
} else {
|
|
161
|
+
continue;
|
|
161
162
|
}
|
|
162
|
-
} else {
|
|
163
|
-
break;
|
|
164
163
|
}
|
|
165
|
-
}
|
|
166
164
|
|
|
167
|
-
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
168
167
|
}
|
|
169
168
|
|
|
170
169
|
/**
|
|
@@ -226,9 +225,7 @@ export default (options = {}) => {
|
|
|
226
225
|
}
|
|
227
226
|
|
|
228
227
|
const next = i === nodes.length - 1 ? until : nodes[i + 1]?.loc?.start || null;
|
|
229
|
-
|
|
230
|
-
multiline = true;
|
|
231
|
-
}
|
|
228
|
+
flush_trailing_comments(child_context, child?.loc?.end || null, next);
|
|
232
229
|
|
|
233
230
|
length += child_context.measure() + 1;
|
|
234
231
|
multiline ||= child_context.multiline;
|
|
@@ -293,7 +290,8 @@ export default (options = {}) => {
|
|
|
293
290
|
let prev_type = null;
|
|
294
291
|
let prev_multiline = false;
|
|
295
292
|
|
|
296
|
-
for (
|
|
293
|
+
for (let i = 0; i < node.body.length; i += 1) {
|
|
294
|
+
const child = node.body[i];
|
|
297
295
|
if (child.type === 'EmptyStatement') continue;
|
|
298
296
|
|
|
299
297
|
const child_context = context.new();
|
|
@@ -309,6 +307,12 @@ export default (options = {}) => {
|
|
|
309
307
|
|
|
310
308
|
context.append(child_context);
|
|
311
309
|
|
|
310
|
+
flush_trailing_comments(
|
|
311
|
+
context,
|
|
312
|
+
child.loc?.end || null,
|
|
313
|
+
node.body[i + 1]?.loc?.end ?? node.loc?.end ?? null
|
|
314
|
+
);
|
|
315
|
+
|
|
312
316
|
prev_type = child.type;
|
|
313
317
|
prev_multiline = child_context.multiline;
|
|
314
318
|
}
|
|
@@ -472,9 +476,7 @@ export default (options = {}) => {
|
|
|
472
476
|
? (node.loc?.end ?? null)
|
|
473
477
|
: (node.arguments[i + 1]?.loc?.start ?? null);
|
|
474
478
|
|
|
475
|
-
|
|
476
|
-
child_context.multiline = true;
|
|
477
|
-
}
|
|
479
|
+
flush_trailing_comments(context, arg.loc?.end ?? null, next);
|
|
478
480
|
|
|
479
481
|
if (!is_last) context.append(join);
|
|
480
482
|
}
|
|
@@ -516,9 +518,9 @@ export default (options = {}) => {
|
|
|
516
518
|
context.write(' ');
|
|
517
519
|
}
|
|
518
520
|
|
|
519
|
-
if (node.implements) {
|
|
520
|
-
context.write('implements
|
|
521
|
-
sequence(context, node.implements, node.body.loc?.start ?? null,
|
|
521
|
+
if (node.implements && node.implements.length > 0) {
|
|
522
|
+
context.write('implements');
|
|
523
|
+
sequence(context, node.implements, node.body.loc?.start ?? null, true);
|
|
522
524
|
}
|
|
523
525
|
|
|
524
526
|
context.visit(node.body);
|
|
@@ -584,17 +586,11 @@ export default (options = {}) => {
|
|
|
584
586
|
|
|
585
587
|
return {
|
|
586
588
|
_(node, context, visit) {
|
|
587
|
-
const is_statement = /(Statement|Declaration)$/.test(node.type);
|
|
588
|
-
|
|
589
589
|
if (node.loc) {
|
|
590
590
|
flush_comments_until(context, null, node.loc.start, true);
|
|
591
591
|
}
|
|
592
592
|
|
|
593
593
|
visit(node);
|
|
594
|
-
|
|
595
|
-
if (is_statement && node.loc) {
|
|
596
|
-
flush_trailing_comments(context, node.loc.end, null);
|
|
597
|
-
}
|
|
598
594
|
},
|
|
599
595
|
|
|
600
596
|
ArrayExpression: shared['ArrayExpression|ArrayPattern'],
|
|
@@ -868,7 +864,8 @@ export default (options = {}) => {
|
|
|
868
864
|
context.visit(node.consequent);
|
|
869
865
|
|
|
870
866
|
if (node.alternate) {
|
|
871
|
-
context.
|
|
867
|
+
context.space();
|
|
868
|
+
context.write('else ');
|
|
872
869
|
context.visit(node.alternate);
|
|
873
870
|
}
|
|
874
871
|
},
|
package/types/index.d.ts
CHANGED
package/types/index.d.ts.map
CHANGED
|
@@ -34,6 +34,6 @@
|
|
|
34
34
|
null,
|
|
35
35
|
null
|
|
36
36
|
],
|
|
37
|
-
"mappings": ";MAGYA,QAAQA;;;;;;;;MAQfC,MAAMA;;MAENC,mBAAmBA;;;;MAIZC,OAAOA;;aAEPC,QAAQA;;;;WAwBHC,QAAQA;;;;;;MAWbC,OAAOA;;kBAEFC,YAAYA;;;;;;;;;iBCHbC,KAAKA;;;;
|
|
37
|
+
"mappings": ";MAGYA,QAAQA;;;;;;;;MAQfC,MAAMA;;MAENC,mBAAmBA;;;;MAIZC,OAAOA;;aAEPC,QAAQA;;;;WAwBHC,QAAQA;;;;;;MAWbC,OAAOA;;kBAEFC,YAAYA;;;;;;;;;iBCHbC,KAAKA;;;;cC5CRC,OAAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cCDPC,sBAAsBA;;aAHZC,IAAIA;MCLfC,SAASA;;;;;WAKXC,QAAQA;;;;;;;kBAODC,OAAOA;;;;;;;;;;MJTZd,QAAQA;;;;;;;;MAQfC,MAAMA;;MAENC,mBAAmBA;;;;MAIZC,OAAOA;;MAEPC,QAAQA;;;;;;;;;;;aKdGO,IAAIA;MDLfC,SAASA;;;;;WAKXC,QAAQA;;;;;;;kBAODC,OAAOA;;;;;;;;;;MJTZd,QAAQA;;;;;;;;MAQfC,MAAMA;;MAENC,mBAAmBA;;;;MAIZC,OAAOA;;MAEPC,QAAQA",
|
|
38
38
|
"ignoreList": []
|
|
39
39
|
}
|