espolar 0.2.1 → 0.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "espolar",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
package/src/api.ts CHANGED
@@ -53,13 +53,12 @@ export interface PrinterContext<Data = any> {
53
53
  data?: Data,
54
54
  ): void;
55
55
  writeNode(node: AST.Node | null | undefined): void;
56
- writeNodeList(
57
- nodes: readonly (AST.Node | null | undefined)[],
58
- separator: string,
56
+ writeNodeList(nodes: readonly (AST.Node | null)[], separator: string): void;
57
+ writeExpressionListWithCommaSep(
58
+ nodes: readonly (AST.Expression | AST.SpreadElement | null)[],
59
59
  ): void;
60
- writeNodeListWithSourceGaps(
61
- nodes: readonly (AST.Node | null | undefined)[],
62
- fallbackSeparator: string,
60
+ writeNodeListWithNewLineSep(
61
+ nodes: readonly (AST.ProgramStatement | AST.ClassElement)[],
63
62
  ): void;
64
63
  writeSource(start: number, end: number, data?: Data): void;
65
64
  writePreservedNode(node: AST.Node): void;
package/src/printer.ts CHANGED
@@ -5,7 +5,11 @@ import type {
5
5
  PrinterContext,
6
6
  Printers,
7
7
  } from "./api.ts";
8
- import { defaultPrinters } from "./printers.ts";
8
+ import {
9
+ defaultPrinters,
10
+ expectAssignmentExprNeedsParen,
11
+ writeComment,
12
+ } from "./printers.ts";
9
13
  import type { AST, AST_NODE_TYPES, Comment, NodeLike } from "./types.ts";
10
14
  import {
11
15
  getNodeRange,
@@ -21,17 +25,6 @@ interface InternalPrinterContext extends PrinterContext<any> {
21
25
  result(): PrintResult<any>;
22
26
  }
23
27
 
24
- function writeComment(context: InternalPrinterContext, comment: Comment): void {
25
- if (comment.type === "Line") {
26
- context.write("//" + comment.value + "\n");
27
- } else {
28
- context.write("/*" + comment.value + "*/");
29
- if (comment.value.includes("\n")) {
30
- context.write("\n");
31
- }
32
- }
33
- }
34
-
35
28
  export function print<Data = undefined>(
36
29
  node: AST.Node,
37
30
  options: PrintOptions<Data>,
@@ -159,8 +152,8 @@ function createPrinterContext<Data>(
159
152
 
160
153
  const leadingComments = options.getLeadingComments?.(node);
161
154
  if (leadingComments) {
162
- for (const c of leadingComments) {
163
- writeComment(context, c);
155
+ for (const comment of leadingComments) {
156
+ writeComment(comment, context);
164
157
  }
165
158
  }
166
159
 
@@ -170,8 +163,8 @@ function createPrinterContext<Data>(
170
163
 
171
164
  const trailingComments = options.getTrailingComments?.(node);
172
165
  if (trailingComments) {
173
- for (const c of trailingComments) {
174
- writeComment(context, c);
166
+ for (const comment of trailingComments) {
167
+ writeComment(comment, context);
175
168
  }
176
169
  }
177
170
  // If children nodes don't emit any mapping but the parent node itself
@@ -203,7 +196,31 @@ function createPrinterContext<Data>(
203
196
  needsSeparator = true;
204
197
  }
205
198
  },
206
- writeNodeListWithSourceGaps(nodes, fallbackSeparator) {
199
+ writeExpressionListWithCommaSep(nodes) {
200
+ let needsSeparator = false;
201
+ for (const node of nodes) {
202
+ if (!node) {
203
+ if (needsSeparator) {
204
+ context.write(", ");
205
+ }
206
+ continue;
207
+ }
208
+
209
+ if (needsSeparator) {
210
+ context.write(", ");
211
+ }
212
+ const needsParens = expectAssignmentExprNeedsParen(node);
213
+ if (needsParens) {
214
+ context.write("(");
215
+ context.writeNode(node);
216
+ context.write(")");
217
+ } else {
218
+ context.writeNode(node);
219
+ }
220
+ needsSeparator = true;
221
+ }
222
+ },
223
+ writeNodeListWithNewLineSep(nodes) {
207
224
  let lastRangeEnd: number | undefined;
208
225
  let wroteNode = false;
209
226
 
@@ -225,7 +242,7 @@ function createPrinterContext<Data>(
225
242
  getMappingData(null),
226
243
  );
227
244
  } else {
228
- context.write(fallbackSeparator);
245
+ context.write("\n");
229
246
  }
230
247
  }
231
248