@typespec/prettier-plugin-typespec 0.42.0 → 0.44.0-dev.1

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: {
@@ -3130,14 +3127,20 @@ path, options, print) {
3130
3127
  return printProjectionStatement(path, options, print);
3131
3128
  case SyntaxKind.ProjectionModelSelector:
3132
3129
  return "model";
3130
+ case SyntaxKind.ProjectionModelPropertySelector:
3131
+ return "modelproperty";
3133
3132
  case SyntaxKind.ProjectionOperationSelector:
3134
3133
  return "op";
3135
3134
  case SyntaxKind.ProjectionUnionSelector:
3136
3135
  return "union";
3136
+ case SyntaxKind.ProjectionUnionVariantSelector:
3137
+ return "unionvariant";
3137
3138
  case SyntaxKind.ProjectionInterfaceSelector:
3138
3139
  return "interface";
3139
3140
  case SyntaxKind.ProjectionEnumSelector:
3140
3141
  return "enum";
3142
+ case SyntaxKind.ProjectionEnumMemberSelector:
3143
+ return "enummember";
3141
3144
  case SyntaxKind.Projection:
3142
3145
  return printProjection(path, options, print);
3143
3146
  case SyntaxKind.ProjectionParameterDeclaration:
@@ -3843,20 +3846,17 @@ function printBooleanLiteral(path, options) {
3843
3846
  return node.value ? "true" : "false";
3844
3847
  }
3845
3848
  function printProjectionStatement(path, options, print) {
3846
- const node = path.getValue();
3847
3849
  const selector = path.call(print, "selector");
3848
3850
  const id = path.call(print, "id");
3849
- const to = node.to ? [hardline, path.call(print, "to")] : "";
3850
- const from = node.from ? [hardline, path.call(print, "from")] : "";
3851
- const body = [to, from];
3851
+ const projections = path.map(print, "projections").flatMap((x) => [hardline, x]);
3852
3852
  return [
3853
3853
  "projection ",
3854
3854
  selector,
3855
3855
  "#",
3856
3856
  id,
3857
3857
  " {",
3858
- indent(body),
3859
- node.to || node.from ? hardline : "",
3858
+ indent(projections),
3859
+ projections.length > 0 ? hardline : "",
3860
3860
  "}",
3861
3861
  ];
3862
3862
  }
@@ -3864,7 +3864,15 @@ function printProjection(path, options, print) {
3864
3864
  const node = path.getValue();
3865
3865
  const params = printProjectionParameters(path, options, print);
3866
3866
  const body = printProjectionExpressionStatements(path, options, print, "body");
3867
- 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
+ ];
3868
3876
  }
3869
3877
  function printProjectionParameters(path, options, print) {
3870
3878
  const node = path.getValue();
@@ -4912,20 +4920,35 @@ function createParser(code, options = {}) {
4912
4920
  message,
4913
4921
  recoverFromKeyword,
4914
4922
  });
4915
- while (parseOptional(Token.Dot)) {
4916
- base = {
4917
- kind: SyntaxKind.MemberExpression,
4918
- base,
4919
- // Error recovery: false arg here means don't treat a keyword as an
4920
- // identifier after `.` in member expression. Otherwise we will
4921
- // parse `@Outer.<missing identifier> model M{}` as having decorator
4922
- // `@Outer.model` applied to invalid statement `M {}` instead of
4923
- // having incomplete decorator `@Outer.` applied to `model M {}`.
4924
- id: parseIdentifier({
4925
- recoverFromKeyword: false,
4926
- }),
4927
- ...finishNode(pos),
4928
- };
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
+ }
4929
4952
  }
4930
4953
  return base;
4931
4954
  }
@@ -5206,37 +5229,51 @@ function createParser(code, options = {}) {
5206
5229
  parseExpected(Token.Hash);
5207
5230
  const id = parseIdentifier();
5208
5231
  parseExpected(Token.OpenBrace);
5209
- let from, to;
5210
- let proj1, proj2;
5211
- if (token() === Token.Identifier) {
5212
- proj1 = parseProjection();
5213
- if (token() === Token.Identifier) {
5214
- 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
+ }
5215
5243
  }
5216
- }
5217
- if (proj1 && proj2 && proj1.direction === proj2.direction) {
5218
- error({ code: "duplicate-symbol", target: proj2, format: { name: "projection" } });
5219
- }
5220
- else if (proj1) {
5221
- [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);
5222
5246
  }
5223
5247
  parseExpected(Token.CloseBrace);
5224
5248
  return {
5225
5249
  kind: SyntaxKind.ProjectionStatement,
5226
5250
  selector,
5227
- from,
5228
- 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"),
5229
5256
  id,
5230
5257
  ...finishNode(pos),
5231
5258
  };
5232
5259
  }
5233
5260
  function parseProjection() {
5234
5261
  const pos = tokenPos();
5235
- const directionId = parseIdentifier({ message: "projectionDirection" });
5262
+ let directionId = parseIdentifier({ message: "projectionDirection" });
5236
5263
  let direction;
5237
- 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") {
5238
5272
  error({ code: "token-expected", messageId: "projectionDirection" });
5239
- direction = "from";
5273
+ direction = "<error>";
5274
+ }
5275
+ else if (isPre) {
5276
+ direction = directionId.sv === "to" ? "pre_to" : "pre_from";
5240
5277
  }
5241
5278
  else {
5242
5279
  direction = directionId.sv;
@@ -5256,6 +5293,7 @@ function createParser(code, options = {}) {
5256
5293
  body,
5257
5294
  direction,
5258
5295
  directionId,
5296
+ modifierIds,
5259
5297
  parameters,
5260
5298
  ...finishNode(pos),
5261
5299
  };
@@ -5687,7 +5725,27 @@ function createParser(code, options = {}) {
5687
5725
  const selectorTok = expectTokenIsOneOf(Token.Identifier, Token.ModelKeyword, Token.OpKeyword, Token.InterfaceKeyword, Token.UnionKeyword, Token.EnumKeyword);
5688
5726
  switch (selectorTok) {
5689
5727
  case Token.Identifier:
5690
- 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;
5691
5749
  case Token.ModelKeyword:
5692
5750
  nextToken();
5693
5751
  return {