-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
159 lines (134 loc) · 3.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/** @import { TSESTree } from '@typescript-eslint/types' */
/** @import { Command, PrintOptions, State } from './types' */
import { handle } from './handlers.js';
import { encode } from '@jridgewell/sourcemap-codec';
/** @type {(str: string) => string} str */
let btoa = () => {
throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
};
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
btoa = (str) => window.btoa(unescape(encodeURIComponent(str)));
// @ts-expect-error
} else if (typeof Buffer === 'function') {
// @ts-expect-error
btoa = (str) => Buffer.from(str, 'utf-8').toString('base64');
}
/**
* @param {{ type: string, [key: string]: any }} node
* @param {PrintOptions} opts
* @returns {{ code: string, map: any }} // TODO
*/
export function print(node, opts = {}) {
if (Array.isArray(node)) {
return print(
{
type: 'Program',
body: node,
sourceType: 'module'
},
opts
);
}
/** @type {State} */
const state = {
commands: [],
comments: [],
multiline: false,
quote: opts.quotes === 'double' ? '"' : "'"
};
handle(/** @type {TSESTree.Node} */ (node), state);
/** @typedef {[number, number, number, number]} Segment */
let code = '';
let current_column = 0;
/** @type {Segment[][]} */
let mappings = [];
/** @type {Segment[]} */
let current_line = [];
/** @param {string} str */
function append(str) {
code += str;
for (let i = 0; i < str.length; i += 1) {
if (str[i] === '\n') {
mappings.push(current_line);
current_line = [];
current_column = 0;
} else {
current_column += 1;
}
}
}
let newline = '\n';
const indent = opts.indent ?? '\t';
/** @param {Command} command */
function run(command) {
if (typeof command === 'string') {
append(command);
return;
}
if (Array.isArray(command)) {
for (let i = 0; i < command.length; i += 1) {
run(command[i]);
}
return;
}
switch (command.type) {
case 'Location':
current_line.push([
current_column,
0, // source index is always zero
command.line - 1,
command.column
]);
break;
case 'Newline':
append(newline);
break;
case 'Indent':
newline += indent;
break;
case 'Dedent':
newline = newline.slice(0, -indent.length);
break;
case 'Comment':
if (command.comment.type === 'Line') {
append(`//${command.comment.value}`);
} else {
append(`/*${command.comment.value.replace(/\n/g, newline)}*/`);
}
break;
}
}
for (let i = 0; i < state.commands.length; i += 1) {
run(state.commands[i]);
}
mappings.push(current_line);
const map = {
version: 3,
/** @type {string[]} */
names: [],
sources: [opts.sourceMapSource || null],
sourcesContent: [opts.sourceMapContent || null],
mappings:
opts.sourceMapEncodeMappings == undefined || opts.sourceMapEncodeMappings
? encode(mappings)
: mappings
};
Object.defineProperties(map, {
toString: {
enumerable: false,
value: function toString() {
return JSON.stringify(this);
}
},
toUrl: {
enumerable: false,
value: function toUrl() {
return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
}
}
});
return {
code,
map
};
}