@typespec/prettier-plugin-typespec 0.46.0-dev.1 → 0.46.0-dev.2

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/dist/index.js CHANGED
@@ -4,7 +4,7 @@ var prettier = require('prettier');
4
4
 
5
5
  //
6
6
  // Generated by scripts/regen-nonascii-map.js
7
- // on node v16.15.0 with unicode 14.0.
7
+ // on node v18.16.0 with unicode 15.0.
8
8
  //
9
9
  /**
10
10
  * @internal
@@ -135,7 +135,7 @@ const nonAsciiIdentifierMap = [
135
135
  0xcdd, 0xcde,
136
136
  0xce0, 0xce3,
137
137
  0xce6, 0xcef,
138
- 0xcf1, 0xcf2,
138
+ 0xcf1, 0xcf3,
139
139
  0xd00, 0xd0c,
140
140
  0xd0e, 0xd10,
141
141
  0xd12, 0xd44,
@@ -165,7 +165,7 @@ const nonAsciiIdentifierMap = [
165
165
  0xea7, 0xebd,
166
166
  0xec0, 0xec4,
167
167
  0xec6, 0xec6,
168
- 0xec8, 0xecd,
168
+ 0xec8, 0xece,
169
169
  0xed0, 0xed9,
170
170
  0xedc, 0xedf,
171
171
  0xf00, 0xf47,
@@ -438,7 +438,7 @@ const nonAsciiIdentifierMap = [
438
438
  0x10e80, 0x10ea9,
439
439
  0x10eab, 0x10ead,
440
440
  0x10eb0, 0x10eb1,
441
- 0x10f00, 0x10f27,
441
+ 0x10efd, 0x10f27,
442
442
  0x10f30, 0x10f59,
443
443
  0x10f70, 0x10f89,
444
444
  0x10fb0, 0x10fcb,
@@ -455,7 +455,7 @@ const nonAsciiIdentifierMap = [
455
455
  0x11180, 0x111df,
456
456
  0x111e1, 0x111f4,
457
457
  0x11200, 0x11211,
458
- 0x11213, 0x1123e,
458
+ 0x11213, 0x11241,
459
459
  0x11280, 0x11286,
460
460
  0x11288, 0x11288,
461
461
  0x1128a, 0x1128d,
@@ -508,6 +508,7 @@ const nonAsciiIdentifierMap = [
508
508
  0x11a00, 0x11a47,
509
509
  0x11a50, 0x11aa2,
510
510
  0x11ab0, 0x11af8,
511
+ 0x11b00, 0x11b09,
511
512
  0x11c00, 0x11c08,
512
513
  0x11c0a, 0x11c36,
513
514
  0x11c38, 0x11c45,
@@ -529,6 +530,9 @@ const nonAsciiIdentifierMap = [
529
530
  0x11d93, 0x11d98,
530
531
  0x11da0, 0x11da9,
531
532
  0x11ee0, 0x11ef8,
533
+ 0x11f00, 0x11f10,
534
+ 0x11f12, 0x11f3a,
535
+ 0x11f3e, 0x11f59,
532
536
  0x11fb0, 0x11fb0,
533
537
  0x11fc0, 0x11ff1,
534
538
  0x11fff, 0x12399,
@@ -536,8 +540,7 @@ const nonAsciiIdentifierMap = [
536
540
  0x12470, 0x12474,
537
541
  0x12480, 0x12543,
538
542
  0x12f90, 0x12ff2,
539
- 0x13000, 0x1342e,
540
- 0x13430, 0x13438,
543
+ 0x13000, 0x13455,
541
544
  0x14400, 0x14646,
542
545
  0x16800, 0x16a38,
543
546
  0x16a40, 0x16a5e,
@@ -564,7 +567,9 @@ const nonAsciiIdentifierMap = [
564
567
  0x1aff5, 0x1affb,
565
568
  0x1affd, 0x1affe,
566
569
  0x1b000, 0x1b122,
570
+ 0x1b132, 0x1b132,
567
571
  0x1b150, 0x1b152,
572
+ 0x1b155, 0x1b155,
568
573
  0x1b164, 0x1b167,
569
574
  0x1b170, 0x1b2fb,
570
575
  0x1bc00, 0x1bc6a,
@@ -579,6 +584,7 @@ const nonAsciiIdentifierMap = [
579
584
  0x1d100, 0x1d126,
580
585
  0x1d129, 0x1d1ea,
581
586
  0x1d200, 0x1d245,
587
+ 0x1d2c0, 0x1d2d3,
582
588
  0x1d2e0, 0x1d2f3,
583
589
  0x1d300, 0x1d356,
584
590
  0x1d360, 0x1d378,
@@ -606,11 +612,14 @@ const nonAsciiIdentifierMap = [
606
612
  0x1da9b, 0x1da9f,
607
613
  0x1daa1, 0x1daaf,
608
614
  0x1df00, 0x1df1e,
615
+ 0x1df25, 0x1df2a,
609
616
  0x1e000, 0x1e006,
610
617
  0x1e008, 0x1e018,
611
618
  0x1e01b, 0x1e021,
612
619
  0x1e023, 0x1e024,
613
620
  0x1e026, 0x1e02a,
621
+ 0x1e030, 0x1e06d,
622
+ 0x1e08f, 0x1e08f,
614
623
  0x1e100, 0x1e12c,
615
624
  0x1e130, 0x1e13d,
616
625
  0x1e140, 0x1e149,
@@ -618,6 +627,7 @@ const nonAsciiIdentifierMap = [
618
627
  0x1e290, 0x1e2ae,
619
628
  0x1e2c0, 0x1e2f9,
620
629
  0x1e2ff, 0x1e2ff,
630
+ 0x1e4d0, 0x1e4f9,
621
631
  0x1e7e0, 0x1e7e6,
622
632
  0x1e7e8, 0x1e7eb,
623
633
  0x1e7ed, 0x1e7ee,
@@ -676,10 +686,10 @@ const nonAsciiIdentifierMap = [
676
686
  0x1f250, 0x1f251,
677
687
  0x1f260, 0x1f265,
678
688
  0x1f300, 0x1f6d7,
679
- 0x1f6dd, 0x1f6ec,
689
+ 0x1f6dc, 0x1f6ec,
680
690
  0x1f6f0, 0x1f6fc,
681
- 0x1f700, 0x1f773,
682
- 0x1f780, 0x1f7d8,
691
+ 0x1f700, 0x1f776,
692
+ 0x1f77b, 0x1f7d9,
683
693
  0x1f7e0, 0x1f7eb,
684
694
  0x1f7f0, 0x1f7f0,
685
695
  0x1f800, 0x1f80b,
@@ -690,25 +700,24 @@ const nonAsciiIdentifierMap = [
690
700
  0x1f8b0, 0x1f8b1,
691
701
  0x1f900, 0x1fa53,
692
702
  0x1fa60, 0x1fa6d,
693
- 0x1fa70, 0x1fa74,
694
- 0x1fa78, 0x1fa7c,
695
- 0x1fa80, 0x1fa86,
696
- 0x1fa90, 0x1faac,
697
- 0x1fab0, 0x1faba,
698
- 0x1fac0, 0x1fac5,
699
- 0x1fad0, 0x1fad9,
700
- 0x1fae0, 0x1fae7,
701
- 0x1faf0, 0x1faf6,
703
+ 0x1fa70, 0x1fa7c,
704
+ 0x1fa80, 0x1fa88,
705
+ 0x1fa90, 0x1fabd,
706
+ 0x1fabf, 0x1fac5,
707
+ 0x1face, 0x1fadb,
708
+ 0x1fae0, 0x1fae8,
709
+ 0x1faf0, 0x1faf8,
702
710
  0x1fb00, 0x1fb92,
703
711
  0x1fb94, 0x1fbca,
704
712
  0x1fbf0, 0x1fbf9,
705
713
  0x20000, 0x2a6df,
706
- 0x2a700, 0x2b738,
714
+ 0x2a700, 0x2b739,
707
715
  0x2b740, 0x2b81d,
708
716
  0x2b820, 0x2cea1,
709
717
  0x2ceb0, 0x2ebe0,
710
718
  0x2f800, 0x2fa1d,
711
719
  0x30000, 0x3134a,
720
+ 0x31350, 0x323af,
712
721
  0xe0001, 0xe0001,
713
722
  0xe0020, 0xe007f,
714
723
  0xe0100, 0xe01ef,
@@ -2885,41 +2894,17 @@ function getTokenDisplayTable(entries) {
2885
2894
  return table;
2886
2895
  }
2887
2896
 
2888
- (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2889
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
2890
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
2891
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
2892
- };
2893
- (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2894
- if (kind === "m") throw new TypeError("Private method is not writable");
2895
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
2896
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
2897
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
2898
- };
2899
- /**
2900
- * A specially typed version of `Array.isArray` to work around [this issue](https://github.com/microsoft/TypeScript/issues/17002).
2901
- */
2902
- function isArray(
2903
- // eslint-disable-next-line @typescript-eslint/ban-types
2904
- arg) {
2905
- return Array.isArray(arg);
2906
- }
2907
- /**
2908
- * Casts away readonly typing.
2909
- *
2910
- * Use it like this when it is safe to override readonly typing:
2911
- * mutate(item).prop = value;
2912
- */
2913
- function mutate(value) {
2914
- return value;
2915
- }
2916
-
2917
2897
  const { util } = prettier;
2918
2898
  /**
2919
2899
  * Override the default behavior to attach comments to syntax node.
2920
2900
  */
2921
2901
  const commentHandler = {
2922
- ownLine: (comment) => [addEmptyInterfaceComment, addEmptyModelComment, addStatementDecoratorComment].some((x) => x(comment)),
2902
+ ownLine: (comment, text, options, ast, isLastComment) => [
2903
+ addEmptyInterfaceComment,
2904
+ addEmptyModelComment,
2905
+ addStatementDecoratorComment,
2906
+ handleOnlyComments,
2907
+ ].some((x) => x({ comment, text, options, ast: ast, isLastComment })),
2923
2908
  };
2924
2909
  /**
2925
2910
  * When a comment is on an empty interface make sure it gets added as a dangling comment on it and not on the identifier.
@@ -2930,7 +2915,7 @@ const commentHandler = {
2930
2915
  * // My comment
2931
2916
  * }
2932
2917
  */
2933
- function addEmptyInterfaceComment(comment) {
2918
+ function addEmptyInterfaceComment({ comment }) {
2934
2919
  const { precedingNode, enclosingNode } = comment;
2935
2920
  if (enclosingNode &&
2936
2921
  enclosingNode.kind === SyntaxKind.InterfaceStatement &&
@@ -2953,7 +2938,7 @@ function addEmptyInterfaceComment(comment) {
2953
2938
  * model Foo {
2954
2939
  * }
2955
2940
  */
2956
- function addStatementDecoratorComment(comment) {
2941
+ function addStatementDecoratorComment({ comment }) {
2957
2942
  const { enclosingNode, precedingNode } = comment;
2958
2943
  if (precedingNode &&
2959
2944
  precedingNode.kind === SyntaxKind.DecoratorExpression &&
@@ -2979,7 +2964,7 @@ function addStatementDecoratorComment(comment) {
2979
2964
  * // My comment
2980
2965
  * }
2981
2966
  */
2982
- function addEmptyModelComment(comment) {
2967
+ function addEmptyModelComment({ comment }) {
2983
2968
  const { precedingNode, enclosingNode } = comment;
2984
2969
  if (enclosingNode &&
2985
2970
  enclosingNode.kind === SyntaxKind.ModelStatement &&
@@ -2993,6 +2978,31 @@ function addEmptyModelComment(comment) {
2993
2978
  }
2994
2979
  return false;
2995
2980
  }
2981
+ function handleOnlyComments({ comment, ast, isLastComment }) {
2982
+ var _a, _b;
2983
+ const { enclosingNode } = comment;
2984
+ if (((_a = ast === null || ast === void 0 ? void 0 : ast.statements) === null || _a === void 0 ? void 0 : _a.length) === 0) {
2985
+ if (isLastComment) {
2986
+ util.addDanglingComment(ast, comment, undefined);
2987
+ }
2988
+ else {
2989
+ util.addLeadingComment(ast, comment);
2990
+ }
2991
+ return true;
2992
+ }
2993
+ if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.kind) === SyntaxKind.TypeSpecScript &&
2994
+ enclosingNode.statements.length === 0 &&
2995
+ ((_b = enclosingNode.directives) === null || _b === void 0 ? void 0 : _b.length) === 0) {
2996
+ if (isLastComment) {
2997
+ util.addDanglingComment(enclosingNode, comment, undefined);
2998
+ }
2999
+ else {
3000
+ util.addLeadingComment(enclosingNode, comment);
3001
+ }
3002
+ return true;
3003
+ }
3004
+ return false;
3005
+ }
2996
3006
 
2997
3007
  /**
2998
3008
  * Check if the current path should be wrapped in parentheses
@@ -3056,9 +3066,7 @@ path, options, print) {
3056
3066
  switch (node.kind) {
3057
3067
  // Root
3058
3068
  case SyntaxKind.TypeSpecScript:
3059
- return [
3060
- printStatementSequence(path, options, print, "statements"),
3061
- ];
3069
+ return printTypeSpecScript(path, options, print);
3062
3070
  // Statements
3063
3071
  case SyntaxKind.ImportStatement:
3064
3072
  return [`import "${node.path.value}";`];
@@ -3205,14 +3213,25 @@ path, options, print) {
3205
3213
  // https://github.com/microsoft/typespec/issues/1319 Tracks pretty-printing doc comments.
3206
3214
  compilerAssert(false, "Currently, doc comments are only handled as regular comments and we do not opt in to parsing them so we shouldn't reach here.");
3207
3215
  return "";
3208
- case SyntaxKind.JsSourceFile:
3209
3216
  case SyntaxKind.EmptyStatement:
3217
+ return "";
3218
+ case SyntaxKind.JsSourceFile:
3210
3219
  case SyntaxKind.InvalidStatement:
3211
3220
  return getRawText(node, options);
3212
3221
  default:
3213
3222
  return getRawText(node, options);
3214
3223
  }
3215
3224
  }
3225
+ function printTypeSpecScript(path, options, print) {
3226
+ const node = path.getValue();
3227
+ const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling);
3228
+ const body = [];
3229
+ if (nodeHasComments) {
3230
+ body.push(printDanglingComments(path, options, { sameIndent: true }));
3231
+ }
3232
+ body.push(printStatementSequence(path, options, print, "statements"));
3233
+ return body;
3234
+ }
3216
3235
  function printAliasStatement(path, options, print) {
3217
3236
  const id = path.call(print, "id");
3218
3237
  const template = printTemplateParameters(path, options, print, "templateParameters");
@@ -3238,6 +3257,7 @@ function canAttachComment(node) {
3238
3257
  return Boolean(kind &&
3239
3258
  kind !== SyntaxKind.LineComment &&
3240
3259
  kind !== SyntaxKind.BlockComment &&
3260
+ kind !== SyntaxKind.EmptyStatement &&
3241
3261
  !(node.flags & 8 /* NodeFlags.Synthetic */));
3242
3262
  }
3243
3263
  function printComment(commentPath, options) {
@@ -3697,26 +3717,18 @@ function printScalarStatement(path, options, print) {
3697
3717
  ];
3698
3718
  }
3699
3719
  function printNamespaceStatement(path, options, print) {
3700
- const printNested = (currentPath, parentNames) => {
3701
- var _a;
3702
- const names = [...parentNames, currentPath.call(print, "id")];
3703
- const currentNode = currentPath.getNode();
3704
- if (!isArray(currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) &&
3705
- ((_a = currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.NamespaceStatement) {
3706
- return path.call((x) => printNested(x, names), "statements");
3707
- }
3708
- const suffix = (currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) === undefined
3709
- ? ";"
3710
- : [
3711
- " {",
3712
- indent([hardline, printStatementSequence(path, options, print, "statements")]),
3713
- hardline,
3714
- "}",
3715
- ];
3716
- const { decorators } = printDecorators(path, options, print, { tryInline: false });
3717
- return [decorators, `namespace `, join(".", names), suffix];
3718
- };
3719
- return printNested(path, []);
3720
+ const names = path.map(print, "ids");
3721
+ const currentNode = path.getNode();
3722
+ const suffix = (currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) === undefined
3723
+ ? ";"
3724
+ : [
3725
+ " {",
3726
+ indent([hardline, printStatementSequence(path, options, print, "statements")]),
3727
+ hardline,
3728
+ "}",
3729
+ ];
3730
+ const { decorators } = printDecorators(path, options, print, { tryInline: false });
3731
+ return [decorators, `namespace `, join(".", names), suffix];
3720
3732
  }
3721
3733
  function printOperationSignatureDeclaration(path, options, print) {
3722
3734
  return ["(", path.call(print, "parameters"), "): ", path.call(print, "returnType")];
@@ -4049,6 +4061,35 @@ function isLineComment(comment) {
4049
4061
  return comment.kind === SyntaxKind.BlockComment;
4050
4062
  }
4051
4063
 
4064
+ (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
4065
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4066
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4067
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
4068
+ };
4069
+ (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
4070
+ if (kind === "m") throw new TypeError("Private method is not writable");
4071
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4072
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
4073
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
4074
+ };
4075
+ /**
4076
+ * A specially typed version of `Array.isArray` to work around [this issue](https://github.com/microsoft/TypeScript/issues/17002).
4077
+ */
4078
+ function isArray(
4079
+ // eslint-disable-next-line @typescript-eslint/ban-types
4080
+ arg) {
4081
+ return Array.isArray(arg);
4082
+ }
4083
+ /**
4084
+ * Casts away readonly typing.
4085
+ *
4086
+ * Use it like this when it is safe to override readonly typing:
4087
+ * mutate(item).prop = value;
4088
+ */
4089
+ function mutate(value) {
4090
+ return value;
4091
+ }
4092
+
4052
4093
  /**
4053
4094
  * The fixed set of options for each of the kinds of delimited lists in TypeSpec.
4054
4095
  */
@@ -6339,6 +6380,213 @@ function createParser(code, options = {}) {
6339
6380
  : undefined;
6340
6381
  }
6341
6382
  }
6383
+ function visitChildren(node, cb) {
6384
+ if (node.directives) {
6385
+ const result = visitEach(cb, node.directives);
6386
+ if (result)
6387
+ return result;
6388
+ }
6389
+ if (node.docs) {
6390
+ const result = visitEach(cb, node.docs);
6391
+ if (result)
6392
+ return result;
6393
+ }
6394
+ switch (node.kind) {
6395
+ case SyntaxKind.TypeSpecScript:
6396
+ return visitNode(cb, node.id) || visitEach(cb, node.statements);
6397
+ case SyntaxKind.ArrayExpression:
6398
+ return visitNode(cb, node.elementType);
6399
+ case SyntaxKind.AugmentDecoratorStatement:
6400
+ return (visitNode(cb, node.target) ||
6401
+ visitNode(cb, node.targetType) ||
6402
+ visitEach(cb, node.arguments));
6403
+ case SyntaxKind.DecoratorExpression:
6404
+ return visitNode(cb, node.target) || visitEach(cb, node.arguments);
6405
+ case SyntaxKind.DirectiveExpression:
6406
+ return visitNode(cb, node.target) || visitEach(cb, node.arguments);
6407
+ case SyntaxKind.ImportStatement:
6408
+ return visitNode(cb, node.path);
6409
+ case SyntaxKind.OperationStatement:
6410
+ return (visitEach(cb, node.decorators) ||
6411
+ visitNode(cb, node.id) ||
6412
+ visitEach(cb, node.templateParameters) ||
6413
+ visitNode(cb, node.signature));
6414
+ case SyntaxKind.OperationSignatureDeclaration:
6415
+ return visitNode(cb, node.parameters) || visitNode(cb, node.returnType);
6416
+ case SyntaxKind.OperationSignatureReference:
6417
+ return visitNode(cb, node.baseOperation);
6418
+ case SyntaxKind.NamespaceStatement:
6419
+ return (visitEach(cb, node.decorators) ||
6420
+ visitNode(cb, node.id) ||
6421
+ (isArray(node.statements) ? visitEach(cb, node.statements) : visitNode(cb, node.statements)));
6422
+ case SyntaxKind.InterfaceStatement:
6423
+ return (visitEach(cb, node.decorators) ||
6424
+ visitNode(cb, node.id) ||
6425
+ visitEach(cb, node.templateParameters) ||
6426
+ visitEach(cb, node.extends) ||
6427
+ visitEach(cb, node.operations));
6428
+ case SyntaxKind.UsingStatement:
6429
+ return visitNode(cb, node.name);
6430
+ case SyntaxKind.IntersectionExpression:
6431
+ return visitEach(cb, node.options);
6432
+ case SyntaxKind.MemberExpression:
6433
+ return visitNode(cb, node.base) || visitNode(cb, node.id);
6434
+ case SyntaxKind.ModelExpression:
6435
+ return visitEach(cb, node.properties);
6436
+ case SyntaxKind.ModelProperty:
6437
+ return (visitEach(cb, node.decorators) ||
6438
+ visitNode(cb, node.id) ||
6439
+ visitNode(cb, node.value) ||
6440
+ visitNode(cb, node.default));
6441
+ case SyntaxKind.ModelSpreadProperty:
6442
+ return visitNode(cb, node.target);
6443
+ case SyntaxKind.ModelStatement:
6444
+ return (visitEach(cb, node.decorators) ||
6445
+ visitNode(cb, node.id) ||
6446
+ visitEach(cb, node.templateParameters) ||
6447
+ visitNode(cb, node.extends) ||
6448
+ visitNode(cb, node.is) ||
6449
+ visitEach(cb, node.properties));
6450
+ case SyntaxKind.ScalarStatement:
6451
+ return (visitEach(cb, node.decorators) ||
6452
+ visitNode(cb, node.id) ||
6453
+ visitEach(cb, node.templateParameters) ||
6454
+ visitNode(cb, node.extends));
6455
+ case SyntaxKind.UnionStatement:
6456
+ return (visitEach(cb, node.decorators) ||
6457
+ visitNode(cb, node.id) ||
6458
+ visitEach(cb, node.templateParameters) ||
6459
+ visitEach(cb, node.options));
6460
+ case SyntaxKind.UnionVariant:
6461
+ return visitEach(cb, node.decorators) || visitNode(cb, node.id) || visitNode(cb, node.value);
6462
+ case SyntaxKind.EnumStatement:
6463
+ return (visitEach(cb, node.decorators) || visitNode(cb, node.id) || visitEach(cb, node.members));
6464
+ case SyntaxKind.EnumMember:
6465
+ return visitEach(cb, node.decorators) || visitNode(cb, node.id) || visitNode(cb, node.value);
6466
+ case SyntaxKind.EnumSpreadMember:
6467
+ return visitNode(cb, node.target);
6468
+ case SyntaxKind.AliasStatement:
6469
+ return (visitNode(cb, node.id) ||
6470
+ visitEach(cb, node.templateParameters) ||
6471
+ visitNode(cb, node.value));
6472
+ case SyntaxKind.DecoratorDeclarationStatement:
6473
+ return (visitEach(cb, node.modifiers) ||
6474
+ visitNode(cb, node.id) ||
6475
+ visitNode(cb, node.target) ||
6476
+ visitEach(cb, node.parameters));
6477
+ case SyntaxKind.FunctionDeclarationStatement:
6478
+ return (visitEach(cb, node.modifiers) ||
6479
+ visitNode(cb, node.id) ||
6480
+ visitEach(cb, node.parameters) ||
6481
+ visitNode(cb, node.returnType));
6482
+ case SyntaxKind.FunctionParameter:
6483
+ return visitNode(cb, node.id) || visitNode(cb, node.type);
6484
+ case SyntaxKind.TypeReference:
6485
+ return visitNode(cb, node.target) || visitEach(cb, node.arguments);
6486
+ case SyntaxKind.ValueOfExpression:
6487
+ return visitNode(cb, node.target);
6488
+ case SyntaxKind.TupleExpression:
6489
+ return visitEach(cb, node.values);
6490
+ case SyntaxKind.UnionExpression:
6491
+ return visitEach(cb, node.options);
6492
+ case SyntaxKind.Projection:
6493
+ return (visitNode(cb, node.directionId) ||
6494
+ visitEach(cb, node.modifierIds) ||
6495
+ visitEach(cb, node.parameters) ||
6496
+ visitEach(cb, node.body));
6497
+ case SyntaxKind.ProjectionExpressionStatement:
6498
+ return visitNode(cb, node.expr);
6499
+ case SyntaxKind.ProjectionCallExpression:
6500
+ return visitNode(cb, node.target) || visitEach(cb, node.arguments);
6501
+ case SyntaxKind.ProjectionMemberExpression:
6502
+ return visitNode(cb, node.base) || visitNode(cb, node.id);
6503
+ // binops
6504
+ case SyntaxKind.ProjectionLogicalExpression:
6505
+ case SyntaxKind.ProjectionRelationalExpression:
6506
+ case SyntaxKind.ProjectionArithmeticExpression:
6507
+ case SyntaxKind.ProjectionEqualityExpression:
6508
+ return visitNode(cb, node.left) || visitNode(cb, node.right);
6509
+ case SyntaxKind.ProjectionUnaryExpression:
6510
+ return visitNode(cb, node.target);
6511
+ case SyntaxKind.ProjectionModelExpression:
6512
+ return visitEach(cb, node.properties);
6513
+ case SyntaxKind.ProjectionModelProperty:
6514
+ return (visitEach(cb, node.decorators) ||
6515
+ visitNode(cb, node.id) ||
6516
+ visitNode(cb, node.value) ||
6517
+ visitNode(cb, node.default));
6518
+ case SyntaxKind.ProjectionModelSpreadProperty:
6519
+ return visitNode(cb, node.target);
6520
+ case SyntaxKind.ProjectionTupleExpression:
6521
+ return visitEach(cb, node.values);
6522
+ case SyntaxKind.ProjectionBlockExpression:
6523
+ return visitEach(cb, node.statements);
6524
+ case SyntaxKind.ProjectionIfExpression:
6525
+ return (visitNode(cb, node.test) || visitNode(cb, node.consequent) || visitNode(cb, node.alternate));
6526
+ case SyntaxKind.ProjectionLambdaExpression:
6527
+ return visitEach(cb, node.parameters) || visitNode(cb, node.body);
6528
+ case SyntaxKind.ProjectionStatement:
6529
+ return (visitNode(cb, node.id) || visitNode(cb, node.selector) || visitEach(cb, node.projections));
6530
+ case SyntaxKind.ProjectionDecoratorReferenceExpression:
6531
+ return visitNode(cb, node.target);
6532
+ case SyntaxKind.Return:
6533
+ return visitNode(cb, node.value);
6534
+ case SyntaxKind.InvalidStatement:
6535
+ return visitEach(cb, node.decorators);
6536
+ case SyntaxKind.TemplateParameterDeclaration:
6537
+ return (visitNode(cb, node.id) || visitNode(cb, node.constraint) || visitNode(cb, node.default));
6538
+ case SyntaxKind.ProjectionLambdaParameterDeclaration:
6539
+ return visitNode(cb, node.id);
6540
+ case SyntaxKind.ProjectionParameterDeclaration:
6541
+ return visitNode(cb, node.id);
6542
+ case SyntaxKind.Doc:
6543
+ return visitEach(cb, node.content) || visitEach(cb, node.tags);
6544
+ case SyntaxKind.DocParamTag:
6545
+ case SyntaxKind.DocTemplateTag:
6546
+ return (visitNode(cb, node.tagName) || visitNode(cb, node.paramName) || visitEach(cb, node.content));
6547
+ case SyntaxKind.DocReturnsTag:
6548
+ case SyntaxKind.DocUnknownTag:
6549
+ return visitNode(cb, node.tagName) || visitEach(cb, node.content);
6550
+ // no children for the rest of these.
6551
+ case SyntaxKind.StringLiteral:
6552
+ case SyntaxKind.NumericLiteral:
6553
+ case SyntaxKind.BooleanLiteral:
6554
+ case SyntaxKind.Identifier:
6555
+ case SyntaxKind.EmptyStatement:
6556
+ case SyntaxKind.ProjectionModelSelector:
6557
+ case SyntaxKind.ProjectionModelPropertySelector:
6558
+ case SyntaxKind.ProjectionUnionSelector:
6559
+ case SyntaxKind.ProjectionUnionVariantSelector:
6560
+ case SyntaxKind.ProjectionInterfaceSelector:
6561
+ case SyntaxKind.ProjectionOperationSelector:
6562
+ case SyntaxKind.ProjectionEnumSelector:
6563
+ case SyntaxKind.ProjectionEnumMemberSelector:
6564
+ case SyntaxKind.VoidKeyword:
6565
+ case SyntaxKind.NeverKeyword:
6566
+ case SyntaxKind.ExternKeyword:
6567
+ case SyntaxKind.UnknownKeyword:
6568
+ case SyntaxKind.JsSourceFile:
6569
+ case SyntaxKind.DocText:
6570
+ return;
6571
+ default:
6572
+ return;
6573
+ }
6574
+ }
6575
+ function visitNode(cb, node) {
6576
+ return node && cb(node);
6577
+ }
6578
+ function visitEach(cb, nodes) {
6579
+ if (!nodes) {
6580
+ return;
6581
+ }
6582
+ for (const node of nodes) {
6583
+ const result = cb(node);
6584
+ if (result) {
6585
+ return result;
6586
+ }
6587
+ }
6588
+ return;
6589
+ }
6342
6590
  function isBlocklessNamespace(node) {
6343
6591
  if (node.kind !== SyntaxKind.NamespaceStatement) {
6344
6592
  return false;
@@ -6351,12 +6599,34 @@ function isBlocklessNamespace(node) {
6351
6599
 
6352
6600
  function parse(text, parsers, opts) {
6353
6601
  const result = parse$1(text, { comments: true, docs: false });
6602
+ flattenNamespaces(result);
6354
6603
  const errors = result.parseDiagnostics.filter((x) => x.severity === "error");
6355
6604
  if (errors.length > 0 && !result.printable) {
6356
6605
  throw new PrettierParserError(errors[0]);
6357
6606
  }
6358
6607
  return result;
6359
6608
  }
6609
+ /**
6610
+ * We are patching the syntax tree to flatten the namespace nodes that are created from namespace Foo.Bar; which have the same pos, end
6611
+ * This causes prettier to not know where comments belong.
6612
+ * https://github.com/microsoft/typespec/pull/2061
6613
+ */
6614
+ function flattenNamespaces(base) {
6615
+ visitChildren(base, (node) => {
6616
+ if (node.kind === SyntaxKind.NamespaceStatement) {
6617
+ let current = node;
6618
+ const ids = [node.id];
6619
+ while (current.statements && "kind" in current.statements) {
6620
+ current = current.statements;
6621
+ ids.push(current.id);
6622
+ }
6623
+ Object.assign(node, current, {
6624
+ ids,
6625
+ });
6626
+ flattenNamespaces(current);
6627
+ }
6628
+ });
6629
+ }
6360
6630
  class PrettierParserError extends Error {
6361
6631
  constructor(error) {
6362
6632
  var _a, _b;