@typespec/prettier-plugin-typespec 0.41.0 → 0.43.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
@@ -874,30 +874,33 @@ var SyntaxKind;
874
874
  SyntaxKind[SyntaxKind["Projection"] = 52] = "Projection";
875
875
  SyntaxKind[SyntaxKind["ProjectionParameterDeclaration"] = 53] = "ProjectionParameterDeclaration";
876
876
  SyntaxKind[SyntaxKind["ProjectionModelSelector"] = 54] = "ProjectionModelSelector";
877
- SyntaxKind[SyntaxKind["ProjectionOperationSelector"] = 55] = "ProjectionOperationSelector";
878
- SyntaxKind[SyntaxKind["ProjectionUnionSelector"] = 56] = "ProjectionUnionSelector";
879
- SyntaxKind[SyntaxKind["ProjectionInterfaceSelector"] = 57] = "ProjectionInterfaceSelector";
880
- SyntaxKind[SyntaxKind["ProjectionEnumSelector"] = 58] = "ProjectionEnumSelector";
881
- SyntaxKind[SyntaxKind["ProjectionExpressionStatement"] = 59] = "ProjectionExpressionStatement";
882
- SyntaxKind[SyntaxKind["ProjectionIfExpression"] = 60] = "ProjectionIfExpression";
883
- SyntaxKind[SyntaxKind["ProjectionBlockExpression"] = 61] = "ProjectionBlockExpression";
884
- SyntaxKind[SyntaxKind["ProjectionMemberExpression"] = 62] = "ProjectionMemberExpression";
885
- SyntaxKind[SyntaxKind["ProjectionLogicalExpression"] = 63] = "ProjectionLogicalExpression";
886
- SyntaxKind[SyntaxKind["ProjectionEqualityExpression"] = 64] = "ProjectionEqualityExpression";
887
- SyntaxKind[SyntaxKind["ProjectionUnaryExpression"] = 65] = "ProjectionUnaryExpression";
888
- SyntaxKind[SyntaxKind["ProjectionRelationalExpression"] = 66] = "ProjectionRelationalExpression";
889
- SyntaxKind[SyntaxKind["ProjectionArithmeticExpression"] = 67] = "ProjectionArithmeticExpression";
890
- SyntaxKind[SyntaxKind["ProjectionCallExpression"] = 68] = "ProjectionCallExpression";
891
- SyntaxKind[SyntaxKind["ProjectionLambdaExpression"] = 69] = "ProjectionLambdaExpression";
892
- SyntaxKind[SyntaxKind["ProjectionLambdaParameterDeclaration"] = 70] = "ProjectionLambdaParameterDeclaration";
893
- SyntaxKind[SyntaxKind["ProjectionModelExpression"] = 71] = "ProjectionModelExpression";
894
- SyntaxKind[SyntaxKind["ProjectionModelProperty"] = 72] = "ProjectionModelProperty";
895
- SyntaxKind[SyntaxKind["ProjectionModelSpreadProperty"] = 73] = "ProjectionModelSpreadProperty";
896
- SyntaxKind[SyntaxKind["ProjectionSpreadProperty"] = 74] = "ProjectionSpreadProperty";
897
- SyntaxKind[SyntaxKind["ProjectionTupleExpression"] = 75] = "ProjectionTupleExpression";
898
- SyntaxKind[SyntaxKind["ProjectionStatement"] = 76] = "ProjectionStatement";
899
- SyntaxKind[SyntaxKind["ProjectionDecoratorReferenceExpression"] = 77] = "ProjectionDecoratorReferenceExpression";
900
- SyntaxKind[SyntaxKind["Return"] = 78] = "Return";
877
+ SyntaxKind[SyntaxKind["ProjectionModelPropertySelector"] = 55] = "ProjectionModelPropertySelector";
878
+ SyntaxKind[SyntaxKind["ProjectionOperationSelector"] = 56] = "ProjectionOperationSelector";
879
+ SyntaxKind[SyntaxKind["ProjectionUnionSelector"] = 57] = "ProjectionUnionSelector";
880
+ SyntaxKind[SyntaxKind["ProjectionUnionVariantSelector"] = 58] = "ProjectionUnionVariantSelector";
881
+ SyntaxKind[SyntaxKind["ProjectionInterfaceSelector"] = 59] = "ProjectionInterfaceSelector";
882
+ SyntaxKind[SyntaxKind["ProjectionEnumSelector"] = 60] = "ProjectionEnumSelector";
883
+ SyntaxKind[SyntaxKind["ProjectionEnumMemberSelector"] = 61] = "ProjectionEnumMemberSelector";
884
+ SyntaxKind[SyntaxKind["ProjectionExpressionStatement"] = 62] = "ProjectionExpressionStatement";
885
+ SyntaxKind[SyntaxKind["ProjectionIfExpression"] = 63] = "ProjectionIfExpression";
886
+ SyntaxKind[SyntaxKind["ProjectionBlockExpression"] = 64] = "ProjectionBlockExpression";
887
+ SyntaxKind[SyntaxKind["ProjectionMemberExpression"] = 65] = "ProjectionMemberExpression";
888
+ SyntaxKind[SyntaxKind["ProjectionLogicalExpression"] = 66] = "ProjectionLogicalExpression";
889
+ SyntaxKind[SyntaxKind["ProjectionEqualityExpression"] = 67] = "ProjectionEqualityExpression";
890
+ SyntaxKind[SyntaxKind["ProjectionUnaryExpression"] = 68] = "ProjectionUnaryExpression";
891
+ SyntaxKind[SyntaxKind["ProjectionRelationalExpression"] = 69] = "ProjectionRelationalExpression";
892
+ SyntaxKind[SyntaxKind["ProjectionArithmeticExpression"] = 70] = "ProjectionArithmeticExpression";
893
+ SyntaxKind[SyntaxKind["ProjectionCallExpression"] = 71] = "ProjectionCallExpression";
894
+ SyntaxKind[SyntaxKind["ProjectionLambdaExpression"] = 72] = "ProjectionLambdaExpression";
895
+ SyntaxKind[SyntaxKind["ProjectionLambdaParameterDeclaration"] = 73] = "ProjectionLambdaParameterDeclaration";
896
+ SyntaxKind[SyntaxKind["ProjectionModelExpression"] = 74] = "ProjectionModelExpression";
897
+ SyntaxKind[SyntaxKind["ProjectionModelProperty"] = 75] = "ProjectionModelProperty";
898
+ SyntaxKind[SyntaxKind["ProjectionModelSpreadProperty"] = 76] = "ProjectionModelSpreadProperty";
899
+ SyntaxKind[SyntaxKind["ProjectionSpreadProperty"] = 77] = "ProjectionSpreadProperty";
900
+ SyntaxKind[SyntaxKind["ProjectionTupleExpression"] = 78] = "ProjectionTupleExpression";
901
+ SyntaxKind[SyntaxKind["ProjectionStatement"] = 79] = "ProjectionStatement";
902
+ SyntaxKind[SyntaxKind["ProjectionDecoratorReferenceExpression"] = 80] = "ProjectionDecoratorReferenceExpression";
903
+ SyntaxKind[SyntaxKind["Return"] = 81] = "Return";
901
904
  })(SyntaxKind = SyntaxKind || (SyntaxKind = {}));
902
905
  var IdentifierKind;
903
906
  (function (IdentifierKind) {
@@ -1235,7 +1238,7 @@ const diagnostics = {
1235
1238
  inDecorator: paramMessage `Cannot resolve ${"id"} in decorator`,
1236
1239
  underNamespace: paramMessage `Namespace ${"namespace"} doesn't have member ${"id"}`,
1237
1240
  underContainer: paramMessage `${"kind"} doesn't have member ${"id"}`,
1238
- node: paramMessage `Cannot resolve '${"id"}' in non-namespace node ${"nodeName"}`,
1241
+ node: paramMessage `Cannot resolve '${"id"}' in node ${"nodeName"} since it has no members. Did you mean to use "::" instead of "."?`,
1239
1242
  },
1240
1243
  },
1241
1244
  "duplicate-property": {
@@ -1250,12 +1253,6 @@ const diagnostics = {
1250
1253
  default: paramMessage `Model has an inherited property named ${"propName"} of type ${"propType"} which cannot override type ${"parentType"}`,
1251
1254
  },
1252
1255
  },
1253
- "override-property-intrinsic": {
1254
- severity: "error",
1255
- messages: {
1256
- default: paramMessage `Model has an inherited property named ${"propName"} of type ${"propType"} which can only override an intrinsic type on the parent property, not ${"parentType"}`,
1257
- },
1258
- },
1259
1256
  "extend-scalar": {
1260
1257
  severity: "error",
1261
1258
  messages: {
@@ -1419,6 +1416,12 @@ const diagnostics = {
1419
1416
  default: paramMessage `Path "${"path"}" cannot be relative. Use {cwd} or {project-root} to specify what the path should be relative to.`,
1420
1417
  },
1421
1418
  },
1419
+ "config-path-not-found": {
1420
+ severity: "error",
1421
+ messages: {
1422
+ default: paramMessage `No configuration file found at config path "${"path"}".`,
1423
+ },
1424
+ },
1422
1425
  /**
1423
1426
  * Program
1424
1427
  */
@@ -3124,14 +3127,20 @@ path, options, print) {
3124
3127
  return printProjectionStatement(path, options, print);
3125
3128
  case SyntaxKind.ProjectionModelSelector:
3126
3129
  return "model";
3130
+ case SyntaxKind.ProjectionModelPropertySelector:
3131
+ return "modelproperty";
3127
3132
  case SyntaxKind.ProjectionOperationSelector:
3128
3133
  return "op";
3129
3134
  case SyntaxKind.ProjectionUnionSelector:
3130
3135
  return "union";
3136
+ case SyntaxKind.ProjectionUnionVariantSelector:
3137
+ return "unionvariant";
3131
3138
  case SyntaxKind.ProjectionInterfaceSelector:
3132
3139
  return "interface";
3133
3140
  case SyntaxKind.ProjectionEnumSelector:
3134
3141
  return "enum";
3142
+ case SyntaxKind.ProjectionEnumMemberSelector:
3143
+ return "enummember";
3135
3144
  case SyntaxKind.Projection:
3136
3145
  return printProjection(path, options, print);
3137
3146
  case SyntaxKind.ProjectionParameterDeclaration:
@@ -3837,20 +3846,17 @@ function printBooleanLiteral(path, options) {
3837
3846
  return node.value ? "true" : "false";
3838
3847
  }
3839
3848
  function printProjectionStatement(path, options, print) {
3840
- const node = path.getValue();
3841
3849
  const selector = path.call(print, "selector");
3842
3850
  const id = path.call(print, "id");
3843
- const to = node.to ? [hardline, path.call(print, "to")] : "";
3844
- const from = node.from ? [hardline, path.call(print, "from")] : "";
3845
- const body = [to, from];
3851
+ const projections = path.map(print, "projections").flatMap((x) => [hardline, x]);
3846
3852
  return [
3847
3853
  "projection ",
3848
3854
  selector,
3849
3855
  "#",
3850
3856
  id,
3851
3857
  " {",
3852
- indent(body),
3853
- node.to || node.from ? hardline : "",
3858
+ indent(projections),
3859
+ projections.length > 0 ? hardline : "",
3854
3860
  "}",
3855
3861
  ];
3856
3862
  }
@@ -3858,7 +3864,15 @@ function printProjection(path, options, print) {
3858
3864
  const node = path.getValue();
3859
3865
  const params = printProjectionParameters(path, options, print);
3860
3866
  const body = printProjectionExpressionStatements(path, options, print, "body");
3861
- return [node.direction, params, " {", indent(body), hardline, "}"];
3867
+ return [
3868
+ ...node.modifierIds.flatMap((i) => [i.sv, " "]),
3869
+ node.directionId.sv,
3870
+ params,
3871
+ " {",
3872
+ indent(body),
3873
+ hardline,
3874
+ "}",
3875
+ ];
3862
3876
  }
3863
3877
  function printProjectionParameters(path, options, print) {
3864
3878
  const node = path.getValue();
@@ -4906,20 +4920,35 @@ function createParser(code, options = {}) {
4906
4920
  message,
4907
4921
  recoverFromKeyword,
4908
4922
  });
4909
- while (parseOptional(Token.Dot)) {
4910
- base = {
4911
- kind: SyntaxKind.MemberExpression,
4912
- base,
4913
- // Error recovery: false arg here means don't treat a keyword as an
4914
- // identifier after `.` in member expression. Otherwise we will
4915
- // parse `@Outer.<missing identifier> model M{}` as having decorator
4916
- // `@Outer.model` applied to invalid statement `M {}` instead of
4917
- // having incomplete decorator `@Outer.` applied to `model M {}`.
4918
- id: parseIdentifier({
4919
- recoverFromKeyword: false,
4920
- }),
4921
- ...finishNode(pos),
4922
- };
4923
+ while (token() !== Token.EndOfFile) {
4924
+ if (parseOptional(Token.Dot)) {
4925
+ base = {
4926
+ kind: SyntaxKind.MemberExpression,
4927
+ base,
4928
+ // Error recovery: false arg here means don't treat a keyword as an
4929
+ // identifier after `.` in member expression. Otherwise we will
4930
+ // parse `@Outer.<missing identifier> model M{}` as having decorator
4931
+ // `@Outer.model` applied to invalid statement `M {}` instead of
4932
+ // having incomplete decorator `@Outer.` applied to `model M {}`.
4933
+ id: parseIdentifier({
4934
+ recoverFromKeyword: false,
4935
+ }),
4936
+ selector: ".",
4937
+ ...finishNode(pos),
4938
+ };
4939
+ }
4940
+ else if (parseOptional(Token.ColonColon)) {
4941
+ base = {
4942
+ kind: SyntaxKind.MemberExpression,
4943
+ base,
4944
+ id: parseIdentifier(),
4945
+ selector: "::",
4946
+ ...finishNode(pos),
4947
+ };
4948
+ }
4949
+ else {
4950
+ break;
4951
+ }
4923
4952
  }
4924
4953
  return base;
4925
4954
  }
@@ -5200,37 +5229,51 @@ function createParser(code, options = {}) {
5200
5229
  parseExpected(Token.Hash);
5201
5230
  const id = parseIdentifier();
5202
5231
  parseExpected(Token.OpenBrace);
5203
- let from, to;
5204
- let proj1, proj2;
5205
- if (token() === Token.Identifier) {
5206
- proj1 = parseProjection();
5207
- if (token() === Token.Identifier) {
5208
- proj2 = parseProjection();
5232
+ const projectionMap = new Map();
5233
+ const projections = [];
5234
+ while (token() === Token.Identifier) {
5235
+ const projection = parseProjection();
5236
+ if (projection.direction !== "<error>") {
5237
+ if (projectionMap.has(projection.direction)) {
5238
+ error({ code: "duplicate-symbol", target: projection, format: { name: "projection" } });
5239
+ }
5240
+ else {
5241
+ projectionMap.set(projection.direction, projection);
5242
+ }
5209
5243
  }
5210
- }
5211
- if (proj1 && proj2 && proj1.direction === proj2.direction) {
5212
- error({ code: "duplicate-symbol", target: proj2, format: { name: "projection" } });
5213
- }
5214
- else if (proj1) {
5215
- [to, from] = proj1.direction === "to" ? [proj1, proj2] : [proj2, proj1];
5244
+ // NOTE: Don't drop projections with error in direction definition from the AST.
5245
+ projections.push(projection);
5216
5246
  }
5217
5247
  parseExpected(Token.CloseBrace);
5218
5248
  return {
5219
5249
  kind: SyntaxKind.ProjectionStatement,
5220
5250
  selector,
5221
- from,
5222
- to,
5251
+ projections,
5252
+ preTo: projectionMap.get("pre_to"),
5253
+ preFrom: projectionMap.get("pre_from"),
5254
+ from: projectionMap.get("from"),
5255
+ to: projectionMap.get("to"),
5223
5256
  id,
5224
5257
  ...finishNode(pos),
5225
5258
  };
5226
5259
  }
5227
5260
  function parseProjection() {
5228
5261
  const pos = tokenPos();
5229
- const directionId = parseIdentifier({ message: "projectionDirection" });
5262
+ let directionId = parseIdentifier({ message: "projectionDirection" });
5230
5263
  let direction;
5231
- if (directionId.sv !== "from" && directionId.sv !== "to") {
5264
+ const modifierIds = [];
5265
+ let isPre = false;
5266
+ if (directionId.sv === "pre") {
5267
+ isPre = true;
5268
+ modifierIds.push(directionId);
5269
+ directionId = parseIdentifier({ message: "projectionDirection" });
5270
+ }
5271
+ if (directionId.sv !== "to" && directionId.sv !== "from") {
5232
5272
  error({ code: "token-expected", messageId: "projectionDirection" });
5233
- direction = "from";
5273
+ direction = "<error>";
5274
+ }
5275
+ else if (isPre) {
5276
+ direction = directionId.sv === "to" ? "pre_to" : "pre_from";
5234
5277
  }
5235
5278
  else {
5236
5279
  direction = directionId.sv;
@@ -5250,6 +5293,7 @@ function createParser(code, options = {}) {
5250
5293
  body,
5251
5294
  direction,
5252
5295
  directionId,
5296
+ modifierIds,
5253
5297
  parameters,
5254
5298
  ...finishNode(pos),
5255
5299
  };
@@ -5681,7 +5725,27 @@ function createParser(code, options = {}) {
5681
5725
  const selectorTok = expectTokenIsOneOf(Token.Identifier, Token.ModelKeyword, Token.OpKeyword, Token.InterfaceKeyword, Token.UnionKeyword, Token.EnumKeyword);
5682
5726
  switch (selectorTok) {
5683
5727
  case Token.Identifier:
5684
- return parseIdentifierOrMemberExpression(undefined, true);
5728
+ const id = parseIdentifierOrMemberExpression(undefined, true);
5729
+ if (id.kind === SyntaxKind.Identifier) {
5730
+ switch (id.sv) {
5731
+ case "modelproperty":
5732
+ return {
5733
+ kind: SyntaxKind.ProjectionModelPropertySelector,
5734
+ ...finishNode(pos),
5735
+ };
5736
+ case "unionvariant":
5737
+ return {
5738
+ kind: SyntaxKind.ProjectionUnionVariantSelector,
5739
+ ...finishNode(pos),
5740
+ };
5741
+ case "enummember":
5742
+ return {
5743
+ kind: SyntaxKind.ProjectionEnumMemberSelector,
5744
+ ...finishNode(pos),
5745
+ };
5746
+ }
5747
+ }
5748
+ return id;
5685
5749
  case Token.ModelKeyword:
5686
5750
  nextToken();
5687
5751
  return {