@typespec/prettier-plugin-typespec 0.56.0-dev.2 → 0.56.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/dist/index.js CHANGED
@@ -9,7 +9,7 @@ function createSourceFile(text, path) {
9
9
  getLineAndCharacterOfPosition,
10
10
  };
11
11
  function getLineStarts() {
12
- return (lineStarts = lineStarts !== null && lineStarts !== void 0 ? lineStarts : scanLineStarts(text));
12
+ return (lineStarts = lineStarts ?? scanLineStarts(text));
13
13
  }
14
14
  function getLineAndCharacterOfPosition(position) {
15
15
  const starts = getLineStarts();
@@ -196,17 +196,6 @@ var ListenerFlow;
196
196
  ListenerFlow[ListenerFlow["NoRecursion"] = 1] = "NoRecursion";
197
197
  })(ListenerFlow || (ListenerFlow = {}));
198
198
 
199
- (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
200
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
201
- 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");
202
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
203
- };
204
- (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
205
- if (kind === "m") throw new TypeError("Private method is not writable");
206
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
207
- 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");
208
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
209
- };
210
199
  /**
211
200
  * A specially typed version of `Array.isArray` to work around [this issue](https://github.com/microsoft/TypeScript/issues/17002).
212
201
  */
@@ -236,7 +225,6 @@ function createDiagnosticCreator(diagnostics, libraryName) {
236
225
  ? `It must match one of the code defined in the library '${libraryName}'`
237
226
  : "It must match one of the code defined in the compiler.";
238
227
  function createDiagnostic(diagnostic) {
239
- var _a;
240
228
  const diagnosticDef = diagnostics[diagnostic.code];
241
229
  if (!diagnosticDef) {
242
230
  const codeStr = Object.keys(diagnostics)
@@ -245,7 +233,7 @@ function createDiagnosticCreator(diagnostics, libraryName) {
245
233
  const code = String(diagnostic.code);
246
234
  throw new Error(`Unexpected diagnostic code '${code}'. ${errorMessage}. Defined codes:\n${codeStr}`);
247
235
  }
248
- const message = diagnosticDef.messages[(_a = diagnostic.messageId) !== null && _a !== void 0 ? _a : "default"];
236
+ const message = diagnosticDef.messages[diagnostic.messageId ?? "default"];
249
237
  if (!message) {
250
238
  const codeStr = Object.keys(diagnosticDef.messages)
251
239
  .map((x) => ` - ${x}`)
@@ -681,6 +669,12 @@ const diagnostics = {
681
669
  withDetails: paramMessage `Type '${"sourceType"}' is not assignable to type '${"targetType"}'\n ${"details"}`,
682
670
  },
683
671
  },
672
+ "property-required": {
673
+ severity: "error",
674
+ messages: {
675
+ default: paramMessage `Property '${"propName"}' is required in type '${"targetType"}' but here is optional.`,
676
+ },
677
+ },
684
678
  "no-prop": {
685
679
  severity: "error",
686
680
  messages: {
@@ -3890,7 +3884,7 @@ function createScanner(source, diagnosticHandler) {
3890
3884
  function error(report, pos, end) {
3891
3885
  const diagnostic = createDiagnostic({
3892
3886
  ...report,
3893
- target: { file, pos: pos !== null && pos !== void 0 ? pos : tokenPosition, end: end !== null && end !== void 0 ? end : position },
3887
+ target: { file, pos: pos ?? tokenPosition, end: end ?? position },
3894
3888
  });
3895
3889
  diagnosticHandler(diagnostic);
3896
3890
  }
@@ -4548,9 +4542,8 @@ function addEmptyModelComment({ comment }) {
4548
4542
  return false;
4549
4543
  }
4550
4544
  function handleOnlyComments({ comment, ast, isLastComment }) {
4551
- var _a, _b;
4552
4545
  const { enclosingNode } = comment;
4553
- if (((_a = ast === null || ast === void 0 ? void 0 : ast.statements) === null || _a === void 0 ? void 0 : _a.length) === 0) {
4546
+ if (ast?.statements?.length === 0) {
4554
4547
  if (isLastComment) {
4555
4548
  util.addDanglingComment(ast, comment, undefined);
4556
4549
  }
@@ -4559,9 +4552,9 @@ function handleOnlyComments({ comment, ast, isLastComment }) {
4559
4552
  }
4560
4553
  return true;
4561
4554
  }
4562
- if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.kind) === SyntaxKind.TypeSpecScript &&
4555
+ if (enclosingNode?.kind === SyntaxKind.TypeSpecScript &&
4563
4556
  enclosingNode.statements.length === 0 &&
4564
- ((_b = enclosingNode.directives) === null || _b === void 0 ? void 0 : _b.length) === 0) {
4557
+ enclosingNode.directives?.length === 0) {
4565
4558
  if (isLastComment) {
4566
4559
  util.addDanglingComment(enclosingNode, comment, undefined);
4567
4560
  }
@@ -5209,14 +5202,13 @@ function printModelStatement(path, options, print) {
5209
5202
  ];
5210
5203
  }
5211
5204
  function printModelPropertiesBlock(path, options, print) {
5212
- var _a;
5213
5205
  const node = path.node;
5214
5206
  const hasProperties = node.properties && node.properties.length > 0;
5215
5207
  const nodeHasComments = hasComments(node, CommentCheckFlags.Dangling);
5216
5208
  if (!hasProperties && !nodeHasComments) {
5217
5209
  return "{}";
5218
5210
  }
5219
- const tryInline = ((_a = path.getParentNode()) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.TemplateParameterDeclaration;
5211
+ const tryInline = path.getParentNode()?.kind === SyntaxKind.TemplateParameterDeclaration;
5220
5212
  const lineDoc = tryInline ? softline : hardline;
5221
5213
  const seperator = isModelAValue(path) ? "," : ";";
5222
5214
  const body = [joinMembersInBlock(path, "properties", options, print, seperator, lineDoc)];
@@ -5267,7 +5259,6 @@ function joinMembersInBlock(path, member, options, print, separator, regularLine
5267
5259
  * - has leading comments
5268
5260
  */
5269
5261
  function shouldWrapMemberInNewLines(path, options) {
5270
- var _a;
5271
5262
  const node = path.node;
5272
5263
  return ((node.kind !== SyntaxKind.ModelSpreadProperty &&
5273
5264
  node.kind !== SyntaxKind.ProjectionModelSpreadProperty &&
@@ -5276,7 +5267,7 @@ function shouldWrapMemberInNewLines(path, options) {
5276
5267
  tryInline: DecoratorsTryInline.modelProperty,
5277
5268
  })) ||
5278
5269
  hasComments(node, CommentCheckFlags.Leading) ||
5279
- (node.docs && ((_a = node.docs) === null || _a === void 0 ? void 0 : _a.length) > 0));
5270
+ (node.docs && node.docs?.length > 0));
5280
5271
  }
5281
5272
  /**
5282
5273
  * Figure out if this model is being used as a definition or value.
@@ -5348,7 +5339,7 @@ function needBacktick(sv) {
5348
5339
  }
5349
5340
  function isModelExpressionInBlock(path) {
5350
5341
  const parent = path.getParentNode();
5351
- switch (parent === null || parent === void 0 ? void 0 : parent.kind) {
5342
+ switch (parent?.kind) {
5352
5343
  case SyntaxKind.OperationSignatureDeclaration:
5353
5344
  return parent.parameters !== path.getNode();
5354
5345
  default:
@@ -5374,7 +5365,7 @@ function printScalarStatement(path, options, print) {
5374
5365
  function printNamespaceStatement(path, options, print) {
5375
5366
  const names = path.map(print, "ids");
5376
5367
  const currentNode = path.getNode();
5377
- const suffix = (currentNode === null || currentNode === void 0 ? void 0 : currentNode.statements) === undefined
5368
+ const suffix = currentNode?.statements === undefined
5378
5369
  ? ";"
5379
5370
  : [
5380
5371
  " {",
@@ -5392,8 +5383,7 @@ function printOperationSignatureReference(path, options, print) {
5392
5383
  return [" is ", path.call(print, "baseOperation")];
5393
5384
  }
5394
5385
  function printOperationStatement(path, options, print) {
5395
- var _a;
5396
- const inInterface = ((_a = path.getParentNode()) === null || _a === void 0 ? void 0 : _a.kind) === SyntaxKind.InterfaceStatement;
5386
+ const inInterface = path.getParentNode()?.kind === SyntaxKind.InterfaceStatement;
5397
5387
  const templateParams = printTemplateParameters(path, options, print, "templateParameters");
5398
5388
  const { decorators } = printDecorators(path, options, print, {
5399
5389
  tryInline: true,
@@ -5944,7 +5934,7 @@ function createParser(code, options = {}) {
5944
5934
  item = parseScalarStatement(pos, decorators);
5945
5935
  break;
5946
5936
  case Token.NamespaceKeyword:
5947
- item = parseNamespaceStatement(pos, decorators, docs);
5937
+ item = parseNamespaceStatement(pos, decorators, docs, directives);
5948
5938
  break;
5949
5939
  case Token.InterfaceKeyword:
5950
5940
  item = parseInterfaceStatement(pos, decorators);
@@ -5984,8 +5974,8 @@ function createParser(code, options = {}) {
5984
5974
  item = parseInvalidStatement(pos, decorators);
5985
5975
  break;
5986
5976
  }
5987
- mutate(item).directives = directives;
5988
5977
  if (tok !== Token.NamespaceKeyword) {
5978
+ mutate(item).directives = directives;
5989
5979
  mutate(item).docs = docs;
5990
5980
  }
5991
5981
  if (isBlocklessNamespace(item)) {
@@ -6035,7 +6025,7 @@ function createParser(code, options = {}) {
6035
6025
  item = parseScalarStatement(pos, decorators);
6036
6026
  break;
6037
6027
  case Token.NamespaceKeyword:
6038
- const ns = parseNamespaceStatement(pos, decorators, docs);
6028
+ const ns = parseNamespaceStatement(pos, decorators, docs, directives);
6039
6029
  if (!Array.isArray(ns.statements)) {
6040
6030
  error({ code: "blockless-namespace-first", messageId: "topLevel", target: ns });
6041
6031
  }
@@ -6103,7 +6093,7 @@ function createParser(code, options = {}) {
6103
6093
  }
6104
6094
  return directives;
6105
6095
  }
6106
- function parseNamespaceStatement(pos, decorators, docs) {
6096
+ function parseNamespaceStatement(pos, decorators, docs, directives) {
6107
6097
  parseExpected(Token.NamespaceKeyword);
6108
6098
  let currentName = parseIdentifierOrMemberExpression();
6109
6099
  const nsSegments = [];
@@ -6125,12 +6115,14 @@ function createParser(code, options = {}) {
6125
6115
  id: nsSegments[0],
6126
6116
  locals: undefined,
6127
6117
  statements,
6118
+ directives: directives,
6128
6119
  ...finishNode(pos),
6129
6120
  };
6130
6121
  for (let i = 1; i < nsSegments.length; i++) {
6131
6122
  outerNs = {
6132
6123
  kind: SyntaxKind.NamespaceStatement,
6133
6124
  decorators: [],
6125
+ directives: [],
6134
6126
  id: nsSegments[i],
6135
6127
  statements: outerNs,
6136
6128
  locals: undefined,
@@ -6977,15 +6969,14 @@ function createParser(code, options = {}) {
6977
6969
  };
6978
6970
  }
6979
6971
  function parseIdentifier(options) {
6980
- var _a;
6981
- if ((options === null || options === void 0 ? void 0 : options.recoverFromKeyword) !== false && isKeyword(token())) {
6972
+ if (options?.recoverFromKeyword !== false && isKeyword(token())) {
6982
6973
  error({ code: "reserved-identifier" });
6983
6974
  }
6984
6975
  else if (token() !== Token.Identifier &&
6985
- (!(options === null || options === void 0 ? void 0 : options.allowStringLiteral) || token() !== Token.StringLiteral)) {
6976
+ (!options?.allowStringLiteral || token() !== Token.StringLiteral)) {
6986
6977
  // Error recovery: when we fail to parse an identifier or expression,
6987
6978
  // we insert a synthesized identifier with a unique name.
6988
- error({ code: "token-expected", messageId: (_a = options === null || options === void 0 ? void 0 : options.message) !== null && _a !== void 0 ? _a : "identifier" });
6979
+ error({ code: "token-expected", messageId: options?.message ?? "identifier" });
6989
6980
  return createMissingIdentifier();
6990
6981
  }
6991
6982
  const pos = tokenPos();
@@ -8131,12 +8122,11 @@ function createParser(code, options = {}) {
8131
8122
  return { kind: SyntaxKind.InvalidStatement, decorators, ...finishNode(pos) };
8132
8123
  }
8133
8124
  function error(report) {
8134
- var _a, _b, _c, _d, _e, _f;
8135
8125
  parseErrorInNextFinishedNode = true;
8136
8126
  const location = {
8137
8127
  file: scanner.file,
8138
- pos: (_b = (_a = report.target) === null || _a === void 0 ? void 0 : _a.pos) !== null && _b !== void 0 ? _b : tokenPos(),
8139
- end: (_d = (_c = report.target) === null || _c === void 0 ? void 0 : _c.end) !== null && _d !== void 0 ? _d : tokenEnd(),
8128
+ pos: report.target?.pos ?? tokenPos(),
8129
+ end: report.target?.end ?? tokenEnd(),
8140
8130
  };
8141
8131
  if (!report.printable) {
8142
8132
  treePrintable = false;
@@ -8145,7 +8135,7 @@ function createParser(code, options = {}) {
8145
8135
  // position. The code path taken by error recovery after logging an error
8146
8136
  // can otherwise produce redundant and less decipherable errors, which this
8147
8137
  // suppresses.
8148
- const realPos = (_f = (_e = report.target) === null || _e === void 0 ? void 0 : _e.realPos) !== null && _f !== void 0 ? _f : location.pos;
8138
+ const realPos = report.target?.realPos ?? location.pos;
8149
8139
  if (realPositionOfLastError === realPos) {
8150
8140
  return;
8151
8141
  }
@@ -8158,11 +8148,10 @@ function createParser(code, options = {}) {
8158
8148
  parseDiagnostics.push(diagnostic);
8159
8149
  }
8160
8150
  function warning(report) {
8161
- var _a, _b, _c, _d;
8162
8151
  const location = {
8163
8152
  file: scanner.file,
8164
- pos: (_b = (_a = report.target) === null || _a === void 0 ? void 0 : _a.pos) !== null && _b !== void 0 ? _b : tokenPos(),
8165
- end: (_d = (_c = report.target) === null || _c === void 0 ? void 0 : _c.end) !== null && _d !== void 0 ? _d : tokenEnd(),
8153
+ pos: report.target?.pos ?? tokenPos(),
8154
+ end: report.target?.end ?? tokenEnd(),
8166
8155
  };
8167
8156
  const diagnostic = createDiagnostic({
8168
8157
  ...report,
@@ -8524,14 +8513,15 @@ function flattenNamespaces(base) {
8524
8513
  });
8525
8514
  }
8526
8515
  class PrettierParserError extends Error {
8516
+ error;
8517
+ loc;
8527
8518
  constructor(error) {
8528
- var _a, _b;
8529
8519
  super(error.message);
8530
8520
  this.error = error;
8531
8521
  const location = getSourceLocation(error.target);
8532
8522
  this.loc = {
8533
- start: (_a = location === null || location === void 0 ? void 0 : location.pos) !== null && _a !== void 0 ? _a : 0,
8534
- end: (_b = location === null || location === void 0 ? void 0 : location.end) !== null && _b !== void 0 ? _b : 0,
8523
+ start: location?.pos ?? 0,
8524
+ end: location?.end ?? 0,
8535
8525
  };
8536
8526
  }
8537
8527
  }