ember-estree 0.6.7 → 0.6.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/print.js +65 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-estree",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "description": "ESTree generator for gjs and gts file used by ember",
5
5
  "keywords": [
6
6
  "AST",
package/src/print.js CHANGED
@@ -26,9 +26,15 @@ export function print(node) {
26
26
  case "Literal":
27
27
  case "StringLiteral":
28
28
  if (typeof node.value === "string") {
29
+ // Prefer the original source: it preserves escape sequences
30
+ // (`\n`, `\t`, `\uXXXX`, escaped quotes) and the original quote
31
+ // style exactly. Emitting the cooked `value` turns escapes into raw
32
+ // characters and corrupts the string (e.g. `'\n'` -> a real newline,
33
+ // which breaks a single-quoted literal).
29
34
  const raw = node.extra?.raw ?? node.raw;
30
- const quote = raw && (raw[0] === "'" || raw[0] === '"' || raw[0] === "`") ? raw[0] : '"';
31
- return `${quote}${node.value}${quote}`;
35
+ if (raw != null) return raw;
36
+ // Synthesized node with no source — quote and escape the value.
37
+ return JSON.stringify(node.value);
32
38
  }
33
39
  if (node.raw != null) return node.raw;
34
40
  return String(node.value);
@@ -83,6 +89,12 @@ export function print(node) {
83
89
  case "ChainExpression":
84
90
  return print(node.expression);
85
91
 
92
+ case "V8IntrinsicExpression": {
93
+ const name = typeof node.name === "string" ? node.name : print(node.name);
94
+ const args = (node.arguments ?? []).map(print).join(", ");
95
+ return `%${name}(${args})`;
96
+ }
97
+
86
98
  case "ParenthesizedExpression":
87
99
  return `(${print(node.expression)})`;
88
100
 
@@ -203,7 +215,7 @@ export function print(node) {
203
215
  case "BlockStatement":
204
216
  case "StaticBlock": {
205
217
  const body = (node.body ?? []).map(print).join("\n");
206
- return `{\n${body}\n}`;
218
+ return braceBlock(body);
207
219
  }
208
220
 
209
221
  case "EmptyStatement":
@@ -244,13 +256,13 @@ export function print(node) {
244
256
  case "SwitchStatement": {
245
257
  const disc = print(node.discriminant);
246
258
  const cases = (node.cases ?? []).map(print).join("\n");
247
- return `switch (${disc}) {\n${cases}\n}`;
259
+ return `switch (${disc}) ${braceBlock(cases)}`;
248
260
  }
249
261
 
250
262
  case "SwitchCase": {
251
263
  const test = node.test ? `case ${print(node.test)}:` : "default:";
252
264
  const body = (node.consequent ?? []).map(print).join("\n");
253
- return `${test}\n${body}`;
265
+ return body ? `${test}\n${indent(body)}` : test;
254
266
  }
255
267
 
256
268
  case "ThrowStatement":
@@ -324,7 +336,7 @@ export function print(node) {
324
336
 
325
337
  case "ClassBody": {
326
338
  const body = (node.body ?? []).map(print).join("\n");
327
- return `{\n${body}\n}`;
339
+ return braceBlock(body);
328
340
  }
329
341
 
330
342
  case "MethodDefinition":
@@ -541,6 +553,19 @@ export function print(node) {
541
553
  case "TSArrayType":
542
554
  return `${print(node.elementType)}[]`;
543
555
 
556
+ case "TSParenthesizedType":
557
+ return `(${print(node.typeAnnotation)})`;
558
+
559
+ // JSDoc type syntax (`?Foo`, `!Foo`, `?`) — `postfix` flips prefix/suffix.
560
+ case "TSJSDocNullableType":
561
+ return node.postfix ? `${print(node.typeAnnotation)}?` : `?${print(node.typeAnnotation)}`;
562
+
563
+ case "TSJSDocNonNullableType":
564
+ return node.postfix ? `${print(node.typeAnnotation)}!` : `!${print(node.typeAnnotation)}`;
565
+
566
+ case "TSJSDocUnknownType":
567
+ return "?";
568
+
544
569
  case "TSTupleType": {
545
570
  const elems = (node.elementTypes ?? []).map(print).join(", ");
546
571
  return `[${elems}]`;
@@ -659,7 +684,7 @@ export function print(node) {
659
684
 
660
685
  case "TSInterfaceBody": {
661
686
  const body = (node.body ?? []).map(print).join("\n");
662
- return `{\n${body}\n}`;
687
+ return braceBlock(body);
663
688
  }
664
689
 
665
690
  case "TSInterfaceHeritage":
@@ -680,8 +705,15 @@ export function print(node) {
680
705
  const declare = node.declare ? "declare " : "";
681
706
  const constKw = node.const ? "const " : "";
682
707
  const id = print(node.id);
708
+ // Newer oxc nests members in a TSEnumBody child; older versions put
709
+ // `members` directly on the declaration.
710
+ const members = (node.body?.members ?? node.members ?? []).map(print).join(",\n");
711
+ return `${declare}${constKw}enum ${id} ${braceBlock(members)}`;
712
+ }
713
+
714
+ case "TSEnumBody": {
683
715
  const members = (node.members ?? []).map(print).join(",\n");
684
- return `${declare}${constKw}enum ${id} {\n${members}\n}`;
716
+ return braceBlock(members);
685
717
  }
686
718
 
687
719
  case "TSEnumMember": {
@@ -698,7 +730,7 @@ export function print(node) {
698
730
 
699
731
  case "TSModuleBlock": {
700
732
  const body = (node.body ?? []).map(print).join("\n");
701
- return `{\n${body}\n}`;
733
+ return braceBlock(body);
702
734
  }
703
735
 
704
736
  case "TSNamespaceExportDeclaration":
@@ -919,6 +951,30 @@ export function print(node) {
919
951
  * @param {object} node
920
952
  * @return {string}
921
953
  */
954
+ /**
955
+ * Indents every non-empty line of a block's inner content by one level.
956
+ * Nesting compounds naturally: each enclosing block re-indents the
957
+ * already-formatted child string.
958
+ * @param {string} text
959
+ * @return {string}
960
+ */
961
+ function indent(text) {
962
+ return text
963
+ .split("\n")
964
+ .map((line) => (line ? ` ${line}` : line))
965
+ .join("\n");
966
+ }
967
+
968
+ /**
969
+ * Wraps already-joined statement text in a brace block, indented one level.
970
+ * Empty content collapses to `{}`.
971
+ * @param {string} inner
972
+ * @return {string}
973
+ */
974
+ function braceBlock(inner) {
975
+ return inner ? `{\n${indent(inner)}\n}` : "{}";
976
+ }
977
+
922
978
  function printTypeAnnotated(name, node) {
923
979
  const optional = node.optional ? "?" : "";
924
980
  const typeAnnotation = node.typeAnnotation ? print(node.typeAnnotation) : "";