esrap 1.0.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/LICENSE +7 -0
- package/README.md +57 -0
- package/package.json +30 -0
- package/src/handlers.js +1373 -0
- package/src/index.js +123 -0
- package/src/types.d.ts +23 -0
- package/types/index.d.ts +16 -0
- package/types/index.d.ts.map +14 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { handle } from './handlers.js';
|
|
2
|
+
import { encode } from '@jridgewell/sourcemap-codec';
|
|
3
|
+
|
|
4
|
+
/** @type {(str: string) => string} str */
|
|
5
|
+
let btoa = () => {
|
|
6
|
+
throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
|
|
10
|
+
btoa = (str) => window.btoa(unescape(encodeURIComponent(str)));
|
|
11
|
+
} else if (typeof Buffer === 'function') {
|
|
12
|
+
btoa = (str) => Buffer.from(str, 'utf-8').toString('base64');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {{
|
|
17
|
+
* sourceMapSource?: string;
|
|
18
|
+
* sourceMapContent?: string;
|
|
19
|
+
* sourceMapEncodeMappings?: boolean; // default true
|
|
20
|
+
* }} PrintOptions
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {import('estree').Node} node
|
|
25
|
+
* @param {PrintOptions} opts
|
|
26
|
+
* @returns {{ code: string, map: any }} // TODO
|
|
27
|
+
*/
|
|
28
|
+
export function print(node, opts = {}) {
|
|
29
|
+
if (Array.isArray(node)) {
|
|
30
|
+
return print(
|
|
31
|
+
{
|
|
32
|
+
type: 'Program',
|
|
33
|
+
body: node,
|
|
34
|
+
sourceType: 'module'
|
|
35
|
+
},
|
|
36
|
+
opts
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const chunks = handle(node, {
|
|
41
|
+
indent: '',
|
|
42
|
+
comments: []
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/** @typedef {[number, number, number, number]} Segment */
|
|
46
|
+
|
|
47
|
+
let code = '';
|
|
48
|
+
let current_column = 0;
|
|
49
|
+
|
|
50
|
+
/** @type {Segment[][]} */
|
|
51
|
+
let mappings = [];
|
|
52
|
+
|
|
53
|
+
/** @type {Segment[]} */
|
|
54
|
+
let current_line = [];
|
|
55
|
+
|
|
56
|
+
for (let i = 0; i < chunks.length; i += 1) {
|
|
57
|
+
const chunk = chunks[i];
|
|
58
|
+
|
|
59
|
+
code += chunk.content;
|
|
60
|
+
|
|
61
|
+
if (chunk.loc) {
|
|
62
|
+
current_line.push([
|
|
63
|
+
current_column,
|
|
64
|
+
0, // source index is always zero
|
|
65
|
+
chunk.loc.start.line - 1,
|
|
66
|
+
chunk.loc.start.column
|
|
67
|
+
]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (let i = 0; i < chunk.content.length; i += 1) {
|
|
71
|
+
if (chunk.content[i] === '\n') {
|
|
72
|
+
mappings.push(current_line);
|
|
73
|
+
current_line = [];
|
|
74
|
+
current_column = 0;
|
|
75
|
+
} else {
|
|
76
|
+
current_column += 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (chunk.loc) {
|
|
81
|
+
current_line.push([
|
|
82
|
+
current_column,
|
|
83
|
+
0, // source index is always zero
|
|
84
|
+
chunk.loc.end.line - 1,
|
|
85
|
+
chunk.loc.end.column
|
|
86
|
+
]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
mappings.push(current_line);
|
|
91
|
+
|
|
92
|
+
const map = {
|
|
93
|
+
version: 3,
|
|
94
|
+
/** @type {string[]} */
|
|
95
|
+
names: [],
|
|
96
|
+
sources: [opts.sourceMapSource || null],
|
|
97
|
+
sourcesContent: [opts.sourceMapContent || null],
|
|
98
|
+
mappings:
|
|
99
|
+
opts.sourceMapEncodeMappings == undefined || opts.sourceMapEncodeMappings
|
|
100
|
+
? encode(mappings)
|
|
101
|
+
: mappings
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
Object.defineProperties(map, {
|
|
105
|
+
toString: {
|
|
106
|
+
enumerable: false,
|
|
107
|
+
value: function toString() {
|
|
108
|
+
return JSON.stringify(this);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
toUrl: {
|
|
112
|
+
enumerable: false,
|
|
113
|
+
value: function toUrl() {
|
|
114
|
+
return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
code,
|
|
121
|
+
map
|
|
122
|
+
};
|
|
123
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Comment, Node } from 'estree';
|
|
2
|
+
|
|
3
|
+
type NodeOf<T extends string, X> = X extends { type: T } ? X : never;
|
|
4
|
+
|
|
5
|
+
type Handler<T> = (node: T, state: State) => Chunk[];
|
|
6
|
+
|
|
7
|
+
export type Handlers = {
|
|
8
|
+
[K in Node['type']]: Handler<NodeOf<K, Node>>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface State {
|
|
12
|
+
indent: string;
|
|
13
|
+
comments: Comment[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface Chunk {
|
|
17
|
+
content: string;
|
|
18
|
+
loc: null | {
|
|
19
|
+
start: { line: number; column: number };
|
|
20
|
+
end: { line: number; column: number };
|
|
21
|
+
};
|
|
22
|
+
has_newline: boolean;
|
|
23
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare module 'esrap' {
|
|
2
|
+
/**
|
|
3
|
+
* @returns // TODO
|
|
4
|
+
*/
|
|
5
|
+
export function print(node: import('estree').Node, opts?: PrintOptions): {
|
|
6
|
+
code: string;
|
|
7
|
+
map: any;
|
|
8
|
+
};
|
|
9
|
+
export type PrintOptions = {
|
|
10
|
+
sourceMapSource?: string;
|
|
11
|
+
sourceMapContent?: string;
|
|
12
|
+
sourceMapEncodeMappings?: boolean;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|