@typespec/prettier-plugin-typespec 0.52.0-dev.1 → 0.52.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
@@ -97,15 +97,17 @@ var SyntaxKind;
97
97
  SyntaxKind[SyntaxKind["ProjectionDecoratorReferenceExpression"] = 87] = "ProjectionDecoratorReferenceExpression";
98
98
  SyntaxKind[SyntaxKind["Return"] = 88] = "Return";
99
99
  SyntaxKind[SyntaxKind["JsNamespaceDeclaration"] = 89] = "JsNamespaceDeclaration";
100
+ SyntaxKind[SyntaxKind["TemplateArgument"] = 90] = "TemplateArgument";
100
101
  })(SyntaxKind || (SyntaxKind = {}));
101
102
  var IdentifierKind;
102
103
  (function (IdentifierKind) {
103
104
  IdentifierKind[IdentifierKind["TypeReference"] = 0] = "TypeReference";
104
- IdentifierKind[IdentifierKind["Decorator"] = 1] = "Decorator";
105
- IdentifierKind[IdentifierKind["Function"] = 2] = "Function";
106
- IdentifierKind[IdentifierKind["Using"] = 3] = "Using";
107
- IdentifierKind[IdentifierKind["Declaration"] = 4] = "Declaration";
108
- IdentifierKind[IdentifierKind["Other"] = 5] = "Other";
105
+ IdentifierKind[IdentifierKind["TemplateArgument"] = 1] = "TemplateArgument";
106
+ IdentifierKind[IdentifierKind["Decorator"] = 2] = "Decorator";
107
+ IdentifierKind[IdentifierKind["Function"] = 3] = "Function";
108
+ IdentifierKind[IdentifierKind["Using"] = 4] = "Using";
109
+ IdentifierKind[IdentifierKind["Declaration"] = 5] = "Declaration";
110
+ IdentifierKind[IdentifierKind["Other"] = 6] = "Other";
109
111
  })(IdentifierKind || (IdentifierKind = {}));
110
112
  /** Used to explicitly specify that a diagnostic has no target. */
111
113
  const NoTarget = Symbol.for("NoTarget");
@@ -117,53 +119,6 @@ var ListenerFlow;
117
119
  ListenerFlow[ListenerFlow["NoRecursion"] = 1] = "NoRecursion";
118
120
  })(ListenerFlow || (ListenerFlow = {}));
119
121
 
120
- /**
121
- * Create a new diagnostics creator.
122
- * @param diagnostics Map of the potential diagnostics.
123
- * @param libraryName Optional name of the library if in the scope of a library.
124
- * @returns @see DiagnosticCreator
125
- */
126
- function createDiagnosticCreator(diagnostics, libraryName) {
127
- const errorMessage = libraryName
128
- ? `It must match one of the code defined in the library '${libraryName}'`
129
- : "It must match one of the code defined in the compiler.";
130
- function createDiagnostic(diagnostic) {
131
- var _a;
132
- const diagnosticDef = diagnostics[diagnostic.code];
133
- if (!diagnosticDef) {
134
- const codeStr = Object.keys(diagnostics)
135
- .map((x) => ` - ${x}`)
136
- .join("\n");
137
- const code = String(diagnostic.code);
138
- throw new Error(`Unexpected diagnostic code '${code}'. ${errorMessage}. Defined codes:\n${codeStr}`);
139
- }
140
- const message = diagnosticDef.messages[(_a = diagnostic.messageId) !== null && _a !== void 0 ? _a : "default"];
141
- if (!message) {
142
- const codeStr = Object.keys(diagnosticDef.messages)
143
- .map((x) => ` - ${x}`)
144
- .join("\n");
145
- const messageId = String(diagnostic.messageId);
146
- const code = String(diagnostic.code);
147
- throw new Error(`Unexpected message id '${messageId}'. ${errorMessage} for code '${code}'. Defined codes:\n${codeStr}`);
148
- }
149
- const messageStr = typeof message === "string" ? message : message(diagnostic.format);
150
- return {
151
- code: libraryName ? `${libraryName}/${String(diagnostic.code)}` : diagnostic.code.toString(),
152
- severity: diagnosticDef.severity,
153
- message: messageStr,
154
- target: diagnostic.target,
155
- };
156
- }
157
- function reportDiagnostic(program, diagnostic) {
158
- const diag = createDiagnostic(diagnostic);
159
- program.reportDiagnostic(diag);
160
- }
161
- return {
162
- diagnostics,
163
- createDiagnostic,
164
- reportDiagnostic,
165
- };
166
- }
167
122
  function createSourceFile(text, path) {
168
123
  let lineStarts = undefined;
169
124
  return {
@@ -328,10 +283,54 @@ function binarySearch(array, value) {
328
283
  return ~low;
329
284
  }
330
285
 
331
- const globalLibraryUrlsLoadedSym = Symbol.for("TYPESPEC_LIBRARY_URLS_LOADED");
332
- if (globalThis[globalLibraryUrlsLoadedSym] === undefined) {
333
- globalThis[globalLibraryUrlsLoadedSym] = new Set();
286
+ /**
287
+ * Create a new diagnostics creator.
288
+ * @param diagnostics Map of the potential diagnostics.
289
+ * @param libraryName Optional name of the library if in the scope of a library.
290
+ * @returns @see DiagnosticCreator
291
+ */
292
+ function createDiagnosticCreator(diagnostics, libraryName) {
293
+ const errorMessage = libraryName
294
+ ? `It must match one of the code defined in the library '${libraryName}'`
295
+ : "It must match one of the code defined in the compiler.";
296
+ function createDiagnostic(diagnostic) {
297
+ var _a;
298
+ const diagnosticDef = diagnostics[diagnostic.code];
299
+ if (!diagnosticDef) {
300
+ const codeStr = Object.keys(diagnostics)
301
+ .map((x) => ` - ${x}`)
302
+ .join("\n");
303
+ const code = String(diagnostic.code);
304
+ throw new Error(`Unexpected diagnostic code '${code}'. ${errorMessage}. Defined codes:\n${codeStr}`);
305
+ }
306
+ const message = diagnosticDef.messages[(_a = diagnostic.messageId) !== null && _a !== void 0 ? _a : "default"];
307
+ if (!message) {
308
+ const codeStr = Object.keys(diagnosticDef.messages)
309
+ .map((x) => ` - ${x}`)
310
+ .join("\n");
311
+ const messageId = String(diagnostic.messageId);
312
+ const code = String(diagnostic.code);
313
+ throw new Error(`Unexpected message id '${messageId}'. ${errorMessage} for code '${code}'. Defined codes:\n${codeStr}`);
314
+ }
315
+ const messageStr = typeof message === "string" ? message : message(diagnostic.format);
316
+ return {
317
+ code: libraryName ? `${libraryName}/${String(diagnostic.code)}` : diagnostic.code.toString(),
318
+ severity: diagnosticDef.severity,
319
+ message: messageStr,
320
+ target: diagnostic.target,
321
+ };
322
+ }
323
+ function reportDiagnostic(program, diagnostic) {
324
+ const diag = createDiagnostic(diagnostic);
325
+ program.reportDiagnostic(diag);
326
+ }
327
+ return {
328
+ diagnostics,
329
+ createDiagnostic,
330
+ reportDiagnostic,
331
+ };
334
332
  }
333
+
335
334
  function paramMessage(strings, ...keys) {
336
335
  const template = (dict) => {
337
336
  const result = [strings[0]];
@@ -544,6 +543,12 @@ const diagnostics = {
544
543
  default: "Required template parameters must not follow optional template parameters",
545
544
  },
546
545
  },
546
+ "invalid-template-argument-name": {
547
+ severity: "error",
548
+ messages: {
549
+ default: "Template parameter argument names must be valid, bare identifiers.",
550
+ },
551
+ },
547
552
  "invalid-template-default": {
548
553
  severity: "error",
549
554
  messages: {
@@ -603,8 +608,11 @@ const diagnostics = {
603
608
  messages: {
604
609
  default: "Invalid template arguments.",
605
610
  notTemplate: "Can't pass template arguments to non-templated type",
606
- tooFew: "Too few template arguments provided.",
607
611
  tooMany: "Too many template arguments provided.",
612
+ unknownName: paramMessage `No parameter named '${"name"}' exists in the target template.`,
613
+ positionalAfterNamed: "Positional template arguments cannot follow named arguments in the same argument list.",
614
+ missing: paramMessage `Template argument '${"name"}' is required and not specified.`,
615
+ specifiedAgain: paramMessage `Cannot specify template argument '${"name"}' again.`,
608
616
  },
609
617
  },
610
618
  "intersect-non-model": {
@@ -4659,6 +4667,8 @@ path, options, print) {
4659
4667
  return printUnionVariant(path, options, print);
4660
4668
  case SyntaxKind.TypeReference:
4661
4669
  return printTypeReference(path, options, print);
4670
+ case SyntaxKind.TemplateArgument:
4671
+ return printTemplateArgument(path, options, print);
4662
4672
  case SyntaxKind.ValueOfExpression:
4663
4673
  return printValueOfExpression(path, options, print);
4664
4674
  case SyntaxKind.TemplateParameterDeclaration:
@@ -5407,6 +5417,16 @@ function printTypeReference(path, options, print) {
5407
5417
  const template = printTemplateParameters(path, options, print, "arguments");
5408
5418
  return [type, template];
5409
5419
  }
5420
+ function printTemplateArgument(path, _options, print) {
5421
+ if (path.getValue().name !== undefined) {
5422
+ const name = path.call(print, "name");
5423
+ const argument = path.call(print, "argument");
5424
+ return group([name, " = ", argument]);
5425
+ }
5426
+ else {
5427
+ return path.call(print, "argument");
5428
+ }
5429
+ }
5410
5430
  function printValueOfExpression(path, options, print) {
5411
5431
  const type = path.call(print, "target");
5412
5432
  return ["valueof ", type];
@@ -6493,7 +6513,7 @@ function createParser(code, options = {}) {
6493
6513
  function parseReferenceExpression(message) {
6494
6514
  const pos = tokenPos();
6495
6515
  const target = parseIdentifierOrMemberExpression(message);
6496
- const args = parseOptionalList(ListKind.TemplateArguments, parseExpression);
6516
+ const args = parseOptionalList(ListKind.TemplateArguments, parseTemplateArgument);
6497
6517
  return {
6498
6518
  kind: SyntaxKind.TypeReference,
6499
6519
  target,
@@ -6501,6 +6521,41 @@ function createParser(code, options = {}) {
6501
6521
  ...finishNode(pos),
6502
6522
  };
6503
6523
  }
6524
+ function parseTemplateArgument() {
6525
+ const pos = tokenPos();
6526
+ // Early error recovery for missing identifier followed by eq
6527
+ if (token() === Token.Equals) {
6528
+ error({ code: "token-expected", messageId: "identifier" });
6529
+ nextToken();
6530
+ return {
6531
+ kind: SyntaxKind.TemplateArgument,
6532
+ name: createMissingIdentifier(),
6533
+ argument: parseExpression(),
6534
+ ...finishNode(pos),
6535
+ };
6536
+ }
6537
+ const expr = parseExpression();
6538
+ const eq = parseOptional(Token.Equals);
6539
+ if (eq) {
6540
+ const isBareIdentifier = exprIsBareIdentifier(expr);
6541
+ if (!isBareIdentifier) {
6542
+ error({ code: "invalid-template-argument-name", target: expr });
6543
+ }
6544
+ return {
6545
+ kind: SyntaxKind.TemplateArgument,
6546
+ name: isBareIdentifier ? expr.target : createMissingIdentifier(),
6547
+ argument: parseExpression(),
6548
+ ...finishNode(pos),
6549
+ };
6550
+ }
6551
+ else {
6552
+ return {
6553
+ kind: SyntaxKind.TemplateArgument,
6554
+ argument: expr,
6555
+ ...finishNode(pos),
6556
+ };
6557
+ }
6558
+ }
6504
6559
  function parseAugmentDecorator() {
6505
6560
  const pos = tokenPos();
6506
6561
  parseExpected(Token.AtAt);
@@ -6932,7 +6987,7 @@ function createParser(code, options = {}) {
6932
6987
  target = {
6933
6988
  kind: SyntaxKind.FunctionParameter,
6934
6989
  id: createMissingIdentifier(),
6935
- type: createMissingIdentifier(),
6990
+ type: createMissingTypeReference(),
6936
6991
  optional: false,
6937
6992
  rest: false,
6938
6993
  ...finishNode(pos),
@@ -7842,6 +7897,15 @@ function createParser(code, options = {}) {
7842
7897
  ...finishNode(pos),
7843
7898
  };
7844
7899
  }
7900
+ function createMissingTypeReference() {
7901
+ const pos = tokenPos();
7902
+ return {
7903
+ kind: SyntaxKind.TypeReference,
7904
+ target: createMissingIdentifier(),
7905
+ arguments: [],
7906
+ ...finishNode(pos),
7907
+ };
7908
+ }
7845
7909
  function finishNode(pos) {
7846
7910
  const flags = parseErrorInNextFinishedNode ? 2 /* NodeFlags.ThisNodeHasError */ : 0 /* NodeFlags.None */;
7847
7911
  parseErrorInNextFinishedNode = false;
@@ -8141,6 +8205,11 @@ function createParser(code, options = {}) {
8141
8205
  : undefined;
8142
8206
  }
8143
8207
  }
8208
+ function exprIsBareIdentifier(expr) {
8209
+ return (expr.kind === SyntaxKind.TypeReference &&
8210
+ expr.target.kind === SyntaxKind.Identifier &&
8211
+ expr.arguments.length === 0);
8212
+ }
8144
8213
  function visitChildren(node, cb) {
8145
8214
  if (node.directives) {
8146
8215
  const result = visitEach(cb, node.directives);
@@ -8296,6 +8365,8 @@ function visitChildren(node, cb) {
8296
8365
  return visitEach(cb, node.decorators);
8297
8366
  case SyntaxKind.TemplateParameterDeclaration:
8298
8367
  return (visitNode(cb, node.id) || visitNode(cb, node.constraint) || visitNode(cb, node.default));
8368
+ case SyntaxKind.TemplateArgument:
8369
+ return (node.name && visitNode(cb, node.name)) || visitNode(cb, node.argument);
8299
8370
  case SyntaxKind.ProjectionLambdaParameterDeclaration:
8300
8371
  return visitNode(cb, node.id);
8301
8372
  case SyntaxKind.ProjectionParameterDeclaration: