esrap 1.3.7 → 1.4.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 +19 -2
- package/package.json +1 -1
- package/src/handlers.js +11 -3
- package/src/index.js +5 -3
- package/src/types.d.ts +3 -0
- package/types/index.d.ts +2 -0
- package/types/index.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -35,12 +35,29 @@ If the nodes of the input AST have `loc` properties (e.g. the AST was generated
|
|
|
35
35
|
|
|
36
36
|
## Options
|
|
37
37
|
|
|
38
|
-
You can pass
|
|
38
|
+
You can pass the following options:
|
|
39
39
|
|
|
40
40
|
```js
|
|
41
41
|
const { code, map } = print(ast, {
|
|
42
|
+
// Populate the `sources` field of the resulting sourcemap
|
|
43
|
+
// (note that the AST is assumed to come from a single file)
|
|
42
44
|
sourceMapSource: 'input.js',
|
|
43
|
-
|
|
45
|
+
|
|
46
|
+
// Populate the `sourcesContent` field of the resulting sourcemap
|
|
47
|
+
sourceMapContent: fs.readFileSync('input.js', 'utf-8'),
|
|
48
|
+
|
|
49
|
+
// Whether to encode the `mappings` field of the resulting sourcemap
|
|
50
|
+
// as a VLQ string, rather than an unencoded array. Defaults to `true`
|
|
51
|
+
sourceMapEncodeMappings: false,
|
|
52
|
+
|
|
53
|
+
// String to use for indentation — defaults to '\t'
|
|
54
|
+
indent: ' ',
|
|
55
|
+
|
|
56
|
+
// Whether to wrap strings in single or double quotes — defaults to 'single'.
|
|
57
|
+
// This only applies to string literals with no `raw` value, which generally
|
|
58
|
+
// means the AST node was generated programmatically, rather than parsed
|
|
59
|
+
// from an original source
|
|
60
|
+
quotes: 'single'
|
|
44
61
|
});
|
|
45
62
|
```
|
|
46
63
|
|
package/package.json
CHANGED
package/src/handlers.js
CHANGED
|
@@ -96,6 +96,14 @@ function prepend_comments(comments, state, newlines) {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
/**
|
|
100
|
+
* @param {string} string
|
|
101
|
+
* @param {'\'' | '"'} char
|
|
102
|
+
*/
|
|
103
|
+
function quote(string, char) {
|
|
104
|
+
return char + string.replaceAll(char, '\\' + char) + char;
|
|
105
|
+
}
|
|
106
|
+
|
|
99
107
|
const OPERATOR_PRECEDENCE = {
|
|
100
108
|
'||': 2,
|
|
101
109
|
'&&': 3,
|
|
@@ -1125,9 +1133,9 @@ const handlers = {
|
|
|
1125
1133
|
// TODO do we need to handle weird unicode characters somehow?
|
|
1126
1134
|
// str.replace(/\\u(\d{4})/g, (m, n) => String.fromCharCode(+n))
|
|
1127
1135
|
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1136
|
+
const value =
|
|
1137
|
+
node.raw ||
|
|
1138
|
+
(typeof node.value === 'string' ? quote(node.value, state.quote) : String(node.value));
|
|
1131
1139
|
|
|
1132
1140
|
state.commands.push(c(value, node));
|
|
1133
1141
|
},
|
package/src/index.js
CHANGED
|
@@ -37,7 +37,8 @@ export function print(node, opts = {}) {
|
|
|
37
37
|
const state = {
|
|
38
38
|
commands: [],
|
|
39
39
|
comments: [],
|
|
40
|
-
multiline: false
|
|
40
|
+
multiline: false,
|
|
41
|
+
quote: opts.quotes === 'double' ? '"' : "'"
|
|
41
42
|
};
|
|
42
43
|
|
|
43
44
|
handle(/** @type {TSESTree.Node} */ (node), state);
|
|
@@ -69,6 +70,7 @@ export function print(node, opts = {}) {
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
let newline = '\n';
|
|
73
|
+
const indent = opts.indent ?? '\t';
|
|
72
74
|
|
|
73
75
|
/** @param {Command} command */
|
|
74
76
|
function run(command) {
|
|
@@ -108,11 +110,11 @@ export function print(node, opts = {}) {
|
|
|
108
110
|
break;
|
|
109
111
|
|
|
110
112
|
case 'Indent':
|
|
111
|
-
newline +=
|
|
113
|
+
newline += indent;
|
|
112
114
|
break;
|
|
113
115
|
|
|
114
116
|
case 'Dedent':
|
|
115
|
-
newline = newline.slice(0, -
|
|
117
|
+
newline = newline.slice(0, -indent.length);
|
|
116
118
|
break;
|
|
117
119
|
|
|
118
120
|
case 'Sequence':
|
package/src/types.d.ts
CHANGED
|
@@ -35,6 +35,7 @@ export interface State {
|
|
|
35
35
|
commands: Command[];
|
|
36
36
|
comments: TSESTree.Comment[];
|
|
37
37
|
multiline: boolean;
|
|
38
|
+
quote: "'" | '"';
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
export interface Chunk {
|
|
@@ -79,4 +80,6 @@ export interface PrintOptions {
|
|
|
79
80
|
sourceMapSource?: string;
|
|
80
81
|
sourceMapContent?: string;
|
|
81
82
|
sourceMapEncodeMappings?: boolean; // default true
|
|
83
|
+
indent?: string; // default tab
|
|
84
|
+
quotes?: 'single' | 'double'; // default single
|
|
82
85
|
}
|
package/types/index.d.ts
CHANGED