esrap 1.3.6 → 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 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 information that will be added to the resulting sourcemap (note that the AST is assumed to come from a single file):
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
- sourceMapContent: fs.readFileSync('input.js', 'utf-8')
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esrap",
3
- "version": "1.3.6",
3
+ "version": "1.4.0",
4
4
  "description": "Parse in reverse",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,6 +19,7 @@
19
19
  },
20
20
  "types": "./types/index.d.ts",
21
21
  "devDependencies": {
22
+ "@changesets/cli": "^2.27.11",
22
23
  "@typescript-eslint/types": "^8.2.0",
23
24
  "@vitest/ui": "^2.1.1",
24
25
  "acorn": "^8.11.3",
@@ -31,7 +32,6 @@
31
32
  },
32
33
  "license": "MIT",
33
34
  "dependencies": {
34
- "@changesets/cli": "^2.27.11",
35
35
  "@jridgewell/sourcemap-codec": "^1.4.15"
36
36
  },
37
37
  "publishConfig": {
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
- let value = node.raw;
1129
- if (!value)
1130
- value = typeof node.value === 'string' ? JSON.stringify(node.value) : String(node.value);
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 += '\t';
113
+ newline += indent;
112
114
  break;
113
115
 
114
116
  case 'Dedent':
115
- newline = newline.slice(0, -1);
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
@@ -3,6 +3,8 @@ declare module 'esrap' {
3
3
  sourceMapSource?: string;
4
4
  sourceMapContent?: string;
5
5
  sourceMapEncodeMappings?: boolean; // default true
6
+ indent?: string; // default tab
7
+ quotes?: 'single' | 'double'; // default single
6
8
  }
7
9
  /**
8
10
  * @returns // TODO
@@ -13,6 +13,6 @@
13
13
  null,
14
14
  null
15
15
  ],
16
- "mappings": ";kBA6EiBA,YAAYA;;;;;;;;iBCtDbC,KAAKA",
16
+ "mappings": ";kBA8EiBA,YAAYA;;;;;;;;;;iBCvDbC,KAAKA",
17
17
  "ignoreList": []
18
18
  }